Skip to content

Commit

Permalink
SLLS-258 refactor handling of omnisharp settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Aug 27, 2024
1 parent 6902ae2 commit 8b6bd0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,33 +264,39 @@ CompletableFuture<Map<String, Object>> requestSonarLintAndOmnisharpConfiguration
if (response != null) {
var settingsMap = Utils.parseToMap(response.get(0));
if (settingsMap != null) {
return updateProperties(uri, response, settingsMap);
return updateProperties(uri, params, response, settingsMap);
}
}
return Collections.emptyMap();
});
}

static Map<String, Object> updateProperties(@org.jetbrains.annotations.Nullable URI workspaceUri, List<Object> response, Map<String, Object> settingsMap) {
static Map<String, Object> updateProperties(@org.jetbrains.annotations.Nullable URI workspaceUri, ConfigurationParams params, List<Object> response, Map<String, Object> settingsMap) {
var analyzerProperties = (Map<String, String>) settingsMap.getOrDefault(ANALYZER_PROPERTIES, Maps.newHashMap());
var analysisExcludes = (String) settingsMap.getOrDefault(ANALYSIS_EXCLUDES, "");
forceIgnoreRazorFiles(analyzerProperties);
var solutionRelativePath = tryGetSetting(response, 1, "");
var solutionRelativePath = tryGetSetting(response, getIndexOf(DOTNET_DEFAULT_SOLUTION_PATH, params), "");
if (!solutionRelativePath.isEmpty() && workspaceUri != null) {
// uri: file:///Users/me/Documents/Sonar/roslyn
// solutionPath: Roslyn.sln
// we want: /Users/me/Documents/Sonar/roslyn/Roslyn.sln
analyzerProperties.put("sonar.cs.internal.solutionPath", Path.of(workspaceUri).resolve(solutionRelativePath).toAbsolutePath().toString());
}
analyzerProperties.put("sonar.cs.internal.useNet6", tryGetSetting(response, 2, "true"));
analyzerProperties.put("sonar.cs.internal.loadProjectOnDemand", tryGetSetting(response, 3, "false"));
analyzerProperties.put("sonar.cs.internal.loadProjectsTimeout", tryGetSetting(response, 4, "60"));
analyzerProperties.put("sonar.cs.internal.useNet6", tryGetSetting(response, getIndexOf(OMNISHARP_USE_MODERN_NET, params), "true"));
analyzerProperties.put("sonar.cs.internal.loadProjectOnDemand", tryGetSetting(response, getIndexOf(OMNISHARP_LOAD_PROJECT_ON_DEMAND, params), "false"));
analyzerProperties.put("sonar.cs.internal.loadProjectsTimeout", tryGetSetting(response, getIndexOf(OMNISHARP_PROJECT_LOAD_TIMEOUT, params), "60"));
settingsMap.put(ANALYZER_PROPERTIES, analyzerProperties);
settingsMap.put(ANALYSIS_EXCLUDES, addVscodeExcludesToSonarLintExcludes(analysisExcludes, response));

return settingsMap;
}

private static int getIndexOf(String section, ConfigurationParams params) {
var configItem =
params.getItems().stream().filter(configurationItem -> configurationItem.getSection().equals(section)).findFirst();
return configItem.map(configurationItem -> params.getItems().indexOf(configurationItem)).orElse(-1);
}

private static String addVscodeExcludesToSonarLintExcludes(String sonarLintExcludes, List<Object> response) {
var vscodeFilesExcludeMap = tryGetSettingMap(response, 5, Map.of());
var globPatterns = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import org.eclipse.lsp4j.ConfigurationParams;
import org.eclipse.lsp4j.MessageType;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -726,7 +727,7 @@ void shouldUpdateAnalyzerProperties() {
new JsonPrimitive("600"));
Map<String, Object> settingsMap = new HashMap<>(Map.of("disableTelemetry", false, "focusOnNewCode", true));

var result = SettingsManager.updateProperties(workspaceUri, response, settingsMap);
var result = SettingsManager.updateProperties(workspaceUri, new ConfigurationParams(), response, settingsMap);

assertThat(result).containsKey(ANALYZER_PROPERTIES);
var analyzerProperties = (Map<String, String>) result.get(ANALYZER_PROPERTIES);
Expand All @@ -742,7 +743,7 @@ void shouldIgnoreRazorFiles() {
List<Object> response = List.of("{\"disableTelemetry\": false,\"focusOnNewCode\": true, \"analyzerProperties\":{\"sonar.cs.file.suffixes\":\".cs\",\".razor\"}");
Map<String, Object> settingsMap = new HashMap<>(Map.of("disableTelemetry", false, "focusOnNewCode", true, "analyzerProperties", new HashMap<>(Map.of("sonar.cs.file.suffixes", ".cs,.razor"))));

var result = SettingsManager.updateProperties(workspaceUri, response, settingsMap);
var result = SettingsManager.updateProperties(workspaceUri, new ConfigurationParams(), response, settingsMap);

assertThat(result).containsKey(ANALYZER_PROPERTIES);
var analyzerProperties = (Map<String, String>) result.get(ANALYZER_PROPERTIES);
Expand Down

0 comments on commit 8b6bd0a

Please sign in to comment.