Skip to content

Commit 095d45e

Browse files
committed
Add parameter for default imports in ResolveVisitor
Signed-off-by: Ben Sherman <bentshermann@gmail.com>
1 parent df323a5 commit 095d45e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

modules/compiler/src/main/java/script/control/ResolveVisitor.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
public class ResolveVisitor extends ScriptExpressionTransformer {
5858

59-
public static final String[] DEFAULT_IMPORTS = { "java.lang.", "java.util.", "java.io.", "java.net.", "groovy.lang.", "groovy.util." };
59+
public static final String[] DEFAULT_PACKAGE_PREFIXES = { "java.lang.", "java.util.", "java.io.", "java.net.", "groovy.lang.", "groovy.util." };
6060

6161
public static final String[] EMPTY_STRING_ARRAY = new String[0];
6262

@@ -66,12 +66,15 @@ public class ResolveVisitor extends ScriptExpressionTransformer {
6666

6767
private ClassNodeResolver classNodeResolver = new ClassNodeResolver();
6868

69-
private List<ClassNode> libClasses;
69+
private List<ClassNode> defaultImports;
7070

71-
public ResolveVisitor(SourceUnit sourceUnit, CompilationUnit compilationUnit, List<ClassNode> libClasses) {
71+
private List<ClassNode> libImports;
72+
73+
public ResolveVisitor(SourceUnit sourceUnit, CompilationUnit compilationUnit, List<ClassNode> defaultImports, List<ClassNode> libImports) {
7274
this.sourceUnit = sourceUnit;
7375
this.compilationUnit = compilationUnit;
74-
this.libClasses = libClasses;
76+
this.defaultImports = defaultImports;
77+
this.libImports = libImports;
7578
}
7679

7780
@Override
@@ -182,7 +185,7 @@ protected boolean resolveFromModule(ClassNode type) {
182185

183186
protected boolean resolveFromLibImports(ClassNode type) {
184187
var name = type.getName();
185-
for( var cn : libClasses ) {
188+
for( var cn : libImports ) {
186189
if( name.equals(cn.getName()) ) {
187190
type.setRedirect(cn);
188191
return true;
@@ -194,7 +197,7 @@ protected boolean resolveFromLibImports(ClassNode type) {
194197
protected boolean resolveFromDefaultImports(ClassNode type) {
195198
// resolve from script imports
196199
var typeName = type.getName();
197-
for( var cn : Types.TYPES ) {
200+
for( var cn : defaultImports ) {
198201
if( typeName.equals(cn.getNameWithoutPackage()) ) {
199202
type.setRedirect(cn);
200203
return true;
@@ -207,7 +210,7 @@ protected boolean resolveFromDefaultImports(ClassNode type) {
207210
return true;
208211
}
209212
// resolve from default imports
210-
if( resolveFromDefaultImports(type, DEFAULT_IMPORTS) ) {
213+
if( resolveFromDefaultImports(type, DEFAULT_PACKAGE_PREFIXES) ) {
211214
return true;
212215
}
213216
if( "BigInteger".equals(typeName) ) {
@@ -223,7 +226,7 @@ protected boolean resolveFromDefaultImports(ClassNode type) {
223226

224227
private static final Map<String, Set<String>> DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE = new UnlimitedConcurrentCache<>();
225228
static {
226-
DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.putAll(VMPluginFactory.getPlugin().getDefaultImportClasses(DEFAULT_IMPORTS));
229+
DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.putAll(VMPluginFactory.getPlugin().getDefaultImportClasses(DEFAULT_PACKAGE_PREFIXES));
227230
}
228231

229232
protected boolean resolveFromDefaultImports(ClassNode type, String[] packagePrefixes) {
@@ -233,7 +236,7 @@ protected boolean resolveFromDefaultImports(ClassNode type, String[] packagePref
233236
if( redirect != null ) {
234237
type.setRedirect(redirect);
235238
// don't update cache when using a cached lookup
236-
if( packagePrefixes == DEFAULT_IMPORTS ) {
239+
if( packagePrefixes == DEFAULT_PACKAGE_PREFIXES ) {
237240
var packagePrefixSet = DEFAULT_IMPORT_CLASS_AND_PACKAGES_CACHE.computeIfAbsent(typeName, key -> new HashSet<>(2));
238241
packagePrefixSet.add(packagePrefix);
239242
}

modules/language-server/src/main/java/nextflow/lsp/services/script/ScriptAstCache.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import nextflow.script.control.ResolveVisitor;
5252
import nextflow.script.control.VariableScopeVisitor;
5353
import nextflow.script.parser.ScriptParserPluginFactory;
54+
import nextflow.script.types.Types;
5455
import org.codehaus.groovy.GroovyBugError;
5556
import org.codehaus.groovy.ast.ASTNode;
5657
import org.codehaus.groovy.ast.ClassNode;
@@ -100,15 +101,15 @@ public void initialize(String rootUri) {
100101
@Override
101102
protected SourceUnit buildAST(URI uri, FileCache fileCache) {
102103
// compile Groovy classes in lib directory
103-
var libClasses = getLibClasses();
104+
var libImports = getLibImports();
104105

105106
// phase 1: syntax resolution
106107
var sourceUnit = compiler.compile(uri, fileCache);
107108

108109
// phase 2: name resolution
109110
// NOTE: must be done before visiting parents because it transforms nodes
110111
if( sourceUnit != null ) {
111-
new ResolveVisitor(sourceUnit, compilationUnit, libClasses).visit();
112+
new ResolveVisitor(sourceUnit, compilationUnit, Types.TYPES, libImports).visit();
112113
new ParameterSchemaVisitor(sourceUnit).visit();
113114
}
114115
return sourceUnit;
@@ -125,7 +126,7 @@ private static class Entry {
125126

126127
private Map<URI,Entry> libCache = new HashMap<>();
127128

128-
private List<ClassNode> getLibClasses() {
129+
private List<ClassNode> getLibImports() {
129130
if( rootUri == null )
130131
return Collections.emptyList();
131132

0 commit comments

Comments
 (0)