Skip to content

Commit

Permalink
✨ add ability to set search scope by file paths (#94)
Browse files Browse the repository at this point in the history
* add ability to filter search scope by file paths

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>

* fix for multi-project workspaces

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>

---------

Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad committed May 20, 2024
1 parent f536b91 commit 03ace7a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

import static java.lang.String.format;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RuleEntryParams {

private final String projectName;
private final String query;
private final int location;
private final String analysisMode;
private final ArrayList<String> includedPaths;

public RuleEntryParams(final String commandId, final List<Object> arguments) {
@SuppressWarnings("unchecked")
Map<String, Object> obj = (Map<String, Object>) arguments.stream().findFirst()
.orElseThrow(() -> new UnsupportedOperationException(format("Command '%s' must be called with one rule entry argument!", commandId)));
.orElseThrow(() -> new UnsupportedOperationException(
format("Command '%s' must be called with one rule entry argument!", commandId)));

this.projectName = (String) obj.get("project");
this.query = (String) obj.get("query");
this.location = Integer.parseInt((String) obj.get("location"));
this.analysisMode = (String) obj.get("analysisMode");
this.includedPaths = (ArrayList<String>) obj.get("includedPaths");
}

public String getProjectName() {
Expand All @@ -39,4 +43,7 @@ public String getAnalysisMode() {
return analysisMode;
}

public ArrayList<String> getIncludedPaths() {
return includedPaths;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.File;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
Expand Down Expand Up @@ -37,7 +46,7 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
case RULE_ENTRY_COMMAND_ID:
logInfo("Here we get the arguments for rule entry: "+arguments);
RuleEntryParams params = new RuleEntryParams(commandId, arguments);
return search(params.getProjectName(), params.getQuery(), params.getLocation(), params.getAnalysisMode(), progress);
return search(params.getProjectName(), params.getIncludedPaths(), params.getQuery(), params.getLocation(), params.getAnalysisMode(), progress);
default:
throw new UnsupportedOperationException(format("Unsupported command '%s'!", commandId));
}
Expand Down Expand Up @@ -137,9 +146,7 @@ private static SearchPattern getPatternSingleQuery(int location, String query) t
throw new Exception("unable to create search pattern");
}

private static List<SymbolInformation> search(String projectName, String query, int location, String analsysisMode, IProgressMonitor monitor) throws Exception {
//IJavaSearchScope scope = SearchEngine.createWorkspaceScope();

private static List<SymbolInformation> search(String projectName, ArrayList<String> includedPaths, String query, int location, String analsysisMode, IProgressMonitor monitor) throws Exception {
IJavaProject[] targetProjects;
IJavaProject project = ProjectUtils.getJavaProject(projectName);
if (project != null) {
Expand Down Expand Up @@ -167,7 +174,43 @@ private static List<SymbolInformation> search(String projectName, String query,
logInfo("for project: " + iJavaProject + " found errors: " + errors + " warnings: " + warnings);
}

IJavaSearchScope scope = SearchEngine.createJavaSearchScope(targetProjects, s);
IJavaSearchScope scope;
if (includedPaths != null && includedPaths.size() > 0) {
ArrayList<IJavaElement> includedFragments = new ArrayList<IJavaElement>();
for (IJavaProject proj : targetProjects) {
for (IPackageFragment fragment : proj.getPackageFragments()) {
IPath fragmentPath = fragment.getPath();
// if there's no file extension, it's a path to java package from source
// else it is a path pointing to jar, ear, etc. we ignore deps for now
if (fragmentPath.getFileExtension() == null) {
// fragment paths are not actual filesystem paths
// they are of form /<artifact>/src/main/java
// we can only compare the relative path
fragmentPath = fragmentPath.removeFirstSegments(1);
for (String includedPath : includedPaths) {
IPath includedIPath = Path.fromOSString(includedPath);
// when there are more than one sub-projects, the paths are of form
// <project-name>/src/main/java/
if (includedPath.startsWith(proj.getElementName())) {
includedIPath = includedIPath.removeFirstSegments(1);
}
// instead of comparing path strings, comparing segments is better for 2 reasons:
// - we don't have to worry about redundant . / etc in input
// - matching sub-trees is easier with segments than strings
if (includedIPath.segmentCount() <= fragmentPath.segmentCount() &&
includedIPath.matchingFirstSegments(fragmentPath) == includedIPath.segmentCount()) {
includedFragments.add(fragment);
}
}
}
}
}
IJavaElement[] includedElements = new IJavaElement[includedFragments.size()];
includedElements = includedFragments.toArray(includedElements);
scope = SearchEngine.createJavaSearchScope(true, includedElements, s);
} else {
scope = SearchEngine.createJavaSearchScope(true, targetProjects, s);
}

logInfo("scope: " + scope);

Expand Down

0 comments on commit 03ace7a

Please sign in to comment.