Skip to content

Commit

Permalink
Preference 'xml.symbols.exclude' for an array of patterns to exclude …
Browse files Browse the repository at this point in the history
…symbols for file(s)

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Jun 14, 2019
1 parent d19768c commit b87140e
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,15 @@ public CompletableFuture<List<? extends DocumentHighlight>> documentHighlight(Te
@Override
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(
DocumentSymbolParams params) {
if (!sharedSettings.symbolSettings.isEnabled()) {

TextDocument document = getDocument(params.getTextDocument().getUri());

if(!sharedSettings.symbolSettings.isEnabled() || sharedSettings.symbolSettings.isExcluded(document.getUri())) {
return CompletableFuture.completedFuture(Collections.emptyList());
}

return computeAsync((cancelChecker) -> {
DOMDocument xmlDocument = getXMLDocument(params.getTextDocument().getUri(), cancelChecker);
DOMDocument xmlDocument = getXMLDocument(document);
if (hierarchicalDocumentSymbolSupport) {
return getXMLLanguageService().findDocumentSymbols(xmlDocument, cancelChecker) //
.stream() //
Expand Down Expand Up @@ -412,7 +416,12 @@ public void updateCompletionSettings(CompletionSettings newCompletion) {
}

public void updateSymbolSettings(XMLSymbolSettings newSettings) {
sharedSettings.symbolSettings.setEnabled(newSettings.isEnabled());
XMLSymbolSettings symbolSettings = sharedSettings.symbolSettings;
symbolSettings.setEnabled(newSettings.isEnabled());
String[] newPatterns = newSettings.getExcluded();
if(newPatterns != null) {
symbolSettings.setExcluded(newPatterns);
}
}

public XMLSymbolSettings getSharedSymbolSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,21 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.settings;

import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.lsp4xml.settings.PathPatternMatcher;
import org.eclipse.lsp4xml.uriresolver.IExternalSchemaLocationProvider;

/**
* XML file association between a XML file pattern (glob) and an XML Schema file
* (systemId).
**/
public class XMLFileAssociation {
public class XMLFileAssociation extends PathPatternMatcher {

private transient PathMatcher pathMatcher;
private transient Map<String, String> externalSchemaLocation;
private String pattern;
private String systemId;

public String getPattern() {
return pattern;
}

public void setPattern(String pattern) {
this.pattern = pattern;
this.pathMatcher = null;
}

public String getSystemId() {
return systemId;
}
Expand All @@ -48,35 +34,6 @@ public void setSystemId(String systemId) {
this.externalSchemaLocation = null;
}

public boolean matches(String uri) {
try {
return matches(new URI(uri));
} catch (Exception e) {
return false;
}
}

public boolean matches(URI uri) {
if (pattern.length() < 1) {
return false;
}
if (pathMatcher == null) {
char c = pattern.charAt(0);
String glob = pattern;
if (c != '*' && c != '?' && c != '/') {
// in case of pattern like this pattern="myFile*.xml", we must add '**/' before
glob = "**/" + glob;
}
pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
}
try {
return pathMatcher.matches(Paths.get(uri));
} catch (Exception e) {
// e.printStackTrace();
}
return false;
}

public Map<String, String> getExternalSchemaLocation() {
if (externalSchemaLocation == null) {
this.externalSchemaLocation = new HashMap<String, String>();
Expand All @@ -89,7 +46,7 @@ public Map<String, String> getExternalSchemaLocation() {
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pattern == null) ? 0 : pattern.hashCode());
result = prime * result + ((getPattern() == null) ? 0 : getPattern().hashCode());
result = prime * result + ((systemId == null) ? 0 : systemId.hashCode());
return result;
}
Expand All @@ -103,10 +60,12 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
XMLFileAssociation other = (XMLFileAssociation) obj;
if (pattern == null) {
if (other.pattern != null)
String thisPattern = getPattern();
String otherPattern = other.getPattern();
if (thisPattern == null) {
if (otherPattern != null)
return false;
} else if (!pattern.equals(other.pattern))
} else if (!thisPattern.equals(otherPattern))
return false;
if (systemId == null) {
if (other.systemId != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4xml.settings;

import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Objects;

public class PathPatternMatcher {

private transient PathMatcher pathMatcher;
private String pattern;

public String getPattern() {
return pattern;
}

public void setPattern(String pattern) {
this.pattern = pattern;
this.pathMatcher = null;
}

public PathMatcher getPathMatcher() {
return pathMatcher;
}

public void setPathMatcher(PathMatcher pathMatcher) {
this.pathMatcher = pathMatcher;
}

public boolean matches(String uri) {
try {
return matches(new URI(uri));
} catch (Exception e) {
return false;
}
}

public boolean matches(URI uri) {
if (pattern.length() < 1) {
return false;
}
if (pathMatcher == null) {
char c = pattern.charAt(0);
String glob = pattern;
if (c != '*' && c != '?' && c != '/') {
// in case of pattern like this pattern="myFile*.xml", we must add '**/' before
glob = "**/" + glob;
}
pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
}
try {
return pathMatcher.matches(Paths.get(uri));
} catch (Exception e) {
// e.printStackTrace();
}
return false;
}

@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}

if(obj instanceof PathPatternMatcher) {
PathPatternMatcher other = (PathPatternMatcher) obj;
if(!Objects.equals(pathMatcher, other.getPathMatcher())) {
return false;
}
if(!Objects.equals(pattern, other.getPattern())) {
return false;
}
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/

package org.eclipse.lsp4xml.settings;

import java.util.Objects;

/**
* XMLExcludedSymbolFiles
*/
public class XMLExcludedSymbolFile extends PathPatternMatcher{

public XMLExcludedSymbolFile(String pattern) {
setPattern(pattern);
}

@Override
public boolean equals(Object obj) {
if(this == obj) {
return true;
}

if(!(obj instanceof XMLExcludedSymbolFile)) {
return false;
}

XMLExcludedSymbolFile comparison = (XMLExcludedSymbolFile) obj;
if(!Objects.equals(comparison.getPattern(), getPattern())) {
return false;
}

if(!Objects.equals(comparison.getPathMatcher(), getPathMatcher())) {
return false;
}
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,62 @@
*/
public class XMLSymbolSettings {

private transient XMLExcludedSymbolFile[] excludedFiles;

private boolean enabled = true;

private String[] excluded;

public XMLExcludedSymbolFile[] getExcludedFiles() {
return excludedFiles;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public String[] getExcluded() {
return excluded;
}

/**
* Will use the excluded pattern strings to create a list of
* {@link XMLExcludedSymbolFile} objects within this object.
* @param excluded
*/
public void setExcluded(String[] excluded) {
XMLExcludedSymbolFile[] exclusions = new XMLExcludedSymbolFile[excluded.length];

for(int i = 0; i < excluded.length; i++) {
exclusions[i] = new XMLExcludedSymbolFile(excluded[i]);
}

excludedFiles = exclusions;
}

/**
* Given a file URI, this will check if it matches any of the given
* file patterns.
*
* A uri is 'excluded' if it matches any of the given patterns.
*
* **Important:** Set the excluded file patterns before calling this using 'setExcluded()'.
* @param uri
* @return
*/
public boolean isExcluded(String uri) {
for (XMLExcludedSymbolFile excludedFile : excludedFiles) {

if(excludedFile.matches(uri)) {
return true;
}

}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.lsp4xml.settings;

import static java.io.File.separator;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -25,6 +26,7 @@
import org.eclipse.lsp4j.FormattingOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.XMLLanguageServer;
import org.eclipse.lsp4xml.XMLTextDocumentService;
import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lsp4xml.utils.FilesUtils;
import org.junit.After;
Expand Down Expand Up @@ -84,7 +86,8 @@ public void cleanup() {
" \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + //
" },\r\n" +
" \"symbols\": {\r\n" + //
" \"enabled\": true\r\n" + //
" \"enabled\": true,\r\n" + //
" \"excluded\": [\"**\\\\*.xsd\", \"**\\\\*.xml\"]\r\n" + //
" }\r\n" +
" }\r\n" +
" }\r\n" +
Expand Down Expand Up @@ -126,6 +129,7 @@ public void initializationOptionsSettings() {
// Symbols
assertNotNull(settings.getSymbols());
assertEquals(true, settings.getSymbols().isEnabled());
assertArrayEquals(new String[]{"**\\*.xsd", "**\\*.xml"}, settings.getSymbols().getExcluded());

}

Expand Down Expand Up @@ -204,4 +208,24 @@ public void cachePathSettings() {
System.setProperty("user.home", originalUserHome);
}
}

@Test
public void symbolSettingsTest() {
//Tests that when the settings are updated the shared settings are also updated correctly

InitializeParams params = createInitializeParams(json);
Object initializationOptionsSettings = InitializationOptionsSettings.getSettings(params);
XMLLanguageServer languageServer = new XMLLanguageServer();
languageServer.updateSettings(initializationOptionsSettings); // This should set/update the sharedSettings

XMLExcludedSymbolFile xsdFile = new XMLExcludedSymbolFile("**\\*.xsd");
XMLExcludedSymbolFile xmlFile = new XMLExcludedSymbolFile("**\\*.xml");
XMLExcludedSymbolFile[] expectedExcludedFiles = new XMLExcludedSymbolFile[] {xsdFile, xmlFile};

XMLExcludedSymbolFile[] actualExpectedFiles = languageServer.getSettings().symbolSettings.getExcludedFiles();
assertArrayEquals(expectedExcludedFiles, actualExpectedFiles);

XMLTextDocumentService textDocumentService = (XMLTextDocumentService) languageServer.getTextDocumentService();
}

}
Loading

0 comments on commit b87140e

Please sign in to comment.