Search in sources :

Example 6 with DidChangeWatchedFilesParams

use of org.eclipse.lsp4j.DidChangeWatchedFilesParams in project xtext-core by eclipse.

the class ServerTest method testTwoFilesDeleteClose.

@Test
public void testTwoFilesDeleteClose() {
    String fileURI = writeFile("Foo.testlang", "");
    initialize();
    String referencingFileURI = getVirtualFile("Bar.testlang");
    String model = "type Bar {\n" + "    Foo foo\n" + "}\n";
    open(referencingFileURI, model);
    Assert.assertFalse("Bar.testlang references missing type Foo from Foo.testlang: expect error", getDiagnostics().get(referencingFileURI).isEmpty());
    open(fileURI, "type Foo {}");
    Assert.assertTrue("Bar.testlang references type Foo from Foo.testlang: expect no error", getDiagnostics().get(referencingFileURI).isEmpty());
    deleteFile(fileURI);
    languageServer.getWorkspaceService().didChangeWatchedFiles(new DidChangeWatchedFilesParams(Lists.newArrayList(new FileEvent(fileURI, FileChangeType.Deleted))));
    Assert.assertTrue("delete file on disk: expect no error", getDiagnostics().get(referencingFileURI).isEmpty());
    close(fileURI);
    Assert.assertFalse("close deleted file: expect error", getDiagnostics().get(referencingFileURI).isEmpty());
}
Also used : FileEvent(org.eclipse.lsp4j.FileEvent) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) Test(org.junit.Test)

Example 7 with DidChangeWatchedFilesParams

use of org.eclipse.lsp4j.DidChangeWatchedFilesParams in project xtext-core by eclipse.

the class OpenDocumentTest method testOpenedDocumentShadowsPersistedFile.

@Test
public void testOpenedDocumentShadowsPersistedFile() {
    String model = "type Test {\n" + "    NonExisting foo\n" + "}";
    String firstFile = writeFile("MyType1.testlang", model);
    initialize();
    assertEquals("Couldn't resolve reference to TypeDeclaration 'NonExisting'.", Iterables.getFirst(getDiagnostics().get(firstFile), null).getMessage());
    String path = writeFile("MyType2.testlang", "\n            type Foo {\n            }\n        ");
    languageServer.getWorkspaceService().didChangeWatchedFiles(new DidChangeWatchedFilesParams(Lists.newArrayList(new FileEvent(path, FileChangeType.Created))));
    assertEquals("Couldn't resolve reference to TypeDeclaration 'NonExisting'.", Iterables.getFirst(getDiagnostics().get(firstFile), null).getMessage());
    String model2 = "type NonExisting {}";
    open(path, model2);
    Assert.assertNull(Iterables.getFirst(getDiagnostics().get(firstFile), null));
    close(path);
    assertEquals("Couldn't resolve reference to TypeDeclaration 'NonExisting'.", Iterables.getFirst(getDiagnostics().get(firstFile), null).getMessage());
}
Also used : FileEvent(org.eclipse.lsp4j.FileEvent) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) Test(org.junit.Test)

Example 8 with DidChangeWatchedFilesParams

use of org.eclipse.lsp4j.DidChangeWatchedFilesParams in project eclipse.jdt.ls by eclipse.

the class WorkspaceEventHandlerTest method testDiscardStaleWorkingCopies.

@Test
public void testDiscardStaleWorkingCopies() throws Exception {
    IJavaProject javaProject = newEmptyProject();
    IFolder src = javaProject.getProject().getFolder("src");
    IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(src);
    IPackageFragment mypack = srcRoot.createPackageFragment("mypack", true, null);
    // @formatter:off
    String contents = "package mypack;\n" + "public class Foo {" + "}\n";
    // @formatter:on
    ICompilationUnit unit = mypack.createCompilationUnit("Foo.java", contents, true, null);
    openDocument(unit, contents, 1);
    assertTrue(unit.isWorkingCopy());
    String oldUri = JDTUtils.getFileURI(srcRoot.getResource());
    String parentUri = JDTUtils.getFileURI(src);
    String newUri = oldUri.replace("mypack", "mynewpack");
    File oldPack = mypack.getResource().getLocation().toFile();
    File newPack = new File(oldPack.getParent(), "mynewpack");
    Files.move(oldPack, newPack);
    assertTrue(unit.isWorkingCopy());
    DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(Arrays.asList(new FileEvent(newUri, FileChangeType.Created), new FileEvent(parentUri, FileChangeType.Changed), new FileEvent(oldUri, FileChangeType.Deleted)));
    new WorkspaceEventsHandler(projectsManager, javaClient, lifeCycleHandler).didChangeWatchedFiles(params);
    assertFalse(unit.isWorkingCopy());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) FileEvent(org.eclipse.lsp4j.FileEvent) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 9 with DidChangeWatchedFilesParams

