forked from prettier/prettier-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguageFilters.ts
29 lines (28 loc) · 1.03 KB
/
languageFilters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Uri } from "vscode";
import { PrettierBuiltInParserName, PrettierSupportLanguage } from "./types";
export function getParserFromLanguageId(
languages: PrettierSupportLanguage[],
uri: Uri,
languageId: string
): PrettierBuiltInParserName | string | undefined {
// This is a workaround for when the vscodeLanguageId is duplicated in multiple
// prettier languages. In these cases the first match is not the preferred match
// so we override with the parser that exactly matches the languageId.
// Specific undesired cases here are:
// `html` matching to `angular`
// `json` matching to `json-stringify`
const languageParsers = ["html", "json"];
if (uri.scheme !== "file" && languageParsers.includes(languageId)) {
return languageId;
}
const language = languages.find(
(lang) =>
lang &&
lang.extensions &&
Array.isArray(lang.vscodeLanguageIds) &&
lang.vscodeLanguageIds.includes(languageId)
);
if (language && language.parsers?.length > 0) {
return language.parsers[0];
}
}