Search in sources :

Example 11 with IBuffer

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()]));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) GroupCategorySet(org.eclipse.ltk.core.refactoring.GroupCategorySet) GroupCategory(org.eclipse.ltk.core.refactoring.GroupCategory) ArrayList(java.util.ArrayList) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) IBuffer(org.eclipse.jdt.core.IBuffer) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEdit(org.eclipse.text.edits.TextEdit) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 12 with IBuffer

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) Image(org.eclipse.swt.graphics.Image) IBuffer(org.eclipse.jdt.core.IBuffer) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 13 with IBuffer

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;
}
Also used : IBuffer(org.eclipse.jdt.core.IBuffer) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 14 with IBuffer

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) JavaModelStatus(org.eclipse.jdt.internal.core.JavaModelStatus) Point(org.eclipse.swt.graphics.Point) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) IDocument(org.eclipse.jface.text.IDocument)

Example 15 with IBuffer

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) TemplateCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.TemplateCompletionProposalComputer) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IBuffer (org.eclipse.jdt.core.IBuffer)16 ArrayList (java.util.ArrayList)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 IDocument (org.eclipse.jface.text.IDocument)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 ISourceRange (org.eclipse.jdt.core.ISourceRange)4 IType (org.eclipse.jdt.core.IType)4 DocumentAdapter (org.eclipse.jdt.internal.core.DocumentAdapter)4 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 TextEdit (org.eclipse.text.edits.TextEdit)3 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)2 JavaAllCompletionProposalComputer (org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 Point (org.eclipse.swt.graphics.Point)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2