Java Code Examples for org.eclipse.text.edits.TextEdit#getOffset()
The following examples show how to use
org.eclipse.text.edits.TextEdit#getOffset() .
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: TextEditUtil.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns true if the given <code>edit</code> is minimal. * <p> * That is if: * <ul> * <li><b>true</b> if <code>edit</code> is a leaf</li> * <li>if <code>edit</code> is a inner node then <b>true</b> if * <ul> * <li><code>edit</code> has same size as all its children</li> * <li><code>isPacked</code> is <b>true</b> for all children</li> * </ul> * </li> * </ul> * </p> * * @param edit the edit to verify * @return true if edit is minimal * @since 3.4 */ public static boolean isPacked(TextEdit edit) { if (!(edit instanceof MultiTextEdit)) return true; if (!edit.hasChildren()) return true; TextEdit[] children= edit.getChildren(); if (edit.getOffset() != children[0].getOffset()) return false; if (edit.getExclusiveEnd() != children[children.length - 1].getExclusiveEnd()) return false; for (int i= 0; i < children.length; i++) { if (!isPacked(children[i])) return false; } return true; }
Example 2
Source File: CompilationUnitChangeNode.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private boolean coveredBy(TextEditBasedChangeGroup group, IRegion sourceRegion) { int sLength= sourceRegion.getLength(); if (sLength == 0) return false; int sOffset= sourceRegion.getOffset(); int sEnd= sOffset + sLength - 1; TextEdit[] edits= group.getTextEdits(); for (int i= 0; i < edits.length; i++) { TextEdit edit= edits[i]; if (edit.isDeleted()) return false; int rOffset= edit.getOffset(); int rLength= edit.getLength(); int rEnd= rOffset + rLength - 1; if (rLength == 0) { if (!(sOffset < rOffset && rOffset <= sEnd)) return false; } else { if (!(sOffset <= rOffset && rEnd <= sEnd)) return false; } } return true; }
Example 3
Source File: NewFieldResolver.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private int findAbsolutePositionOfFieldNameBeginning(IDocument doc, TextEdit te, String name) { try { String temp = doc.get(te.getOffset(), te.getLength()); if (temp.contains("=")) { temp = temp.substring(0, temp.indexOf("=")); } int nameOcc = temp.lastIndexOf(name); if (nameOcc != -1) { return te.getOffset() + nameOcc; } // Variable name not included // We try if we can determine the position with some basic logic // This case should not happen but it's a good idea to have a fallback int offset = temp.length(); boolean semiColonFound = false; while (offset >= 0 && semiColonFound == false) { if (temp.charAt(offset) == ' ' && semiColonFound) return te.getOffset() + offset; if (temp.charAt(offset) == ';') semiColonFound = true; } } catch (BadLocationException e) { } return te.getOffset(); }
Example 4
Source File: FormatterHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static org.eclipse.lsp4j.TextEdit convertEdit(TextEdit edit, IDocument document) { org.eclipse.lsp4j.TextEdit textEdit = new org.eclipse.lsp4j.TextEdit(); if (edit instanceof ReplaceEdit) { ReplaceEdit replaceEdit = (ReplaceEdit) edit; textEdit.setNewText(replaceEdit.getText()); int offset = edit.getOffset(); textEdit.setRange(new Range(createPosition(document, offset), createPosition(document, offset + edit.getLength()))); } return textEdit; }
Example 5
Source File: OrganizeImportsHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
public static TextEdit wrapStaticImports(TextEdit edit, CompilationUnit root, ICompilationUnit unit) throws MalformedTreeException, CoreException { String[] favourites = PreferenceManager.getPrefs(unit.getResource()).getJavaCompletionFavoriteMembers(); if (favourites.length == 0) { return edit; } IJavaProject project = unit.getJavaProject(); if (JavaModelUtil.is50OrHigher(project)) { List<SimpleName> typeReferences = new ArrayList<>(); List<SimpleName> staticReferences = new ArrayList<>(); ImportReferencesCollector.collect(root, project, null, typeReferences, staticReferences); if (staticReferences.isEmpty()) { return edit; } ImportRewrite importRewrite = CodeStyleConfiguration.createImportRewrite(root, true); AST ast = root.getAST(); ASTRewrite astRewrite = ASTRewrite.create(ast); for (SimpleName node : staticReferences) { addImports(root, unit, favourites, importRewrite, ast, astRewrite, node, true); addImports(root, unit, favourites, importRewrite, ast, astRewrite, node, false); } TextEdit staticEdit = importRewrite.rewriteImports(null); if (staticEdit != null && staticEdit.getChildrenSize() > 0) { TextEdit lastStatic = staticEdit.getChildren()[staticEdit.getChildrenSize() - 1]; if (lastStatic instanceof DeleteEdit) { if (edit.getChildrenSize() > 0) { TextEdit last = edit.getChildren()[edit.getChildrenSize() - 1]; if (last instanceof DeleteEdit && lastStatic.getOffset() == last.getOffset() && lastStatic.getLength() == last.getLength()) { edit.removeChild(last); } } } TextEdit firstStatic = staticEdit.getChildren()[0]; if (firstStatic instanceof InsertEdit) { if (edit.getChildrenSize() > 0) { TextEdit firstEdit = edit.getChildren()[0]; if (firstEdit instanceof InsertEdit) { if (areEqual((InsertEdit) firstEdit, (InsertEdit) firstStatic)) { edit.removeChild(firstEdit); } } } } try { staticEdit.addChild(edit); return staticEdit; } catch (MalformedTreeException e) { JavaLanguageServerPlugin.logException("Failed to resolve static organize imports source action", e); } } } return edit; }
Example 6
Source File: JavaFormatter.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private List<TypedPosition> createRangeMarkers(TemplateVariable[] variables, IDocument document) throws MalformedTreeException, BadLocationException { Map<ReplaceEdit, String> markerToOriginal= new HashMap<ReplaceEdit, String>(); MultiTextEdit root= new MultiTextEdit(0, document.getLength()); List<TextEdit> edits= new ArrayList<TextEdit>(); boolean hasModifications= false; for (int i= 0; i != variables.length; i++) { final TemplateVariable variable= variables[i]; int[] offsets= variable.getOffsets(); String value= variable.getDefaultValue(); if (isWhitespaceVariable(value)) { // replace whitespace positions with unformattable comments String placeholder= COMMENT_START + value + COMMENT_END; for (int j= 0; j != offsets.length; j++) { ReplaceEdit replace= new ReplaceEdit(offsets[j], value.length(), placeholder); root.addChild(replace); hasModifications= true; markerToOriginal.put(replace, value); edits.add(replace); } } else { for (int j= 0; j != offsets.length; j++) { RangeMarker marker= new RangeMarker(offsets[j], value.length()); root.addChild(marker); edits.add(marker); } } } if (hasModifications) { // update the document and convert the replaces to markers root.apply(document, TextEdit.UPDATE_REGIONS); } List<TypedPosition> positions= new ArrayList<TypedPosition>(); for (Iterator<TextEdit> it= edits.iterator(); it.hasNext();) { TextEdit edit= it.next(); try { // abuse TypedPosition to piggy back the original contents of the position final TypedPosition pos= new TypedPosition(edit.getOffset(), edit.getLength(), markerToOriginal.get(edit)); document.addPosition(CATEGORY, pos); positions.add(pos); } catch (BadPositionCategoryException x) { Assert.isTrue(false); } } return positions; }
Example 7
Source File: JavaFormatter.java From goclipse with Eclipse Public License 1.0 | 4 votes |
private List<TypedPosition> createRangeMarkers(TemplateVariable[] variables, IDocument document) throws MalformedTreeException, BadLocationException { Map<ReplaceEdit, String> markerToOriginal= new HashMap<ReplaceEdit, String>(); MultiTextEdit root= new MultiTextEdit(0, document.getLength()); List<TextEdit> edits= new ArrayList<TextEdit>(); boolean hasModifications= false; for (int i= 0; i != variables.length; i++) { final TemplateVariable variable= variables[i]; int[] offsets= variable.getOffsets(); String value= variable.getDefaultValue(); if (isWhitespaceVariable(value)) { // replace whitespace positions with unformattable comments String placeholder= COMMENT_START + value + COMMENT_END; for (int j= 0; j != offsets.length; j++) { ReplaceEdit replace= new ReplaceEdit(offsets[j], value.length(), placeholder); root.addChild(replace); hasModifications= true; markerToOriginal.put(replace, value); edits.add(replace); } } else { for (int j= 0; j != offsets.length; j++) { RangeMarker marker= new RangeMarker(offsets[j], value.length()); root.addChild(marker); edits.add(marker); } } } if (hasModifications) { // update the document and convert the replaces to markers root.apply(document, TextEdit.UPDATE_REGIONS); } List<TypedPosition> positions= new ArrayList<TypedPosition>(); for (Iterator<TextEdit> it= edits.iterator(); it.hasNext();) { TextEdit edit= it.next(); try { // abuse TypedPosition to piggy back the original contents of the position final TypedPosition pos= new TypedPosition(edit.getOffset(), edit.getLength(), markerToOriginal.get(edit)); document.addPosition(CATEGORY, pos); positions.add(pos); } catch (BadPositionCategoryException x) { Assert.isTrue(false); } } return positions; }