Skip to content

Commit

Permalink
Contribute to exclude property from validation
Browse files Browse the repository at this point in the history
Fixes eclipse-lsp4mp#95

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Nov 3, 2020
1 parent ab86a54 commit 6079e9c
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.lsp4mp.settings.AllMicroProfileSettings;
import org.eclipse.lsp4mp.settings.InitializationOptionsSettings;
import org.eclipse.lsp4mp.settings.MicroProfileCodeLensSettings;
import org.eclipse.lsp4mp.settings.MicroProfileExtensionSettings;
import org.eclipse.lsp4mp.settings.MicroProfileFormattingSettings;
import org.eclipse.lsp4mp.settings.MicroProfileGeneralClientSettings;
import org.eclipse.lsp4mp.settings.MicroProfileSymbolSettings;
Expand All @@ -62,6 +63,8 @@ public class MicroProfileLanguageServer implements LanguageServer, ProcessLangua
private final MicroProfileTextDocumentService textDocumentService;
private final WorkspaceService workspaceService;

private final MicroProfileExtensionSettings extensionSettings;

private Integer parentProcessId;
private MicroProfileLanguageClientAPI languageClient;
private MicroProfileCapabilityManager capabilityManager;
Expand All @@ -70,6 +73,7 @@ public MicroProfileLanguageServer() {
microProfileLanguageService = new MicroProfileLanguageService();
textDocumentService = new MicroProfileTextDocumentService(this);
workspaceService = new MicroProfileWorkspaceService(this);
this.extensionSettings = new MicroProfileExtensionSettings();
}

@Override
Expand Down Expand Up @@ -120,6 +124,8 @@ public synchronized void updateSettings(Object initializationOptionsSettings) {
MicroProfileGeneralClientSettings clientSettings = MicroProfileGeneralClientSettings
.getGeneralMicroProfileSettings(initializationOptionsSettings);
if (clientSettings != null) {
// Merge client settings with extension settings
extensionSettings.merge(clientSettings);
MicroProfileSymbolSettings newSymbols = clientSettings.getSymbols();
if (newSymbols != null) {
textDocumentService.updateSymbolSettings(newSymbols);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.eclipse.lsp4mp.settings;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.eclipse.lsp4mp.services.ValidationType;

import com.google.gson.Gson;

public class MicroProfileExtensionSettings {

private List<MicroProfileGeneralClientSettings> extensionSettings;

public void merge(MicroProfileGeneralClientSettings settings) {
getExtensionSettings().forEach(extensionSettings -> {
merge(settings, extensionSettings);
});
}

private void merge(MicroProfileGeneralClientSettings settings,
MicroProfileGeneralClientSettings extensionSettings) {
// Merge validation unknown excluded
List<String> extensionValidationUnknownExcluded = getValidationExcluded(extensionSettings,
ValidationType.unknown, false);
if (extensionValidationUnknownExcluded != null && !extensionValidationUnknownExcluded.isEmpty()) {
List<String> validationUnknownExcluded = getValidationExcluded(settings, ValidationType.unknown,
true);
merge(extensionValidationUnknownExcluded, validationUnknownExcluded);
}
}

private void merge(List<String> from, List<String> to) {
for (String value : from) {
if (!to.contains(value)) {
to.add(value);
}
}
}

private static List<String> getValidationExcluded(MicroProfileGeneralClientSettings settings, ValidationType type,
boolean create) {
MicroProfileValidationSettings validation = settings.getValidation();
if (validation == null && create) {
validation = new MicroProfileValidationSettings();
settings.setValidation(validation);
}
if (validation == null) {
return null;
}
MicroProfileValidationTypeSettings validationType = null;
switch (type) {
case unknown:
validationType = validation.getUnknown();
if (validationType == null && create) {
validationType = new MicroProfileValidationTypeSettings();
validation.setUnknown(validationType);
}
break;
default:
break;
}
if (validationType == null) {
return null;
}
List<String> excluded = validationType.getExcluded();
if (excluded == null && create) {
excluded = new ArrayList<>();
validationType.setExcluded(excluded);
}
return excluded;
}

public List<MicroProfileGeneralClientSettings> getExtensionSettings() {
if (extensionSettings == null) {
extensionSettings = loadExtensionSettings();
}
return extensionSettings;
}

private synchronized List<MicroProfileGeneralClientSettings> loadExtensionSettings() {
if (extensionSettings != null) {
return extensionSettings;
}
List<MicroProfileGeneralClientSettings> extensionSettings = new ArrayList<>();

try {
Enumeration<URL> resources = this.getClass().getClassLoader().getResources("META-INF/lsp4mp/settings.json");
while (resources.hasMoreElements()) {
URL url1 = resources.nextElement();
MicroProfileGeneralClientSettings settings = new Gson().fromJson(
new InputStreamReader((InputStream) url1.getContent()),
MicroProfileGeneralClientSettings.class);
extensionSettings.add(settings);
}
} catch (IOException e) {

}
return extensionSettings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4mp.utils.AntPathMatcher;
Expand All @@ -30,7 +29,7 @@ public class MicroProfileValidationTypeSettings {

private String severity;

private String[] excluded;
private List<String> excluded;

private transient List<ExcludedProperty> excludedProperties;

Expand Down Expand Up @@ -87,7 +86,7 @@ public void setSeverity(String severity) {
*
* @return the array of properties to ignore for this validation type.
*/
public String[] getExcluded() {
public List<String> getExcluded() {
return excluded;
}

Expand All @@ -96,7 +95,7 @@ public String[] getExcluded() {
*
* @param excluded the array of properties to ignore for this validation type.
*/
public void setExcluded(String[] excluded) {
public void setExcluded(List<String> excluded) {
this.excluded = excluded;
}

Expand Down Expand Up @@ -170,7 +169,7 @@ private synchronized List<ExcludedProperty> createExcludedProperties() {
}
AntPathMatcher matcher = new AntPathMatcher();
matcher.setCachePatterns(true);
return Stream.of(excluded) //
return excluded.stream() //
.map(p -> new ExcludedProperty(p, matcher)) //
.collect(Collectors.toList());
}
Expand Down
Loading

0 comments on commit 6079e9c

Please sign in to comment.