1313import java .net .URI ;
1414import java .net .URL ;
1515import java .util .ArrayList ;
16- import java .util .HashMap ;
1716import java .util .List ;
18- import java .util .Map ;
1917
2018import org .eclipse .core .runtime .FileLocator ;
21- import org .eclipse .core .runtime .IConfigurationElement ;
2219import org .eclipse .core .runtime .IStatus ;
23- import org .eclipse .core .runtime .Platform ;
2420import org .eclipse .core .runtime .Status ;
25- import org .eclipse .core .runtime .content .IContentType ;
26- import org .eclipse .core .runtime .content .IContentTypeManager ;
27- import org .eclipse .jface .preference .IPreferenceStore ;
28- import org .eclipse .jface .util .IPropertyChangeListener ;
2921import org .eclipse .jface .util .PropertyChangeEvent ;
30- import org .eclipse .lsp4e .LanguageServersRegistry ;
31- import org .eclipse .lsp4e .LanguageServersRegistry .LanguageServerDefinition ;
32- import org .eclipse .lsp4e .LanguageServiceAccessor ;
33- import org .eclipse .lsp4e .server .ProcessStreamConnectionProvider ;
3422import org .eclipse .lsp4j .DidChangeConfigurationParams ;
3523import org .eclipse .lsp4j .InitializeResult ;
3624import org .eclipse .lsp4j .jsonrpc .messages .Message ;
3725import org .eclipse .lsp4j .jsonrpc .messages .ResponseMessage ;
3826import org .eclipse .lsp4j .services .LanguageServer ;
3927import org .eclipse .wildwebdeveloper .Activator ;
40- import org .eclipse .wildwebdeveloper .SchemaAssociationRegistry ;
4128import org .eclipse .wildwebdeveloper .SchemaAssociationsPreferenceInitializer ;
4229import org .eclipse .wildwebdeveloper .embedder .node .NodeJSManager ;
43- import org .eclipse .wildwebdeveloper .json .JSonLanguageServer ;
30+ import org .eclipse .wildwebdeveloper .ui .preferences .ProcessStreamConnectionProviderWithPreference ;
31+ import org .eclipse .wildwebdeveloper .yaml .ui .preferences .YAMLPreferenceServerConstants ;
4432
45- import com .google .gson .Gson ;
46- import com .google .gson .reflect .TypeToken ;
47-
48- @ SuppressWarnings ("restriction" )
49- public class YAMLLanguageServer extends ProcessStreamConnectionProvider {
50- private static final String YAML_KEY = "yaml" ;
51- private static final String VALIDATE_KEY = "validate" ;
52- private static final String COMPLETION_KEY = "completion" ;
53- private static final String HOVER_KEY = "hover" ;
54- private static final String SCHEMAS_KEY = "schemas" ;
33+ /**
34+ * YAML language server.
35+ *
36+ */
37+ public class YAMLLanguageServer extends ProcessStreamConnectionProviderWithPreference {
5538
56- private static final IPreferenceStore PREFERENCE_STORE = Activator .getDefault ().getPreferenceStore ();
57- private static final LanguageServerDefinition YAML_LS_DEFINITION = LanguageServersRegistry .getInstance ()
58- .getDefinition ("org.eclipse.wildwebdeveloper.yaml" );
59- private static final IPropertyChangeListener PROPERTY_CHANGE_LISTENER = new IPropertyChangeListener () {
60- @ Override
61- public void propertyChange (PropertyChangeEvent event ) {
62- if (SchemaAssociationsPreferenceInitializer .SCHEMA_ASSOCIATIONS_PREFERENCE .equals (event .getProperty ())) {
63- Map <String , Object > settings = new HashMap <>();
64- settings .put (YAML_KEY , getYamlConfigurationOptions ());
39+ private static final String YAML_LANGUAGE_SERVER_ID = "org.eclipse.wildwebdeveloper.yaml" ;
6540
66- DidChangeConfigurationParams params = new DidChangeConfigurationParams (settings );
67- LanguageServiceAccessor .getActiveLanguageServers (null ).stream ()
68- .filter (server -> YAML_LS_DEFINITION
69- .equals (LanguageServiceAccessor .resolveServerDefinition (server ).get ()))
70- .forEach (ls -> ls .getWorkspaceService ().didChangeConfiguration (params ));
71- }
72- }
73- };
41+ private static final String [] SUPPORTED_SECTIONS = { "yaml" };
7442
7543 public YAMLLanguageServer () {
44+ super (YAML_LANGUAGE_SERVER_ID , Activator .getDefault ().getPreferenceStore (), SUPPORTED_SECTIONS );
7645 List <String > commands = new ArrayList <>();
7746 commands .add (NodeJSManager .getNodeJsLocation ().getAbsolutePath ());
7847 try {
@@ -92,85 +61,28 @@ public YAMLLanguageServer() {
9261 public void handleMessage (Message message , LanguageServer languageServer , URI rootUri ) {
9362 if (message instanceof ResponseMessage responseMessage ) {
9463 if (responseMessage .getResult () instanceof InitializeResult ) {
95- Map <String , Object > settings = new HashMap <>();
96- settings .put (YAML_KEY , getYamlConfigurationOptions ());
64+ Object settings = createSettings ();
9765
9866 DidChangeConfigurationParams params = new DidChangeConfigurationParams (settings );
9967 languageServer .getWorkspaceService ().didChangeConfiguration (params );
10068 }
10169 }
10270 }
10371
104- static Map <String , Object > getYamlConfigurationOptions () {
105- Map <String , Object > yaml = new HashMap <>();
106- yaml .put (SCHEMAS_KEY , getSchemaAssociations ());
107- yaml .put (VALIDATE_KEY , true );
108- yaml .put (COMPLETION_KEY , true );
109- yaml .put (HOVER_KEY , true );
110- yaml .put ("schemaStore.enable" , true );
111- return yaml ;
72+ @ Override
73+ protected boolean isAffected (PropertyChangeEvent event ) {
74+ return super .isAffected (event )
75+ || SchemaAssociationsPreferenceInitializer .SCHEMA_ASSOCIATIONS_PREFERENCE .equals (event .getProperty ());
11276 }
11377
114- private static Map <String , Object > getSchemaAssociations () {
115- IPreferenceStore preferenceStore = Activator .getDefault ().getPreferenceStore ();
116- String schemaString = preferenceStore
117- .getString (SchemaAssociationsPreferenceInitializer .SCHEMA_ASSOCIATIONS_PREFERENCE );
118-
119- Map <String , Object > contentTypeAssociations = new Gson ().fromJson (schemaString ,
120- new TypeToken <HashMap <String , String >>() {
121- }.getType ());
122-
123- IContentTypeManager contentTypeManager = Platform .getContentTypeManager ();
124- IContentType yamlBaseContentType = contentTypeManager .getContentType ("org.eclipse.wildwebdeveloper.yaml" );
125-
126- Map <String , Object > associations = new HashMap <>();
127-
128- contentTypeAssociations .forEach ((key , value ) -> {
129- IContentType contentType = contentTypeManager .getContentType (key );
130- if (contentType != null && contentType .getBaseType ().equals (yamlBaseContentType )) {
131- String [] fileNames = contentType .getFileSpecs (IContentType .FILE_NAME_SPEC );
132- for (String fileName : fileNames ) {
133- associations .put (value .toString (), fileName );
134- }
135-
136- String [] filePatterns = contentType .getFileSpecs (IContentType .FILE_PATTERN_SPEC );
137- for (String pattern : filePatterns ) {
138- associations .put (value .toString (), pattern );
139- }
140-
141- String [] fileExtensions = contentType .getFileSpecs (IContentType .FILE_EXTENSION_SPEC );
142- for (String extension : fileExtensions ) {
143- associations .put (value .toString (), "*." + extension );
144- }
145- }
146- });
147-
148- IConfigurationElement [] conf = Platform .getExtensionRegistry ().getConfigurationElementsFor (JSonLanguageServer .SCHEMA_EXT );
149- for (IConfigurationElement el : conf ) {
150- String url = el .getAttribute (JSonLanguageServer .URL_ATTR );
151- String pattern = el .getAttribute (JSonLanguageServer .PATTERN_ATTR );
152- if (!url .isBlank () && !pattern .isBlank ()) {
153- associations .put (SchemaAssociationRegistry .translate (url ), pattern );
154- }
155- }
156-
157- return associations ;
78+ @ Override
79+ protected Object createSettings () {
80+ return YAMLPreferenceServerConstants .getGlobalSettings ();
15881 }
15982
16083 @ Override
16184 public String toString () {
16285 return "YAML Language Server: " + super .toString ();
16386 }
16487
165- @ Override
166- public void start () throws IOException {
167- super .start ();
168- PREFERENCE_STORE .addPropertyChangeListener (PROPERTY_CHANGE_LISTENER );
169- }
170-
171- @ Override
172- public void stop () {
173- PREFERENCE_STORE .removePropertyChangeListener (PROPERTY_CHANGE_LISTENER );
174- super .stop ();
175- }
17688}
0 commit comments