Java Code Examples for org.eclipse.text.edits.TextEdit#getChildrenSize()

The following examples show how to use org.eclipse.text.edits.TextEdit#getChildrenSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrganizeImportsCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) {
	try {
		InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1);
		CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", CodeActionKind.SourceOrganizeImports, unit, null, IProposalRelevance.ORGANIZE_IMPORTS) {
			@Override
			protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
				CompilationUnit astRoot = context.getASTRoot();
				OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
				TextEdit edit = op.createTextEdit(null);
				TextEdit staticEdit = OrganizeImportsHandler.wrapStaticImports(edit, astRoot, unit);
				if (staticEdit.getChildrenSize() > 0) {
					editRoot.addChild(staticEdit);
				}
			}
		};

		addWorkspaceEdit(unit, proposal, rootEdit);
	} catch (CoreException e) {
		JavaLanguageServerPlugin.logException("Problem organize imports ", e);
	}
}
 
Example 2
Source File: SourceAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private TextEdit getOrganizeImportsProposal(IInvocationContext context) {
	ICompilationUnit unit = context.getCompilationUnit();
	CompilationUnit astRoot = context.getASTRoot();
	OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
	try {
		TextEdit edit = op.createTextEdit(null);
		TextEdit staticEdit = OrganizeImportsHandler.wrapStaticImports(edit, astRoot, unit);
		if (staticEdit.getChildrenSize() > 0) {
			return staticEdit;
		}
		return edit;
	} catch (OperationCanceledException | CoreException e) {
		JavaLanguageServerPlugin.logException("Resolve organize imports source action", e);
	}

	return null;
}
 
Example 3
Source File: ImportsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static ICleanUpFix createCleanUp(final CompilationUnit cu, CodeGenerationSettings settings, boolean organizeImports, RefactoringStatus status) throws CoreException {
if (!organizeImports)
	return null;

final boolean hasAmbiguity[]= new boolean[] { false };
IChooseImportQuery query= new IChooseImportQuery() {
	public TypeNameMatch[] chooseImports(TypeNameMatch[][] openChoices, ISourceRange[] ranges) {
		hasAmbiguity[0]= true;
		return new TypeNameMatch[0];
	}
};

final ICompilationUnit unit= (ICompilationUnit)cu.getJavaElement();
OrganizeImportsOperation op= new OrganizeImportsOperation(unit, cu, settings.importIgnoreLowercase, false, false, query);
final TextEdit edit= op.createTextEdit(null);
if (hasAmbiguity[0]) {
	status.addInfo(Messages.format(ActionMessages.OrganizeImportsAction_multi_error_unresolvable, getLocationString(cu)));
}

if (op.getParseError() != null) {
	status.addInfo(Messages.format(ActionMessages.OrganizeImportsAction_multi_error_parse, getLocationString(cu)));
	return null;
}

if (edit == null || (edit instanceof MultiTextEdit && edit.getChildrenSize() == 0))
	return null;

return new ImportsFix(edit, unit, FixMessages.ImportsFix_OrganizeImports_Description);
  }
 
Example 4
Source File: OrganizeImportsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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;
}