vscode-languageclient#DocumentFilter TypeScript Examples

The following examples show how to use vscode-languageclient#DocumentFilter. 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: 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 #2
Source File: vscode.ts    From ide-vscode with MIT License 5 votes vote down vote up
DafnyDocumentFilter: DocumentFilter & VsDocumentFilter = {
  scheme: 'file',
  language: LanguageConstants.Id
}
Example #3
Source File: plugin.test.ts    From vscode-microprofile with Apache License 2.0 5 votes vote down vote up
describe("Language server plugin", () => {
  it('Should collect lsp4mp extensions', () => {
    const quarkusPackageJSON = JSON.parse(
      fs.readFileSync(
        path.join(__dirname, "../../../../src/test/resources/quarkus-package.json"),
        "utf8"
      )
    );

    const fakeVscodeQuarkus: vscode.Extension<any> = {
      id: "fake-vscode-quarkus",
      extensionPath: "",
      extensionUri: vscode.Uri.parse("https://example.org"),
      isActive: true,
      packageJSON: quarkusPackageJSON,
      exports: "",
      activate: null,
      extensionKind: vscode.ExtensionKind.Workspace,
    };

    const noPluginPackageJSON = JSON.parse(
      fs.readFileSync(
        path.join(__dirname, "../../../../src/test/resources/no-lsp4mp-extension-package.json"),
        "utf8"
      )
    );

    const fakeNoPluginExtension: vscode.Extension<any> = {
      id: "fake-no-plugin-extension",
      extensionUri: vscode.Uri.parse("https://example.org"),
      extensionPath: "",
      isActive: true,
      packageJSON: noPluginPackageJSON,
      exports: "",
      activate: null,
      extensionKind: vscode.ExtensionKind.Workspace,
    };

    const extensions = [fakeVscodeQuarkus, fakeNoPluginExtension];
    const result: MicroProfileContribution[] = plugin.collectMicroProfileJavaExtensions(extensions);
    expect(result).to.have.length(1);

    expect(result[0].jarExtensions).to.have.length(1);

    const expectedPath: string = path.join('server', 'com.redhat.quarkus.ls.jar').replace("\\", "\\\\");
    expect(result[0].jarExtensions[0]).to.be.a("string").and.match(new RegExp(`${expectedPath}$`), `String should end with "${expectedPath}".`);

    expect(result[0].documentSelector).to.have.length(1);
    expect(result[0].documentSelector[0]).has.all.keys(["scheme", "language"]);

    const documentFilter: DocumentFilter = result[0].documentSelector[0] as DocumentFilter;
    expect(documentFilter.scheme).to.be.a("string").and.equal("file");
    expect(documentFilter.language).to.be.a("string").and.equal("quarkus-properties");
  });
});
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;
  }
});