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)

No tests yet

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Jun 12, 2019
1 parent fd86020 commit 17460ab
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.eclipse.lsp4xml.services.extensions.CompletionSettings;
import org.eclipse.lsp4xml.services.extensions.save.AbstractSaveContext;
import org.eclipse.lsp4xml.settings.SharedSettings;
import org.eclipse.lsp4xml.settings.XMLExcludedSymbolFiles;
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.XMLSymbolSettings;

Expand Down Expand Up @@ -206,11 +207,21 @@ 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());

Boolean doSymbols = document.doSymbols();
if(doSymbols == null) {
doSymbols = !XMLExcludedSymbolFiles.matches(sharedSettings.symbolSettings.getExcludedFiles(), document);
document.setDoSymbols(doSymbols);
}

if(!sharedSettings.symbolSettings.isEnabled() || !doSymbols) {
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 +423,13 @@ 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.setExcludedFiles(XMLExcludedSymbolFiles.getExcludedSymbolFiles(newPatterns));
}
documents.resetDoSymbols();
}

public XMLSymbolSettings getSharedSymbolSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class TextDocument extends TextDocumentItem {

private static String DEFAULT_DELIMTER = System.lineSeparator();

private Boolean doSymbols;

private ListLineTracker lineTracker;

// Buffer of the text document used only in incremental mode.
Expand All @@ -52,6 +54,20 @@ public void setIncremental(boolean incremental) {
}
}

/**
* @param doSymbols the doSymbols to set
*/
public void setDoSymbols(Boolean doSymbols) {
this.doSymbols = doSymbols;
}

/**
* @return the doSymbols
*/
public Boolean doSymbols() {
return doSymbols;
}

@Override
public void setText(String text) {
super.setText(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ public Collection<TextDocument> all() {
return documents.values();
}
}

public void resetDoSymbols() {
for (TextDocument document : documents.values()) {
document.setDoSymbols(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
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.uriresolver.IExternalSchemaLocationProvider;
import org.eclipse.lsp4xml.utils.URIUtils;

/**
* XML file association between a XML file pattern (glob) and an XML Schema file
Expand Down Expand Up @@ -49,32 +48,11 @@ public void setSystemId(String systemId) {
}

public boolean matches(String uri) {
try {
return matches(new URI(uri));
} catch (Exception e) {
return false;
}
return URIUtils.matches(pattern, uri, pathMatcher);
}

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;
return URIUtils.matches(pattern, uri, pathMatcher);
}

public Map<String, String> getExternalSchemaLocation() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
* 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.PathMatcher;

import org.eclipse.lsp4xml.commons.TextDocument;
import org.eclipse.lsp4xml.utils.URIUtils;

/**
* XMLExcludedSymbolFiles
*/
public class XMLExcludedSymbolFiles {

private transient PathMatcher pathMatcher;

private String pattern;

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


public PathMatcher getPathMatcher() {
return pathMatcher;
}

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

public String getPattern() {
return pattern;
}

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

public boolean matches(String uri) {
return URIUtils.matches(pattern, uri, pathMatcher);
}

public boolean matches(URI uri) {
return URIUtils.matches(pattern, uri, pathMatcher);
}

public static boolean matches(XMLExcludedSymbolFiles[] excluded, TextDocument document) {
if(excluded == null) {
return false;
}

String uri = document.getUri();

int i = 0;
while(i < excluded.length) {
XMLExcludedSymbolFiles currentExclusion = excluded[i];
if(currentExclusion == null) {
return false;
}

if(currentExclusion.matches(uri)) {
return true;
}
i++;
}
return false;
}

public static XMLExcludedSymbolFiles[] getExcludedSymbolFiles(String[] patterns) {
XMLExcludedSymbolFiles[] exclusions = new XMLExcludedSymbolFiles[patterns.length];

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

return exclusions;
}


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

private transient XMLExcludedSymbolFiles[] excludedFiles;

private boolean enabled = true;

private String[] excluded;

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

public void setExcludedFiles(XMLExcludedSymbolFiles[] excludedFiles) {
this.excludedFiles = excludedFiles;
}

public boolean isEnabled() {
return enabled;
}

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

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

public void setExcluded(String[] excluded) {
this.excluded = excluded;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

package org.eclipse.lsp4xml.utils;

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

/**
* URIUtils
*/
Expand Down Expand Up @@ -60,4 +65,33 @@ public static String sanitizingUri(String uri) {
}
return uri;
}

public static boolean matches(String pattern, String uri, PathMatcher pathMatcher) {
try {
return matches(pattern, new URI(uri), pathMatcher);
} catch (Exception e) {
return false;
}
}

public static boolean matches(String pattern, URI uri, PathMatcher pathMatcher) {
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;
}
}

0 comments on commit 17460ab

Please sign in to comment.