Repository: atom/atom
File: packages/grammar-selector/lib/grammar-list-view.js (JavaScript)

Code before:
    }
    this.selectListView.focus();
    this.selectListView.reset();
  }

  async toggle() {
    if (this.panel != null) {
      this.cancel();
      return;
    }

    const editor = atom.workspace.getActiveTextEditor();
    if (editor) {
      this.editor = editor;
      this.currentGrammar = this.editor.getGrammar();
      if (this.currentGrammar === atom.grammars.nullGrammar) {
        this.currentGrammar = this.autoDetect;
      }

      let grammars = atom.grammars.getGrammars().filter(grammar => {
        return grammar !== atom.grammars.nullGrammar && grammar.name;
      });

      if (atom.config.get('grammar-selector.hideDuplicateTextMateGrammars')) {
        const oldGrammars = grammars;
        grammars = [];
        const blacklist = new Set();
        for (const grammar of oldGrammars) {
          if (isTreeSitter(grammar)) {
            blacklist.add(grammar.name);
            grammars.push(grammar);
          }
        }
        atom.grammars.getGrammars({ textMateOnly: true }).forEach(grammar => {
          if (
            grammar !== atom.grammars.nullGrammar &&
            grammar.name &&
            !blacklist.has(grammar.name)
          ) {
            grammars.push(grammar);
          }
        });
      }

      grammars.sort((a, b) => {
        if (a.scopeName === 'text.plain') {
          return -1;
        } else if (b.scopeName === 'text.plain') {
          return 1;
        } else if (a.name === b.name) {
          return compareGrammarType(a, b);

Review comment (suggestion):
In this case, do you really need the second call to `atom.grammars.getGrammars({ textMateOnly: true }`? It seems like you could simply iterate through the grammars array twice, once to populate the blacklist of Tree Sitter grammars and then a second time to filter. It's not a huge deal though.

Code after:
    }
    this.selectListView.focus();
    this.selectListView.reset();
  }

  async toggle() {
    if (this.panel != null) {
      this.cancel();
      return;
    }

    const editor = atom.workspace.getActiveTextEditor();
    if (editor) {
      this.editor = editor;
      this.currentGrammar = this.editor.getGrammar();
      if (this.currentGrammar === atom.grammars.nullGrammar) {
        this.currentGrammar = this.autoDetect;
      }

      let grammars = atom.grammars
        .getGrammars({ includeTreeSitter: true })
        .filter(grammar => {
          return grammar !== atom.grammars.nullGrammar && grammar.name;
        });

      if (atom.config.get('grammar-selector.hideDuplicateTextMateGrammars')) {
        const blacklist = new Set();
        grammars.forEach(grammar => {
          if (isTreeSitter(grammar)) {
            blacklist.add(grammar.name);
          }
        });
        grammars = grammars.filter(
          grammar => isTreeSitter(grammar) || !blacklist.has(grammar.name)
        );
      }

      grammars.sort((a, b) => {
        if (a.scopeName === 'text.plain') {
          return -1;
        } else if (b.scopeName === 'text.plain') {
          return 1;
        } else if (a.name === b.name) {
          return compareGrammarType(a, b);
        }
        return a.name.localeCompare(b.name);
      });
      grammars.unshift(this.autoDetect);
      await this.selectListView.update({ items: grammars });
      this.attach();
    }