vscode-languageclient#DocumentSelector TypeScript Examples

The following examples show how to use vscode-languageclient#DocumentSelector. 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 plugin-vscode with Apache License 2.0 6 votes vote down vote up
// TODO initializations should be contributions from each component
function onBeforeInit(langClient: ExtendedLangClient) {
    class TraceLogsFeature implements StaticFeature {
        fillClientCapabilities(capabilities: ClientCapabilities): void {
            capabilities.experimental = capabilities.experimental || {};
            capabilities.experimental.introspection = true;
        }
        initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector | undefined): void {
        }
    }

    class ShowFileFeature implements StaticFeature {
        fillClientCapabilities(capabilities: ClientCapabilities): void {
            capabilities.experimental = capabilities.experimental || {};
            capabilities.experimental.showTextDocument = true;
        }
        initialize(capabilities: ServerCapabilities, documentSelector: DocumentSelector | undefined): void {
        }
    }

    langClient.registerFeature(new TraceLogsFeature());
    langClient.registerFeature(new ShowFileFeature());
}
Example #2
Source File: plugin.ts    From vscode-microprofile with Apache License 2.0 6 votes vote down vote up
function setDocumentSelectorIfExists(obj: MicroProfileContribution, section: any): void {
  if (!Array.isArray(section.documentSelector)) {
    return;
  }
  const documentSelector: DocumentSelector = [];
  section.documentSelector.forEach((selector: any) => {
    if (typeof selector === 'string') {
      documentSelector.push(selector);
    } else if (selector) {
      const documentFilter: {[key: string]: string} = {};
      if (typeof selector.language === 'string') {
        documentFilter.language = selector.language;
      }
      if (typeof selector.scheme === 'string') {
        documentFilter.scheme = selector.scheme;
      }
      if (typeof selector.pattern === 'string') {
        documentFilter.pattern = selector.pattern;
      }
      if (documentFilter.language || documentFilter.scheme || documentFilter.pattern) {
        documentSelector.push(documentFilter as DocumentFilter);
      }
    }
  });
  obj.documentSelector = obj.documentSelector.concat(documentSelector);
}
Example #3
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;
  }
}
Example #4
Source File: documentSelectorPlugin.test.ts    From vscode-microprofile with Apache License 2.0 4 votes vote down vote up
/**
 * This file ensures that DocumentSelectors contributed by other VS Code extensions
 * (using `contributes.microprofile.documentSelector` key within package.json) are being
 * collected correctly.
 */
