use of org.python.pydev.parser.visitors.scope.ASTEntry 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.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class RefactoringRenameTestBase method asStr.
@SuppressWarnings("unchecked")
protected String asStr(Map<Tuple<String, File>, HashSet<ASTEntry>> referencesForModuleRename) throws Exception {
Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = referencesForModuleRename.entrySet();
FastStringBuffer buf = new FastStringBuffer();
ArrayList<Entry<Tuple<String, File>, HashSet<ASTEntry>>> lst = new ArrayList<>(entrySet);
Comparator<Entry<Tuple<String, File>, HashSet<ASTEntry>>> c = new Comparator<Entry<Tuple<String, File>, HashSet<ASTEntry>>>() {
@Override
public int compare(Entry<Tuple<String, File>, HashSet<ASTEntry>> o1, Entry<Tuple<String, File>, HashSet<ASTEntry>> o2) {
return o1.getKey().o1.compareTo(o2.getKey().o1);
}
};
Collections.sort(lst, c);
for (Entry<Tuple<String, File>, HashSet<ASTEntry>> entry : lst) {
HashSet<ASTEntry> value = entry.getValue();
if (value.size() > 0) {
ArrayList<ASTEntry> lst2 = new ArrayList<>(value);
Comparator<ASTEntry> c2 = new Comparator<ASTEntry>() {
@Override
public int compare(ASTEntry o1, ASTEntry o2) {
return o1.toString().compareTo(o2.toString());
}
};
Collections.sort(lst2, c2);
File f = entry.getKey().o2;
String fileContents = FileUtils.getFileContents(f);
Document initialDoc = new Document(fileContents);
buf.append(entry.getKey().o1).append("\n");
for (ASTEntry e : lst2) {
buf.append(" ");
buf.append(e.toString()).append("\n");
List<TextEdit> edits = (List<TextEdit>) e.getAdditionalInfo(AstEntryScopeAnalysisConstants.AST_ENTRY_REPLACE_EDIT, null);
if (edits == null) {
if (!(e instanceof ASTEntryWithSourceModule)) {
throw new AssertionError("Only ASTEntryWithSourceModule can have null edits. Found: " + e);
}
} else {
Document changedDoc = new Document(fileContents);
for (TextEdit textEdit : edits) {
textEdit.apply(changedDoc);
}
List<String> changedLines = getChangedLines(initialDoc, changedDoc);
for (String i : changedLines) {
buf.append(" ");
buf.append(StringUtils.rightTrim(i)).append("\n");
}
}
}
buf.append("\n");
}
}
return buf.toString();
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class FindOccurrencesSearchQuery method run.
@Override
public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
try {
monitor.beginTask("Searching...", 100);
req.pushMonitor(monitor);
FindOccurrencesSearchResult searchResult = (FindOccurrencesSearchResult) getSearchResult();
Map<Tuple<String, File>, HashSet<ASTEntry>> occurrences;
try {
req.pushMonitor(new SubProgressMonitor(monitor, 80));
occurrences = pyRefactoring.findAllOccurrences(req);
} finally {
req.popMonitor().done();
}
if (occurrences == null) {
return Status.OK_STATUS;
}
int length = req.qualifier.length();
HashSet<Integer> foundOffsets = new HashSet<Integer>();
try {
req.pushMonitor(new SubProgressMonitor(monitor, 20));
Set<Entry<Tuple<String, File>, HashSet<ASTEntry>>> entrySet = occurrences.entrySet();
req.getMonitor().beginTask("Resolving occurrences...", entrySet.size());
for (Map.Entry<Tuple<String, File>, HashSet<ASTEntry>> o : entrySet) {
foundOffsets.clear();
IFile workspaceFile = null;
try {
IProject project = null;
IPythonNature nature = req.nature;
if (nature != null) {
project = nature.getProject();
}
workspaceFile = FindWorkspaceFiles.getWorkspaceFile(o.getKey().o2, project);
if (workspaceFile == null) {
Log.logInfo(StringUtils.format("Ignoring: %s. " + "Unable to resolve to a file in the Eclipse workspace.", o.getKey().o2));
continue;
}
} catch (IllegalStateException e) {
// this can happen on tests (but if not on tests, we want to re-throw it
String message = e.getMessage();
if (message == null || !message.equals("Workspace is closed.")) {
throw e;
}
// otherwise, let's just keep going in the test...
continue;
}
IDocument doc = FileUtilsFileBuffer.getDocFromResource(workspaceFile);
req.getMonitor().setTaskName("Resolving occurrences... " + workspaceFile);
for (ASTEntry entry : o.getValue()) {
int offset = AbstractRenameRefactorProcess.getOffset(doc, entry);
if (!foundOffsets.contains(offset)) {
foundOffsets.add(offset);
if (DebugFlags.DEBUG_FIND_REFERENCES) {
System.out.println("Adding match:" + workspaceFile);
}
PySelection ps = new PySelection(doc, offset);
int lineNumber = ps.getLineOfOffset();
String lineContents = ps.getLine(lineNumber);
int lineStartOffset = ps.getLineOffset(lineNumber);
LineElement element = new LineElement(workspaceFile, lineNumber, lineStartOffset, lineContents, offset - lineStartOffset);
searchResult.addMatch(new FileMatch(workspaceFile, offset, length, element));
}
}
}
} finally {
req.popMonitor().done();
}
} catch (CoreException e) {
Log.log(e);
} finally {
req.popMonitor().done();
}
return Status.OK_STATUS;
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class PyRenameInFileAction method fillWithOccurrences.
/**
* Puts the found positions referente to the occurrences in the group
*
* @param document the document that will contain this positions
* @param group the group that will contain this positions
* @param ps the selection used
* @return
*
* @throws BadLocationException
* @throws OperationCanceledException
* @throws CoreException
* @throws MisconfigurationException
*/
private boolean fillWithOccurrences(IDocument document, LinkedPositionGroup group, IProgressMonitor monitor, PySelection ps) throws BadLocationException, OperationCanceledException, CoreException, MisconfigurationException {
RefactoringRequest req = MarkOccurrencesJob.getRefactoringRequest(pyEdit, MarkOccurrencesJob.getRefactorAction(pyEdit), ps);
if (monitor.isCanceled()) {
return false;
}
PyRenameEntryPoint processor = new PyRenameEntryPoint(req);
// process it to get what we need
processor.checkInitialConditions(monitor);
processor.checkFinalConditions(monitor, null);
HashSet<ASTEntry> occurrences = processor.getOccurrences();
if (monitor.isCanceled()) {
return false;
}
// used so that we don't add duplicates
Set<Tuple<Integer, Integer>> found = new HashSet<Tuple<Integer, Integer>>();
List<ProposalPosition> groupPositions = new ArrayList<ProposalPosition>();
if (occurrences != null) {
// first, just sort by position (line, col)
ArrayList<ASTEntry> sortedOccurrences = new ArrayList<ASTEntry>(occurrences);
Collections.sort(sortedOccurrences, new Comparator<ASTEntry>() {
@Override
public int compare(ASTEntry o1, ASTEntry o2) {
int thisVal = o1.node.beginLine;
int anotherVal = o2.node.beginLine;
int ret;
if (thisVal == anotherVal) {
// if it's in the same line, let's sort by column
thisVal = o1.node.beginColumn;
anotherVal = o2.node.beginColumn;
ret = (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
} else {
ret = (thisVal < anotherVal ? -1 : 1);
}
return ret;
}
});
// now, gather positions to add to the group
int i = 0;
int firstPosition = -1;
int absoluteCursorOffset = ps.getAbsoluteCursorOffset();
for (ASTEntry entry : sortedOccurrences) {
try {
IRegion lineInformation = document.getLineInformation(entry.node.beginLine - 1);
int colDef = NodeUtils.getClassOrFuncColDefinition(entry.node) - 1;
int offset = lineInformation.getOffset() + colDef;
int len = req.qualifier.length();
Tuple<Integer, Integer> foundAt = new Tuple<Integer, Integer>(offset, len);
if (!found.contains(foundAt)) {
i++;
ProposalPosition proposalPosition = new ProposalPosition(document, offset, len, i, new ICompletionProposal[0]);
found.add(foundAt);
groupPositions.add(proposalPosition);
if (offset <= absoluteCursorOffset && absoluteCursorOffset < offset + len) {
firstPosition = i;
}
}
} catch (Exception e) {
Log.log(e);
return false;
}
}
if (firstPosition != -1) {
ArrayList<ProposalPosition> newGroupPositions = new ArrayList<ProposalPosition>();
// add from current to end
for (i = firstPosition - 1; i < groupPositions.size(); i++) {
newGroupPositions.add(groupPositions.get(i));
}
// and now from the start up to the current
for (i = 0; i < firstPosition - 1; i++) {
newGroupPositions.add(groupPositions.get(i));
}
groupPositions = newGroupPositions;
}
for (ProposalPosition proposalPosition : groupPositions) {
group.addPosition(proposalPosition);
}
}
return groupPositions.size() > 0;
}
use of org.python.pydev.parser.visitors.scope.ASTEntry in project Pydev by fabioz.
the class NodeUtils method isValidContextForSetNext.
/**
* Identifies the context for both source and target line
*
* @param ASTEntry
* ast
* @param int sourceLine: the line at which debugger is stopped currently
* (starts at 1)
* @param int targetLine: the line at which we need to set next (starts at
* 0)
* @return
*/
public static boolean isValidContextForSetNext(SimpleNode ast, int sourceLine, int targetLine) {
String sourceFunctionName = NodeUtils.getContextName((sourceLine - 1), ast);
String targetFunctionName = NodeUtils.getContextName(targetLine, ast);
if (compareMethodName(sourceFunctionName, targetFunctionName)) {
ASTEntry sourceAST = NodeUtils.getLoopContextName(sourceLine, ast);
ASTEntry targetAST = NodeUtils.getLoopContextName(targetLine + 1, ast);
if (targetAST == null) {
// Target line is not inside some loop
return true;
}
if (isValidElseBlock(sourceAST, targetAST, sourceLine, targetLine)) {
// Debug pointer can be set inside else block of
return true;
// for..else/while..else
}
if (sourceAST == null && targetAST != null) {
// Source is outside loop and target is inside
return false;
// loop
}
if (sourceAST != null && targetAST != null) {
// Both Source and Target is inside some loop
if (sourceAST.equals(targetAST)) {
return isValidInterLoopContext(sourceLine, targetLine, sourceAST, targetAST);
} else {
ASTEntry last = sourceAST;
boolean retVal = false;
while (last != null) {
ASTEntry parentAST = last.parent;
if (parentAST != null && parentAST.equals(targetAST)) {
retVal = true;
break;
}
last = parentAST;
}
return retVal;
}
}
return true;
} else {
return false;
}
}
Aggregations