-
Notifications
You must be signed in to change notification settings - Fork 846
/
Java21InputAstVisitor.java
110 lines (99 loc) · 3.12 KB
/
Java21InputAstVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2023 The google-java-format Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.googlejavaformat.java.java21;
import com.google.googlejavaformat.OpsBuilder;
import com.google.googlejavaformat.java.java17.Java17InputAstVisitor;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ConstantCaseLabelTree;
import com.sun.source.tree.DeconstructionPatternTree;
import com.sun.source.tree.DefaultCaseLabelTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.PatternCaseLabelTree;
import com.sun.source.tree.PatternTree;
import com.sun.source.tree.StringTemplateTree;
import javax.lang.model.element.Name;
/**
* Extends {@link Java17InputAstVisitor} with support for AST nodes that were added or modified in
* Java 21.
*/
public class Java21InputAstVisitor extends Java17InputAstVisitor {
public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
super(builder, indentMultiplier);
}
@Override
protected ExpressionTree getGuard(final CaseTree node) {
return node.getGuard();
}
@Override
public Void visitDefaultCaseLabel(DefaultCaseLabelTree node, Void unused) {
token("default");
return null;
}
@Override
public Void visitPatternCaseLabel(PatternCaseLabelTree node, Void unused) {
scan(node.getPattern(), null);
return null;
}
@Override
public Void visitConstantCaseLabel(ConstantCaseLabelTree node, Void aVoid) {
scan(node.getConstantExpression(), null);
return null;
}
@Override
public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unused) {
sync(node);
scan(node.getDeconstructor(), null);
builder.open(plusFour);
token("(");
builder.breakOp();
boolean first = true;
for (PatternTree pattern : node.getNestedPatterns()) {
if (!first) {
token(",");
builder.breakOp(" ");
}
first = false;
scan(pattern, null);
}
builder.close();
token(")");
return null;
}
@SuppressWarnings("preview")
@Override
public Void visitStringTemplate(StringTemplateTree node, Void unused) {
sync(node);
builder.open(plusFour);
scan(node.getProcessor(), null);
token(".");
token(builder.peekToken().get());
for (int i = 0; i < node.getFragments().size() - 1; i++) {
token("{");
builder.breakOp();
scan(node.getExpressions().get(i), null);
token("}");
token(builder.peekToken().get());
}
builder.close();
return null;
}
@Override
protected void variableName(Name name) {
if (name.isEmpty()) {
token("_");
} else {
visit(name);
}
}
}