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);
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations