Search in sources :

Example 1 with PySourceViewer

use of org.python.pydev.editor.codefolding.PySourceViewer in project Pydev by fabioz.

the class MarkOccurrencesJob method getAnnotationsToAddAsMap.

/**
 * @param markOccurrencesRequest
 * @return true if the annotations were removed and added without any problems and false otherwise
 */
@Override
protected synchronized Map<Annotation, Position> getAnnotationsToAddAsMap(final BaseEditor baseEditor, IAnnotationModel annotationModel, MarkOccurrencesRequest markOccurrencesRequest, IProgressMonitor monitor) throws BadLocationException {
    PyEdit pyEdit = (PyEdit) baseEditor;
    PySourceViewer viewer = pyEdit.getPySourceViewer();
    if (viewer == null || monitor.isCanceled()) {
        return null;
    }
    if (monitor.isCanceled()) {
        return null;
    }
    if (markOccurrencesRequest instanceof TextBasedLocalMarkOccurrencesRequest) {
        TextBasedLocalMarkOccurrencesRequest textualMarkOccurrencesRequest = (TextBasedLocalMarkOccurrencesRequest) markOccurrencesRequest;
        PySelection pySelection = PySelection.fromTextSelection(ps);
        Tuple<Integer, Integer> startEndLines = pySelection.getCurrentMethodStartEndLines();
        int initialOffset = pySelection.getAbsoluteCursorOffset(startEndLines.o1, 0);
        int finalOffset = pySelection.getEndLineOffset(startEndLines.o2);
        List<IRegion> occurrences = ps.searchOccurrences(textualMarkOccurrencesRequest.currToken);
        if (occurrences.size() == 0) {
            return null;
        }
        Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
        for (Iterator<IRegion> it = occurrences.iterator(); it.hasNext(); ) {
            IRegion iRegion = it.next();
            if (iRegion.getOffset() < initialOffset || iRegion.getOffset() > finalOffset) {
                continue;
            }
            try {
                Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
                Position position = new Position(iRegion.getOffset(), iRegion.getLength());
                toAddAsMap.put(annotation, position);
            } catch (Exception e) {
                Log.log(e);
            }
        }
        return toAddAsMap;
    }
    PyMarkOccurrencesRequest pyMarkOccurrencesRequest = (PyMarkOccurrencesRequest) markOccurrencesRequest;
    Set<ASTEntry> occurrences = pyMarkOccurrencesRequest.getOccurrences();
    if (occurrences == null) {
        if (DEBUG) {
            System.out.println("Occurrences == null");
        }
        return null;
    }
    IDocument doc = pyEdit.getDocument();
    Map<Annotation, Position> toAddAsMap = new HashMap<Annotation, Position>();
    boolean markOccurrencesInStrings = MarkOccurrencesPreferencesPage.useMarkOccurrencesInStrings();
    // get the annotations to add
    for (ASTEntry entry : occurrences) {
        if (!markOccurrencesInStrings) {
            if (entry.node instanceof Name) {
                Name name = (Name) entry.node;
                if (name.ctx == Name.Artificial) {
                    continue;
                }
            }
        }
        SimpleNode node = entry.getNameNode();
        IRegion lineInformation = doc.getLineInformation(node.beginLine - 1);
        try {
            Annotation annotation = new Annotation(getOccurrenceAnnotationsType(), false, "occurrence");
            Position position = new Position(lineInformation.getOffset() + node.beginColumn - 1, pyMarkOccurrencesRequest.getInitialName().length());
            toAddAsMap.put(annotation, position);
        } catch (Exception e) {
            Log.log(e);
        }
    }
    return toAddAsMap;
}
Also used : Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IRegion(org.eclipse.jface.text.IRegion) Annotation(org.eclipse.jface.text.source.Annotation) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) Name(org.python.pydev.parser.jython.ast.Name) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PySelection(org.python.pydev.core.docutils.PySelection) PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) PyEdit(org.python.pydev.editor.PyEdit) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with PySourceViewer

use of org.python.pydev.editor.codefolding.PySourceViewer in project Pydev by fabioz.

the class AbstractAnalysisMarkersParticipants method getProps.

