|
| 1 | +diff --git a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImpl.java b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImpl.java |
| 2 | +index da4898786f11..2e0e5a3aa4e6 100644 |
| 3 | +--- a/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImpl.java |
| 4 | ++++ b/java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImpl.java |
| 5 | +@@ -20,10 +20,11 @@ |
| 6 | + |
| 7 | + import java.io.File; |
| 8 | + import java.net.URI; |
| 9 | ++import java.util.ArrayList; |
| 10 | + import java.util.HashMap; |
| 11 | ++import java.util.List; |
| 12 | + import java.util.Map; |
| 13 | + import java.util.Objects; |
| 14 | +-import java.util.Set; |
| 15 | + import java.util.WeakHashMap; |
| 16 | + import javax.swing.event.ChangeEvent; |
| 17 | + import javax.swing.event.ChangeListener; |
| 18 | +@@ -54,19 +55,37 @@ public Result optionsFor(FileObject file) { |
| 19 | + if (workspaceFolder != null) { |
| 20 | + return getResult(workspace, workspaceFolder); |
| 21 | + } else { |
| 22 | +- Set<Workspace> workspaces; |
| 23 | ++ List<Workspace> workspaces; |
| 24 | + |
| 25 | + synchronized (this) { |
| 26 | +- workspaces = workspace2Settings.keySet(); |
| 27 | ++ workspaces = new ArrayList<>(workspace2Settings.keySet()); |
| 28 | + } |
| 29 | + |
| 30 | ++ int count = 0; |
| 31 | + for (Workspace w : workspaces) { |
| 32 | ++ if (w == null) |
| 33 | ++ continue; // Since a WeakHashMap is in use, it is possible to receive a null value. |
| 34 | + FileObject folder = findWorkspaceFolder(w, file); |
| 35 | + if (folder != null) { |
| 36 | + return getResult(w, folder); |
| 37 | + } |
| 38 | ++ if (count++ == 0 && workspace == null) |
| 39 | ++ workspace = w; |
| 40 | + } |
| 41 | + |
| 42 | ++ if (count == 1) { |
| 43 | ++ // Since this is a single source file, associate it with the single open workspace, |
| 44 | ++ // even when it is not a descendant of one of the root folders. |
| 45 | ++ FileObject folder; |
| 46 | ++ if (file.isFolder()) { |
| 47 | ++ folder = file; |
| 48 | ++ } else { |
| 49 | ++ folder = file.getParent(); |
| 50 | ++ if (folder == null) |
| 51 | ++ folder = file; |
| 52 | ++ } |
| 53 | ++ return getResult(workspace, folder); |
| 54 | ++ } |
| 55 | + return null; |
| 56 | + } |
| 57 | + } |
| 58 | +diff --git a/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImplTest.java b/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImplTest.java |
| 59 | +index 4c6d3c812f3a..a61ea98d7a24 100644 |
| 60 | +--- a/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImplTest.java |
| 61 | ++++ b/java/java.lsp.server/test/unit/src/org/netbeans/modules/java/lsp/server/singlesourcefile/SingleFileOptionsQueryImplTest.java |
| 62 | +@@ -19,6 +19,7 @@ |
| 63 | + package org.netbeans.modules.java.lsp.server.singlesourcefile; |
| 64 | + |
| 65 | + import java.util.Arrays; |
| 66 | ++import java.util.Collections; |
| 67 | + import java.util.List; |
| 68 | + import java.util.concurrent.atomic.AtomicInteger; |
| 69 | + import org.netbeans.junit.NbTestCase; |
| 70 | +@@ -58,6 +59,17 @@ public void testFindWorkspaceFolder() throws Exception { |
| 71 | + assertEquals(workspace2, SingleFileOptionsQueryImpl.findWorkspaceFolder(workspace, source2)); |
| 72 | + assertEquals(workspace2, SingleFileOptionsQueryImpl.findWorkspaceFolder(workspace, source2.getParent())); |
| 73 | + assertEquals(workspace2, SingleFileOptionsQueryImpl.findWorkspaceFolder(workspace, source2.getParent().getParent())); |
| 74 | ++ |
| 75 | ++ FileObject singleSourceDir = FileUtil.createFolder(wd, "standalone"); |
| 76 | ++ FileObject singleSourceFile = FileUtil.createData(singleSourceDir, "Test.java"); |
| 77 | ++ |
| 78 | ++ assertNull(SingleFileOptionsQueryImpl.findWorkspaceFolder(workspace, singleSourceFile)); |
| 79 | ++ assertNull(SingleFileOptionsQueryImpl.findWorkspaceFolder(workspace, singleSourceDir)); |
| 80 | ++ |
| 81 | ++ Workspace emptyWorkspace = new WorkspaceImpl(Collections.emptyList()); |
| 82 | ++ |
| 83 | ++ assertNull(SingleFileOptionsQueryImpl.findWorkspaceFolder(emptyWorkspace, singleSourceFile)); |
| 84 | ++ assertNull(SingleFileOptionsQueryImpl.findWorkspaceFolder(emptyWorkspace, singleSourceDir)); |
| 85 | + } |
| 86 | + |
| 87 | + public void testWorkspaceOptions() throws Exception { |
| 88 | +@@ -84,6 +96,17 @@ public void testWorkspaceOptions() throws Exception { |
| 89 | + assertEquals("-Dtest=test", query.optionsFor(source2.getParent()).getOptions()); |
| 90 | + assertEquals(workspace2.toURI(), query.optionsFor(source2.getParent()).getWorkDirectory()); |
| 91 | + |
| 92 | ++ assertNotNull(query.optionsFor(source3)); |
| 93 | ++ assertEquals("-Dtest=test", query.optionsFor(source3).getOptions()); |
| 94 | ++ assertEquals(source3.getParent().toURI(), query.optionsFor(source3).getWorkDirectory()); |
| 95 | ++ |
| 96 | ++ assertNotNull(query.optionsFor(source3.getParent())); |
| 97 | ++ assertEquals("-Dtest=test", query.optionsFor(source3.getParent()).getOptions()); |
| 98 | ++ assertEquals(source3.getParent().toURI(), query.optionsFor(source3.getParent()).getWorkDirectory()); |
| 99 | ++ |
| 100 | ++ assertEquals(query.optionsFor(source3), query.optionsFor(source3.getParent())); |
| 101 | ++ assertNull(query.optionsFor(wd)); |
| 102 | ++ |
| 103 | + AtomicInteger changeCount = new AtomicInteger(); |
| 104 | + |
| 105 | + query.optionsFor(source1).addChangeListener(evt -> changeCount.incrementAndGet()); |
| 106 | +@@ -149,6 +172,24 @@ public void testWorkspaceOptions() throws Exception { |
| 107 | + assertEquals("-Dtest=test2", query.optionsFor(source2.getParent()).getOptions()); |
| 108 | + assertEquals(workspace2.toURI(), query.optionsFor(source2.getParent()).getWorkDirectory()); |
| 109 | + |
| 110 | ++ assertNotNull(query.optionsFor(source3)); |
| 111 | ++ assertEquals("-Dtest=test2", query.optionsFor(source3).getOptions()); |
| 112 | ++ assertEquals(source3.getParent().toURI(), query.optionsFor(source3).getWorkDirectory()); |
| 113 | ++ assertNotNull(query.optionsFor(source3.getParent())); |
| 114 | ++ assertEquals("-Dtest=test2", query.optionsFor(source3.getParent()).getOptions()); |
| 115 | ++ assertEquals(source3.getParent().toURI(), query.optionsFor(source3.getParent()).getWorkDirectory()); |
| 116 | ++ assertEquals(query.optionsFor(source3), query.optionsFor(source3.getParent())); |
| 117 | ++ |
| 118 | ++ // with multiple open workspaces: |
| 119 | ++ Workspace emptyWorkspace = new WorkspaceImpl(Collections.emptyList()); |
| 120 | ++ query.setConfiguration(emptyWorkspace, "-Dtest=empty", null); |
| 121 | ++ |
| 122 | ++ assertEquals("-Dtest=test2", query.optionsFor(source1).getOptions()); |
| 123 | ++ assertEquals(workspace1.toURI(), query.optionsFor(source1).getWorkDirectory()); |
| 124 | ++ |
| 125 | ++ assertEquals("-Dtest=test2", query.optionsFor(source2).getOptions()); |
| 126 | ++ assertEquals(workspace2.toURI(), query.optionsFor(source2).getWorkDirectory()); |
| 127 | ++ |
| 128 | + assertNull(query.optionsFor(source3)); |
| 129 | + assertNull(query.optionsFor(source3.getParent())); |
| 130 | + } |
0 commit comments