describe("Document selector collection from language server plugins", () => {

  it ('Should collect document selector when all keys exist', () => {
    const selector: DocumentSelector = collectDocumentSelectors([{ scheme: "file", language: "quarkus-properties", pattern: "**/*.properties" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["scheme", "language", "pattern"]);
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");
    expect((selector[0] as DocumentFilter).language).to.equal("quarkus-properties");
    expect((selector[0] as DocumentFilter).pattern).to.equal("**/*.properties");
  });

  it ('Should collect all document selector when two keys exist', () => {
    let selector: DocumentSelector = collectDocumentSelectors([{ scheme: "file", language: "quarkus-properties" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["scheme", "language"]);
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");
    expect((selector[0] as DocumentFilter).language).to.equal("quarkus-properties");

    selector = collectDocumentSelectors([{ language: "quarkus-properties", pattern: "**/*.properties" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["language", "pattern"]);
    expect((selector[0] as DocumentFilter).language).to.equal("quarkus-properties");
    expect((selector[0] as DocumentFilter).pattern).to.equal("**/*.properties");

    selector = collectDocumentSelectors([{ pattern: "**/*.properties", scheme: "file" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["pattern", "scheme"]);
    expect((selector[0] as DocumentFilter).pattern).to.equal("**/*.properties");
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");
  });

  it ('Should collect document selector when one key exist', () => {
    let selector: DocumentSelector = collectDocumentSelectors([{ scheme: "file" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["scheme"]);
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");

    selector = collectDocumentSelectors([{ language: "quarkus-properties" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["language"]);
    expect((selector[0] as DocumentFilter).language).to.equal("quarkus-properties");

    selector = collectDocumentSelectors([{ pattern: "**/*.properties" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["pattern"]);
    expect((selector[0] as DocumentFilter).pattern).to.equal("**/*.properties");
  });

  it ('Should collect document selector when a valid key and an invalid key exists', () => {
    // valid, but the "invalid" key is ignored
    let selector: DocumentSelector = collectDocumentSelectors([{ scheme: "file", invalid: "file" }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["scheme"]);
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");

    // valid, but the "language" key is ignored since the value has the wrong type
    selector = collectDocumentSelectors([{ scheme: "file", language: 12 }]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.have.all.keys(["scheme"]);
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");
  });

  it ('Should collect document selector strings', () => {
    const selector: DocumentSelector = collectDocumentSelectors(["document-selector-string"]);
    expect(selector).to.have.length(1);
    expect(selector[0]).to.be.a('string');
    expect(selector[0]).to.equal("document-selector-string");
  });

  it ('Should not collect document selector when there are no correct keys', () => {
    let selector: DocumentSelector = collectDocumentSelectors([{ invalid: "file" }]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([{}]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([]);
    expect(selector).to.have.length(0);
  });

  it ('Should not collect document selector when containing invalid types', () => {
    let selector: DocumentSelector = collectDocumentSelectors([12]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([/test/]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([true]);
    expect(selector).to.have.length(0);
  });

  it ('Should not collect document selector when all keys have incorrect types', () => {

    let selector: DocumentSelector = collectDocumentSelectors([{ scheme: 12 }]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([{ scheme: { key: "value" } }]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([{ language: 12 }]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([{ language: { key: "value" } }]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([{ pattern: 12 }]);
    expect(selector).to.have.length(0);

    selector = collectDocumentSelectors([{ pattern: { key: "value" } }]);
    expect(selector).to.have.length(0);
  });

  it ('Should collect multiple document selectors', () => {
    const selector: DocumentSelector = collectDocumentSelectors([
      { scheme: "file", language: "quarkus-properties", pattern: "**/*.properties" }, // valid
      { language: "my-properties" }, // valid
      "document-selector-string", // valid
      { key: "value" }, // invalid
      12 // invalid
    ]);

    expect(selector).to.have.length(3);

    expect(selector[0]).to.have.all.keys(["scheme", "language", "pattern"]);
    expect((selector[0] as DocumentFilter).scheme).to.equal("file");
    expect((selector[0] as DocumentFilter).language).to.equal("quarkus-properties");
    expect((selector[0] as DocumentFilter).pattern).to.equal("**/*.properties");

    expect(selector[1]).to.have.all.keys(["language"]);
    expect((selector[1] as DocumentFilter).language).to.equal("my-properties");

    expect(selector[2]).to.be.a('string');
    expect(selector[2]).to.equal("document-selector-string");
  });

  /**
   * Returns the DocumentSelector created from the provided `pluginDocumentSelector`.
   *
   * `pluginDocumentSelector` represents the DocumentSelector that other VS Code
   * extensions would contribute to vscode-microprofile.
   *
   * @param pluginDocumentSelector array of objects to create a DocumentSelector from.
   */
  function collectDocumentSelectors(pluginDocumentSelector: any[]): DocumentSelector {
    const fakePlugin: vscode.Extension<any> = {
      id: "fake-no-plugin-extension",
      extensionUri: vscode.Uri.parse("https://example.org"),
      extensionPath: "",
      isActive: true,
      packageJSON: {
        contributes: {
          microprofile: {
            documentSelector: pluginDocumentSelector
          }
        }
      },
      exports: "",
      activate: null,
      extensionKind: vscode.ExtensionKind.Workspace,
    };

    const contribution: MicroProfileContribution[] = plugin.collectMicroProfileJavaExtensions([ fakePlugin ]);
    expect(contribution).to.have.length(1);

    const selector: DocumentSelector = contribution[0].documentSelector;
    return contribution[0].documentSelector;
  }
});