vscode-languageclient#DidChangeConfigurationNotification TypeScript Examples

The following examples show how to use vscode-languageclient#DidChangeConfigurationNotification. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: extension.ts    From vscode-microprofile with Apache License 2.0 4 votes vote down vote up
function connectToLS(context: ExtensionContext, api: JavaExtensionAPI) {
  const microprofileContributions: MicroProfileContribution[] = collectMicroProfileJavaExtensions(extensions.all);
  return requirements.resolveRequirements(api).then(requirements => {
    const clientOptions: LanguageClientOptions = {
      documentSelector: getDocumentSelector(microprofileContributions),
      // wrap with key 'settings' so it can be handled same a DidChangeConfiguration
      initializationOptions: {
        settings: getVSCodeMicroProfileSettings(),
        extendedClientCapabilities: {
          commands: {
            commandsKind: {
              valueSet: [
                CommandKind.COMMAND_CONFIGURATION_UPDATE,
                CommandKind.COMMAND_OPEN_URI
              ]
            }
          },
          completion: {
            skipSendingJavaCompletionThroughLanguageServer: true
          },
          shouldLanguageServerExitOnShutdown: true
        }
      },
      synchronize: {
        // preferences starting with these will trigger didChangeConfiguration
        configurationSection: ['microprofile', '[microprofile]']
      },
      middleware: {
        workspace: {
          didChangeConfiguration: () => {
            languageClient.sendNotification(DidChangeConfigurationNotification.type, { settings: getVSCodeMicroProfileSettings() });
          }
        }
      }
    };

    const serverOptions = prepareExecutable(requirements, getMicroProfileJarExtensions(microprofileContributions));

    languageClient = new LanguageClient('microprofile.tools', 'Tools for MicroProfile', serverOptions, clientOptions);
    context.subscriptions.push(languageClient.start());

    if (extensions.onDidChange) {// Theia doesn't support this API yet
      context.subscriptions.push(extensions.onDidChange(() => {
        // if extensions that contribute mp java extensions change we need to reload the window
        handleExtensionChange(extensions.all);
      }));
    }

    return languageClient.onReady();
  });

  /**
   * Returns the document selector.
   *
   * The returned document selector contains the microprofile-properties and java document selectors
   * and all document selectors contained in `microProfileContributions`.
   *
   * @param microProfileContributions MicroProfile language server contributions from other VS Code extensions
   */
  function getDocumentSelector(microProfileContributions: MicroProfileContribution[]): DocumentSelector {
    let documentSelector: DocumentSelector = [
      { scheme: 'file', language: 'microprofile-properties' },
      { scheme: 'file', language: 'java' }
    ];
    microProfileContributions.forEach((contribution: MicroProfileContribution) => {
      documentSelector = documentSelector.concat(contribution.documentSelector);
    });
    return documentSelector;
  }

  /**
   * Returns a json object with key 'microprofile' and a json object value that
   * holds all microprofile settings.
   */
  function getVSCodeMicroProfileSettings(): { microprofile: any } {
    const defaultMicroProfileSettings = {};
    const configMicroProfile = workspace.getConfiguration().get('microprofile');
    const microprofileSettings = configMicroProfile ? configMicroProfile : defaultMicroProfileSettings;

    return {
      microprofile: microprofileSettings,
    };
  }

  /**
   * Returns an array of paths to MicroProfileLS extension jars within `microProfileContributions`
   *
   * @param microProfileContributions MicroProfile language server contributions from other VS Code extensions
   */
  function getMicroProfileJarExtensions(microProfileContributions: MicroProfileContribution[]): string[] {
    let jarPaths: string[] = [];
    microProfileContributions.forEach((contribution: MicroProfileContribution) => {
      if (contribution.jarExtensions && contribution.jarExtensions.length > 0) {
        jarPaths = jarPaths.concat(contribution.jarExtensions);
      }
    });
    return jarPaths;
  }
}