Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ignore targetDirectory and add a ignore variable. #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/main/java/net/prominic/groovyls/GroovyServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public class GroovyServices implements TextDocumentService, WorkspaceService, La
private LanguageClient languageClient;

private Path workspaceRoot;

Path targetDirectory;

List<Path> ignoredDirectory;
private ICompilationUnitFactory compilationUnitFactory;
private GroovyLSCompilationUnit compilationUnit;
private ASTNodeVisitor astVisitor;
Expand Down Expand Up @@ -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<String> classpathList = new ArrayList<>();
List<Path> ignoredDirList = new ArrayList<>();
boolean change = false;

if (settings.has("groovy") && settings.get("groovy").isJsonObject()) {
JsonObject groovy = settings.get("groovy").getAsJsonObject();
Expand All @@ -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();
Expand Down Expand Up @@ -271,7 +309,7 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
}
}

return result;
return result;
}

@Override
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,11 +67,15 @@ public void invalidateCompilationUnit() {
classLoader = null;
}

public GroovyLSCompilationUnit create(Path workspaceRoot, FileContentsTracker fileContentsTracker) {
public GroovyLSCompilationUnit create(Path workspaceRoot, Path targetDirectory,List<Path> 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);
}
Expand All @@ -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<URI> urisToAdd = changedUris;
fileContentsTracker.getOpenURIs().forEach(uri -> {
// if we're only tracking changes, skip all files that haven't
Expand Down Expand Up @@ -157,11 +168,30 @@ protected void getClasspathList(List<String> result) {
}
}

protected void addDirectoryToCompilationUnit(Path dirPath, GroovyLSCompilationUnit compilationUnit,
protected void addDirectoryToCompilationUnit(Path dirPath, List<Path> ignoredPaths, GroovyLSCompilationUnit compilationUnit,
FileContentsTracker fileContentsTracker, Set<URI> changedUris) {
try {

File targetDirectory = compilationUnit.getConfiguration().getTargetDirectory();

boolean testTargetDir = targetDirectory != null && targetDirectory.exists();

try {
BiPredicate<Path, BasicFileAttributes> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> ignoredDirectory, FileContentsTracker fileContentsTracker);
}