Search in sources :

Example 11 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method replaceLineCommentAfterJavaElement.

private void replaceLineCommentAfterJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
    if (i - 1 < 0) {
        throw new NotImplementedException(lineComment, "for a line comment situated after the java elements that it documents," + " and this line comment is not the last line comment to add to the javadoc.");
    }
    final LineComment previousLineComment = lineComments.get(i - 1);
    final int position = getEndPosition(previousLineComment);
    final String indent = getIndentForJavadoc(previousLineComment, source, lineStarts).substring(source);
    final StringBuilder newJavadoc = new StringBuilder().append(lineSeparator).append(indent).append(" *");
    appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
    newJavadoc.append(lineSeparator).append(indent).append(" */");
    commentEdits.add(new InsertEdit(position, newJavadoc.toString()));
    deleteLineCommentAfterNode(commentEdits, source, lineComment);
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) NotImplementedException(org.autorefactor.util.NotImplementedException) LineComment(org.eclipse.jdt.core.dom.LineComment)

Example 12 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.

the class DocumentLifeCycleHandler method handleChanged.

public void handleChanged(DidChangeTextDocumentParams params) {
    ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null || !unit.isWorkingCopy() || params.getContentChanges().isEmpty()) {
        return;
    }
    try {
        if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
            sharedASTProvider.disposeAST();
        }
        List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
        for (TextDocumentContentChangeEvent changeEvent : contentChanges) {
            Range range = changeEvent.getRange();
            int length;
            if (range != null) {
                length = changeEvent.getRangeLength().intValue();
            } else {
                // range is optional and if not given, the whole file content is replaced
                length = unit.getSource().length();
                range = JDTUtils.toRange(unit, 0, length);
            }
            int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter());
            TextEdit edit = null;
            String text = changeEvent.getText();
            if (length == 0) {
                edit = new InsertEdit(startOffset, text);
            } else if (text.isEmpty()) {
                edit = new DeleteEdit(startOffset, length);
            } else {
                edit = new ReplaceEdit(startOffset, length, text);
            }
            IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
            edit.apply(document, TextEdit.NONE);
        }
        triggerValidation(unit);
    } catch (JavaModelException | MalformedTreeException | BadLocationException e) {
        JavaLanguageServerPlugin.logException("Error while handling document change", e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) JavaModelException(org.eclipse.jdt.core.JavaModelException) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Range(org.eclipse.lsp4j.Range) DeleteEdit(org.eclipse.text.edits.DeleteEdit) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 13 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project titan.EclipsePlug-ins by eclipse.

the class ChangeCreator method insertField.

public int insertField(final TTCN3_Set_Seq_Choice_BaseType ss, final ILocateableNode node, final MultiTextEdit rootEdit, int vmLen) {
    final Location nodeLocation = node.getLocation();
    final int noc = ss.getNofComponents();
    if (settings.getPosition() < noc) {
        vmLen += 6;
        final ILocateableNode cf = (ILocateableNode) ss.getComponentByIndex(settings.getPosition());
        final Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), cf.getLocation().getOffset(), cf.getLocation().getEndOffset() + vmLen);
        rootEdit.addChild(new InsertEdit(l.getOffset(), settings.getType() + " " + settings.getId().getTtcnName() + ", \n  "));
    } else {
        vmLen += 5;
        final ILocateableNode cf = (ILocateableNode) ss.getComponentByIndex(noc - 1);
        final Location l = new Location(nodeLocation.getFile(), nodeLocation.getLine(), cf.getLocation().getEndOffset(), cf.getLocation().getEndOffset() + vmLen);
        rootEdit.addChild(new InsertEdit(l.getOffset() - 1, ",\n  " + settings.getType() + " " + settings.getId().getTtcnName()));
    }
    return vmLen;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) ILocateableNode(org.eclipse.titan.designer.AST.ILocateableNode) Location(org.eclipse.titan.designer.AST.Location)

