Refine search
public String apply(String contents) { final TextEdit edit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, contents, 0, contents.length(), 0, Constants.LINE_SEPARATOR); if (edit == null) { // TODO log a fatal or warning here. Throwing an exception is causing the actual freemarker error to be lost return contents; } IDocument document = new Document(contents); try { edit.apply(document); } catch (Exception e) { throw new RuntimeException( "Failed to format the generated source code.", e); } return document.get(); } }
@Override protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException { if (fResultingEdit != null) { editRoot.addChild(fResultingEdit); } }
protected static boolean covers(TextEdit thisEdit, TextEdit otherEdit) { if (thisEdit.getLength() == 0) // an insertion point can't cover anything return false; int thisOffset = thisEdit.getOffset(); int thisEnd = thisEdit.getExclusiveEnd(); if (otherEdit.getLength() == 0) { int otherOffset = otherEdit.getOffset(); return thisOffset < otherOffset && otherOffset < thisEnd; } int otherOffset = otherEdit.getOffset(); int otherEnd = otherEdit.getExclusiveEnd(); return thisOffset <= otherOffset && otherEnd <= thisEnd; }
private void applyTransformation(IDocument document) throws MalformedTreeException { TextEdit newEdit= new MultiTextEdit(0, document.getLength()); ReplaceEdit[] replaces= fModifier.getModifications(document.get()); for (int i= 0; i < replaces.length; i++) { newEdit.addChild(replaces[i]); } try { newEdit.apply(document, TextEdit.NONE); } catch (BadLocationException cannotHappen) { Assert.isTrue(false); } }
return null; if (!edit.hasChildren()) return null; TextEdit[] children= edit.getChildren(); for (int i= 0; i < children.length; i++) { if (!(children[i] instanceof ReplaceEdit)) edit.copy().apply(doc, TextEdit.NONE); if (content.equals(doc.get())) return null; } catch (MalformedTreeException e) {
public static void insert(TextEdit parent, TextEdit edit) throws MalformedTreeException { if (!parent.hasChildren()) { parent.addChild(edit); return; } TextEdit[] children= parent.getChildren(); // First dive down to find the right parent. for (int i= 0; i < children.length; i++) { TextEdit child= children[i]; if (covers(child, edit)) { insert(child, edit); return; } } // We have the right parent. Now check if some of the children have to // be moved under the new edit since it is covering it. int removed= 0; for (int i= 0; i < children.length; i++) { TextEdit child= children[i]; if (covers(edit, child)) { parent.removeChild(i - removed++); edit.addChild(child); } } parent.addChild(edit); }
edits.apply(document); FileUtils.writeStringToFile(file, document.get());
private List<org.eclipse.lsp4j.TextEdit> format(ICompilationUnit cu, IDocument document, IRegion region, FormattingOptions options, boolean includeComments, IProgressMonitor monitor) { if (cu == null || document == null || region == null || monitor.isCanceled()) { return Collections.emptyList(); } CodeFormatter formatter = ToolFactory.createCodeFormatter(getOptions(options, cu)); String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document); String sourceToFormat = document.get(); int kind = getFormattingKind(cu, includeComments); TextEdit format = formatter.format(kind, sourceToFormat, region.getOffset(), region.getLength(), 0, lineDelimiter); if (format == null || format.getChildren().length == 0 || monitor.isCanceled()) { // nothing to return return Collections.<org.eclipse.lsp4j.TextEdit>emptyList(); } MultiTextEdit flatEdit = TextEditUtil.flatten(format); return convertEdits(flatEdit.getChildren(), document); }
public void apply(IDocument document, char trigger, int offset) { try { super.apply(document, trigger, offset); if (fImportRewrite != null && fImportRewrite.hasRecordedChanges()) { int oldLen= document.getLength(); fImportRewrite.rewriteImports(new NullProgressMonitor()).apply(document, TextEdit.UPDATE_REGIONS); setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen); } } catch (CoreException e) { JavaPlugin.log(e); } catch (BadLocationException e) { JavaPlugin.log(e); } }
private static void createEdit(TextEdit source, TextEdit target, Map<TextEdit, TextEdit> editMap) { TextEdit[] children= source.getChildren(); for (TextEdit child : children) { // a deleted child remains deleted even if the temporary buffer // gets modified. if (child.isDeleted()) continue; RangeMarker marker= new RangeMarker(child.getOffset(), child.getLength()); target.addChild(marker); editMap.put(marker, child); createEdit(child, marker, editMap); } }
/** * Convenience method that applies the edit returned from {@link #replace(ISelection, String)} * to the underlying document and adapts the selection accordingly. * * @param selection the selection to replace * @param replacement the replacement text * @throws BadLocationException if accessing the document failed */ public void doReplace(ISelection selection, String replacement) throws BadLocationException { TextEdit edit= replace(selection, replacement); boolean complex= edit.hasChildren(); if (complex && fRewriteTarget != null) fRewriteTarget.beginCompoundChange(); try { edit.apply(fDocument, TextEdit.UPDATE_REGIONS); if (fSelectionProvider != null) { ISelection empty= makeReplaceSelection(selection, replacement); fSelectionProvider.setSelection(empty); } } finally { if (complex && fRewriteTarget != null) fRewriteTarget.endCompoundChange(); } }
private static void flatten(TextEdit edit, MultiTextEdit result) { if (!edit.hasChildren()) { result.addChild(edit); } else { TextEdit[] children= edit.getChildren(); for (int i= 0; i < children.length; i++) { TextEdit child= children[i]; child.getParent().removeChild(0); flatten(child, result); } } }
private static void restorePositions(Map<TextEdit, TextEdit> editMap) { for (Entry<TextEdit, TextEdit> entry: editMap.entrySet()) { TextEdit marker = entry.getKey(); TextEdit edit= entry.getValue(); if (marker.isDeleted()) { edit.markAsDeleted(); } else { edit.adjustOffset(marker.getOffset() - edit.getOffset()); edit.adjustLength(marker.getLength() - edit.getLength()); } } } }
/** * Replaces given {@link ReplaceEdit} by new ReplaceEdit with each line commented out. * <p> * New ReplaceEdit has the same offset and length as the given ReplaceEdit. Text of new ReplaceEdit * has each line but the first one commented out. Given ReplaceEdit is removed from its parent, * and new ReplaceEdit is inserted in its place. * @param replaceEdit * @return new ReplaceEdit */ protected ReplaceEdit commentOutReplaceEdit(ReplaceEdit replaceEdit) { TextEdit parent = replaceEdit.getParent(); String newText = commentOutEachLine(replaceEdit.getText()); ReplaceEdit newEdit = new ReplaceEdit(replaceEdit.getOffset(), replaceEdit.getLength(), newText); parent.removeChild(replaceEdit); parent.addChild(newEdit); return newEdit; }
private static IRegion getCorrespondingEditChangeRange(SearchMatch searchResult, TextChangeManager manager) { TextChange change= getTextChange(searchResult, manager); if (change == null) return null; IRegion oldMatchRange= createTextRange(searchResult); TextEditChangeGroup[] editChanges= change.getTextEditChangeGroups(); for (int i= 0; i < editChanges.length; i++) { if (oldMatchRange.equals(editChanges[i].getRegion())) return TextEdit.getCoverage(change.getPreviewEdits(editChanges[i].getTextEdits())); } return null; }
private void flatten(List result, TextEdit edit) { result.add(edit); TextEdit[] children= edit.getChildren(); for (int i= 0; i < children.length; i++) { flatten(result, children[i]); } } }
private static TextEdit pack(TextEdit edit) { final List edits= new ArrayList(); edit.accept(new TextEditVisitor() { public boolean visitNode(TextEdit node) { if (node instanceof MultiTextEdit) return true; edits.add(node); return false; } }); MultiTextEdit result= new MultiTextEdit(); for (Iterator iterator= edits.iterator(); iterator.hasNext();) { TextEdit child= (TextEdit)iterator.next(); child.getParent().removeChild(child); TextChangeCompatibility.insert(result, child); } return result; }
protected boolean annotateEdit(TextEdit edit, String startTag, String endTag) { unchangedUntil(edit.getOffset()); fBuf.append(startTag); appendContent(fPreviewDocument, edit.getOffset(), edit.getExclusiveEnd(), false); fBuf.append(endTag); fWrittenToPos= edit.getExclusiveEnd(); return false; }