Java Code Examples for org.eclipse.jface.text.Document#replace()

The following examples show how to use org.eclipse.jface.text.Document#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeScriptAutoIndentStrategy.java    From typescript.java with MIT License 6 votes vote down vote up
/**
 * Cuts the visual equivalent of <code>toDelete</code> characters out of the
 * indentation of line <code>line</code> in <code>document</code>. Leaves
 * leading comment signs alone.
 *
 * @param document the document
 * @param line the line
 * @param toDelete the number of space equivalents to delete
 * @param tabLength the length of a tab
 * @throws BadLocationException on concurrent document modification
 */
private void cutIndent(Document document, int line, int toDelete, int tabLength) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int from= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// go behind line comments
	while (from < endOffset - 2 && document.get(from, 2).equals(LINE_COMMENT))
		from += 2;

	int to= from;
	while (toDelete > 0 && to < endOffset) {
		char ch= document.getChar(to);
		if (!Character.isWhitespace(ch))
			break;
		toDelete -= computeVisualLength(ch, tabLength);
		if (toDelete >= 0)
			to++;
		else
			break;
	}

	document.replace(from, to - from, ""); //$NON-NLS-1$
}
 
Example 2
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Cuts the visual equivalent of <code>toDelete</code> characters out of the
 * indentation of line <code>line</code> in <code>document</code>. Leaves
 * leading comment signs alone.
 *
 * @param document the document
 * @param line the line
 * @param toDelete the number of space equivalents to delete
 * @param tabLength the length of a tab
 * @throws BadLocationException on concurrent document modification
 */
private void cutIndent(Document document, int line, int toDelete, int tabLength) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int from= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// go behind line comments
	while (from < endOffset - 2 && document.get(from, 2).equals(LINE_COMMENT))
		from += 2;

	int to= from;
	while (toDelete > 0 && to < endOffset) {
		char ch= document.getChar(to);
		if (!Character.isWhitespace(ch))
			break;
		toDelete -= computeVisualLength(ch, tabLength);
		if (toDelete >= 0)
			to++;
		else
			break;
	}

	document.replace(from, to - from, ""); //$NON-NLS-1$
}
 
Example 3
Source File: PyParserEditorIntegrationTest.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public void testIntegration() throws Exception {
    IEclipsePreferences preferences = new InMemoryEclipsePreferences();
    PyParserManager pyParserManager = PyParserManager.getPyParserManager(preferences);

    Document doc = new Document();
    PyEditStub pyEdit = new PyEditStub(doc, new PydevFileEditorInputStub());
    pyParserManager.attachParserTo(pyEdit);
    checkParserChanged(pyEdit, 1);

    doc.replace(0, 0, "\r\ntest");
    checkParserChanged(pyEdit, 2);

    pyParserManager.attachParserTo(pyEdit);
    checkParserChanged(pyEdit, 3);

    doc.replace(0, 0, "\r\ntest"); //after this change, only 1 reparse should be asked, as the editor and doc is the same
    checkParserChanged(pyEdit, 4);

    pyParserManager.notifyEditorDisposed(pyEdit);
    doc.replace(0, 0, "\r\ntest"); //after this change, only 1 reparse should be asked, as the editor and doc is the same
    waitABit();
    assertEquals(4, pyEdit.parserChanged);

    assertEquals(0, pyParserManager.getParsers().size());
}
 
Example 4
Source File: LangCompletionProposal.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
protected String doGetAdditionalProposalInfo(IProgressMonitor monitor) {
	Document tempDocument = new Document(sourceOpContext.getSource());
	doApply(tempDocument, false);
	try {
		tempDocument.replace(endPositionAfterApply, 0, " ");
	} catch(BadLocationException e) {
		
	}
	
	DocumentSourceBuffer tempSourceBuffer = new DocumentSourceBuffer(tempDocument) {
		@Override
		public Location getLocation_orNull() {
			return sourceOpContext.getOptionalFileLocation().orElse(null);
		}
	};
	String doc = new DocDisplayInfoSupplier(tempSourceBuffer, getReplaceOffset())
			.doGetDocumentation(EclipseUtils.om(monitor));
	
	if(doc == null) {
		return null;
	}
	return BrowserControlCreator.wrapHTMLBody(doc);
}
 
Example 5
Source File: AbstractDamagerRepairerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected IRegion check(String before, int start, int replaceLength, String text) throws Exception {
	Document doc = createDocument(before);
	damager.setDocument(doc);
	doc.addDocumentListener(this);
	doc.replace(start, replaceLength, text);
	return lastRegion;
}
 
Example 6
Source File: AbstractDamagerRepairerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected IRegion check(String before, int start, int replaceLength, String text) throws Exception {
	Document doc = createDocument(before);
	damager.setDocument(doc);
	doc.addDocumentListener(this);
	doc.replace(start, replaceLength, text);
	return lastRegion;
}
 
Example 7
Source File: TypeScriptAutoIndentStrategy.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
 * Leaves leading comment signs alone.
 *
 * @param document the document
 * @param line the line
 * @param indent the indentation to insert
 * @param tabLength the length of a tab
 * @throws BadLocationException on concurrent document modification
 */
private void addIndent(Document document, int line, CharSequence indent, int tabLength) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int insert= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// Compute insert after all leading line comment markers
	int newInsert= insert;
	while (newInsert < endOffset - 2 && document.get(newInsert, 2).equals(LINE_COMMENT))
		newInsert += 2;

	// Heuristic to check whether it is commented code or just a comment
	if (newInsert > insert) {
		int whitespaceCount= 0;
		int i= newInsert;
		while (i < endOffset - 1) {
			 char ch= document.get(i, 1).charAt(0);
			 if (!Character.isWhitespace(ch))
				 break;
			 whitespaceCount= whitespaceCount + computeVisualLength(ch, tabLength);
			 i++;
		}

		if (whitespaceCount != 0 && whitespaceCount >= /*CodeFormatterUtil.getIndentWidth(fProject)*/ fProject.getFormatOptions().getIndentSize())
			insert= newInsert;
	}

	// Insert indent
	document.replace(insert, 0, indent.toString());
}
 
Example 8
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Indents line <code>line</code> in <code>document</code> with <code>indent</code>.
 * Leaves leading comment signs alone.
 *
 * @param document the document
 * @param line the line
 * @param indent the indentation to insert
 * @param tabLength the length of a tab
 * @throws BadLocationException on concurrent document modification
 */
private void addIndent(Document document, int line, CharSequence indent, int tabLength) throws BadLocationException {
	IRegion region= document.getLineInformation(line);
	int insert= region.getOffset();
	int endOffset= region.getOffset() + region.getLength();

	// Compute insert after all leading line comment markers
	int newInsert= insert;
	while (newInsert < endOffset - 2 && document.get(newInsert, 2).equals(LINE_COMMENT))
		newInsert += 2;

	// Heuristic to check whether it is commented code or just a comment
	if (newInsert > insert) {
		int whitespaceCount= 0;
		int i= newInsert;
		while (i < endOffset - 1) {
			 char ch= document.get(i, 1).charAt(0);
			 if (!Character.isWhitespace(ch))
				 break;
			 whitespaceCount= whitespaceCount + computeVisualLength(ch, tabLength);
			 i++;
		}

		if (whitespaceCount != 0 && whitespaceCount >= CodeFormatterUtil.getIndentWidth(fProject))
			insert= newInsert;
	}

	// Insert indent
	document.replace(insert, 0, indent.toString());
}
 
Example 9
Source File: PyParserTest.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
public void testTryReparse() throws BadLocationException {
    Document doc = new Document("");
    for (int i = 0; i < 5; i++) {
        doc.replace(0, 0, "this is a totally and completely not parseable doc\n");
    }

    PyParser.ParserInfo parserInfo = new PyParser.ParserInfo(doc, IPythonNature.LATEST_GRAMMAR_PY2_VERSION, null);
    ParseOutput reparseDocument = PyParser.reparseDocument(parserInfo);
    assertTrue(reparseDocument.ast == null);
    assertTrue(reparseDocument.error != null);
}
 
