From 946ef8e74e69f58d08a8faa9d8917c32ab103737 Mon Sep 17 00:00:00 2001 From: Nikolas Komonen Date: Tue, 4 Jun 2019 16:33:01 -0400 Subject: [PATCH] Preference to turn off document symbols Fixes #413 Signed-off-by: Nikolas Komonen --- .../eclipse/lsp4xml/XMLLanguageServer.java | 6 + .../lsp4xml/XMLTextDocumentService.java | 9 + .../lsp4xml/settings/SharedSettings.java | 2 + .../settings/XMLGeneralClientSettings.java | 10 ++ .../lsp4xml/settings/XMLSymbolSettings.java | 27 +++ .../ClientCapabilitiesWrapper.java | 2 +- .../ServerCapabilitiesInitializer.java | 2 +- .../capabilities/XMLCapabilityManager.java | 11 +- .../lsp4xml/settings/SettingsTest.java | 167 ++++++++++-------- 9 files changed, 153 insertions(+), 83 deletions(-) create mode 100644 org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLSymbolSettings.java diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java index 08db5d5e71..3baf142ad9 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLLanguageServer.java @@ -49,6 +49,7 @@ import org.eclipse.lsp4xml.settings.XMLExperimentalCapabilities; import org.eclipse.lsp4xml.settings.XMLFormattingOptions; import org.eclipse.lsp4xml.settings.XMLGeneralClientSettings; +import org.eclipse.lsp4xml.settings.XMLSymbolSettings; import org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesInitializer; import org.eclipse.lsp4xml.settings.capabilities.XMLCapabilityManager; import org.eclipse.lsp4xml.utils.FilesUtils; @@ -139,6 +140,11 @@ public void updateSettings(Object initializationOptionsSettings) { xmlTextDocumentService.updateCompletionSettings(newCompletions); } + XMLSymbolSettings newSymbols = xmlClientSettings.getSymbols(); + if(newSymbols != null) { + xmlTextDocumentService.updateSymbolSettings(newSymbols); + } + ServerSettings serverSettings = xmlClientSettings.getServer(); if(serverSettings != null) { String workDir = serverSettings.getNormalizedWorkDir(); diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java index f7b159bd19..b6baedaf00 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/XMLTextDocumentService.java @@ -73,6 +73,7 @@ import org.eclipse.lsp4xml.services.extensions.save.AbstractSaveContext; import org.eclipse.lsp4xml.settings.SharedSettings; import org.eclipse.lsp4xml.settings.XMLFormattingOptions; +import org.eclipse.lsp4xml.settings.XMLSymbolSettings; /** * XML text document service. @@ -431,6 +432,14 @@ public void updateCompletionSettings(CompletionSettings newCompletion) { sharedSettings.completionSettings.setAutoCloseTags(newCompletion.isAutoCloseTags()); } + public void updateSymbolSettings(XMLSymbolSettings newSettings) { + sharedSettings.symbolSettings.setEnabled(newSettings.isEnabled()); + } + + public XMLSymbolSettings getSharedSymbolSettings() { + return sharedSettings.symbolSettings; + } + public boolean isIncrementalSupport() { return documents.isIncremental(); } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/SharedSettings.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/SharedSettings.java index 02b7cdb206..180d9d7e78 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/SharedSettings.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/SharedSettings.java @@ -24,12 +24,14 @@ public class SharedSettings { public final FoldingRangeCapabilities foldingSettings; public XMLFormattingOptions formattingSettings; public final XMLValidationSettings validationSettings; + public final XMLSymbolSettings symbolSettings; public SharedSettings() { this.completionSettings = new CompletionSettings(); this.foldingSettings = new FoldingRangeCapabilities(); this.formattingSettings = new XMLFormattingOptions(true); this.validationSettings = new XMLValidationSettings(); + this.symbolSettings = new XMLSymbolSettings(); } public void setFormattingSettings(XMLFormattingOptions formattingOptions) { diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLGeneralClientSettings.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLGeneralClientSettings.java index 83c40c7116..db189fe9e4 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLGeneralClientSettings.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLGeneralClientSettings.java @@ -44,10 +44,20 @@ public class XMLGeneralClientSettings { private ServerSettings server; + private XMLSymbolSettings symbols; + public void setLogs(LogsSettings logs) { this.logs = logs; } + public XMLSymbolSettings getSymbols() { + return symbols; + } + + public void setSymbols(XMLSymbolSettings symbols) { + this.symbols = symbols; + } + public LogsSettings getLogs() { return logs; } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLSymbolSettings.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLSymbolSettings.java new file mode 100644 index 0000000000..e31befd555 --- /dev/null +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/XMLSymbolSettings.java @@ -0,0 +1,27 @@ +/******************************************************************************* +* 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; + +/** + * XMLSymbolPreferences for Document Symbols + */ +public class XMLSymbolSettings { + + private boolean enabled = true; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} \ No newline at end of file diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ClientCapabilitiesWrapper.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ClientCapabilitiesWrapper.java index ad9799be04..994d58e14f 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ClientCapabilitiesWrapper.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ClientCapabilitiesWrapper.java @@ -68,7 +68,7 @@ public boolean isRenameDynamicRegistrationSupported() { return v3Supported && isDynamicRegistrationSupported(getTextDocument().getRename()); } - public boolean isDocumentSymbolDynamicRegistered() { + public boolean isDocumentSymbolDynamicRegistrationSupported() { return v3Supported && isDynamicRegistrationSupported(getTextDocument().getDocumentSymbol()); } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ServerCapabilitiesInitializer.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ServerCapabilitiesInitializer.java index a747b34f9a..c1041a3eb7 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ServerCapabilitiesInitializer.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/ServerCapabilitiesInitializer.java @@ -43,7 +43,7 @@ public static ServerCapabilities getNonDynamicServerCapabilities(ClientCapabilit serverCapabilities .setTextDocumentSync(isIncremental ? TextDocumentSyncKind.Incremental : TextDocumentSyncKind.Full); - serverCapabilities.setDocumentSymbolProvider(!clientCapabilities.isDocumentSymbolDynamicRegistered()); + serverCapabilities.setDocumentSymbolProvider(!clientCapabilities.isDocumentSymbolDynamicRegistrationSupported()); serverCapabilities.setDocumentHighlightProvider(!clientCapabilities.isDocumentHighlightDynamicRegistered()); serverCapabilities.setCodeActionProvider(!clientCapabilities.isCodeActionDynamicRegistered()); serverCapabilities diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/XMLCapabilityManager.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/XMLCapabilityManager.java index e899b532d9..a1032b79b5 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/XMLCapabilityManager.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/settings/capabilities/XMLCapabilityManager.java @@ -45,6 +45,7 @@ import org.eclipse.lsp4j.services.LanguageClient; import org.eclipse.lsp4xml.XMLTextDocumentService; import org.eclipse.lsp4xml.settings.XMLFormattingOptions; +import org.eclipse.lsp4xml.settings.XMLSymbolSettings; /** * Manager for capability related tasks @@ -125,9 +126,6 @@ public void initializeCapabilities() { if (this.getClientCapabilities().isDocumentHighlightDynamicRegistered()) { registerCapability(DOCUMENT_HIGHLIGHT_ID, TEXT_DOCUMENT_HIGHLIGHT); } - if (this.getClientCapabilities().isDocumentSymbolDynamicRegistered()) { - registerCapability(DOCUMENT_SYMBOL_ID, TEXT_DOCUMENT_DOCUMENT_SYMBOL); - } if (this.getClientCapabilities().isRangeFoldingDynamicRegistrationSupported()) { registerCapability(FOLDING_RANGE_ID, TEXT_DOCUMENT_FOLDING_RANGE); } @@ -165,6 +163,13 @@ public void syncDynamicCapabilitiesWithPreferences() { toggleCapability(formattingPreferences.isEnabled(), FORMATTING_RANGE_ID, ServerCapabilitiesConstants.TEXT_DOCUMENT_RANGE_FORMATTING, null); } + + XMLSymbolSettings symbolSettings = this.textDocumentService.getSharedSymbolSettings(); + + if (this.getClientCapabilities().isDocumentSymbolDynamicRegistrationSupported()) { + toggleCapability(symbolSettings.isEnabled(), DOCUMENT_SYMBOL_ID, + TEXT_DOCUMENT_DOCUMENT_SYMBOL, null); + } } public Set getRegisteredCapabilities() { diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java index e5e5cd24d8..315639ad2e 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/settings/SettingsTest.java @@ -10,79 +10,86 @@ */ package org.eclipse.lsp4xml.settings; +import static java.io.File.separator; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + import org.eclipse.lsp4j.FormattingOptions; import org.eclipse.lsp4j.InitializeParams; import org.eclipse.lsp4xml.XMLLanguageServer; import org.eclipse.lsp4xml.extensions.contentmodel.settings.ContentModelSettings; import org.eclipse.lsp4xml.utils.FilesUtils; import org.junit.After; -import org.junit.Assert; import org.junit.Test; -import static org.junit.Assert.fail; - -import java.io.File; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import static java.io.File.separator; - /** * Tests for settings. */ public class SettingsTest { private static String testFolder = "TestXMLCacheFolder"; - private static String targetTestFolder = "target" + separator +"generated-test-sources"; - - + private static String targetTestFolder = "target" + separator + "generated-test-sources"; + @After public void cleanup() { String path = System.getProperty("user.dir") + separator + targetTestFolder + separator + testFolder; - + File f = new File(path); if (f.exists()) { f.delete(); } } - - private final String json = "{\r\n" + // - " \"settings\": {\r\n" + // - // Content model settings - " \"xml\": {\r\n" + - " \"fileAssociations\": [\r\n" + // - " {\r\n" + // - " \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + // - " \"pattern\": \"**/test*.xml\"\r\n" + // - " },\r\n" + // - " {\r\n" + // - " \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + // - " \"pattern\": \"projectDescription.xml\"\r\n" + // - " }\r\n" + // - " ],\r\n" + // - " \"catalogs\": [\r\n" + // - " \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + // - " ],\r\n" + // - " \"validation\": {\r\n" + // - " \"enabled\": true,\r\n" + // - " \"schema\": false\r\n" + // - " },\r\n" + // - // Client (commons) settings - " \"format\": {\r\n" + // - " \"tabSize\": 10,\r\n" + // - " \"insertSpaces\": false,\r\n" + // - " \"splitAttributes\": true,\r\n" + // - " \"joinCDATALines\": true,\r\n" + // - " \"formatComments\": true,\r\n" + // - " \"joinCommentLines\": true,\r\n" + // - " \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + // - " },\r\n" + - " \"server\": {\r\n" + // - " \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + // - " }\r\n" + - " }\r\n" + - " }\r\n" + - "}"; + // @formatter:off + private final String json = + "{\r\n" + + " \"settings\": {\r\n" + // + // Content model settings + " \"xml\": {\r\n" + + " \"fileAssociations\": [\r\n" + // + " {\r\n" + // + " \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\spring-beans-3.0.xsd\",\r\n" + // + " \"pattern\": \"**/test*.xml\"\r\n" + // + " },\r\n" + // + " {\r\n" + // + " \"systemId\": \"src\\\\test\\\\resources\\\\xsd\\\\projectDescription.xsd\",\r\n" + // + " \"pattern\": \"projectDescription.xml\"\r\n" + // + " }\r\n" + // + " ],\r\n" + // + " \"catalogs\": [\r\n" + // + " \"src\\\\test\\\\resources\\\\catalogs\\\\catalog.xml\"\r\n" + // + " ],\r\n" + // + " \"validation\": {\r\n" + // + " \"enabled\": true,\r\n" + // + " \"schema\": false\r\n" + // + " },\r\n" + // + // Client (commons) settings + " \"format\": {\r\n" + // + " \"tabSize\": 10,\r\n" + // + " \"insertSpaces\": false,\r\n" + // + " \"splitAttributes\": true,\r\n" + // + " \"joinCDATALines\": true,\r\n" + // + " \"formatComments\": true,\r\n" + // + " \"joinCommentLines\": true,\r\n" + // + " \"quotations\": " + XMLFormattingOptions.DOUBLE_QUOTES_VALUE + "\r\n" + // + " },\r\n" + + " \"server\": {\r\n" + // + " \"workDir\": \"~/" + testFolder + "/Nested\"\r\n" + // + " },\r\n" + + " \"symbols\": {\r\n" + // + " \"enabled\": true\r\n" + // + " }\r\n" + + " }\r\n" + + " }\r\n" + + "}"; + // @formatter:on @Test public void initializationOptionsSettings() { @@ -96,27 +103,30 @@ public void initializationOptionsSettings() { // Test client commons settings initializationOptionsSettings = AllXMLSettings.getAllXMLSettings(initializationOptionsSettings); XMLGeneralClientSettings settings = XMLGeneralClientSettings.getGeneralXMLSettings(initializationOptionsSettings); - Assert.assertNotNull(settings); + assertNotNull(settings); // Server - Assert.assertEquals("~/" + testFolder + "/Nested", settings.getServer().getWorkDir()); + assertEquals("~/" + testFolder + "/Nested", settings.getServer().getWorkDir()); // Test content model extension settings ContentModelSettings cmSettings = ContentModelSettings.getContentModelXMLSettings(initializationOptionsSettings); - Assert.assertNotNull(cmSettings); + assertNotNull(cmSettings); // Catalog - Assert.assertNotNull(cmSettings.getCatalogs()); - Assert.assertEquals(1, cmSettings.getCatalogs().length); - Assert.assertEquals("src\\test\\resources\\catalogs\\catalog.xml", cmSettings.getCatalogs()[0]); + assertNotNull(cmSettings.getCatalogs()); + assertEquals(1, cmSettings.getCatalogs().length); + assertEquals("src\\test\\resources\\catalogs\\catalog.xml", cmSettings.getCatalogs()[0]); // File associations - Assert.assertNotNull(cmSettings.getFileAssociations()); - Assert.assertEquals(2, cmSettings.getFileAssociations().length); - Assert.assertEquals("src\\test\\resources\\xsd\\spring-beans-3.0.xsd", - cmSettings.getFileAssociations()[0].getSystemId()); - Assert.assertEquals("**/test*.xml", cmSettings.getFileAssociations()[0].getPattern()); + assertNotNull(cmSettings.getFileAssociations()); + assertEquals(2, cmSettings.getFileAssociations().length); + assertEquals("src\\test\\resources\\xsd\\spring-beans-3.0.xsd", cmSettings.getFileAssociations()[0].getSystemId()); + assertEquals("**/test*.xml", cmSettings.getFileAssociations()[0].getPattern()); // Diagnostics - Assert.assertNotNull(cmSettings.getValidation()); - Assert.assertEquals(true, cmSettings.getValidation().isEnabled()); - Assert.assertEquals(false, cmSettings.getValidation().isSchema()); + assertNotNull(cmSettings.getValidation()); + assertEquals(true, cmSettings.getValidation().isEnabled()); + assertEquals(false, cmSettings.getValidation().isSchema()); + // Symbols + assertNotNull(settings.getSymbols()); + assertEquals(true, settings.getSymbols().isEnabled()); + } private static InitializeParams createInitializeParams(String json) { @@ -143,27 +153,27 @@ public void formatSettings() { xmlFormattingOptions.setQuotations("InvalidValue"); // set a value that is not recognized - Assert.assertEquals(5, xmlFormattingOptions.getTabSize()); // value coming from the request formattingOptions - Assert.assertFalse(xmlFormattingOptions.isInsertSpaces()); // formattingOptions doesn't defines insert spaces + assertEquals(5, xmlFormattingOptions.getTabSize()); // value coming from the request formattingOptions + assertFalse(xmlFormattingOptions.isInsertSpaces()); // formattingOptions doesn't defines insert spaces // flag - Assert.assertFalse(xmlFormattingOptions.isJoinCommentLines());// Since default for JoinCommentLines is False + assertFalse(xmlFormattingOptions.isJoinCommentLines());// Since default for JoinCommentLines is False // Assumes the "InvalidValue" will be overridden - Assert.assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.DOUBLE_QUOTES_VALUE)); + assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.DOUBLE_QUOTES_VALUE)); // merge with shared sharedXMLFormattingOptions (formatting settings created in // the InitializeParams xmlFormattingOptions.merge(sharedXMLFormattingOptions); - Assert.assertEquals(5, xmlFormattingOptions.getTabSize()); // tab size is kept as 5 (and not updated with + assertEquals(5, xmlFormattingOptions.getTabSize()); // tab size is kept as 5 (and not updated with // shared value 10), because only the request's // formattingOptions object is allowed to define it. - Assert.assertFalse(xmlFormattingOptions.isInsertSpaces()); // insert spaces is kept as false because only the - // request's + assertFalse(xmlFormattingOptions.isInsertSpaces()); // insert spaces is kept as false because only the + // request's // formattingOptions object is allowed to define it. - Assert.assertTrue(xmlFormattingOptions.isJoinCommentLines()); + assertTrue(xmlFormattingOptions.isJoinCommentLines()); - Assert.assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.SINGLE_QUOTES_VALUE)); + assertTrue(xmlFormattingOptions.getQuotations().equals(XMLFormattingOptions.SINGLE_QUOTES_VALUE)); } @Test @@ -171,7 +181,7 @@ public void cachePathSettings() { // Emulate InitializeParams#getInitializationOptions() object created as // JSONObject when XMLLanguageServer#initialize(InitializeParams params) is // called - + InitializeParams params = createInitializeParams(json); Object initializationOptionsSettings = InitializationOptionsSettings.getSettings(params); XMLLanguageServer languageServer = new XMLLanguageServer(); @@ -180,11 +190,12 @@ public void cachePathSettings() { String userDir = System.getProperty("user.dir"); try { System.setProperty("user.home", userDir + separator + targetTestFolder); // .../org.eclipse.lsp4xml/target/generated-test-sources/ - + languageServer.updateSettings(initializationOptionsSettings); - //Ensure the expanded absolute path is being used. - Assert.assertEquals(System.getProperty("user.home") + separator + testFolder + separator + "Nested", FilesUtils.getCachePathSetting()); + // Ensure the expanded absolute path is being used. + assertEquals(System.getProperty("user.home") + separator + testFolder + separator + "Nested", + FilesUtils.getCachePathSetting()); } catch (Exception e) { fail(); } finally {