use of org.python.pydev.shared_core.structure.Tuple3 in project Pydev by fabioz.
the class PyRefactoringFindDefinition method findActualTokenFromImportFromDefinition.
/**
* Given some definition, find its actual token (if that's possible)
* @param request the original request
* @param tok the token we're looking for
* @param lFindInfo place to store info
* @param selected place to add the new definition (if found)
* @param d the definition found before (this function will only work if this definition
* @param completionCache all completions found previously
* @param searchForMethodParameterFromParticipants find definition for method parameters
* maps to an ImportFrom)
*
* @return true if we found a new definition (and false otherwise)
* @throws Exception
*/
private static boolean findActualTokenFromImportFromDefinition(IPythonNature nature, String tok, List<IDefinition> selected, Definition d, ICompletionState completionCache, boolean searchForMethodParameterFromParticipants) throws Exception {
boolean didFindNewDef = false;
Set<Tuple3<String, Integer, Integer>> whereWePassed = new HashSet<Tuple3<String, Integer, Integer>>();
// in an import..from, the last part will always be the token imported
tok = FullRepIterable.getLastPart(tok);
if (searchForMethodParameterFromParticipants) {
if (d.ast instanceof Name) {
Name name = (Name) d.ast;
if (name.ctx == Name.Param) {
if (d.scope != null && !d.scope.getScopeStack().empty()) {
Object peek = d.scope.getScopeStack().peek();
if (peek instanceof FunctionDef) {
IDefinition found = CompletionParticipantsHelper.findDefinitionForMethodParameterFromParticipants(d, nature, completionCache);
if (found != null) {
selected.add(found);
return true;
}
}
}
}
}
}
while (d.ast instanceof ImportFrom) {
IDefinition[] found = followDefinition(d, whereWePassed, tok, nature, completionCache);
if (found != null && found.length == 1) {
Tuple3<String, Integer, Integer> tupFromDefinition = getTupFromDefinition((Definition) found[0]);
if (tupFromDefinition == null) {
break;
}
didFindNewDef = true;
d = (Definition) found[0];
} else {
break;
}
}
if (didFindNewDef) {
selected.add(d);
}
return didFindNewDef;
}
use of org.python.pydev.shared_core.structure.Tuple3 in project Pydev by fabioz.
the class ImportArranger method perform.
/**
* @param executeOnlyIfChanged: if 'true' initially, we'll check if something changes first. If something changes
* it'll call itself again with 'false' to force the changes.
*/
private void perform(boolean groupFromImports, boolean executeOnlyIfChanged, IPyFormatStdProvider edit) {
List<Tuple3<Integer, String, ImportHandle>> list = collectImports();
if (list.isEmpty()) {
return;
}
int lineOfFirstOldImport = list.get(0).o1;
List<Tuple<Integer, String>> linesToDelete = deleteImports(list);
if (!executeOnlyIfChanged) {
for (Tuple<Integer, String> tup : linesToDelete) {
PySelection.deleteLine(doc, tup.o1);
}
}
lineForNewImports = insertImportsHere(lineOfFirstOldImport);
if (this.removeUnusedImports) {
pruneEmptyImports(list);
}
sortImports(list);
// now, re-add the imports
String finalStr = createImportsStr(groupFromImports, list);
if (executeOnlyIfChanged) {
// If going automatic, let's check the contents before actually doing the organize
// (and skip if the order is ok).
ArrayList<String> list2 = new ArrayList<String>();
for (Tuple<Integer, String> tup : linesToDelete) {
list2.add(tup.o2);
}
Collections.reverse(list2);
String join = StringUtils.join("", list2).trim();
String other = StringUtils.replaceNewLines(finalStr, "").trim();
if (join.equals(other)) {
// System.out.println("Equals");
} else {
// System.out.println("Not equal!");
// System.out.println("\n\n---");
// System.out.println(join);
// System.out.println("---");
// System.out.println(other);
// System.out.println("---\n");
perform(groupFromImports, false, edit);
}
return;
}
try {
PyFormatAction std = new PyFormatAction();
boolean throwSyntaxError = false;
ISelectionProvider selectionProvider = null;
int[] regionsToFormat = null;
IDocument psDoc = new Document(finalStr);
PySelection ps = new PySelection(psDoc);
std.applyFormatAction(edit, ps, regionsToFormat, throwSyntaxError, selectionProvider);
finalStr = psDoc.get();
if (addNewLinesToImports) {
// Leave 2 empty new lines separating imports from code
String expectedEnd = endLineDelim + endLineDelim + endLineDelim;
while (!finalStr.endsWith(expectedEnd)) {
finalStr += endLineDelim;
}
}
} catch (Exception e) {
Log.log(e);
}
PySelection.addLine(doc, endLineDelim, finalStr, lineForNewImports);
}
use of org.python.pydev.shared_core.structure.Tuple3 in project Pydev by fabioz.
the class ImportArranger method deleteImports.
private List<Tuple<Integer, String>> deleteImports(List<Tuple3<Integer, String, ImportHandle>> list) {
// sort in inverse order (for removal of the string of the document).
List<Tuple<Integer, String>> linesToDelete = new ArrayList<>();
Collections.sort(list, new Comparator<Tuple3<Integer, String, ImportHandle>>() {
@Override
public int compare(Tuple3<Integer, String, ImportHandle> o1, Tuple3<Integer, String, ImportHandle> o2) {
return o2.o1.compareTo(o1.o1);
}
});
// ok, now we have to delete all lines with imports.
for (Iterator<Tuple3<Integer, String, ImportHandle>> iter = list.iterator(); iter.hasNext(); ) {
Tuple3<Integer, String, ImportHandle> element = iter.next();
String s = element.o2;
int max = StringUtils.countLineBreaks(s);
for (int i = 0; i <= max; i++) {
int lineToDel = (element.o1).intValue();
int j = lineToDel + i;
linesToDelete.add(new Tuple(j, PySelection.getLine(doc, j)));
}
}
Comparator<? super Tuple<Integer, String>> c = new Comparator<Tuple<Integer, String>>() {
@Override
public int compare(Tuple<Integer, String> o1, Tuple<Integer, String> o2) {
// reversed compare (o2 first o1 last).
return Integer.compare(o2.o1, o1.o1);
}
};
Collections.sort(linesToDelete, c);
return linesToDelete;
}
use of org.python.pydev.shared_core.structure.Tuple3 in project Pydev by fabioz.
the class ImportArranger method pruneEmptyImports.
private void pruneEmptyImports(List<Tuple3<Integer, String, ImportHandle>> list) {
Iterator<Tuple3<Integer, String, ImportHandle>> it = list.iterator();
while (it.hasNext()) {
ImportHandle ih = it.next().o3;
List<ImportHandleInfo> info = ih.getImportInfo();
Iterator<ImportHandleInfo> itInfo = info.iterator();
while (itInfo.hasNext()) {
if (itInfo.next().getImportedStr().isEmpty()) {
itInfo.remove();
}
}
if (info.size() == 0) {
it.remove();
}
}
}
use of org.python.pydev.shared_core.structure.Tuple3 in project Pydev by fabioz.
the class ScopeAnalyzerVisitorWithoutImports method getEntryOccurrences.
/**
* We get the occurrences as tokens for the name we're looking for. Note that the complete name (may be a dotted name)
* we're looking for may not be equal to the 'partial' name.
*
* This can happen when we're looking for some import such as os.path, and are looking just for the 'path' part.
* So, when this happens, the return is analyzed and only returns names as the one we're looking for (with
* the correct line and col positions).
*/
public List<ASTEntry> getEntryOccurrences() {
checkFinished();
Set<Tuple3<String, Integer, Integer>> s = new HashSet<Tuple3<String, Integer, Integer>>();
ArrayList<Tuple4<IToken, Integer, ASTEntry, Found>> complete = getCompleteTokenOccurrences();
ArrayList<ASTEntry> ret = new ArrayList<ASTEntry>();
for (Tuple4<IToken, Integer, ASTEntry, Found> tup : complete) {
IToken token = tup.o1;
if (!(token instanceof SourceToken)) {
// we want only the source tokens for this module
continue;
}
// if it is different, we have to make partial names
SourceToken sourceToken = (SourceToken) tup.o1;
SimpleNode ast = (sourceToken).getAst();
String representation = null;
if (ast instanceof ImportFrom) {
ImportFrom f = (ImportFrom) ast;
// f.names may be empty if it is a wild import
for (aliasType t : f.names) {
NameTok importName = NodeUtils.getNameForAlias(t);
String importRep = NodeUtils.getFullRepresentationString(importName);
if (importRep.equals(nameToFind)) {
ast = importName;
representation = importRep;
break;
}
}
} else if (ast instanceof Import) {
representation = NodeUtils.getFullRepresentationString(ast);
Import f = (Import) ast;
NameTok importName = NodeUtils.getNameForRep(f.names, representation);
if (importName != null) {
ast = importName;
}
} else {
representation = NodeUtils.getFullRepresentationString(ast);
}
if (representation == null) {
// do nothing
// can happen on wild imports
} else if (nameToFind.equals(representation)) {
if (ast instanceof Attribute) {
// it can happen, as we won't go up to the part of the actual call (if there's one).
ast = NodeUtils.getAttributeParts((Attribute) ast).get(0);
ASTEntry entry = new ASTEntry(tup.o3, ast);
entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
ret.add(entry);
} else {
ASTEntry entry = new ASTEntry(tup.o3, ast);
entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
ret.add(entry);
}
} else if (FullRepIterable.containsPart(representation, nameToFind)) {
Name nameAst = new Name(nameToFind, Name.Store, false);
List<String> strings = StringUtils.dotSplit(representation);
int plus = 0;
for (String string : strings) {
if (string.equals(nameToFind) && (plus + nameToFind.length() >= tup.o2)) {
break;
}
// len + dot
plus += string.length() + 1;
}
nameAst.beginColumn = AbstractMessage.getStartCol(token, document) + plus;
nameAst.beginLine = AbstractMessage.getStartLine(token, document);
Tuple3<String, Integer, Integer> t = new Tuple3<String, Integer, Integer>(nameToFind, nameAst.beginColumn, nameAst.beginLine);
if (!s.contains(t)) {
s.add(t);
ASTEntry entry = new ASTEntry(tup.o3, nameAst);
entry.setAdditionalInfo(FOUND_ADDITIONAL_INFO_IN_AST_ENTRY, tup.o4);
ret.add(entry);
}
}
}
return ret;
}
Aggregations