Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Mar 1, 2025
1 parent aeadc1f commit d11c4d8
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import org.python.pydev.parser.jython.ast.FunctionDef;
import org.python.pydev.parser.jython.ast.Index;
import org.python.pydev.parser.jython.ast.Name;
import org.python.pydev.parser.jython.ast.NameTok;
import org.python.pydev.parser.jython.ast.Num;
import org.python.pydev.parser.jython.ast.Str;
import org.python.pydev.parser.jython.ast.Subscript;
import org.python.pydev.parser.jython.ast.TypeAlias;
import org.python.pydev.parser.jython.ast.UnaryOp;
import org.python.pydev.parser.jython.ast.exprType;
import org.python.pydev.parser.visitors.NodeUtils;
import org.python.pydev.parser.visitors.TypeInfo;
import org.python.pydev.shared_core.model.ISimpleNode;
import org.python.pydev.shared_core.string.FullRepIterable;
import org.python.pydev.shared_core.string.StringUtils;

Expand Down Expand Up @@ -176,6 +179,26 @@ public TokensList getCompletionsFromDefinition(Definition definition, ICompletio
ast = assign.targets[0];
}
}
if (ast instanceof NameTok) {
NameTok nameTok = (NameTok) definition.ast;
if (nameTok.ctx == NameTok.TypeAliasName) {
ISimpleNode foundAtASTNode = definition.scope.getFoundAtASTNode();
if (foundAtASTNode instanceof TypeAlias) {
String rep = NodeUtils.getRepresentationString(((TypeAlias) foundAtASTNode).value);
if (rep != null) {
ICompletionState cp = state.getCopyWithActTok(rep);

ret.addAll(manager.getCompletionsUnpackingInLocalScope(cp, definition.module,
definition.scope));
if (ret != null && ret.size() > 0) {
return ret;
}
}

}
}
}

