Skip to content

Commit b82ccbb

Browse files
fix: in memory folder config [IDE-668] (#280)
* fix: change folderconfigs to use in-memory store * refactor: change folder config variable names for readability * fix: typo in reference folder text * fix: update content root node label when folder config changes * refactor: separate name and label for contentrootnode for easier updating * fix: typo * chore: bump LS protocol version (#281) * fix: only send folder config to ls if the ls has snnt them first * fix: make folder config update tracking folder specific * fix: allow project-specific additional parameters * fix: pmd issues * chore: remove unnecessary imports --------- Co-authored-by: Bastian Doetsch <[email protected]>
1 parent c369c25 commit b82ccbb

13 files changed

+162
-71
lines changed

plugin/src/main/java/io/snyk/eclipse/plugin/preferences/InMemoryPreferenceStore.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ public void addPropertyChangeListener(IPropertyChangeListener listener) {
5050

5151
@Override
5252
public boolean contains(String name) {
53-
// TODO Auto-generated method stub
54-
return false;
53+
return store.containsKey(name);
5554
}
5655

5756
@Override
@@ -128,8 +127,7 @@ public long getLong(String name) {
128127

129128
@Override
130129
public String getString(String name) {
131-
// TODO Auto-generated method stub
132-
return null;
130+
return store.get(name);
133131
}
134132

135133
@Override
@@ -224,8 +222,7 @@ public void setValue(String name, long value) {
224222

225223
@Override
226224
public void setValue(String name, String value) {
227-
// TODO Auto-generated method stub
228-
225+
this.store.put(name, value);
229226
}
230227

231228
@Override

plugin/src/main/java/io/snyk/eclipse/plugin/properties/FolderConfigs.java

+24-26
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@
66
import java.util.Arrays;
77
import java.util.Collections;
88
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
import java.util.concurrent.ConcurrentHashMap;
12+
import java.util.concurrent.ConcurrentSkipListSet;
913

1014
import org.eclipse.core.resources.IProject;
1115
import org.eclipse.core.resources.ProjectScope;
12-
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
1316
import org.eclipse.core.runtime.preferences.IScopeContext;
14-
import org.eclipse.core.runtime.preferences.InstanceScope;
15-
16-
import com.google.gson.Gson;
1717

1818
import io.snyk.eclipse.plugin.Activator;
1919
import io.snyk.eclipse.plugin.utils.ResourceUtils;
2020
import io.snyk.eclipse.plugin.utils.SnykLogger;
2121
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfig;
2222

2323
public class FolderConfigs {
24+
public static Set<Path> LanguageServerConfigReceived = new ConcurrentSkipListSet<>();
25+
2426
protected static FolderConfigs instance;
25-
private static final IEclipsePreferences instancePreferences = InstanceScope.INSTANCE.getNode(Activator.PLUGIN_ID);
26-
private static final Gson gson = new Gson();
27+
28+
private static Map<Path, FolderConfig> inMemoryConfigs = new ConcurrentHashMap<>();
2729

2830
private FolderConfigs() {
2931
}
@@ -36,22 +38,15 @@ public static synchronized FolderConfigs getInstance() {
3638
}
3739

3840
public void addFolderConfig(FolderConfig folderConfig) {
39-
var folderPath = Paths.get(folderConfig.getFolderPath());
40-
persist(folderPath, folderConfig);
41+
storeInMemory(Paths.get(folderConfig.getFolderPath()), folderConfig);
4142
}
4243

4344
public List<String> getLocalBranches(Path projectPath) {
4445
return getFolderConfig(projectPath).getLocalBranches();
4546
}
4647

47-
private void persist(Path path, FolderConfig folderConfig) {
48-
String updatedJson = gson.toJson(folderConfig);
49-
instancePreferences.put(path.normalize().toString(), updatedJson);
50-
try {
51-
instancePreferences.flush();
52-
} catch (Exception e) {
53-
SnykLogger.logError(e);
54-
}
48+
private void storeInMemory(Path path, FolderConfig folderConfig) {
49+
inMemoryConfigs.put(path.normalize(), folderConfig);
5550
}
5651

5752
public String getBaseBranch(Path projectPath) {
@@ -77,10 +72,10 @@ public List<FolderConfig> getAll() {
7772
var additionalParamsList = Arrays.asList(additionalParams.split(" "));
7873
var folderConfig = getFolderConfig(path);
7974
folderConfig.setAdditionalParameters(additionalParamsList);
80-
persist(path, folderConfig);
75+
storeInMemory(path, folderConfig);
8176
folderConfigs.add(folderConfig);
8277
}
83-
78+
8479
return Collections.unmodifiableList(folderConfigs);
8580
}
8681

@@ -90,18 +85,21 @@ public List<FolderConfig> getAll() {
9085
* @return a folder config (always)
9186
*/
9287
public FolderConfig getFolderConfig(Path folderPath) {
93-
Path path = folderPath.normalize();
94-
String json = instancePreferences.get(path.toString(), null);
95-
if (json == null) {
96-
SnykLogger.logInfo("No valid configuration for path: " + folderPath.toString());
97-
FolderConfig folderConfig = new FolderConfig(path.toString());
98-
persist(path, folderConfig);
99-
return folderConfig;
88+
var path = folderPath.normalize();
89+
90+
FolderConfig folderConfig = inMemoryConfigs.get(path);
91+
if (folderConfig == null) {
92+
SnykLogger.logInfo("Did not find FolderConfig for path" + path + ", creating new one.");
93+
folderConfig = new FolderConfig(path.toString());
10094
}
101-
return gson.fromJson(json, FolderConfig.class);
95+
return folderConfig;
10296
}
10397

10498
public static void setInstance(FolderConfigs folderConfigs) {
10599
instance = folderConfigs;
106100
}
101+
102+
public void setLanguageServerConfigReceived(Path path) {
103+
LanguageServerConfigReceived.add(path);
104+
}
107105
}

plugin/src/main/java/io/snyk/eclipse/plugin/properties/ProjectPropertyPage.java

+46-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package io.snyk.eclipse.plugin.properties;
22

3+
import java.nio.file.Path;
4+
import java.util.Arrays;
35
import java.util.concurrent.CompletableFuture;
46

57
import org.eclipse.core.resources.IProject;
68
import org.eclipse.core.resources.ProjectScope;
79
import org.eclipse.core.runtime.IAdaptable;
8-
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
910
import org.eclipse.core.runtime.preferences.IScopeContext;
1011
import org.eclipse.jface.preference.FieldEditorPreferencePage;
1112
import org.eclipse.jface.preference.StringFieldEditor;
1213
import org.eclipse.ui.IWorkbenchPropertyPage;
1314
import org.eclipse.ui.preferences.ScopedPreferenceStore;
1415

1516
import io.snyk.eclipse.plugin.Activator;
17+
import io.snyk.eclipse.plugin.utils.ResourceUtils;
1618
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
1719

1820
/**
@@ -21,32 +23,64 @@
2123
public class ProjectPropertyPage extends FieldEditorPreferencePage implements IWorkbenchPropertyPage {
2224
public static final String SNYK_ADDITIONAL_PARAMETERS = "snyk.additionalParameters";
2325
private IAdaptable element;
24-
IEclipsePreferences projectNode;
26+
private IProject project;
27+
private StringFieldEditor additionalParamsEditor;
28+
private Path projectPath;
2529

2630
public ProjectPropertyPage() {
2731
super(GRID);
2832
}
2933

3034
@Override
3135
public void createFieldEditors() {
32-
init();
33-
addField(new StringFieldEditor(SNYK_ADDITIONAL_PARAMETERS, "Additional Parameters:", getFieldEditorParent()));
36+
init();
37+
additionalParamsEditor = new StringFieldEditor(SNYK_ADDITIONAL_PARAMETERS, "Additional Parameters:",
38+
getFieldEditorParent());
39+
40+
addField(additionalParamsEditor);
41+
42+
populate();
3443
}
3544

36-
public void init() {
37-
IProject project = (IProject) getElement().getAdapter(IProject.class);
38-
if (project != null) {
39-
IScopeContext projectScope = new ProjectScope(project);
40-
projectNode = projectScope.getNode(Activator.PLUGIN_ID);
41-
setPreferenceStore(new ScopedPreferenceStore(projectScope, Activator.PLUGIN_ID));
45+
private void populate() {
46+
if (!FolderConfigs.LanguageServerConfigReceived.contains(projectPath)) {
47+
additionalParamsEditor.setEnabled(false, getFieldEditorParent());
48+
} else {
49+
var folderConfig = FolderConfigs.getInstance().getFolderConfig(projectPath);
50+
if (folderConfig.getAdditionalParameters() != null && folderConfig.getAdditionalParameters().size()>0) {
51+
additionalParamsEditor.setEnabled(true, getFieldEditorParent());
52+
final var addParams = String.join(" ", folderConfig.getAdditionalParameters());
53+
54+
final var preferenceStore = getPreferenceStore();
55+
preferenceStore.setDefault(SNYK_ADDITIONAL_PARAMETERS, addParams);
56+
preferenceStore.setValue(SNYK_ADDITIONAL_PARAMETERS, addParams);
57+
58+
additionalParamsEditor.setStringValue(addParams);
59+
}
4260
}
4361
}
4462

63+
public void init() {
64+
project = (IProject) getElement().getAdapter(IProject.class);
65+
if (project == null)
66+
return;
67+
68+
projectPath = ResourceUtils.getFullPath(project);
69+
IScopeContext projectScope = new ProjectScope(project);
70+
setPreferenceStore(new ScopedPreferenceStore(projectScope, Activator.PLUGIN_ID));
71+
}
72+
4573
@Override
4674
public boolean performOk() {
4775
var retValue = super.performOk();
48-
CompletableFuture
49-
.runAsync(() -> SnykExtendedLanguageClient.getInstance().updateConfiguration());
76+
final var addParams = this.additionalParamsEditor.getStringValue().split(" ");
77+
CompletableFuture.runAsync(() -> {
78+
var folderConfig = FolderConfigs.getInstance().getFolderConfig(projectPath);
79+
folderConfig.setAdditionalParameters(Arrays.asList(addParams));
80+
FolderConfigs.getInstance().addFolderConfig(folderConfig);
81+
SnykExtendedLanguageClient.getInstance().updateConfiguration();
82+
});
83+
5084
return retValue;
5185
}
5286

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/BaseTreeNode.java

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import org.eclipse.swt.graphics.ImageDataProvider;
1515
import org.eclipse.ui.model.WorkbenchLabelProvider;
1616

17-
import io.snyk.eclipse.plugin.utils.SnykIcons;
18-
1917
public class BaseTreeNode extends TreeNode {
2018
private ImageDescriptor imageDescriptor;
2119
private String text = "";

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ContentRootNode.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515
public final class ContentRootNode extends BaseTreeNode {
1616
private Path path;
1717
private String name;
18+
private String label;
1819

19-
public ContentRootNode(String name, Path value) {
20+
public ContentRootNode(String name, String label, Path value) {
2021
super(value);
2122
this.reset();
2223
this.setName(name);
24+
this.setLabel(label);
2325
this.setPath(value);
2426
}
2527

2628
@Override
2729
public ImageDescriptor getImageDescriptor() {
30+
if (path == null) return null;
2831
var iResource = ResourceUtils.getProjectByPath(path);
2932
return getImageDescriptor(iResource);
3033
}
@@ -68,7 +71,7 @@ public void reset() {
6871

6972
@Override
7073
public String getText() {
71-
return getName();
74+
return getName() + getLabel();
7275
}
7376

7477
public Path getPath() {
@@ -85,7 +88,16 @@ public String getName() {
8588
return name;
8689
}
8790

91+
public String getLabel() {
92+
return label;
93+
}
94+
95+
8896
public final void setName(String name) {
8997
this.name = name;
9098
}
99+
100+
public final void setLabel(String label) {
101+
this.label = label;
102+
}
91103
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ISnykToolView.java

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public interface ISnykToolView {
7777
*/
7878
abstract void refreshTree();
7979

80+
/**
81+
* Refreshes the delta reference labels
82+
*/
83+
abstract void refreshDeltaReference();
84+
8085
/**
8186
* Refreshes the browser
8287
*/

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ReferenceChooserDialog.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.swt.widgets.Shell;
1818
import org.eclipse.swt.widgets.Text;
1919

20+
import io.snyk.eclipse.plugin.preferences.Preferences;
2021
import io.snyk.eclipse.plugin.properties.FolderConfigs;
2122
import io.snyk.eclipse.plugin.wizards.SWTWidgetHelper;
2223
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
@@ -103,7 +104,9 @@ public boolean close() {
103104
CompletableFuture.runAsync(() -> {
104105
final var lc = SnykExtendedLanguageClient.getInstance();
105106
lc.updateConfiguration();
106-
lc.triggerScan(projectPath);
107+
if (Preferences.getInstance().getBooleanPref(Preferences.SCANNING_MODE_AUTOMATIC)) {
108+
lc.triggerScan(projectPath);
109+
}
107110
});
108111

109112
return super.close();

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/RootNode.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public void reset() {
1919
List<IProject> openProjects = ResourceUtils.getAccessibleTopLevelProjects();
2020

2121
if (openProjects.isEmpty()) {
22-
var contentRoot = new ContentRootNode("No projects in workspace to scan", null);
22+
var contentRoot = new ContentRootNode("No projects in workspace to scan", "", null);
2323
this.addChild(contentRoot);
2424
return;
2525
}
2626

2727
for (IProject project : openProjects) {
2828
Path path = ResourceUtils.getFullPath(project);
29-
BaseTreeNode contentRoot = new ContentRootNode(project.getName(), path); // NOPMD by bdoetsch on 3/12/25, 11:48 AM
29+
BaseTreeNode contentRoot = new ContentRootNode(project.getName(), "", path); // NOPMD by bdoetsch on 3/12/25, 11:48 AM
3030
this.addChild(contentRoot);
3131
}
3232
}

0 commit comments

Comments
 (0)