WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d6ca37c

Browse files
authored
Adding ability for the handled paths to be abosolute paths to individual src files (#112)
Signed-off-by: Shawn Hurley <[email protected]>
1 parent a9d267c commit d6ca37c

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/SampleDelegateCommandHandler.java

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.eclipse.core.runtime.IPath;
1111
import org.eclipse.core.runtime.IProgressMonitor;
1212
import org.eclipse.core.runtime.Path;
13+
import org.eclipse.jdt.core.ICompilationUnit;
1314
import org.eclipse.jdt.core.IJavaElement;
1415
import org.eclipse.jdt.core.IJavaProject;
1516
import org.eclipse.jdt.core.IPackageFragment;
@@ -20,6 +21,7 @@
2021
import org.eclipse.jdt.core.search.SearchPattern;
2122
import org.eclipse.jdt.internal.core.search.JavaSearchParticipant;
2223
import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler;
24+
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
2325
import org.eclipse.jdt.ls.core.internal.JobHelpers;
2426
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
2527
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
@@ -212,32 +214,66 @@ private static List<SymbolInformation> search(String projectName, ArrayList<Stri
212214
}
213215

214216
IJavaSearchScope scope;
217+
var workspaceDirectoryLocation = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getRootPaths();
218+
if (workspaceDirectoryLocation == null || workspaceDirectoryLocation.size() == 0) {
219+
logInfo("unable to find workspace directory location");
220+
return new ArrayList<>();
221+
}
222+
215223
if (includedPaths != null && includedPaths.size() > 0) {
216224
ArrayList<IJavaElement> includedFragments = new ArrayList<IJavaElement>();
217225
for (IJavaProject proj : targetProjects) {
218-
for (IPackageFragment fragment : proj.getPackageFragments()) {
219-
IPath fragmentPath = fragment.getPath();
220-
// if there's no file extension, it's a path to java package from source
221-
// else it is a path pointing to jar, ear, etc. we ignore deps for now
222-
if (fragmentPath.getFileExtension() == null) {
226+
for (String includedPath : includedPaths) {
227+
IPath includedIPath = Path.fromOSString(includedPath);
228+
if (includedIPath.isAbsolute()) {
229+
includedIPath = includedIPath.makeRelativeTo(workspaceDirectoryLocation.iterator().next());
230+
// we need to remove the /src/java from the path
231+
if (includedIPath.segment(0).equals("src")) {
232+
includedIPath = includedIPath.removeFirstSegments(1);
233+
}
234+
if (includedIPath.segment(0).equals("main")) {
235+
includedIPath = includedIPath.removeFirstSegments(1);
236+
}
237+
if (includedIPath.segment(0).equals("java")) {
238+
includedIPath = includedIPath.removeFirstSegments(1);
239+
}
240+
var element = proj.findElement(includedIPath);
241+
if (element == null) {
242+
element = proj.findElement(includedIPath.removeLastSegments(1));
243+
continue;
244+
}
245+
if (element instanceof ICompilationUnit) {
246+
var x = element.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
247+
if (x != null) {
248+
includedFragments.add(x);
249+
}
250+
} else {
251+
includedFragments.add(element);
252+
}
253+
continue;
254+
}
255+
for (IPackageFragment fragment : proj.getPackageFragments()) {
256+
IPath fragmentPath = fragment.getPath();
257+
// if there's no file extension, it's a path to java package from source
258+
// else it is a path pointing to jar, ear, etc. we ignore deps for now
259+
if (fragmentPath.getFileExtension() != null) {
260+
continue;
261+
}
223262
// fragment paths are not actual filesystem paths
224263
// they are of form /<artifact>/src/main/java
225264
// we can only compare the relative path
226265
fragmentPath = fragmentPath.removeFirstSegments(1);
227-
for (String includedPath : includedPaths) {
228-
IPath includedIPath = Path.fromOSString(includedPath);
229-
// when there are more than one sub-projects, the paths are of form
230-
// <project-name>/src/main/java/
231-
if (includedPath.startsWith(proj.getElementName())) {
232-
includedIPath = includedIPath.removeFirstSegments(1);
233-
}
234-
// instead of comparing path strings, comparing segments is better for 2 reasons:
235-
// - we don't have to worry about redundant . / etc in input
236-
// - matching sub-trees is easier with segments than strings
237-
if (includedIPath.segmentCount() <= fragmentPath.segmentCount() &&
238-
includedIPath.matchingFirstSegments(fragmentPath) == includedIPath.segmentCount()) {
239-
includedFragments.add(fragment);
240-
}
266+
// when there are more than one sub-projects, the paths are of form
267+
// <project-name>/src/main/java/
268+
if (includedPath.startsWith(proj.getElementName())) {
269+
includedIPath = includedIPath.removeFirstSegments(1);
270+
}
271+
// instead of comparing path strings, comparing segments is better for 2 reasons:
272+
// - we don't have to worry about redundant . / etc in input
273+
// - matching sub-trees is easier with segments than strings
274+
if (includedIPath.segmentCount() <= fragmentPath.segmentCount() &&
275+
includedIPath.matchingFirstSegments(fragmentPath) == includedIPath.segmentCount()) {
276+
includedFragments.add(fragment);
241277
}
242278
}
243279
}

0 commit comments

Comments
 (0)