From 03b8a0d41c3844bf358aefd0216b76d3668b4ab6 Mon Sep 17 00:00:00 2001 From: yamada2915 Date: Mon, 30 Oct 2023 15:29:21 +0900 Subject: [PATCH 1/4] fix: Format if workspaceFolder cannot be obtained --- server/src/server.ts | 96 ++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index f7cfd1d..a92d3a9 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -133,55 +133,71 @@ async function getWorkspaceFolder( return undefined; } -async function formatText( +async function determineConfigPath( uri: string, textDocument: TextDocument, - version: number, - selections: Range[], -): Promise { - const settings: ConfigurationSettings = await getSettings(uri); - +): Promise { const workspaceFolder: string | undefined = await getWorkspaceFolder(textDocument); - if (!workspaceFolder) { - connection.window.showErrorMessage("The workspace folder is undefined"); - return []; - } - // version check - if (version !== textDocument.version) { - return []; + if (!workspaceFolder) { + return null; } - // remove scheme const workspaceFolderPath = URI.parse(workspaceFolder).fsPath; - const defaultConfigPath = path.join( - workspaceFolderPath, - ".uroborosqlfmtrc.json", - ); - let configPath: string | null = null; + const settings: ConfigurationSettings = await getSettings(uri); if (!settings.configurationFilePath) { + // remove scheme + const defaultConfigPath = path.join( + workspaceFolderPath, + ".uroborosqlfmtrc.json", + ); + // The path of configuration file is not specified. // If defaultConfigPath doesn't exist, fomatters default config will be used. - if (fs.existsSync(defaultConfigPath)) { - configPath = defaultConfigPath; - } - // else { configPath = null; } - } else { - let specifiedConfigPath = settings.configurationFilePath; - if (!path.isAbsolute(specifiedConfigPath)) { - specifiedConfigPath = path.join(workspaceFolderPath, specifiedConfigPath); + if (!fs.existsSync(defaultConfigPath)) { + return null; } - if (fs.existsSync(specifiedConfigPath)) { - configPath = specifiedConfigPath; - } else { - connection.window.showErrorMessage( - `${specifiedConfigPath} doesn't exist.`, - ); - return []; + return defaultConfigPath; + } + + let specifiedConfigPath = settings.configurationFilePath; + if (!path.isAbsolute(specifiedConfigPath)) { + specifiedConfigPath = path.join(workspaceFolderPath, specifiedConfigPath); + } + + if (!fs.existsSync(specifiedConfigPath)) { + // If the path is explicitly specified but the file does not exist, + // it is not formatted and an error is generated. + throw new Error(`${specifiedConfigPath} doesn't exist.`); + } + + return specifiedConfigPath; +} + +async function formatText( + uri: string, + textDocument: TextDocument, + version: number, + selections: Range[], +): Promise { + // version check + if (version !== textDocument.version) { + return []; + } + + let configPath: string | null; + + try { + configPath = await determineConfigPath(uri, textDocument); + } catch (e) { + if (e instanceof Error) { + connection.window.showErrorMessage(e.message); } + + return []; } const changes: TextEdit[] = []; @@ -194,27 +210,27 @@ async function formatText( continue; } - let formatted_text: string; + let formattedText: string; try { - formatted_text = runfmt(text, configPath); + formattedText = runfmt(text, configPath); } catch (e) { console.error(e); return []; } // フォーマット - changes.push(TextEdit.replace(selection, formatted_text)); + changes.push(TextEdit.replace(selection, formattedText)); } if (!changes.length) { // テキスト全体を取得 const text = textDocument.getText(); - let formatted_text: string; + let formattedText: string; const startTime = performance.now(); try { - formatted_text = runfmt(text, configPath); + formattedText = runfmt(text, configPath); } catch (e) { console.error(e); connection.window.showErrorMessage( @@ -233,7 +249,7 @@ async function formatText( Position.create(0, 0), textDocument.positionAt(text.length), ), - formatted_text, + formattedText, ), ); } From 5d586ff8b8dfdf83bc2ef0ca6595fe9c463b02e5 Mon Sep 17 00:00:00 2001 From: yamada2915 Date: Mon, 30 Oct 2023 15:30:51 +0900 Subject: [PATCH 2/4] chore: Add changeset --- .changeset/popular-doors-film.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/popular-doors-film.md diff --git a/.changeset/popular-doors-film.md b/.changeset/popular-doors-film.md new file mode 100644 index 0000000..e80a47d --- /dev/null +++ b/.changeset/popular-doors-film.md @@ -0,0 +1,5 @@ +--- +"uroborosql-fmt": patch +--- + +Allow formatting even if the workspace folder cannot be obtained From 875d6119b4dfd32b7cee1b88f9b55747e8ecf523 Mon Sep 17 00:00:00 2001 From: yamada2915 Date: Mon, 30 Oct 2023 16:11:18 +0900 Subject: [PATCH 3/4] fix comment --- server/src/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/server.ts b/server/src/server.ts index a92d3a9..fb66206 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -144,11 +144,11 @@ async function determineConfigPath( return null; } + // remove scheme const workspaceFolderPath = URI.parse(workspaceFolder).fsPath; const settings: ConfigurationSettings = await getSettings(uri); if (!settings.configurationFilePath) { - // remove scheme const defaultConfigPath = path.join( workspaceFolderPath, ".uroborosqlfmtrc.json", From 76a82a28c9dc77ad6de67f4da262971c13c49271 Mon Sep 17 00:00:00 2001 From: yamada2915 Date: Tue, 31 Oct 2023 09:40:43 +0900 Subject: [PATCH 4/4] fix version check --- server/src/server.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index fb66206..53d871d 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -183,11 +183,6 @@ async function formatText( version: number, selections: Range[], ): Promise { - // version check - if (version !== textDocument.version) { - return []; - } - let configPath: string | null; try { @@ -200,6 +195,11 @@ async function formatText( return []; } + // version check + if (version !== textDocument.version) { + return []; + } + const changes: TextEdit[] = []; // 全ての選択範囲に対して実行