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;
}
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;
}
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));
}
}
}
}
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));
}
}
}
}
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();
}
Aggregations