Skip to content

Commit b7c6947

Browse files
committed
resolve untitled json as json parser. Fixes #1435
1 parent bae8740 commit b7c6947

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/LanguageResolver.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import { ModuleResolver } from "./ModuleResolver";
55

66
export class LanguageResolver {
77
constructor(private moduleResolver: ModuleResolver) {}
8-
public getParsersFromLanguageId(
8+
public getParserFromLanguageId(
99
uri: Uri,
1010
languageId: string
11-
): prettier.BuiltInParserName[] | string[] {
12-
if (uri.scheme === "untitled" && languageId === "html") {
13-
// This is a workaround for the HTML language when it is unsaved. By default,
14-
// the Angular parser matches first because both register the language 'html'
15-
return ["html"];
11+
): prettier.BuiltInParserName | string | undefined {
12+
// This is a workaround for when the vscodeLanguageId is duplicated in multiple
13+
// prettier languages. In these cases the first match is not the preferred match
14+
// so we override with the parser that exactly matches the languageId.
15+
// Specific undesired cases here are:
16+
// `html` matching to `angular`
17+
// `json` matching to `json-stringify`
18+
const languageParsers = ["html", "json"];
19+
if (uri.scheme === "untitled" && languageParsers.includes(languageId)) {
20+
return languageId;
1621
}
1722
const language = this.getSupportLanguages(uri.fsPath).find(
1823
(lang) =>
@@ -21,10 +26,9 @@ export class LanguageResolver {
2126
Array.isArray(lang.vscodeLanguageIds) &&
2227
lang.vscodeLanguageIds.includes(languageId)
2328
);
24-
if (!language) {
25-
return [];
29+
if (language && language.parsers?.length > 0) {
30+
return language.parsers[0];
2631
}
27-
return language.parsers;
2832
}
2933

3034
public allEnabledLanguages(fsPath?: string): string[] {

src/PrettierEditService.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,7 @@ export default class PrettierEditService implements Disposable {
279279
this.loggingService.logWarning(
280280
"Parser not inferred, using VS Code language."
281281
);
282-
const dynamicParsers = this.languageResolver.getParsersFromLanguageId(
283-
uri,
284-
languageId
285-
);
286-
if (dynamicParsers.length > 0) {
287-
parser = dynamicParsers[0];
288-
this.loggingService.logInfo(`Resolved parser to '${parser}'`);
289-
}
282+
parser = this.languageResolver.getParserFromLanguageId(uri, languageId);
290283
}
291284

292285
if (!parser) {

0 commit comments

Comments
 (0)