Skip to content

Commit

Permalink
Patch findLanguageIdFromScopeName grammar priority
Browse files Browse the repository at this point in the history
Use "last match wins" in grammar scope search to match core.
  • Loading branch information
zm-cttae committed Nov 13, 2024
1 parent 499fa00 commit 640675c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/services/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as vscode from 'vscode';
import * as vscodeTextmate from 'vscode-textmate';
import { readFileText, loadMessageBundle } from '../util/loader';
import { ContributorData, isGrammarLanguageDefinition, plaintextGrammarDefinition } from '../util/contributes';
import type { GrammarLanguageDefinition, LanguageDefinition } from '../util/contributes';
import type { GrammarDefinition, GrammarLanguageDefinition, LanguageDefinition } from '../util/contributes';

const localize = loadMessageBundle();

Expand Down Expand Up @@ -84,6 +84,10 @@ export class ResolverService implements vscodeTextmate.RegistryOptions {
return this._contributes.getLanguageDefinitionFromFilename(fileName);
}

public getGrammarDefinitionFromScopeName(scopeName: string): GrammarDefinition {
return this._contributes.getGrammarDefinitionFromScopeName(scopeName);
}

public getGrammarDefinitionFromLanguageId(languageId: string): GrammarLanguageDefinition {
return this._contributes.getGrammarDefinitionFromLanguageId(languageId);
}
Expand Down
13 changes: 11 additions & 2 deletions src/util/contributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export class ContributorData {
}

public findLanguageIdFromScopeName(scopeName: string): string {
const grammarData = this.grammars.find(g => g.scopeName === scopeName);
const grammarData = this.getGrammarDefinitionFromScopeName(scopeName);
if (grammarData && isGrammarLanguageDefinition(grammarData)) {
return grammarData.language;
}
Expand Down Expand Up @@ -311,13 +311,22 @@ export class ContributorData {
if (!languageId) {
return plaintextLanguageDefinition;
}
const languageData = this.sources.languages[languageId];
const languageData = this.getExtensionFromLanguageId(languageId);
if (!languageData) {
return plaintextLanguageDefinition;
}
return languageData;
}

public getGrammarDefinitionFromScopeName(scopeName: string): GrammarDefinition {
for (const grammar of this.grammars.reverse()) {
if (grammar.scopeName === scopeName) {
return grammar;
}
}
return plaintextGrammarDefinition;
}

public getGrammarDefinitionFromLanguageId(languageId: string): GrammarLanguageDefinition {
for (const grammar of this.grammars.reverse()) {
if (isGrammarLanguageDefinition(grammar) && grammar.language === languageId) {
Expand Down

0 comments on commit 640675c

Please sign in to comment.