Skip to content

Commit

Permalink
Pretty-printing nonlocal now works.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Oct 1, 2023
1 parent 1a1f7b7 commit ab66dee
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
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.NonLocal;
import org.python.pydev.parser.jython.ast.Num;
import org.python.pydev.parser.jython.ast.Pass;
import org.python.pydev.parser.jython.ast.Print;
Expand Down Expand Up @@ -859,6 +860,14 @@ public Object visitClassDef(ClassDef node) throws Exception {
return null;
}

@Override
public Object visitNonLocal(NonLocal node) throws Exception {
fixNode(node);
node.traverse(this);
fixAfterNode(node);
return null;
}

public boolean isFilled(SimpleNode[] nodes) {
return (nodes != null) && (nodes.length > 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.python.pydev.parser.jython.ast.MatchValue;
import org.python.pydev.parser.jython.ast.Name;
import org.python.pydev.parser.jython.ast.NameTok;
import org.python.pydev.parser.jython.ast.NonLocal;
import org.python.pydev.parser.jython.ast.Num;
import org.python.pydev.parser.jython.ast.Pass;
import org.python.pydev.parser.jython.ast.Print;
Expand Down Expand Up @@ -878,20 +879,54 @@ public Object visitStrJoin(StrJoin node) throws Exception {
}

@Override
public Object visitGlobal(Global node) throws Exception {
public Object visitNonLocal(NonLocal node) throws Exception {
beforeNode(node);
doc.addRequire("global", node);

int id = doc.pushRecordChanges();
doc.addRequire("nonlocal ", node);
if (node.names != null) {
for (int i = 0; i < node.names.length; i++) {
if (i > 0) {
doc.addRequire(",", lastNode);
if (node.names.length > 0) {
for (int i = 0; i < node.names.length; i++) {
if (i > 0) {
doc.addRequire(",", lastNode);
}
if (node.names[i] != null) {
node.names[i].accept(this);
}
}
if (node.names[i] != null) {
node.names[i].accept(this);
}
}
java.util.List<ILinePart> recordedChanges = this.doc.popRecordChanges(id);
this.doc.replaceRecorded(recordedChanges, "nonlocal", "nonlocal ");

if (node.value != null) {
doc.addRequire("=", lastNode);
node.value.accept(this);
}

afterNode(node);
return null;
}

@Override
public Object visitGlobal(Global node) throws Exception {
beforeNode(node);
int id = doc.pushRecordChanges();
doc.addRequire("global ", node);
if (node.names != null) {
if (node.names.length > 0) {
for (int i = 0; i < node.names.length; i++) {
if (i > 0) {
doc.addRequire(",", lastNode);
}
if (node.names[i] != null) {
node.names[i].accept(this);
}
}
}
}
java.util.List<ILinePart> recordedChanges = this.doc.popRecordChanges(id);
this.doc.replaceRecorded(recordedChanges, "global", "global ");

if (node.value != null) {
doc.addRequire("=", lastNode);
node.value.accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,25 @@ public void testOthers1() throws Exception {
checkPrettyPrintEqual(s, expected);
}

public void testNonlocal() throws Exception {
String s = ""
+ "def another():\n"
+ " a,b,c = 3\n"
+ " def method():\n"
+ " nonlocal a\n"
+ " nonlocal b,c\n";

checkPrettyPrintEqual(s);
}

public void testGlobal() throws Exception {
String s = "def method():\n"
+ " global a\n"
+ " global b,c\n";

checkPrettyPrintEqual(s);
}

public void testOthers2() throws Throwable {
final String s = "" +
"def _format(node):\n" +
Expand Down

0 comments on commit ab66dee

Please sign in to comment.