Search in sources :

Example 1 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath in project eclipse.jdt.ls by eclipse.

the class BuildPathCommandTest method testBuildPathOperationInMavenProject.

@Test
public void testBuildPathOperationInMavenProject() throws Exception {
    importProjects("maven/salut");
    IProject project = WorkspaceHelper.getProject("salut");
    PreferenceManager manager = JavaLanguageServerPlugin.getPreferencesManager();
    manager.getPreferences().setRootPaths(Arrays.asList(project.getLocation()));
    ListCommandResult listResult = (ListCommandResult) BuildPathCommand.listSourcePaths();
    assertTrue(listResult.status);
    SourcePath[] sourcePaths = listResult.data;
    assertNotNull(sourcePaths);
    assertEquals(sourcePaths.length, 5);
    IResource srcResource = project.findMember("src");
    Result addSrcResult = BuildPathCommand.addToSourcePath(JDTUtils.getFileURI(srcResource));
    assertFalse(addSrcResult.status);
    assertEquals(addSrcResult.message, MavenBuildSupport.UNSUPPORTED_ON_MAVEN);
    IResource mainJavaResource = project.findMember("src/main/java");
    Result addMainJavaResult = BuildPathCommand.removeFromSourcePath(JDTUtils.getFileURI(mainJavaResource));
    assertFalse(addMainJavaResult.status);
    assertEquals(addMainJavaResult.message, MavenBuildSupport.UNSUPPORTED_ON_MAVEN);
}
Also used : SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) Result(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.Result) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 2 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath in project eclipse.jdt.ls by eclipse.

the class BuildPathCommandTest method testBuildPathOperationInWorkspaceProject.

@Test
public void testBuildPathOperationInWorkspaceProject() throws IOException {
    String path = "singlefile/lesson1";
    File file = copyFiles(path, true);
    IPath workspaceRoot = Path.fromOSString(file.getAbsolutePath());
    PreferenceManager manager = JavaLanguageServerPlugin.getPreferencesManager();
    manager.getPreferences().setRootPaths(Arrays.asList(workspaceRoot));
    IPath srcPath = workspaceRoot.append("src");
    Result addSrcResult = BuildPathCommand.addToSourcePath(srcPath.toFile().toURI().toString());
    assertTrue(addSrcResult.status);
    String invisibleProjectName = ProjectUtils.getWorkspaceInvisibleProjectName(workspaceRoot);
    IProject invisibleProject = ResourcesPlugin.getWorkspace().getRoot().getProject(invisibleProjectName);
    assertTrue(invisibleProject.exists());
    assertTrue(ProjectUtils.isJavaProject(invisibleProject));
    IPath mainJavaPath = workspaceRoot.append("src/main/java");
    Result addMainJavaResult = BuildPathCommand.addToSourcePath(mainJavaPath.toFile().toURI().toString());
    assertFalse(addMainJavaResult.status);
    IPath samplesPath = workspaceRoot.append("samples");
    Result addSamplesResult = BuildPathCommand.addToSourcePath(samplesPath.toFile().toURI().toString());
    assertTrue(addSamplesResult.status);
    ListCommandResult listResult = (ListCommandResult) BuildPathCommand.listSourcePaths();
    assertTrue(listResult.status);
    SourcePath[] sourcePaths = listResult.data;
    assertNotNull(sourcePaths);
    assertEquals(sourcePaths.length, 2);
    assertEquals(sourcePaths[0].displayPath, new Path("lesson1").append("src").toOSString());
    assertEquals(sourcePaths[1].displayPath, new Path("lesson1").append("samples").toOSString());
}
Also used : SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) IPath(org.eclipse.core.runtime.IPath) SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) Path(org.eclipse.core.runtime.Path) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) IPath(org.eclipse.core.runtime.IPath) File(java.io.File) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) IProject(org.eclipse.core.resources.IProject) Result(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.Result) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath in project eclipse.jdt.ls by eclipse.

the class BuildPathCommandTest method testBuildPathOperationInGradleProject.

@Test
public void testBuildPathOperationInGradleProject() throws Exception {
    importProjects("gradle/simple-gradle");
    IProject project = WorkspaceHelper.getProject("simple-gradle");
    PreferenceManager manager = JavaLanguageServerPlugin.getPreferencesManager();
    manager.getPreferences().setRootPaths(Arrays.asList(project.getLocation()));
    ListCommandResult listResult = (ListCommandResult) BuildPathCommand.listSourcePaths();
    assertTrue(listResult.status);
    SourcePath[] sourcePaths = listResult.data;
    assertNotNull(sourcePaths);
    assertEquals(sourcePaths.length, 2);
    IResource srcResource = project.findMember("src");
    Result addSrcResult = BuildPathCommand.addToSourcePath(JDTUtils.getFileURI(srcResource));
    assertFalse(addSrcResult.status);
    assertEquals(addSrcResult.message, GradleBuildSupport.UNSUPPORTED_ON_GRADLE);
    IResource mainJavaResource = project.findMember("src/main/java");
    Result addMainJavaResult = BuildPathCommand.removeFromSourcePath(JDTUtils.getFileURI(mainJavaResource));
    assertFalse(addMainJavaResult.status);
    assertEquals(addMainJavaResult.message, GradleBuildSupport.UNSUPPORTED_ON_GRADLE);
}
Also used : SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) Result(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.Result) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 4 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath in project eclipse.jdt.ls by eclipse.

the class FileEventHandler method getSourcePaths.

private static SourcePath[] getSourcePaths() {
    SourcePath[] sourcePaths = new SourcePath[0];
    ListCommandResult result = (ListCommandResult) BuildPathCommand.listSourcePaths();
    if (result.status && result.data != null && result.data.length > 0) {
        sourcePaths = result.data;
    }
    Arrays.sort(sourcePaths, (a, b) -> {
        return b.path.length() - a.path.length();
    });
    return sourcePaths;
}
Also used : SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult)

Example 5 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath in project eclipse.jdt.ls by eclipse.

the class FileEventHandler method resolvePackage.

private static Object resolvePackage(IPath javaElementLocation, SourcePath[] sourcePaths, boolean returnModel) {
    for (SourcePath sourcePath : sourcePaths) {
        IPath sourceLocation = Path.fromOSString(sourcePath.path);
        IPath sourceEntry = Path.fromOSString(sourcePath.classpathEntry);
        if (sourceLocation.isPrefixOf(javaElementLocation)) {
            try {
                IJavaProject javaProject = ProjectUtils.getJavaProject(sourcePath.projectName);
                if (javaProject == null) {
                    return null;
                }
                IPackageFragmentRoot packageRoot = javaProject.findPackageFragmentRoot(sourceEntry);
                if (packageRoot == null) {
                    return null;
                }
                String packageName = String.join(".", javaElementLocation.makeRelativeTo(sourceLocation).segments());
                if (returnModel) {
                    return packageRoot.getPackageFragment(packageName);
                }
                return packageName;
            } catch (CoreException e) {
                JavaLanguageServerPlugin.logException("Failed to resolve the package fragment", e);
            }
            return null;
        }
    }
    return null;
}
Also used : SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

SourcePath (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath)8 ListCommandResult (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult)6 IProject (org.eclipse.core.resources.IProject)4 IResource (org.eclipse.core.resources.IResource)4 IPath (org.eclipse.core.runtime.IPath)4 Result (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.Result)4 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)4 PreferenceManager (org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager)4 Path (org.eclipse.core.runtime.Path)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 Test (org.junit.Test)3 CoreException (org.eclipse.core.runtime.CoreException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 FileRename (org.eclipse.lsp4j.FileRename)2 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1