use of org.eclipse.lsp4j.DidChangeWatchedFilesParams in project eclipse.jdt.ls by eclipse.

the class WorkspaceEventHandlerTest method testDeleteProjectFolder.

@Test
public void testDeleteProjectFolder() throws Exception {
    importProjects("maven/multimodule3");
    IProject module2 = ProjectUtils.getProject("module2");
    assertTrue(module2 != null && module2.exists());
    String projectUri = JDTUtils.getFileURI(module2);
    FileUtils.deleteDirectory(module2.getLocation().toFile());
    assertTrue(module2.exists());
    clientRequests.clear();
    DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams(Arrays.asList(new FileEvent(projectUri, FileChangeType.Deleted)));
    new WorkspaceEventsHandler(projectsManager, javaClient, lifeCycleHandler).didChangeWatchedFiles(params);
    waitForBackgroundJobs();
    assertFalse(module2.exists());
    List<PublishDiagnosticsParams> diags = getClientRequests("publishDiagnostics");
    assertEquals(9L, diags.size());
    assertEndsWith(diags.get(0).getUri(), "/module2");
    assertEndsWith(diags.get(1).getUri(), "/multimodule3");
    assertEndsWith(diags.get(2).getUri(), "/multimodule3/pom.xml");
    assertEndsWith(diags.get(3).getUri(), "/module2/pom.xml");
    assertEquals(0L, diags.get(3).getDiagnostics().size());
    assertEndsWith(diags.get(4).getUri(), "/module2");
    assertEquals(0L, diags.get(4).getDiagnostics().size());
    assertEndsWith(diags.get(5).getUri(), "/App.java");
    assertEquals(0L, diags.get(5).getDiagnostics().size());
    assertEndsWith(diags.get(6).getUri(), "/AppTest.java");
    assertEquals(0L, diags.get(6).getDiagnostics().size());
    assertEndsWith(diags.get(7).getUri(), "/multimodule3");
    assertEndsWith(diags.get(8).getUri(), "/multimodule3/pom.xml");
}
Also used : FileEvent(org.eclipse.lsp4j.FileEvent) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) IProject(org.eclipse.core.resources.IProject) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 10 with DidChangeWatchedFilesParams

use of org.eclipse.lsp4j.DidChangeWatchedFilesParams in project eclipse.jdt.ls by eclipse.

the class InitHandlerTest method testWatchers.

