From 9cbf4a5b3fabdcba66b203d7915c10289012653f Mon Sep 17 00:00:00 2001 From: trihims Date: Thu, 26 Oct 2023 14:11:53 +0530 Subject: [PATCH] Ignore targetDirectory and add a ignore variable. --- .../net/prominic/groovyls/GroovyServices.java | 46 +++++++++++++++++-- .../config/CompilationUnitFactory.java | 42 ++++++++++++++--- .../config/ICompilationUnitFactory.java | 2 +- 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/prominic/groovyls/GroovyServices.java b/src/main/java/net/prominic/groovyls/GroovyServices.java index 93b3f7d..b33eb8d 100644 --- a/src/main/java/net/prominic/groovyls/GroovyServices.java +++ b/src/main/java/net/prominic/groovyls/GroovyServices.java @@ -112,6 +112,10 @@ public class GroovyServices implements TextDocumentService, WorkspaceService, La private LanguageClient languageClient; private Path workspaceRoot; + + Path targetDirectory; + + List ignoredDirectory; private ICompilationUnitFactory compilationUnitFactory; private GroovyLSCompilationUnit compilationUnit; private ASTNodeVisitor astVisitor; @@ -182,11 +186,13 @@ public void didChangeConfiguration(DidChangeConfigurationParams params) { return; } JsonObject settings = (JsonObject) params.getSettings(); - this.updateClasspath(settings); + this.updateSettings(settings); } - private void updateClasspath(JsonObject settings) { + private void updateSettings(JsonObject settings) { List classpathList = new ArrayList<>(); + List ignoredDirList = new ArrayList<>(); + boolean change = false; if (settings.has("groovy") && settings.get("groovy").isJsonObject()) { JsonObject groovy = settings.get("groovy").getAsJsonObject(); @@ -196,11 +202,43 @@ private void updateClasspath(JsonObject settings) { classpathList.add(element.getAsString()); }); } + + if(groovy.has("targetDirectory")){ + String tempTargetDirectory =groovy.get("targetDirectory").getAsString(); + + if(tempTargetDirectory!=null && tempTargetDirectory.length()>0){ + Path finalPath = workspaceRoot.resolve(Paths.get(tempTargetDirectory)); + if(!finalPath.equals(targetDirectory)) { + targetDirectory=finalPath; + change = true; + } + } + } + if (groovy.has("ignoredDirectory") && groovy.get("ignoredDirectory").isJsonArray()) { + JsonArray ignoredDirectory = groovy.get("ignoredDirectory").getAsJsonArray(); + ignoredDirectory.forEach(element -> { + String path = element.getAsString(); + if(path!=null && path.length()>0){ + ignoredDirList.add(workspaceRoot.resolve(path)); + } + }); + } + } + + + if(!ignoredDirList.equals(ignoredDirectory)){ + ignoredDirectory=ignoredDirList; + change=true; } if (!classpathList.equals(compilationUnitFactory.getAdditionalClasspathList())) { compilationUnitFactory.setAdditionalClasspathList(classpathList); + change=true; + } + + if(change) + { createOrUpdateCompilationUnit(); compile(); visitAST(); @@ -271,7 +309,7 @@ public CompletableFuture, CompletionList>> completio } } - return result; + return result; } @Override @@ -410,7 +448,7 @@ private boolean createOrUpdateCompilationUnit() { } GroovyLSCompilationUnit oldCompilationUnit = compilationUnit; - compilationUnit = compilationUnitFactory.create(workspaceRoot, fileContentsTracker); + compilationUnit = compilationUnitFactory.create(workspaceRoot, targetDirectory, ignoredDirectory,fileContentsTracker); fileContentsTracker.resetChangedFiles(); if (compilationUnit != null) { diff --git a/src/main/java/net/prominic/groovyls/config/CompilationUnitFactory.java b/src/main/java/net/prominic/groovyls/config/CompilationUnitFactory.java index 3a89786..791cc3e 100644 --- a/src/main/java/net/prominic/groovyls/config/CompilationUnitFactory.java +++ b/src/main/java/net/prominic/groovyls/config/CompilationUnitFactory.java @@ -25,11 +25,13 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.BiPredicate; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.SourceUnit; @@ -65,11 +67,15 @@ public void invalidateCompilationUnit() { classLoader = null; } - public GroovyLSCompilationUnit create(Path workspaceRoot, FileContentsTracker fileContentsTracker) { + public GroovyLSCompilationUnit create(Path workspaceRoot, Path targetDirectory,List ignoredDirectory, FileContentsTracker fileContentsTracker) { if (config == null) { config = getConfiguration(); } + if(ignoredDirectory==null){ + ignoredDirectory=new ArrayList<>(); + } + if (classLoader == null) { classLoader = new GroovyClassLoader(ClassLoader.getSystemClassLoader().getParent(), config, true); } @@ -94,9 +100,14 @@ public GroovyLSCompilationUnit create(Path workspaceRoot, FileContentsTracker fi compilationUnit.removeSources(sourcesToRemove); } + if(targetDirectory!=null){ + compilationUnit.getConfiguration().setTargetDirectory(targetDirectory.toFile()); + } + if (workspaceRoot != null) { - addDirectoryToCompilationUnit(workspaceRoot, compilationUnit, fileContentsTracker, changedUris); - } else { + addDirectoryToCompilationUnit(workspaceRoot, ignoredDirectory, compilationUnit, fileContentsTracker, changedUris); + } + else { final Set urisToAdd = changedUris; fileContentsTracker.getOpenURIs().forEach(uri -> { // if we're only tracking changes, skip all files that haven't @@ -157,11 +168,30 @@ protected void getClasspathList(List result) { } } - protected void addDirectoryToCompilationUnit(Path dirPath, GroovyLSCompilationUnit compilationUnit, + protected void addDirectoryToCompilationUnit(Path dirPath, List ignoredPaths, GroovyLSCompilationUnit compilationUnit, FileContentsTracker fileContentsTracker, Set changedUris) { - try { + + File targetDirectory = compilationUnit.getConfiguration().getTargetDirectory(); + + boolean testTargetDir = targetDirectory != null && targetDirectory.exists(); + + try { + BiPredicate matcher = (path, basicFileAttributes) -> { + if (testTargetDir && path.startsWith(targetDirectory.toPath())) { + return false; + } + + for (Path p : ignoredPaths) { + if (path.startsWith(p)) { + return false; + } + } + + return true; + }; + if (Files.exists(dirPath)) { - Files.walk(dirPath).forEach((filePath) -> { + Files.find(dirPath,Integer.MAX_VALUE,matcher).forEach((filePath) -> { if (!filePath.toString().endsWith(FILE_EXTENSION_GROOVY)) { return; } diff --git a/src/main/java/net/prominic/groovyls/config/ICompilationUnitFactory.java b/src/main/java/net/prominic/groovyls/config/ICompilationUnitFactory.java index e6ea483..e7d3204 100644 --- a/src/main/java/net/prominic/groovyls/config/ICompilationUnitFactory.java +++ b/src/main/java/net/prominic/groovyls/config/ICompilationUnitFactory.java @@ -39,5 +39,5 @@ public interface ICompilationUnitFactory { /** * Returns a compilation unit. */ - public GroovyLSCompilationUnit create(Path workspaceRoot, FileContentsTracker fileContentsTracker); + public GroovyLSCompilationUnit create(Path workspaceRoot,Path targetDirectory, List ignoredDirectory, FileContentsTracker fileContentsTracker); } \ No newline at end of file