Search in sources :

Example 36 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project liferay-ide by liferay.

the class JSPQuickAssistProcessor method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext context) {
    ICompletionProposal[] retval = null;
    List<ICompletionProposal> proposals = new ArrayList<>();
    ISourceViewer sourceViewer = context.getSourceViewer();
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    Iterator<Annotation> annotations = annotationModel.getAnnotationIterator();
    while (annotations.hasNext()) {
        Annotation annotation = annotations.next();
        Position position = annotationModel.getPosition(annotation);
        try {
            IMarker marker = _createTempMarker(annotation);
            int lineNum = sourceViewer.getDocument().getLineOfOffset(position.getOffset()) + 1;
            int currentLineNum = sourceViewer.getDocument().getLineOfOffset(context.getOffset()) + 1;
            if ((marker != null) && (currentLineNum == lineNum)) {
                if (marker.getAttribute(LiferayBaseValidator.MARKER_QUERY_ID, null) != null) {
                    ICompletionProposal[] resolutions = _createFromMarkerResolutions(marker);
                    if (ListUtil.isNotEmpty(resolutions)) {
                        Collections.addAll(proposals, resolutions);
                        if (annotation instanceof IQuickFixableAnnotation) {
                            IQuickFixableAnnotation quick = (IQuickFixableAnnotation) annotation;
                            quick.setQuickFixable(true);
                        }
                    }
                }
            }
        } catch (BadLocationException ble) {
            LiferayXMLSearchUI.logError("Error finding quick assists", ble);
        }
    }
    if (ListUtil.isNotEmpty(proposals)) {
        retval = proposals.toArray(new ICompletionProposal[0]);
    }
    return retval;
}
Also used : IQuickFixableAnnotation(org.eclipse.jface.text.quickassist.IQuickFixableAnnotation) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IQuickFixableAnnotation(org.eclipse.jface.text.quickassist.IQuickFixableAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IMarker(org.eclipse.core.resources.IMarker) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 37 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project liferay-ide by liferay.

the class AbstractQuickAssistProcessorFromMarkerResolution method computeQuickAssistProposals.

@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext context) {
    ICompletionProposal[] retval = null;
    List<ICompletionProposal> proposals = new ArrayList<>();
    ISourceViewer sourceViewer = context.getSourceViewer();
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    Iterator<Annotation> annotations = annotationModel.getAnnotationIterator();
    while (annotations.hasNext()) {
        Annotation annotation = annotations.next();
        Position position = annotationModel.getPosition(annotation);
        try {
            IMarker marker = getMarkerFromAnnotation(annotation);
            int lineNum = sourceViewer.getDocument().getLineOfOffset(position.getOffset()) + 1;
            int currentLineNum = sourceViewer.getDocument().getLineOfOffset(context.getOffset()) + 1;
            if ((marker != null) && (currentLineNum == lineNum)) {
                ICompletionProposal[] resolutions = createFromMarkerResolutions(marker);
                if (ListUtil.isNotEmpty(resolutions)) {
                    Collections.addAll(proposals, resolutions);
                    if (annotation instanceof IQuickFixableAnnotation) {
                        IQuickFixableAnnotation quick = (IQuickFixableAnnotation) annotation;
                        quick.setQuickFixable(true);
                    }
                }
            }
        } catch (BadLocationException ble) {
            LiferayXMLSearchUI.logError("Error finding quick assists", ble);
        }
    }
    if (ListUtil.isNotEmpty(proposals)) {
        retval = proposals.toArray(new ICompletionProposal[0]);
    }
    return retval;
}
Also used : IQuickFixableAnnotation(org.eclipse.jface.text.quickassist.IQuickFixableAnnotation) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IQuickFixableAnnotation(org.eclipse.jface.text.quickassist.IQuickFixableAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IMarker(org.eclipse.core.resources.IMarker) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 38 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project mylyn.docs by eclipse.

the class TextHover method getHoverRegion.

@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
    if (annotationModel != null) {
        int start = Integer.MAX_VALUE;
        int end = -1;
        Iterator<?> iterator = annotationModel.getAnnotationIterator();
        while (iterator.hasNext()) {
            Annotation next = (Annotation) iterator.next();
            Position position = annotationModel.getPosition(next);
            if (position.getOffset() <= offset && (position.getLength() + position.getOffset()) >= offset) {
                start = Math.min(start, position.getOffset());
                end = Math.max(end, position.getOffset() + position.getLength());
            }
        }
        if (start <= end && end > -1) {
            return new Region(start, end - start);
        }
    }
    return super.getHoverRegion(textViewer, offset);
}
Also used : Position(org.eclipse.jface.text.Position) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Point(org.eclipse.swt.graphics.Point) TitleAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.TitleAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) AnchorHrefAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.AnchorHrefAnnotation)

