From 5901c3e6eed55afcdb2585dcdac803621c1b9883 Mon Sep 17 00:00:00 2001 From: jlchmura Date: Mon, 19 Feb 2024 15:04:20 -0500 Subject: [PATCH] goto error position when formatter encouters error --- src/extension.ts | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index eefd74f..99495f9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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, @@ -15,9 +16,9 @@ const getConfigs = async ( useCache: false, })) ?? {}; - const pairVar = extensionSettings.get("pairVariables"); + const pairVar = extensionSettings.get("pairVariables"); return { - ...formattingOptions, + ...formattingOptions, ...getIndentationConfig(extensionSettings, formattingOptions), ...prettierConfig, pairVariables: pairVar, @@ -32,10 +33,10 @@ const getIndentationConfig = ( formattingOptions: vscode.FormattingOptions ): Partial => { // override tab settings if ignoreTabSettings is true - if (extensionSettings.get('ignoreTabSettings')) { + if (extensionSettings.get("ignoreTabSettings")) { return { - tabWidth: extensionSettings.get('tabSizeOverride'), - useTabs: !extensionSettings.get('insertSpacesOverride'), + tabWidth: extensionSettings.get("tabSizeOverride"), + useTabs: !extensionSettings.get("insertSpacesOverride"), }; } else { return { @@ -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); + } } } );