if (NodeUtils.isParamName(ast)) {
Name name = (Name) ast;
String scopeStackPathNames = definition.scope.getScopeStackPathNames();
Expand Down Expand Up @@ -527,7 +550,7 @@ private TokensList getNonFunctionDefCompletionsFromAssign(ICodeCompletionASTMana
}

private static List<String> extractTypingUnionValues(ICodeCompletionASTManager manager,
IModule module, exprType node)
IModule module, SimpleNode node)
throws CompletionRecursionException {
if (manager.isNodeTypingUnionSubscript(module, node)) {
Subscript subscript = (Subscript) node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.python.pydev.parser.jython.ast.ImportFrom;
import org.python.pydev.parser.jython.ast.Module;
import org.python.pydev.parser.jython.ast.Name;
import org.python.pydev.parser.jython.ast.NameTok;
import org.python.pydev.parser.jython.ast.Str;
import org.python.pydev.parser.jython.ast.TypeAlias;
import org.python.pydev.parser.jython.ast.exprType;
Expand Down Expand Up @@ -867,6 +868,9 @@ public void addOptional(Definition definition) {
} else if (definition.ast instanceof Name) {
Name name = (Name) definition.ast;
this.optionalIsParam = name.ctx == Name.Param || name.ctx == Name.KwOnlyParam;
} else if (definition.ast instanceof NameTok) {
NameTok name = (NameTok) definition.ast;
this.optionalIsParam = name.ctx == NameTok.TypeAliasName;
}

if (optional == null) {
Expand Down Expand Up @@ -992,6 +996,7 @@ private Definition[] findDefinition(ICompletionState state, int line, int col, f
// as MyClass.foo = 2, keep on going because we'd like to get to the definition
// of 'foo' inside of MyClass (and later on just append these as definitions found
// if they aren't duplicated).

if (toRet.optionalIsParam) {
return toRet.toArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import org.python.pydev.core.ILocalScope;
import org.python.pydev.core.IModule;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.Assign;
import org.python.pydev.parser.jython.ast.exprType;
import org.python.pydev.parser.jython.ast.TypeAlias;

public class AssignDefinition extends Definition {

Expand Down Expand Up @@ -44,9 +45,9 @@ public class AssignDefinition extends Definition {
* This is the value node found (can be used later to determine if it's a
* Call or some regular attribute.
*/
public final exprType nodeValue;
public final SimpleNode nodeValue;

public final exprType nodeType;
public final SimpleNode nodeType;

/**
* If it's an assign we should unpack.
Expand All @@ -62,7 +63,20 @@ public class AssignDefinition extends Definition {
* The line and col are defined starting at 1 (and not 0)
*/
public AssignDefinition(String value, String type, String target, int targetPos, Assign ast, int line, int col,
ILocalScope scope, IModule module, exprType nodeValue, exprType nodeType, int unpackPos) {
ILocalScope scope, IModule module, SimpleNode nodeValue, SimpleNode nodeType, int unpackPos) {
super(line, col, value, type, nodeType, ast, scope, module);
this.target = target;
this.targetPos = targetPos;
this.nodeValue = nodeValue;
this.nodeType = nodeType;
this.unpackPos = unpackPos;
}

/**
* The line and col are defined starting at 1 (and not 0)
*/
public AssignDefinition(String value, String type, String target, int targetPos, TypeAlias ast, int line, int col,
ILocalScope scope, IModule module, SimpleNode nodeValue, SimpleNode nodeType, int unpackPos) {
super(line, col, value, type, nodeType, ast, scope, module);
this.target = target;
this.targetPos = targetPos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.python.pydev.core.TokensList;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.Assign;
import org.python.pydev.parser.jython.ast.exprType;
import org.python.pydev.parser.visitors.NodeUtils;
import org.python.pydev.shared_core.string.FastStringBuffer;
import org.python.pydev.shared_core.string.FullRepIterable;
Expand Down Expand Up @@ -70,7 +69,7 @@ public class Definition implements IDefinition {
*/
public final String type;

public final exprType nodeType;
public final SimpleNode nodeType;

/**
* This is the module where the definition is.
Expand Down Expand Up @@ -111,7 +110,7 @@ public Definition(int line, int col, String value, String type, SimpleNode ast,
/**
* The line and col are defined starting at 1
*/
public Definition(int line, int col, String value, String type, exprType nodeType, SimpleNode ast,
public Definition(int line, int col, String value, String type, SimpleNode nodeType, SimpleNode ast,
ILocalScope scope, IModule module) {
this(line, col, value, type, nodeType, ast, scope, module, false);
}
Expand Down Expand Up @@ -170,7 +169,7 @@ public Definition(int line, int col, String value, String type, SimpleNode ast,
this.foundAsLocal = foundAsLocal;
}

public Definition(int line, int col, String value, String type, exprType nodeType, SimpleNode ast,
public Definition(int line, int col, String value, String type, SimpleNode nodeType, SimpleNode ast,
ILocalScope scope, IModule module,
boolean foundAsLocal) {
Assert.isNotNull(value, "Invalid value.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,38 @@ public Object visitAssign(Assign node) throws Exception {
return this.visitAssign(node, -1);
}

@Override
public Object visitTypeAlias(TypeAlias node) throws Exception {
ILocalScope scope = new LocalScope(nature, this.defsStack, this.module.get());
scope.setFoundAtASTNode(node);
if (foundAsDefinition && !scope.equals(definitionFound.scope)) { //if it is found as a definition it is an 'exact' match, so, we do not keep checking it
return null;
}
int unpackPos = -1;

NameTokType target = node.name;
String rep = NodeUtils.getFullRepresentationString(target);

if (tokenToFind.equals(rep)) { //note, order of equals is important (because one side may be null).
//get the line and column correspondent to the target
int line = NodeUtils.getLineDefinition(target);
int col = NodeUtils.getColDefinition(target);
AssignDefinition definition = getAssignDefinition(node, rep, 0, line, col, scope, module.get(),
unpackPos);

//mark it as global (if it was found as global in some of the previous contexts).
for (Set<String> globals : globalDeclarationsStack) {
if (globals.contains(rep)) {
definition.foundAsGlobal = true;
}
}

definitions.add(definition);
}

return super.visitTypeAlias(node);
}

public Object visitAssign(Assign node, int unpackPos) throws Exception {
ILocalScope scope = new LocalScope(nature, this.defsStack, this.module.get());
scope.setFoundAtASTNode(node);
Expand Down Expand Up @@ -553,7 +585,7 @@ public static AssignDefinition getAssignDefinition(Assign node, String target, i
public static AssignDefinition getAssignDefinition(TypeAlias node, String target, int targetPos, int line, int col,
ILocalScope scope, IModule module, int unpackPos) {
exprType nodeValue = node.value;
exprType nodeType = NodeUtils.extractOptionalValueSubscript(node.type);
NameTokType nodeType = node.name;
String value = NodeUtils.getFullRepresentationString(nodeValue);
String type = NodeUtils.getFullRepresentationString(nodeType);
if (value == null) {
Expand All @@ -562,6 +594,7 @@ public static AssignDefinition getAssignDefinition(TypeAlias node, String target
if (type == null) {
type = "";
}

return new AssignDefinition(value, type, target, targetPos, node, line, col, scope, module, nodeValue, nodeType,
unpackPos);
}
Expand Down
7 changes: 0 additions & 7 deletions plugins/org.python.pydev.refactoring/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ Bundle-ActivationPolicy: lazy
Bundle-ClassPath: refactoring.jar
Export-Package: org.python.pydev.refactoring,
org.python.pydev.refactoring.ast,
org.python.pydev.refactoring.ast.adapters,
org.python.pydev.refactoring.ast.adapters.offsetstrategy,
org.python.pydev.refactoring.ast.visitors,
org.python.pydev.refactoring.ast.visitors.context,
org.python.pydev.refactoring.ast.visitors.info,
org.python.pydev.refactoring.ast.visitors.position,
org.python.pydev.refactoring.ast.visitors.renamer,
org.python.pydev.refactoring.ast.visitors.rewriter,
org.python.pydev.refactoring.ast.visitors.selection,
org.python.pydev.refactoring.codegenerator.constructorfield,
org.python.pydev.refactoring.codegenerator.constructorfield.edit,
org.python.pydev.refactoring.codegenerator.constructorfield.request,
Expand Down
1 change: 0 additions & 1 deletion plugins/org.python.pydev.shared_ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Export-Package: org.python.pydev.overview_ruler,
org.python.pydev.shared_ui.search,
org.python.pydev.shared_ui.search.replace,
org.python.pydev.shared_ui.swt,
org.python.pydev.shared_ui.templates,
org.python.pydev.shared_ui.tooltips,
org.python.pydev.shared_ui.tooltips.presenter,
org.python.pydev.shared_ui.tree,
Expand Down
1 change: 0 additions & 1 deletion plugins/org.python.pydev/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Export-Package: com.python.pydev.actions,
org.python.pydev.builder.syntaxchecker,
org.python.pydev.builder.todo,
org.python.pydev.changed_lines,
org.python.pydev.codingstd,
org.python.pydev.compare,
org.python.pydev.consoles,
org.python.pydev.dltk.console.codegen,
Expand Down

0 comments on commit d11c4d8

Please sign in to comment.