📄 prettyprinter.java
字号:
return null;
}
public void visitTryPart(SimpleNode node, stmtType[] body) throws Exception{
//try:
auxComment.writeSpecialsBefore(node);
state.indent();
fixNewStatementCondition();
for(stmtType st:body){
st.accept(this);
}
fixNewStatementCondition();
dedent();
auxComment.writeSpecialsAfter(node);
}
public void visitOrElsePart(suiteType orelse) throws Exception{
if(orelse != null){
auxComment.writeSpecialsBefore(orelse);
state.indent();
afterNode(orelse);
for (stmtType st : orelse.body){
st.accept(this);
}
auxComment.writeSpecialsAfter(orelse);
dedent();
}
}
@Override
public Object visitExec(Exec node) throws Exception {
return visitGeneric(node, "visitExec");
}
@Override
public Object visitTryFinally(TryFinally node) throws Exception {
visitTryPart(node, node.body);
visitOrElsePart(node.finalbody);
return null;
}
@Override
public Object visitTryExcept(TryExcept node) throws Exception {
visitTryPart(node, node.body);
for(excepthandlerType h:node.handlers){
state.pushInStmt(h);
state.indent();
auxComment.writeSpecialsBefore(h);
if(h.type != null || h.name != null){
state.write(" ");
}
if(h.type != null){
h.type.accept(this);
}
if(h.name != null){
h.name.accept(this);
}
state.popInStmt();
auxComment.writeSpecialsAfter(h);
fixNewStatementCondition();
for (stmtType st : h.body) {
st.accept(this);
}
dedent();
}
visitOrElsePart(node.orelse);
return null;
}
@Override
public Object visitSlice(Slice node) throws Exception {
return visitGeneric(node, "visitSlice", false);
}
@Override
public Object visitIndex(Index node) throws Exception {
return visitGeneric(node, "visitIndex", false);
}
@Override
public Object visitReturn(Return node) throws Exception {
return visitGeneric(node, "visitReturn", true);
}
@Override
public Object visitPrint(Print node) throws Exception {
return visitGeneric(node, "visitPrint", true, null, false, false);
}
@Override
public Object visitAttribute(Attribute node) throws Exception {
state.pushInStmt(node);
auxComment.writeSpecialsBefore(node);
node.value.accept(this);
state.write(".");
node.attr.accept(this);
auxComment.writeSpecialsAfter(node);
state.popInStmt();
return null;
}
@Override
public Object visitCall(Call node) throws Exception {
//make the visit
auxComment.writeSpecialsBefore(node, new String[]{"("}, null, false);
state.pushInStmt(node);
node.func.accept(this);
state.popInStmt();
auxComment.writeSpecialsBefore(node, null, new String[]{"("}, true);
//print the arguments within the call
printCallArguments(node, node.args, node.keywords, node.starargs, node.kwargs);
auxComment.writeSpecialsAfter(node);
if(!state.inStmt()){
fixNewStatementCondition();
}
return null;
}
@Override
public Object visitIf(If node) throws Exception {
fixNewStatementCondition();
auxComment.writeSpecialsBefore(node);
auxComment.moveComments(node.test, node.body[0], false, true, true);
state.pushInStmt(node.test);
node.test.accept(this);
state.popInStmt();
//write the 'if test:'
state.indent();
if(!fixNewStatementCondition()){
state.writeIndentString();
}
//write the body and dedent
for (SimpleNode n : node.body){
n.accept(this);
}
dedent();
if(node.orelse != null && node.orelse.length > 0){
boolean inElse = false;
auxComment.writeSpecialsAfter(node);
//now, if it is an elif, it will end up calling the 'visitIf' again,
//but if it is an 'else:' we will have to make the indent again
if(node.specialsAfter != null && node.specialsAfter.contains(new SpecialStr("else:",0,0))){ //the SpecialStr only compares with its String
inElse = true;
state.indent();
if(!fixNewStatementCondition()){
state.writeIndentString();
}
}
for (SimpleNode n : node.orelse) {
n.accept(this);
}
if(inElse){
dedent();
}
}
return null;
}
@Override
public Object visitStrJoin(StrJoin node) throws Exception {
return super.visitGeneric(node, "visitStrJoin", false);
}
@Override
public Object visitAssert(Assert node) throws Exception {
auxComment.moveComments(node.test, node, true, false);
return super.visitGeneric(node, "visitAssert", true, null, false, false);
}
@Override
public Object visitStr(Str node) throws Exception {
auxComment.writeSpecialsBefore(node);
state.write(NodeUtils.getStringToPrint(node));
if(!state.inStmt()){
fixNewStatementCondition();
}
auxComment.writeSpecialsAfter(node);
return null;
}
@Override
public Object visitClassDef(ClassDef node) throws Exception {
fixNewStatementCondition();
auxComment.writeSpecialsBefore(node.name);
auxComment.writeSpecialsBefore(node);
state.write("class ");
NameTok name = (NameTok) node.name;
state.write(name.id);
//we want the comments to be indented too
state.indent();
{
auxComment.writeSpecialsAfter(name);
if(node.bases.length > 0){
for (exprType expr : node.bases) {
state.pushInStmt(expr);
expr.accept(this);
state.popInStmt();
}
}
checkEndRecord();
for(SimpleNode n: node.body){
n.accept(this);
}
dedent();
}
auxComment.writeSpecialsAfter(node, false);
fixNewStatementCondition();
state.writeLinesAfterClass();
return null;
}
public void visitNode(SimpleNode node) throws Exception{
if(node != null){
beforeNode(node);
node.accept(this);
afterNode(node);
}
}
public Object visitDecoratorsType(decoratorsType node) throws Exception {
beforeNode(node);
visitNode(node.func);
if (node.args != null) {
for (int i = node.args.length-1; i >= 0; i--) {
if (node.args[i] != null)
node.args[i].accept(this);
}
}
if (node.keywords != null) {
for (int i = node.keywords.length-1; i >= 0; i--) {
if (node.keywords[i] != null)
visitNode(node.keywords[i]);
}
}
if (node.starargs != null)
node.starargs.accept(this);
if (node.kwargs != null)
node.kwargs.accept(this);
afterNode(node);
return null;
}
@Override
public Object visitFunctionDef(FunctionDef node) throws Exception {
decoratorsType[] decs = node.decs;
for (decoratorsType dec : decs) {
auxComment.writeSpecialsBefore(dec);
fixNewStatementCondition();
state.write("@");
state.pushInStmt(node);
visitDecoratorsType(dec);
state.popInStmt();
auxComment.writeSpecialsAfter(dec);
}
fixNewStatementCondition();
auxComment.writeSpecialsBefore(node);
state.write("def ");
state.indent();
int lastWrite = state.getLastWrite();
node.name.accept(this);
auxComment.writeStringsAfter(node);
{
//arguments
makeArgs(node.args.args, node.args);
//end arguments
if(!fixNewStatementCondition()){
if(lastWrite == state.getLastWrite()){
state.writeIndentString();
}
}
for(SimpleNode n: node.body){
n.accept(this);
}
dedent();
}
auxComment.writeCommentsAfter(node);
state.writeLinesAfterMethod();
return null;
}
protected void makeArgs(exprType[] args, argumentsType completeArgs) throws Exception {
exprType[] d = completeArgs.defaults;
int argsLen = args.length;
int defaultsLen = d.length;
int diff = argsLen-defaultsLen;
int i = 0;
for (exprType type : args) {
state.pushInStmt(type);
if(i >= diff){
state.pushTempBuffer();
exprType arg = d[i-diff];
if(arg != null){
arg.accept(this);
type.getSpecialsAfter().add(0, state.popTempBuffer());
type.getSpecialsAfter().add(0, "=");
}else{
state.popTempBuffer();
}
}
type.accept(this);
i++;
state.popInStmt();
}
if(completeArgs.vararg != null){
completeArgs.vararg.accept(this);
}
if(completeArgs.kwarg != null){
completeArgs.kwarg.accept(this);
}
}
@Override
public Object visitWith(With node) throws Exception{
//with a as b: print b
state.pushInStmt(node);
//with
auxComment.writeSpecialsBefore(node);
state.indent();
//with a
node.context_expr.accept(this);
//as b:
if(node.optional_vars != null){
node.optional_vars.accept(this);
}
state.popInStmt();
//in b
afterNode(node);
fixNewStatementCondition();
for(SimpleNode n: node.body.body){
n.accept(this);
}
dedent();
return null;
}
@Override
public Object visitYield(Yield node) throws Exception {
return visitGeneric(node, "visitYield");
}
@Override
public Object visitGlobal(Global node) throws Exception {
return visitGeneric(node, "visitGlobal");
}
@Override
public Object visitPass(Pass node) throws Exception {
return visitGeneric(node, "visitPass");
}
@Override
public Object visitNum(Num node) throws Exception {
return visitGeneric(node, "visitNum", false, node.n.toString());
}
@Override
public Object visitName(Name node) throws Exception {
return visitGeneric(node, "visitName", false, node.id, false, false);
}
@Override
public Object visitBreak(Break node) throws Exception {
return visitGeneric(node, "visitBreak");
}
@Override
public Object visitContinue(Continue node) throws Exception {
return visitGeneric(node, "visitContinue");
}
@Override
public Object visitIfExp(IfExp node) throws Exception {
//we have to change the order a bit...
node.body.accept(this);
node.test.accept(this);
if(node.orelse != null){
node.orelse.accept(this);
}
return null;
}
@Override
public Object visitNameTok(NameTok node) throws Exception {
auxComment.writeSpecialsBefore(node);
state.write(node.id);
auxComment.writeSpecialsAfter(node, false);
return null;
}
@Override
public String toString() {
return state.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -