use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.
the class ChangeUtil method convertCreateCompilationUnitChange.
private static void convertCreateCompilationUnitChange(WorkspaceEdit edit, CreateCompilationUnitChange change) {
ICompilationUnit unit = change.getCu();
CreateFile createFile = new CreateFile();
createFile.setUri(JDTUtils.toURI(unit));
createFile.setOptions(new CreateFileOptions(false, true));
edit.getDocumentChanges().add(Either.forRight(createFile));
InsertEdit textEdit = new InsertEdit(0, change.getPreview());
convertTextEdit(edit, unit, textEdit);
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.
the class OrganizeImportsHandler method wrapStaticImports.
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;
}
use of org.eclipse.text.edits.InsertEdit in project eclipse.jdt.ls by eclipse.
the class CompletionHandlerTest method testSkipAdditionalEditForImport2.
@Test
public void testSkipAdditionalEditForImport2() throws JavaModelException, MalformedTreeException, BadLocationException {
// @formatter:off
ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java", "package org.sample;\n" + "import " + "public class Test {\n" + "}");
// @formatter:on
// mock the user's input behavior
int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), 1, 7);
InsertEdit edit = new InsertEdit(startOffset, "java.util.Arr");
IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
edit.apply(document, org.eclipse.text.edits.TextEdit.NONE);
CompletionList list = requestCompletions(unit, "java.util.Arr");
assertNotNull(list);
List<CompletionItem> items = new ArrayList<>(list.getItems());
CompletionItem item = items.get(0);
CompletionItem resolved = server.resolveCompletionItem(item).join();
assertNull(resolved.getAdditionalTextEdits());
}
use of org.eclipse.text.edits.InsertEdit in project che by eclipse.
the class RefactoringCorrectionProposal method createTextChange.
@Override
protected TextChange createTextChange() throws CoreException {
init(fRefactoring);
fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor());
if (fRefactoringStatus.hasFatalError()) {
//$NON-NLS-1$
TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource());
//$NON-NLS-1$
dummyChange.setEdit(new InsertEdit(0, ""));
return dummyChange;
}
return (TextChange) fRefactoring.createChange(new NullProgressMonitor());
}
use of org.eclipse.text.edits.InsertEdit in project che by eclipse.
the class CorrectPackageDeclarationProposal method addEdits.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
* .TextBuffer)
*/
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
ICompilationUnit cu = getCompilationUnit();
IPackageFragment parentPack = (IPackageFragment) cu.getParent();
IPackageDeclaration[] decls = cu.getPackageDeclarations();
if (parentPack.isDefaultPackage() && decls.length > 0) {
for (int i = 0; i < decls.length; i++) {
ISourceRange range = decls[i].getSourceRange();
root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
}
return;
}
if (!parentPack.isDefaultPackage() && decls.length == 0) {
String lineDelim = StubUtility.getLineDelimiterUsed(cu);
//$NON-NLS-1$
String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
root.addChild(new InsertEdit(0, str));
return;
}
root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Aggregations