Example 10
Source File: TypeScriptAutoIndentStrategy.java    From typescript.java with MIT License 4 votes vote down vote up
private int getPeerPosition(IDocument document, DocumentCommand command) {
if (document.getLength() == 0)
	return 0;
  	/*
  	 * Search for scope closers in the pasted text and find their opening peers
  	 * in the document.
  	 */
  	Document pasted= new Document(command.text);
  	installJavaStuff(pasted);
  	int firstPeer= command.offset;

  	JavaHeuristicScanner pScanner= new JavaHeuristicScanner(pasted);
  	JavaHeuristicScanner dScanner= new JavaHeuristicScanner(document);

  	// add scope relevant after context to peer search
  	int afterToken= dScanner.nextToken(command.offset + command.length, JavaHeuristicScanner.UNBOUND);
  	try {
	switch (afterToken) {
	case Symbols.TokenRBRACE:
		pasted.replace(pasted.getLength(), 0, "}"); //$NON-NLS-1$
		break;
	case Symbols.TokenRPAREN:
		pasted.replace(pasted.getLength(), 0, ")"); //$NON-NLS-1$
		break;
	case Symbols.TokenRBRACKET:
		pasted.replace(pasted.getLength(), 0, "]"); //$NON-NLS-1$
		break;
	}
} catch (BadLocationException e) {
	// cannot happen
	Assert.isTrue(false);
}

  	int pPos= 0; // paste text position (increasing from 0)
  	int dPos= Math.max(0, command.offset - 1); // document position (decreasing from paste offset)
  	while (true) {
  		int token= pScanner.nextToken(pPos, JavaHeuristicScanner.UNBOUND);
 			pPos= pScanner.getPosition();
  		switch (token) {
  			case Symbols.TokenLBRACE:
  			case Symbols.TokenLBRACKET:
  			case Symbols.TokenLPAREN:
  				pPos= skipScope(pScanner, pPos, token);
  				if (pPos == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				break; // closed scope -> keep searching
  			case Symbols.TokenRBRACE:
  				int peer= dScanner.findOpeningPeer(dPos, '{', '}');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenRBRACKET:
  				peer= dScanner.findOpeningPeer(dPos, '[', ']');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenRPAREN:
  				peer= dScanner.findOpeningPeer(dPos, '(', ')');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenCASE:
  			case Symbols.TokenDEFAULT:
  				TypeScriptIndenter indenter= new TypeScriptIndenter(document, dScanner, fProject);
  				peer= indenter.findReferencePosition(dPos, false, false, false, true);
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching

  			case Symbols.TokenEOF:
  				return firstPeer;
  			default:
  				// keep searching
  		}
  	}
  }
 
Example 11
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private int getPeerPosition(IDocument document, DocumentCommand command) {
if (document.getLength() == 0)
	return 0;
  	/*
  	 * Search for scope closers in the pasted text and find their opening peers
  	 * in the document.
  	 */
  	Document pasted= new Document(command.text);
  	installJavaStuff(pasted);
  	int firstPeer= command.offset;

  	JavaHeuristicScanner pScanner= new JavaHeuristicScanner(pasted);
  	JavaHeuristicScanner dScanner= new JavaHeuristicScanner(document);

  	// add scope relevant after context to peer search
  	int afterToken= dScanner.nextToken(command.offset + command.length, JavaHeuristicScanner.UNBOUND);
  	try {
	switch (afterToken) {
	case Symbols.TokenRBRACE:
		pasted.replace(pasted.getLength(), 0, "}"); //$NON-NLS-1$
		break;
	case Symbols.TokenRPAREN:
		pasted.replace(pasted.getLength(), 0, ")"); //$NON-NLS-1$
		break;
	case Symbols.TokenRBRACKET:
		pasted.replace(pasted.getLength(), 0, "]"); //$NON-NLS-1$
		break;
	}
} catch (BadLocationException e) {
	// cannot happen
	Assert.isTrue(false);
}

  	int pPos= 0; // paste text position (increasing from 0)
  	int dPos= Math.max(0, command.offset - 1); // document position (decreasing from paste offset)
  	while (true) {
  		int token= pScanner.nextToken(pPos, JavaHeuristicScanner.UNBOUND);
 			pPos= pScanner.getPosition();
  		switch (token) {
  			case Symbols.TokenLBRACE:
  			case Symbols.TokenLBRACKET:
  			case Symbols.TokenLPAREN:
  				pPos= skipScope(pScanner, pPos, token);
  				if (pPos == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				break; // closed scope -> keep searching
  			case Symbols.TokenRBRACE:
  				int peer= dScanner.findOpeningPeer(dPos, '{', '}');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenRBRACKET:
  				peer= dScanner.findOpeningPeer(dPos, '[', ']');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenRPAREN:
  				peer= dScanner.findOpeningPeer(dPos, '(', ')');
  				dPos= peer - 1;
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching
  			case Symbols.TokenCASE:
  			case Symbols.TokenDEFAULT:
  				JavaIndenter indenter= new JavaIndenter(document, dScanner, fProject);
  				peer= indenter.findReferencePosition(dPos, false, false, false, true);
  				if (peer == JavaHeuristicScanner.NOT_FOUND)
  					return firstPeer;
  				firstPeer= peer;
  				break; // keep searching

  			case Symbols.TokenEOF:
  				return firstPeer;
  			default:
  				// keep searching
  		}
  	}
  }
 
Example 12
Source File: PyParserEditorIntegrationTest.java    From Pydev with Eclipse Public License 1.0 4 votes vote down vote up
public void testDifferentEditorsSameInput() throws Exception {
    IEclipsePreferences preferences = new InMemoryEclipsePreferences();
    PyParserManager pyParserManager = PyParserManager.getPyParserManager(preferences);

    Document doc = new Document();
    PydevFileEditorInputStub input = new PydevFileEditorInputStub();
    //create them with the same input
    PyEditStub pyEdit1 = new PyEditStub(doc, input);
    PyEditStub pyEdit2 = new PyEditStub(doc, input);
    pyParserManager.attachParserTo(pyEdit1);
    checkParserChanged(pyEdit1, 1);

    doc.replace(0, 0, "\r\ntest");
    checkParserChanged(pyEdit1, 2);

    pyParserManager.attachParserTo(pyEdit2);
    checkParserChanged(pyEdit1, 3);
    checkParserChanged(pyEdit2, 1);

    IDocument doc2 = new Document();
    pyEdit2.setDocument(doc2);
    pyParserManager.notifyEditorDisposed(pyEdit1);

    checkParserChanged(pyEdit1, 3);
    checkParserChanged(pyEdit2, 2);

    assertNull(pyParserManager.getParser(pyEdit1));
    doc2.replace(0, 0, "\r\ntest");
    checkParserChanged(pyEdit1, 3);
    checkParserChanged(pyEdit2, 3);

    doc.replace(0, 0, "\r\ntest"); //no one's listening this one anymore
    waitABit();
    checkParserChanged(pyEdit1, 3);
    checkParserChanged(pyEdit2, 3);

    pyParserManager.notifyEditorDisposed(pyEdit2);
    assertNull(pyParserManager.getParser(pyEdit2));
    doc2.replace(0, 0, "\r\ntest"); //no one's listening this one anymore
    doc.replace(0, 0, "\r\ntest"); //no one's listening this one anymore
    waitABit();
    checkParserChanged(pyEdit1, 3);
    checkParserChanged(pyEdit2, 3);
    assertEquals(0, pyParserManager.getParsers().size());

}