@Test
public void testWatchers() throws Exception {
    ClientPreferences mockCapabilies = mock(ClientPreferences.class);
    when(mockCapabilies.isWorkspaceChangeWatchedFilesDynamicRegistered()).thenReturn(Boolean.TRUE);
    when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
    importProjects(Arrays.asList("maven/salut", "gradle/simple-gradle"));
    newEmptyProject();
    List<FileSystemWatcher> watchers = projectsManager.registerWatchers();
    // 8 basic + 3 project roots
    assertEquals("Unexpected watchers:\n" + toString(watchers), 12, watchers.size());
    List<FileSystemWatcher> projectWatchers = watchers.subList(9, 12);
    assertTrue(projectWatchers.get(0).getGlobPattern().endsWith("/TestProject"));
    assertTrue(WatchKind.Delete == projectWatchers.get(0).getKind());
    assertTrue(projectWatchers.get(1).getGlobPattern().endsWith("/maven/salut"));
    assertTrue(projectWatchers.get(2).getGlobPattern().endsWith("/gradle/simple-gradle"));
    watchers = watchers.subList(0, 9);
    Collections.sort(watchers, new Comparator<FileSystemWatcher>() {

        @Override
        public int compare(FileSystemWatcher o1, FileSystemWatcher o2) {
            return o1.getGlobPattern().compareTo(o2.getGlobPattern());
        }
    });
    assertEquals("**/*.gradle", watchers.get(0).getGlobPattern());
    assertEquals("**/*.gradle.kts", watchers.get(1).getGlobPattern());
    assertEquals("**/*.java", watchers.get(2).getGlobPattern());
    assertEquals("**/.classpath", watchers.get(3).getGlobPattern());
    assertEquals("**/.project", watchers.get(4).getGlobPattern());
    assertEquals("**/.settings/*.prefs", watchers.get(5).getGlobPattern());
    assertEquals("**/gradle.properties", watchers.get(6).getGlobPattern());
    assertEquals("**/pom.xml", watchers.get(7).getGlobPattern());
    assertEquals("**/src/**", watchers.get(8).getGlobPattern());
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("salut");
    String location = project.getLocation().toString();
    IJavaProject javaProject = JavaCore.create(project);
    // for test purposes only
    removeExclusionPattern(javaProject);
    File outputDir = new File(new File(location), javaProject.getOutputLocation().removeFirstSegments(1).toOSString());
    File outputFile = new File(outputDir, "test.properties");
    String resourceName = location + "/src/main/resources/test.properties";
    String uri = "file://" + resourceName;
    File sourceFile = new Path(resourceName).toFile();
    assertTrue(FileUtils.contentEquals(sourceFile, outputFile));
    FileUtils.writeStringToFile(sourceFile, TEST_CONTENT);
    FileEvent fileEvent = new FileEvent(uri, FileChangeType.Changed);
    DidChangeWatchedFilesParams params = new DidChangeWatchedFilesParams();
    params.getChanges().add(fileEvent);
    server.didChangeWatchedFiles(params);
    JobHelpers.waitForJobsToComplete();
    assertTrue(FileUtils.contentEquals(sourceFile, outputFile));
    verify(client, times(1)).registerCapability(any());
    List<FileSystemWatcher> newWatchers = projectsManager.registerWatchers();
    verify(client, times(1)).registerCapability(any());
    assertEquals("Unexpected watchers:\n" + toString(watchers), 12, newWatchers.size());
    projectWatchers = newWatchers.subList(9, 12);
    assertTrue(projectWatchers.get(0).getGlobPattern().endsWith("/TestProject"));
    assertTrue(projectWatchers.get(1).getGlobPattern().endsWith("/maven/salut"));
    assertTrue(projectWatchers.get(2).getGlobPattern().endsWith("/gradle/simple-gradle"));
    newWatchers = watchers.subList(0, 9);
    Collections.sort(newWatchers, new Comparator<FileSystemWatcher>() {

        @Override
        public int compare(FileSystemWatcher o1, FileSystemWatcher o2) {
            return o1.getGlobPattern().compareTo(o2.getGlobPattern());
        }
    });
    assertEquals(newWatchers, watchers);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) FileEvent(org.eclipse.lsp4j.FileEvent) FileSystemWatcher(org.eclipse.lsp4j.FileSystemWatcher) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(java.io.File) IProject(org.eclipse.core.resources.IProject) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

DidChangeWatchedFilesParams (org.eclipse.lsp4j.DidChangeWatchedFilesParams)14 FileEvent (org.eclipse.lsp4j.FileEvent)13 Test (org.junit.Test)9 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)4 File (java.io.File)3 IProject (org.eclipse.core.resources.IProject)3 IFile (org.eclipse.core.resources.IFile)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 List (java.util.List)1 IFolder (org.eclipse.core.resources.IFolder)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 ClientPreferences (org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 Diagnostic (org.eclipse.lsp4j.Diagnostic)1 FileSystemWatcher (org.eclipse.lsp4j.FileSystemWatcher)1 PublishDiagnosticsParams (org.eclipse.lsp4j.PublishDiagnosticsParams)1