use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class StringFix method createCleanUp.
private static ICleanUpFix createCleanUp(CompilationUnit compilationUnit, boolean addNLSTag, boolean removeNLSTag, IProblemLocation[] problems) throws CoreException, JavaModelException {
ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
if (!cu.isStructureKnown())
//[clean up] 'Remove unnecessary $NLS-TAGS$' removes necessary ones in case of syntax errors: https://bugs.eclipse.org/bugs/show_bug.cgi?id=285814 :
return null;
List<CategorizedTextEditGroup> result = new ArrayList<CategorizedTextEditGroup>();
List<IProblemLocation> missingNLSProblems = new ArrayList<IProblemLocation>();
for (int i = 0; i < problems.length; i++) {
IProblemLocation problem = problems[i];
if (addNLSTag && problem.getProblemId() == IProblem.NonExternalizedStringLiteral) {
missingNLSProblems.add(problem);
}
if (removeNLSTag && problem.getProblemId() == IProblem.UnnecessaryNLSTag) {
IBuffer buffer = cu.getBuffer();
if (buffer != null) {
TextEdit edit = StringFix.getReplace(problem.getOffset(), problem.getLength(), buffer, false);
if (edit != null) {
String label = FixMessages.StringFix_RemoveNonNls_description;
result.add(new CategorizedTextEditGroup(label, edit, new GroupCategorySet(new GroupCategory(label, label, label))));
}
}
}
}
if (!missingNLSProblems.isEmpty()) {
int[] positions = new int[missingNLSProblems.size()];
int i = 0;
for (Iterator<IProblemLocation> iter = missingNLSProblems.iterator(); iter.hasNext(); ) {
IProblemLocation problem = iter.next();
positions[i] = problem.getOffset();
i++;
}
//TODO nls
//NLSUtil.createNLSEdits(cu, positions);
TextEdit[] edits = null;
if (edits != null) {
for (int j = 0; j < edits.length; j++) {
String label = FixMessages.StringFix_AddNonNls_description;
result.add(new CategorizedTextEditGroup(label, edits[j], new GroupCategorySet(new GroupCategory(label, label, label))));
}
}
}
if (result.isEmpty())
return null;
//$NON-NLS-1$
return new StringFix("", compilationUnit, result.toArray(new TextEditGroup[result.size()]));
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class QuickAssistProcessor method getConvertToMultiCatchProposals.
private static boolean getConvertToMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject()))
return false;
CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE);
if (catchClause == null) {
return false;
}
Statement statement = ASTResolving.findParentStatement(covering);
if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
// selection is in a statement inside the body
return false;
}
Type type1 = catchClause.getException().getType();
Type selectedMultiCatchType = null;
if (type1.isUnionType() && covering instanceof Name) {
Name topMostName = ASTNodes.getTopMostName((Name) covering);
ASTNode parent = topMostName.getParent();
if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
selectedMultiCatchType = (Type) parent;
}
}
if (selectedMultiCatchType != null)
return false;
TryStatement tryStatement = (TryStatement) catchClause.getParent();
List<CatchClause> catchClauses = tryStatement.catchClauses();
if (catchClauses.size() <= 1)
return false;
String commonSource = null;
try {
IBuffer buffer = context.getCompilationUnit().getBuffer();
for (Iterator<CatchClause> iterator = catchClauses.iterator(); iterator.hasNext(); ) {
CatchClause catchClause1 = iterator.next();
Block body = catchClause1.getBody();
String source = buffer.getText(body.getStartPosition(), body.getLength());
if (commonSource == null) {
commonSource = source;
} else {
if (!commonSource.equals(source))
return false;
}
}
} catch (JavaModelException e) {
return false;
}
if (resultingCollections == null)
return true;
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
TightSourceRangeComputer sourceRangeComputer = new TightSourceRangeComputer();
sourceRangeComputer.addTightSourceNode(catchClauses.get(catchClauses.size() - 1));
rewrite.setTargetSourceRangeComputer(sourceRangeComputer);
CatchClause firstCatchClause = catchClauses.get(0);
UnionType newUnionType = ast.newUnionType();
List<Type> types = newUnionType.types();
for (Iterator<CatchClause> iterator = catchClauses.iterator(); iterator.hasNext(); ) {
CatchClause catchClause1 = iterator.next();
Type type = catchClause1.getException().getType();
if (type instanceof UnionType) {
List<Type> types2 = ((UnionType) type).types();
for (Iterator<Type> iterator2 = types2.iterator(); iterator2.hasNext(); ) {
types.add((Type) rewrite.createCopyTarget(iterator2.next()));
}
} else {
types.add((Type) rewrite.createCopyTarget(type));
}
}
SingleVariableDeclaration newExceptionDeclaration = ast.newSingleVariableDeclaration();
newExceptionDeclaration.setType(newUnionType);
newExceptionDeclaration.setName((SimpleName) rewrite.createCopyTarget(firstCatchClause.getException().getName()));
rewrite.replace(firstCatchClause.getException(), newExceptionDeclaration, null);
for (int i = 1; i < catchClauses.size(); i++) {
rewrite.remove(catchClauses.get(i), null);
}
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
String label = CorrectionMessages.QuickAssistProcessor_convert_to_single_multicatch_block;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.COMBINE_CATCH_BLOCKS, image);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class JavadocContentAccess method internalGetContentReader.
/**
* Gets a reader for an IMember's Javadoc comment content from the source attachment.
* The content does contain only the text from the comment without the Javadoc leading star characters.
* Returns <code>null</code> if the member does not contain a Javadoc comment or if no source is available.
*
* @param member
* The member to get the Javadoc of.
* @return Returns a reader for the Javadoc comment content or <code>null</code> if the member
* does not contain a Javadoc comment or if no source is available
* @throws org.eclipse.jdt.core.JavaModelException
* is thrown when the elements javadoc can not be accessed
* @since 3.4
*/
private static Reader internalGetContentReader(IMember member) throws JavaModelException {
IBuffer buf = member.getOpenable().getBuffer();
if (buf == null) {
// no source attachment found
return null;
}
ISourceRange javadocRange = member.getJavadocRange();
if (javadocRange != null) {
JavaDocCommentReader reader = new JavaDocCommentReader(buf, javadocRange.getOffset(), javadocRange.getOffset() + javadocRange.getLength() - 1);
if (!containsOnlyInheritDoc(reader, javadocRange.getLength())) {
reader.reset();
return reader;
}
}
return null;
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class CodeAssist method computeAssistProposals.
@SuppressWarnings("unchecked")
public Proposals computeAssistProposals(IJavaProject project, String fqn, int offset, List<Problem> problems) throws CoreException {
ICompilationUnit compilationUnit;
IType type = project.findType(fqn);
if (type == null) {
return null;
}
if (type.isBinary()) {
throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.CORE_EXCEPTION, "Can't calculate Quick Assist on binary file"));
} else {
compilationUnit = type.getCompilationUnit();
}
IBuffer buffer = compilationUnit.getBuffer();
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(compilationUnit.getPath(), LocationKind.IFILE, new NullProgressMonitor());
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(compilationUnit.getPath(), LocationKind.IFILE);
IDocument document = textFileBuffer.getDocument();
TextViewer viewer = new TextViewer(document, new Point(offset, 0));
AssistContext context = new AssistContext(compilationUnit, offset, 0);
ArrayList proposals = new ArrayList<>();
JavaCorrectionProcessor.collectProposals(context, problems, true, true, proposals);
return convertProposals(offset, compilationUnit, viewer, proposals);
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class CodeAssist method computeProposals.
public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException {
WorkingCopyOwner copyOwner = new WorkingCopyOwner() {
@Override
public IBuffer createBuffer(ICompilationUnit workingCopy) {
return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content);
}
};
ICompilationUnit compilationUnit;
IType type = project.findType(fqn);
if (type == null) {
return null;
}
if (type.isBinary()) {
compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
} else {
compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
}
IBuffer buffer = compilationUnit.getBuffer();
IDocument document;
if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
} else {
document = new DocumentAdapter(buffer);
}
TextViewer viewer = new TextViewer(document, new Point(offset, 0));
JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
List<ICompletionProposal> proposals = new ArrayList<>();
proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
Collections.sort(proposals, new RelevanceSorter());
return convertProposals(offset, compilationUnit, viewer, proposals);
}
Aggregations