/** * Degenerates the given edit tree into a list.<br> * All nodes of the result are leafs.<br> * <strong>The given edit is modified and can no longer be used.</strong> * * @param edit the edit tree to flatten * @return a list of edits * @since 3.4 */ public static MultiTextEdit flatten(TextEdit edit) { MultiTextEdit result= new MultiTextEdit(); flatten(edit, result); return result; }
/** * Create an edit which contains <code>edit1</code> and <code>edit2</code> * <p>If <code>edit1</code> overlaps <code>edit2</code> this method fails with a {@link MalformedTreeException}</p> * <p><strong>The given edits are modified and they can no longer be used.</strong></p> * * @param edit1 the edit to merge with edit2 * @param edit2 the edit to merge with edit1 * @return the merged tree * @throws MalformedTreeException if {@link #overlaps(TextEdit, TextEdit)} returns <b>true</b> * @see #overlaps(TextEdit, TextEdit) * @since 3.4 */ public static TextEdit merge(TextEdit edit1, TextEdit edit2) { if (edit1 instanceof MultiTextEdit && !edit1.hasChildren()) { return edit2; } if (edit2 instanceof MultiTextEdit && !edit2.hasChildren()) { return edit1; } MultiTextEdit result= new MultiTextEdit(); merge(edit1, edit2, result); return result; }
if (!TextEditUtil.isPacked(formatEdit)) { formatEdit= TextEditUtil.flatten(formatEdit); if (j < lineExclusiveEnd) { DeleteEdit edit= new DeleteEdit(j, lineExclusiveEnd - j); if (!TextEditUtil.overlaps(formatEdit, edit)) { otherEdit.addChild(edit); group.addTextEdit(edit); if (j < lineExclusiveEnd) { DeleteEdit edit= new DeleteEdit(j, lineExclusiveEnd - j); if (!TextEditUtil.overlaps(formatEdit, edit)) { otherEdit.addChild(edit); group.addTextEdit(edit); TextEdit child= children[i]; edit.removeChild(child); if (!TextEditUtil.overlaps(formatEdit, child) && !TextEditUtil.overlaps(otherEdit, child)) { otherEdit.addChild(child); group.addTextEdit(child); if (!TextEditUtil.overlaps(formatEdit, edit) && !TextEditUtil.overlaps(otherEdit, edit)) { otherEdit.addChild(edit); group.addTextEdit(edit); TextEdit resultEdit= TextEditUtil.merge(formatEdit, otherEdit); if (!resultEdit.hasChildren()) return null;
if (!isPacked(children[i])) return false;
if (!TextEditUtil.isPacked(formatEdit)) { formatEdit= TextEditUtil.flatten(formatEdit); if (j < lineExclusiveEnd) { DeleteEdit edit= new DeleteEdit(j, lineExclusiveEnd - j); if (!TextEditUtil.overlaps(formatEdit, edit)) { otherEdit.addChild(edit); group.addTextEdit(edit); if (j < lineExclusiveEnd) { DeleteEdit edit= new DeleteEdit(j, lineExclusiveEnd - j); if (!TextEditUtil.overlaps(formatEdit, edit)) { otherEdit.addChild(edit); group.addTextEdit(edit); TextEdit child= children[i]; edit.removeChild(child); if (!TextEditUtil.overlaps(formatEdit, child) && !TextEditUtil.overlaps(otherEdit, child)) { otherEdit.addChild(child); group.addTextEdit(child); if (!TextEditUtil.overlaps(formatEdit, edit) && !TextEditUtil.overlaps(otherEdit, edit)) { otherEdit.addChild(edit); group.addTextEdit(edit); TextEdit resultEdit= TextEditUtil.merge(formatEdit, otherEdit); if (!resultEdit.hasChildren()) return null;
if (!isPacked(children[i])) return false;
/** * Degenerates the given edit tree into a list.<br> * All nodes of the result are leafs.<br> * <strong>The given edit is modified and can no longer be used.</strong> * * @param edit the edit tree to flatten * @return a list of edits * @since 3.4 */ public static MultiTextEdit flatten(TextEdit edit) { MultiTextEdit result= new MultiTextEdit(); flatten(edit, result); return result; }
/** * Create an edit which contains <code>edit1</code> and <code>edit2</code> * <p>If <code>edit1</code> overlaps <code>edit2</code> this method fails with a {@link MalformedTreeException}</p> * <p><strong>The given edits are modified and they can no longer be used.</strong></p> * * @param edit1 the edit to merge with edit2 * @param edit2 the edit to merge with edit1 * @return the merged tree * @throws MalformedTreeException if {@link #overlaps(TextEdit, TextEdit)} returns <b>true</b> * @see #overlaps(TextEdit, TextEdit) * @since 3.4 */ public static TextEdit merge(TextEdit edit1, TextEdit edit2) { if (edit1 instanceof MultiTextEdit && !edit1.hasChildren()) { return edit2; } if (edit2 instanceof MultiTextEdit && !edit2.hasChildren()) { return edit1; } MultiTextEdit result= new MultiTextEdit(); merge(edit1, edit2, result); return result; }
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); }
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 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); } } }