@Override
public List<ICompletionProposalHandle> getProps(PySelection ps, IImageCache imageCache, File f, IPythonNature nature, IPyEdit edit, int offset) throws BadLocationException {
    fillParticipants();
    PySourceViewer s = ((PyEdit) edit).getPySourceViewer();
    int line = ps.getLineOfOffset(offset);
    OrderedSet<MarkerAnnotationAndPosition> markersAtLine = new OrderedSet<MarkerAnnotationAndPosition>();
    // Add it to a set to make sure that the entries are unique.
    // -- i.e.: the code analysis seems to be creating 2 markers in the following case (when sys is undefined):
    // sys.call1().call2()
    // So, we add it to a set to make sure we'll only analyze unique markers.
    // Note that it'll check equality by the marker type and text (not by position), so, if a given error
    // appears twice in the same line being correct, we'll only show the options once here (which is what
    // we want).
    List<MarkerAnnotationAndPosition> markersAtLine2 = s.getMarkersAtLine(line, getMarkerType());
    markersAtLine.addAll(markersAtLine2);
    ArrayList<ICompletionProposalHandle> props = new ArrayList<ICompletionProposalHandle>();
    if (markersAtLine != null) {
        IAnalysisPreferences analysisPreferences = new AnalysisPreferences(edit);
        String currLine = ps.getLine();
        for (MarkerAnnotationAndPosition marker : markersAtLine) {
            for (IAnalysisMarkersParticipant participant : participants) {
                try {
                    participant.addProps(marker, analysisPreferences, currLine, ps, offset, nature, (PyEdit) edit, props);
                } catch (Exception e) {
                    Log.log("Error when getting proposals.", e);
                }
            }
        }
    }
    return props;
}
Also used : OrderedSet(org.python.pydev.shared_core.structure.OrderedSet) AnalysisPreferences(com.python.pydev.analysis.AnalysisPreferences) IAnalysisPreferences(org.python.pydev.ast.analysis.IAnalysisPreferences) ArrayList(java.util.ArrayList) BadLocationException(org.eclipse.jface.text.BadLocationException) IAnalysisPreferences(org.python.pydev.ast.analysis.IAnalysisPreferences) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) IPyEdit(org.python.pydev.core.IPyEdit) PyEdit(org.python.pydev.editor.PyEdit) MarkerAnnotationAndPosition(org.python.pydev.editor.codefolding.MarkerAnnotationAndPosition)

Example 3 with PySourceViewer

use of org.python.pydev.editor.codefolding.PySourceViewer in project Pydev by fabioz.

the class PyMoveImportsToLocalCompletionProposal method apply.

@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    IDocument doc = viewer.getDocument();
    apply(doc);
    if (forceReparseOnApply) {
        // and after applying it, let's request a reanalysis
        if (viewer instanceof PySourceViewer) {
            PySourceViewer sourceViewer = (PySourceViewer) viewer;
            PyEdit edit = sourceViewer.getEdit();
            if (edit != null) {
                edit.getParser().forceReparse(new Tuple<String, Boolean>(IMiscConstants.ANALYSIS_PARSER_OBSERVER_FORCE, true));
            }
        }
    }
}
Also used : PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) IDocument(org.eclipse.jface.text.IDocument) PyEdit(org.python.pydev.editor.PyEdit)

Example 4 with PySourceViewer

use of org.python.pydev.editor.codefolding.PySourceViewer in project Pydev by fabioz.

the class CtxInsensitiveImportComplProposalReparseOnApply method apply.

@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    if ((stateMask & SWT.SHIFT) != 0) {
        this.setAddLocalImport(true);
    }
    super.apply(viewer, trigger, stateMask, offset);
    if (forceReparseOnApply) {
        // and after applying it, let's request a reanalysis
        if (viewer instanceof PySourceViewer) {
            PySourceViewer sourceViewer = (PySourceViewer) viewer;
            PyEdit edit = sourceViewer.getEdit();
            if (edit != null) {
                edit.getParser().forceReparse(new Tuple<String, Boolean>(IMiscConstants.ANALYSIS_PARSER_OBSERVER_FORCE, true));
            }
        }
    }
}
Also used : PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer) PyEdit(org.python.pydev.editor.PyEdit)

Example 5 with PySourceViewer

use of org.python.pydev.editor.codefolding.PySourceViewer in project Pydev by fabioz.

the class PyMarkerTextHover method getHoverInfo.

/*
     * (non-Javadoc)
     * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
     */
@Override
public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    FastStringBuffer buf = new FastStringBuffer();
    if (textViewer instanceof PySourceViewer) {
        PySourceViewer s = (PySourceViewer) textViewer;
        getMarkerHover(hoverRegion, s, buf);
    }
    return buf.toString();
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) PySourceViewer(org.python.pydev.editor.codefolding.PySourceViewer)

Aggregations

PySourceViewer (org.python.pydev.editor.codefolding.PySourceViewer)8 PyEdit (org.python.pydev.editor.PyEdit)4 ArrayList (java.util.ArrayList)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 PySelection (org.python.pydev.core.docutils.PySelection)2 MarkerAnnotationAndPosition (org.python.pydev.editor.codefolding.MarkerAnnotationAndPosition)2 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)2 AnalysisPreferences (com.python.pydev.analysis.AnalysisPreferences)1 HashMap (java.util.HashMap)1 IMarker (org.eclipse.core.resources.IMarker)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 Annotation (org.eclipse.jface.text.source.Annotation)1 IAnalysisPreferences (org.python.pydev.ast.analysis.IAnalysisPreferences)1 IPyEdit (org.python.pydev.core.IPyEdit)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 SimpleNode (org.python.pydev.parser.jython.SimpleNode)1