|
10 | 10 | import org.eclipse.core.runtime.IPath; |
11 | 11 | import org.eclipse.core.runtime.IProgressMonitor; |
12 | 12 | import org.eclipse.core.runtime.Path; |
| 13 | +import org.eclipse.jdt.core.ICompilationUnit; |
13 | 14 | import org.eclipse.jdt.core.IJavaElement; |
14 | 15 | import org.eclipse.jdt.core.IJavaProject; |
15 | 16 | import org.eclipse.jdt.core.IPackageFragment; |
|
20 | 21 | import org.eclipse.jdt.core.search.SearchPattern; |
21 | 22 | import org.eclipse.jdt.internal.core.search.JavaSearchParticipant; |
22 | 23 | import org.eclipse.jdt.ls.core.internal.IDelegateCommandHandler; |
| 24 | +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; |
23 | 25 | import org.eclipse.jdt.ls.core.internal.JobHelpers; |
24 | 26 | import org.eclipse.jdt.ls.core.internal.ProjectUtils; |
25 | 27 | import org.eclipse.jdt.ls.core.internal.ResourceUtils; |
@@ -212,32 +214,66 @@ private static List<SymbolInformation> search(String projectName, ArrayList<Stri |
212 | 214 | } |
213 | 215 |
|
214 | 216 | 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 | + |
215 | 223 | if (includedPaths != null && includedPaths.size() > 0) { |
216 | 224 | ArrayList<IJavaElement> includedFragments = new ArrayList<IJavaElement>(); |
217 | 225 | 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 | + } |
223 | 262 | // fragment paths are not actual filesystem paths |
224 | 263 | // they are of form /<artifact>/src/main/java |
225 | 264 | // we can only compare the relative path |
226 | 265 | 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); |
241 | 277 | } |
242 | 278 | } |
243 | 279 | } |
|
0 commit comments