Skip to content

Commit 58c131d

Browse files
committed
fix: refactor get open projects function
1 parent 3f786fd commit 58c131d

4 files changed

Lines changed: 33 additions & 45 deletions

File tree

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.stream.Collectors;
56

7+
import org.eclipse.core.resources.IProject;
68
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
79
import org.eclipse.core.runtime.preferences.InstanceScope;
8-
import org.osgi.service.prefs.BackingStoreException;
910

1011
import com.google.gson.Gson;
11-
import com.google.gson.JsonSyntaxException;
1212

13+
import io.snyk.eclipse.plugin.utils.ResourceUtils;
1314
import io.snyk.eclipse.plugin.utils.SnykLogger;
1415
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfig;
1516
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfigsParam;
@@ -71,22 +72,34 @@ public String getBaseBranch(String folderPath) {
7172
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
7273
return folderConfig.getBaseBranch();
7374
}
74-
75+
7576
public void addAll(List<FolderConfig> folderConfigs) {
7677
for (FolderConfig folderConfig : folderConfigs) {
7778
addFolderConfig(folderConfig);
7879
}
7980
}
80-
81-
public FolderConfigsParam getFolderConfigs(List<String> folderPaths) {
82-
List<FolderConfig> folderConfigs = new ArrayList<>();
83-
for (String folderPath : folderPaths) {
84-
String json = preferenceState.get(folderPath, null);
85-
if (json != null) {
86-
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
87-
folderConfigs.add(folderConfig);
88-
}
89-
}
90-
return new FolderConfigsParam(folderConfigs);
81+
82+
public FolderConfigsParam updateFolderConfigs() {
83+
List<IProject> openProjects = ResourceUtils.getOpenProjects();
84+
85+
List<String> projectPaths = openProjects.stream().map(project -> project.getLocation().toOSString())
86+
.collect(Collectors.toList());
87+
88+
FolderConfigsParam folderConfigs = getFolderConfigs(projectPaths);
89+
90+
return folderConfigs;
91+
92+
}
93+
94+
private FolderConfigsParam getFolderConfigs(List<String> folderPaths) {
95+
List<FolderConfig> folderConfigs = new ArrayList<>();
96+
for (String folderPath : folderPaths) {
97+
String json = preferenceState.get(folderPath, null);
98+
if (json != null) {
99+
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
100+
folderConfigs.add(folderConfig);
101+
}
102+
}
103+
return new FolderConfigsParam(folderConfigs);
91104
}
92105
}

plugin/src/main/java/io/snyk/eclipse/plugin/utils/ResourceUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,3 @@ public static List<IProject> getAccessibleTopLevelProjects() {
106106

107107
return new ArrayList<>(topLevel);
108108
}
109-
}

plugin/src/main/java/io/snyk/languageserver/LsConfigurationUpdater.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package io.snyk.languageserver;
22

33
import java.io.File;
4-
import java.util.List;
5-
import java.util.stream.Collectors;
64

75
import org.apache.commons.lang3.SystemUtils;
8-
import org.eclipse.core.resources.IProject;
96
import org.eclipse.lsp4j.DidChangeConfigurationParams;
107

118
import io.snyk.eclipse.plugin.Activator;
@@ -65,32 +62,14 @@ Settings getCurrentSettings() {
6562
authMethod = "token";
6663
}
6764
String enableDeltaFindings = preferences.getPref(Preferences.FILTER_DELTA_NEW_ISSUES, Boolean.FALSE.toString());
68-
FolderConfigsParam folderConfigsParam = updateFolderConfigs();
65+
FolderConfigsParam folderConfigsParam = FolderConfigs.getInstance().updateFolderConfigs();
6966
return new Settings(activateSnykOpenSource, activateSnykCodeSecurity, activateSnykCodeQuality, activateSnykIac,
7067
insecure, endpoint, additionalParams, additionalEnv, path, sendErrorReports, enableTelemetry,
7168
organization, manageBinariesAutomatically, cliPath, token, integrationName, integrationVersion,
7269
automaticAuthentication, trustedFolders, enableTrustedFolderFeature, scanningMode, enableDeltaFindings,
7370
authMethod, folderConfigsParam);
7471
}
7572

76-
// TODO is there a better place for this function?
77-
private FolderConfigsParam updateFolderConfigs() {
78-
SnykExtendedLanguageClient lc = SnykExtendedLanguageClient.getInstance();
79-
if (lc != null) {
80-
List<IProject> openProjects = lc.getOpenProjects();
81-
82-
List<String> projectPaths = openProjects.stream().map(project -> project.getLocation().toOSString())
83-
.collect(Collectors.toList());
84-
85-
FolderConfigs configs = FolderConfigs.getInstance();
86-
FolderConfigsParam folderConfigs = configs.getFolderConfigs(projectPaths);
87-
88-
return folderConfigs;
89-
}
90-
91-
return null;
92-
}
93-
9473
static class Settings {
9574
private final String activateSnykOpenSource;
9675
private final String activateSnykCodeSecurity;

plugin/src/main/java/io/snyk/languageserver/protocolextension/SnykExtendedLanguageClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,6 @@ private void toggleIgnores(Boolean enableConsistentIgnores) {
157157
});
158158
}
159159

160-
private void createIssueCaches() {
161-
List<IProject> openProjects = getOpenProjects();
162-
for (IProject iProject : openProjects) {
163-
IssueCacheHolder.getInstance().getCacheInstance(iProject);
164-
}
165-
}
166-
167160
private void registerPluginInstalledEventTask() {
168161
if (!Preferences.getInstance().getBooleanPref(Preferences.ANALYTICS_PLUGIN_INSTALLED_SENT, false)) {
169162
if (taskProcessor == null) {
@@ -688,13 +681,17 @@ public void setToolWindow(ISnykToolView toolView) {
688681
}
689682

690683
public void clearCache() {
691-
List<IProject> openProjects = getOpenProjects();
684+
List<IProject> openProjects = ResourceUtils.getOpenProjects();
692685
for (IProject iProject : openProjects) {
693686
IssueCacheHolder.getInstance().getCacheInstance(iProject).clearAll();
694687
}
695688

696689
}
697690

691+
public void setProgressMgr(ProgressManager progressMgr) {
692+
this.progressManager = progressMgr;
693+
}
694+
698695
@Override
699696
public void notifyProgress(final ProgressParams params) {
700697
if (params.getValue() == null) {

0 commit comments

Comments
 (0)