Skip to content

Commit

Permalink
goto error position when formatter encouters error
Browse files Browse the repository at this point in the history
  • Loading branch information
jlchmura committed Feb 19, 2024
1 parent b385c69 commit 5901c3e
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from "vscode";
import path from "node:path";
import prettier, { Config, RequiredOptions, resolveConfig } from "prettier";
import * as vscode from "vscode";
import { Range, TextDocument, TextEdit, window } from "vscode";
import { ParserError } from "./parser/lpcParser";
import plugin, { LPCOptions } from "./plugin";
import prettier, { Config, Options, RequiredOptions, resolveConfig } from "prettier";
import { Position, Range, TextDocument, TextEdit, window } from "vscode";

const getConfigs = async (
extensionSettings: vscode.WorkspaceConfiguration,
Expand All @@ -15,9 +16,9 @@ const getConfigs = async (
useCache: false,
})) ?? {};

const pairVar = extensionSettings.get<string[]>("pairVariables");
const pairVar = extensionSettings.get<string[]>("pairVariables");
return {
...formattingOptions,
...formattingOptions,
...getIndentationConfig(extensionSettings, formattingOptions),
...prettierConfig,
pairVariables: pairVar,
Expand All @@ -32,10 +33,10 @@ const getIndentationConfig = (
formattingOptions: vscode.FormattingOptions
): Partial<RequiredOptions> => {
// override tab settings if ignoreTabSettings is true
if (extensionSettings.get<boolean>('ignoreTabSettings')) {
if (extensionSettings.get<boolean>("ignoreTabSettings")) {
return {
tabWidth: extensionSettings.get<number>('tabSizeOverride'),
useTabs: !extensionSettings.get<boolean>('insertSpacesOverride'),
tabWidth: extensionSettings.get<number>("tabSizeOverride"),
useTabs: !extensionSettings.get<boolean>("insertSpacesOverride"),
};
} else {
return {
Expand Down Expand Up @@ -109,7 +110,20 @@ const formatSelectionCommand = vscode.commands.registerCommand(
}
});
} catch (e) {
vscode.window.showErrorMessage("Unable to format LPC:\n" + e);
if (e instanceof ParserError) {
vscode.window.showErrorMessage("Unable to format LPC:\n" + e.message);

if (!!e.loc) {
let target = editor.document.positionAt(e.loc);
target = editor.document.validatePosition(target);

editor.revealRange(new vscode.Range(target, target));
editor.selection = new vscode.Selection(target, target);
}
console.error(e);
} else {
vscode.window.showErrorMessage("Unable to format LPC:\n" + e);
}
}
}
);
Expand Down

0 comments on commit 5901c3e

Please sign in to comment.