Example 14 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project titan.EclipsePlug-ins by eclipse.

the class ExtractToFunctionRefactoring method createChange.

@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    createFunctionText();
    createFunctionCallText();
    // 
    final TextFileChange tfc = new TextFileChange(selectionFinder.getSelectedFile().getName(), selectionFinder.getSelectedFile());
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    // replace selection with function call & new declarations
    final int offset = selectionFinder.getSelectedStatements().getLocation().getOffset();
    final int len = selectionFinder.getSelectedStatements().getLocation().getEndOffset() - offset;
    rootEdit.addChild(new ReplaceEdit(offset, len, functionCallTextReady));
    // add new function after the one in which the selection is
    if (parentFunc != null && selectionFinder.getInsertLoc() >= 0) {
        rootEdit.addChild(new InsertEdit(selectionFinder.getInsertLoc(), functionTextReady));
    }
    return tfc;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 15 with InsertEdit

use of org.eclipse.text.edits.InsertEdit in project titan.EclipsePlug-ins by eclipse.

the class ChangeCreator method createFileChange.

private Change createFileChange(final IFile toVisit) {
    if (toVisit == null) {
        return null;
    }
    final ProjectSourceParser sourceParser = GlobalParser.getProjectSourceParser(toVisit.getProject());
    final Module module = sourceParser.containedModule(toVisit);
    if (module == null) {
        return null;
    }
    final DefinitionVisitor vis = new DefinitionVisitor();
    module.accept(vis);
    final List<FormalParameter> nodes = vis.getLocations();
    // Calculate edit locations
    final List<Location> locations = new ArrayList<Location>();
    try {
        final WorkspaceJob job1 = calculateEditLocations(nodes, toVisit, locations);
        job1.join();
    } catch (InterruptedException ie) {
        ErrorReporter.logExceptionStackTrace(ie);
    } catch (CoreException ce) {
        ErrorReporter.logError("LazyficationRefactoring: " + "CoreException while calculating edit locations in " + toVisit.getName() + ".");
        ErrorReporter.logExceptionStackTrace(ce);
    }
    if (locations.isEmpty()) {
        return null;
    }
    // Create a change for each edit location
    final TextFileChange tfc = new TextFileChange(toVisit.getName(), toVisit);
    final MultiTextEdit rootEdit = new MultiTextEdit();
    tfc.setEdit(rootEdit);
    for (Location l : locations) {
        rootEdit.addChild(new InsertEdit(l.getOffset(), "@lazy "));
    }
    return tfc;
}
Also used : FormalParameter(org.eclipse.titan.designer.AST.TTCN3.definitions.FormalParameter) InsertEdit(org.eclipse.text.edits.InsertEdit) ArrayList(java.util.ArrayList) WorkspaceJob(org.eclipse.core.resources.WorkspaceJob) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) ProjectSourceParser(org.eclipse.titan.designer.parsers.ProjectSourceParser) CoreException(org.eclipse.core.runtime.CoreException) Module(org.eclipse.titan.designer.AST.Module) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Location(org.eclipse.titan.designer.AST.Location)

Aggregations

InsertEdit (org.eclipse.text.edits.InsertEdit)73 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)33 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)25 TextEdit (org.eclipse.text.edits.TextEdit)24 DeleteEdit (org.eclipse.text.edits.DeleteEdit)22 Test (org.junit.Test)22 BadLocationException (org.eclipse.jface.text.BadLocationException)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 ArrayList (java.util.ArrayList)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)10 IDocument (org.eclipse.jface.text.IDocument)8 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)8 Location (org.eclipse.titan.designer.AST.Location)8 Module (org.eclipse.titan.designer.AST.Module)7 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)7 WorkspaceJob (org.eclipse.core.resources.WorkspaceJob)5 CoreException (org.eclipse.core.runtime.CoreException)5 IFile (org.eclipse.core.resources.IFile)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4