Example 39 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project mylyn.docs by eclipse.

the class TextHover method getHoverInfo.

@SuppressWarnings({ "deprecation" })
@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    IAnnotationModel model = getAnnotationModel(sourceViewer);
    if (model == null) {
        return null;
    }
    Iterator<?> e = model.getAnnotationIterator();
    while (e.hasNext()) {
        Annotation a = (Annotation) e.next();
        if (isIncluded(a)) {
            Position p = model.getPosition(a);
            if (p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
                String msg = a.getText();
                if (msg != null && msg.trim().length() > 0) {
                    if (a.getType().equals(AnchorHrefAnnotation.TYPE)) {
                        if (msg.startsWith("#")) {
                            // links
                            return null;
                        } else {
                            return NLS.bind(Messages.TextHover_hyperlinkHover, msg);
                        }
                    }
                    return msg;
                }
            }
        }
    }
    return null;
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) TitleAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.TitleAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) AnchorHrefAnnotation(org.eclipse.mylyn.wikitext.ui.annotation.AnchorHrefAnnotation)

Example 40 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse-cs by checkstyle.

the class AbstractASTResolution method run.

/**
 * {@inheritDoc}
 */
@Override
public void run(IMarker marker) {
    IResource resource = marker.getResource();
    if (!(resource instanceof IFile)) {
        return;
    }
    ICompilationUnit compilationUnit = getCompilationUnit(marker);
    if (compilationUnit == null) {
        return;
    }
    ITextFileBufferManager bufferManager = null;
    IPath path = compilationUnit.getPath();
    try {
        final IProgressMonitor monitor = new NullProgressMonitor();
        // open the file the editor
        JavaUI.openInEditor(compilationUnit);
        // reimplemented according to this article
        // http://www.eclipse.org/articles/Article-JavaCodeManipulation_AST/index.html
        bufferManager = FileBuffers.getTextFileBufferManager();
        bufferManager.connect(path, null);
        ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path);
        IDocument document = textFileBuffer.getDocument();
        IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
        MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
        // by a previous quickfix
        if (annotation == null) {
            return;
        }
        Position pos = annotationModel.getPosition(annotation);
        final IRegion lineInfo = document.getLineInformationOfOffset(pos.getOffset());
        final int markerStart = pos.getOffset();
        ASTParser astParser = ASTParser.newParser(AST.JLS3);
        astParser.setKind(ASTParser.K_COMPILATION_UNIT);
        astParser.setSource(compilationUnit);
        CompilationUnit ast = (CompilationUnit) astParser.createAST(monitor);
        ast.recordModifications();
        ast.accept(handleGetCorrectingASTVisitor(lineInfo, markerStart));
        // rewrite all recorded changes to the document
        TextEdit edit = ast.rewrite(document, compilationUnit.getJavaProject().getOptions(true));
        edit.apply(document);
        // commit changes to underlying file
        if (mAutoCommit) {
            textFileBuffer.commit(monitor, false);
        }
    } catch (CoreException e) {
        CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
    } catch (MalformedTreeException e) {
        CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
    } catch (BadLocationException e) {
        CheckstyleLog.log(e, Messages.AbstractASTResolution_msgErrorQuickfix);
    } finally {
        if (bufferManager != null) {
            try {
                bufferManager.disconnect(path, null);
            } catch (CoreException e) {
                // $NON-NLS-1$
                CheckstyleLog.log(e, "Error processing quickfix");
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ITextFileBufferManager(org.eclipse.core.filebuffers.ITextFileBufferManager) Position(org.eclipse.jface.text.Position) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IRegion(org.eclipse.jface.text.IRegion) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IResource(org.eclipse.core.resources.IResource) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10