|
23 | 23 | import com.sun.source.tree.Tree;
|
24 | 24 | import java.util.ArrayList;
|
25 | 25 | import java.util.Collections;
|
26 |
| -import java.util.EnumSet; |
27 |
| -import java.util.HashSet; |
28 | 26 | import java.util.List;
|
29 | 27 | import java.util.concurrent.atomic.AtomicBoolean;
|
30 | 28 | import javax.swing.text.Document;
|
|
34 | 32 | import com.sun.source.tree.ExpressionTree;
|
35 | 33 | import com.sun.source.util.SourcePositions;
|
36 | 34 | import com.sun.source.util.TreePath;
|
| 35 | +import java.util.Set; |
37 | 36 | import org.netbeans.api.java.lexer.JavaTokenId;
|
38 | 37 | import org.netbeans.api.java.source.CompilationController;
|
39 | 38 | import org.netbeans.api.java.source.JavaSource.Phase;
|
@@ -74,7 +73,7 @@ private JavaCodeTemplateFilter(Document doc, int startOffset, int endOffset) {
|
74 | 73 | final AtomicBoolean cancel = new AtomicBoolean();
|
75 | 74 | BaseProgressUtils.runOffEventDispatchThread(() -> {
|
76 | 75 | try {
|
77 |
| - ParserManager.parse(Collections.singleton(source), new UserTask() { |
| 76 | + ParserManager.parse(Set.of(source), new UserTask() { |
78 | 77 | @Override
|
79 | 78 | public void run(ResultIterator resultIterator) throws Exception {
|
80 | 79 | if (cancel.get()) {
|
@@ -205,22 +204,12 @@ public synchronized boolean accept(CodeTemplate template) {
|
205 | 204 | if (treeKindCtx == null && stringCtx == null) {
|
206 | 205 | return false;
|
207 | 206 | }
|
208 |
| - EnumSet<Tree.Kind> treeKindContexts = EnumSet.noneOf(Tree.Kind.class); |
209 |
| - HashSet stringContexts = new HashSet(); |
210 |
| - getTemplateContexts(template, treeKindContexts, stringContexts); |
211 |
| - return treeKindContexts.isEmpty() && stringContexts.isEmpty() && treeKindCtx != Tree.Kind.STRING_LITERAL || treeKindContexts.contains(treeKindCtx) || stringContexts.contains(stringCtx); |
212 |
| - } |
213 |
| - |
214 |
| - private void getTemplateContexts(CodeTemplate template, EnumSet<Tree.Kind> treeKindContexts, HashSet<String> stringContexts) { |
215 | 207 | List<String> contexts = template.getContexts();
|
216 |
| - if (contexts != null) { |
217 |
| - for(String context : contexts) { |
218 |
| - try { |
219 |
| - treeKindContexts.add(Tree.Kind.valueOf(context)); |
220 |
| - } catch (IllegalArgumentException iae) { |
221 |
| - stringContexts.add(context); |
222 |
| - } |
223 |
| - } |
| 208 | + if (contexts == null || contexts.isEmpty()) { |
| 209 | + return treeKindCtx != Tree.Kind.STRING_LITERAL; |
| 210 | + } else { |
| 211 | + return (treeKindCtx != null && contexts.contains(treeKindCtx.name())) |
| 212 | + || (stringCtx != null && contexts.contains(stringCtx)); |
224 | 213 | }
|
225 | 214 | }
|
226 | 215 |
|
|
0 commit comments