Java Code Examples for org.eclipse.xtext.ui.editor.model.IXtextDocument#replace()
The following examples show how to use
org.eclipse.xtext.ui.editor.model.IXtextDocument#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: XtendQuickfixProvider.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
protected void replaceKeyword(Keyword keyword, String replacement, EObject container, IXtextDocument document) throws BadLocationException { ICompositeNode node = NodeModelUtils.findActualNodeFor(container); if (node != null) { for (ILeafNode leafNode : node.getLeafNodes()) { if (leafNode.getGrammarElement() == keyword) { ITextRegion leafRegion = leafNode.getTextRegion(); String actualReplacement = replacement; if (!Character.isWhitespace(document.getChar(leafRegion.getOffset() - 1))) { actualReplacement = " " + replacement; } document.replace(leafRegion.getOffset(), leafRegion.getLength(), actualReplacement); } } } }
Example 2
Source File: XbaseQuickfixProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected void moveUp(ICompositeNode node, ICompositeNode previousNode, IModificationContext context) throws BadLocationException { IXtextDocument document = context.getXtextDocument(); String text = node.getText() + previousNode.getText(); text = text.trim(); remove(document, node); document.replace(previousNode.getOffset(), previousNode.getLength(), text); }
Example 3
Source File: XbaseQuickfixProvider.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
protected void remove(IXtextDocument document, ICompositeNode node) throws BadLocationException { int offset = node.getOffset(); int length = node.getLength(); if (node.hasPreviousSibling()) { INode previousSibling = node.getPreviousSibling(); int endOffset = previousSibling.getEndOffset(); length = length + (offset - endOffset); offset = endOffset; } document.replace(offset, length, ""); }
Example 4
Source File: SARLQuickfixProvider.java From sarl with Apache License 2.0 | 5 votes |
/** Remove the portion of text, and the whitespaces before the text until the given separator. * * @param offset the offset where to start to remove. * @param length the length of the text to remove. * @param document the document. * @param separator the separator to consider. * @return <code>true</code> if the separator was found, <code>false</code> if not. * @throws BadLocationException if there is a problem with the location of the element. */ public boolean removeToPreviousSeparator(int offset, int length, IXtextDocument document, String separator) throws BadLocationException { // Skip spaces before the identifier until the separator int index = offset - 1; char c = document.getChar(index); while (Character.isWhitespace(c)) { index--; c = document.getChar(index); } // Test if it previous non-space character is the separator final boolean foundSeparator = document.getChar(index) == separator.charAt(0); if (foundSeparator) { index--; c = document.getChar(index); // Skip the previous spaces while (Character.isWhitespace(c)) { index--; c = document.getChar(index); } final int delta = offset - index - 1; document.replace(index + 1, length + delta, ""); //$NON-NLS-1$ } return foundSeparator; }
Example 5
Source File: SARLQuickfixProvider.java From sarl with Apache License 2.0 | 5 votes |
/** Remove the element related to the issue, and the whitespaces after the element until the given separator. * * @param issue the issue. * @param document the document. * @param separator the separator to consider. * @return <code>true</code> if the separator was found, <code>false</code> if not. * @throws BadLocationException if there is a problem with the location of the element. */ public boolean removeToNextSeparator(Issue issue, IXtextDocument document, String separator) throws BadLocationException { // Skip spaces after the identifier until the separator int index = issue.getOffset() + issue.getLength(); char c = document.getChar(index); while (Character.isWhitespace(c)) { index++; c = document.getChar(index); } // Test if it next non-space character is the separator final boolean foundSeparator = document.getChar(index) == separator.charAt(0); if (foundSeparator) { index++; c = document.getChar(index); // Skip the previous spaces while (Character.isWhitespace(c)) { index++; c = document.getChar(index); } final int newLength = index - issue.getOffset(); document.replace(issue.getOffset(), newLength, ""); //$NON-NLS-1$ } return foundSeparator; }
Example 6
Source File: SARLQuickfixProvider.java From sarl with Apache License 2.0 | 5 votes |
/** Remove the element related to the issue, and the whitespaces before the element until one of the given * keywords is encountered. * * @param issue the issue. * @param document the document. * @param keyword1 the first keyword to consider. * @param otherKeywords other keywords. * @return <code>true</code> if one keyword was found, <code>false</code> if not. * @throws BadLocationException if there is a problem with the location of the element. */ public boolean removeToPreviousKeyword(Issue issue, IXtextDocument document, String keyword1, String... otherKeywords) throws BadLocationException { // Skip spaces before the element int index = issue.getOffset() - 1; char c = document.getChar(index); while (Character.isWhitespace(c)) { index--; c = document.getChar(index); } // Skip non-spaces before the identifier final StringBuffer kw = new StringBuffer(); while (!Character.isWhitespace(c)) { kw.insert(0, c); index--; c = document.getChar(index); } if (kw.toString().equals(keyword1) || Arrays.contains(otherKeywords, kw.toString())) { // Skip spaces before the previous keyword while (Character.isWhitespace(c)) { index--; c = document.getChar(index); } final int delta = issue.getOffset() - index - 1; document.replace(index + 1, issue.getLength() + delta, ""); //$NON-NLS-1$ return true; } return false; }
Example 7
Source File: PasteJavaCodeHandler.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
private void doPasteJavaCode(final XtextEditor activeXtextEditor, final String javaCode, final JavaImportData javaImports) throws ExecutionException { ISourceViewer sourceViewer = activeXtextEditor.getInternalSourceViewer(); final IXtextDocument xtextDocument = activeXtextEditor.getDocument(); IJavaProject project = null; IProject iProject = null; IEditorInput editorInput = activeXtextEditor.getEditorInput(); if (editorInput instanceof IFileEditorInput) { iProject = ((IFileEditorInput) editorInput).getFile().getProject(); project = JavaCore.create(iProject); } final int selectionOffset = Math.max(0, sourceViewer.getSelectedRange().x - 1); EObject targetElement = xtextDocument.readOnly(new IUnitOfWork<EObject, XtextResource>() { @Override public EObject exec(XtextResource state) throws Exception { IParseResult parseResult = state.getParseResult(); if (parseResult == null) { return null; } ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), selectionOffset); if (leafNode == null) { return parseResult.getRootASTElement(); } return leafNode.getSemanticElement(); } }); JavaConverter javaConverter = javaConverterProvider.get(); final String xtendCode = javaConverter.toXtend(javaCode, javaImports != null ? javaImports.getImports() : null, targetElement, project, conditionalExpressionsAllowed(iProject)); if (!Strings.isEmpty(xtendCode)) { if (javaImports != null) { importsUtil.addImports(javaImports.getImports(), javaImports.getStaticImports(), new String[] {}, xtextDocument); } Point selection = sourceViewer.getSelectedRange(); try { xtextDocument.replace(selection.x, selection.y, xtendCode); } catch (BadLocationException e) { throw new ExecutionException("Failed to replace content.", e); } //TODO enable formatting, when performance became better // https://bugs.eclipse.org/bugs/show_bug.cgi?id=457814 // doFormat(sourceViewer, xtendCode, selection); } }
Example 8
Source File: GamlQuickfixProvider.java From gama with GNU General Public License v3.0 | 4 votes |
@Override public void apply(final IModificationContext context) throws BadLocationException { final IXtextDocument xtextDocument = context.getXtextDocument(); xtextDocument.replace(offset, length, text); }
Example 9
Source File: GamlQuickfixProvider.java From gama with GNU General Public License v3.0 | 4 votes |
@Override public void apply(final IModificationContext context) throws BadLocationException { final IXtextDocument xtextDocument = context.getXtextDocument(); final String tmp = text + xtextDocument.get(offset, length) + suffix; xtextDocument.replace(offset, length, tmp); }
Example 10
Source File: SARLQuickfixProvider.java From sarl with Apache License 2.0 | 4 votes |
/** Remove the element related to the issue, and the whitespaces before the element until the begin separator, * and the whitespaces after the element until the end separator. * * @param issue the issue. * @param document the document. * @param beginSeparator the separator before the element. * @param endSeparator the separator after the element. * @return <code>true</code> if the separator was found, <code>false</code> if not. * @throws BadLocationException if there is a problem with the location of the element. */ public boolean removeBetweenSeparators(Issue issue, IXtextDocument document, String beginSeparator, String endSeparator) throws BadLocationException { int offset = issue.getOffset(); int length = issue.getLength(); // Skip spaces before the identifier until the separator int index = offset - 1; char c = document.getChar(index); while (Character.isWhitespace(c)) { index--; c = document.getChar(index); } // Test if it previous non-space character is the separator boolean foundSeparator = document.getChar(index) == beginSeparator.charAt(0); if (foundSeparator) { index--; c = document.getChar(index); // Skip the previous spaces while (Character.isWhitespace(c)) { index--; c = document.getChar(index); } length = length + (offset - index - 1); offset = index + 1; // Skip spaces after the identifier until the separator index = offset + length; c = document.getChar(index); while (Character.isWhitespace(c)) { index++; c = document.getChar(index); } // Test if it next non-space character is the separator foundSeparator = document.getChar(index) == endSeparator.charAt(0); if (foundSeparator) { index++; length = index - offset; document.replace(offset, length, ""); //$NON-NLS-1$ } } return foundSeparator; }
Example 11
Source File: ReturnTypeReplaceModification.java From sarl with Apache License 2.0 | 4 votes |
@Override public void apply(EObject element, IModificationContext context) throws Exception { final IXtextDocument document = context.getXtextDocument(); document.replace(getIssue().getOffset(), getIssue().getLength(), this.expectedType); }