Skip to content

Commit

Permalink
resolve untitled json as json parser. Fixes #1435
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Jun 19, 2020
1 parent bae8740 commit b7c6947
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
22 changes: 13 additions & 9 deletions src/LanguageResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { ModuleResolver } from "./ModuleResolver";

export class LanguageResolver {
constructor(private moduleResolver: ModuleResolver) {}
public getParsersFromLanguageId(
public getParserFromLanguageId(
uri: Uri,
languageId: string
): prettier.BuiltInParserName[] | string[] {
if (uri.scheme === "untitled" && languageId === "html") {
// This is a workaround for the HTML language when it is unsaved. By default,
// the Angular parser matches first because both register the language 'html'
return ["html"];
): prettier.BuiltInParserName | 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 === "untitled" && languageParsers.includes(languageId)) {
return languageId;
}
const language = this.getSupportLanguages(uri.fsPath).find(
(lang) =>
Expand All @@ -21,10 +26,9 @@ export class LanguageResolver {
Array.isArray(lang.vscodeLanguageIds) &&
lang.vscodeLanguageIds.includes(languageId)
);
if (!language) {
return [];
if (language && language.parsers?.length > 0) {
return language.parsers[0];
}
return language.parsers;
}

public allEnabledLanguages(fsPath?: string): string[] {
Expand Down
9 changes: 1 addition & 8 deletions src/PrettierEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,7 @@ export default class PrettierEditService implements Disposable {
this.loggingService.logWarning(
"Parser not inferred, using VS Code language."
);
const dynamicParsers = this.languageResolver.getParsersFromLanguageId(
uri,
languageId
);
if (dynamicParsers.length > 0) {
parser = dynamicParsers[0];
this.loggingService.logInfo(`Resolved parser to '${parser}'`);
}
parser = this.languageResolver.getParserFromLanguageId(uri, languageId);
}

if (!parser) {
Expand Down

0 comments on commit b7c6947

Please sign in to comment.