Skip to content

Commit

Permalink
Fix format even if workspace folder cannot be obtained (#36)
Browse files Browse the repository at this point in the history
* fix: Format if workspaceFolder cannot be obtained

* chore: Add changeset

* fix comment

* fix version check

---------

Co-authored-by: yamada2915 <[email protected]>
  • Loading branch information
tanzaku and future-yamada2915 authored Oct 31, 2023
1 parent 4e8ba9e commit 804f9d7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-doors-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"uroborosql-fmt": patch
---

Allow formatting even if the workspace folder cannot be obtained
94 changes: 55 additions & 39 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,55 +133,71 @@ async function getWorkspaceFolder(
return undefined;
}

async function formatText(
async function determineConfigPath(
uri: string,
textDocument: TextDocument,
version: number,
selections: Range[],
): Promise<TextEdit[]> {
const settings: ConfigurationSettings = await getSettings(uri);

): Promise<string | null> {
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) {
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<TextEdit[]> {
let configPath: string | null;

try {
configPath = await determineConfigPath(uri, textDocument);
} catch (e) {
if (e instanceof Error) {
connection.window.showErrorMessage(e.message);
}

return [];
}

// version check
if (version !== textDocument.version) {
return [];
}

const changes: TextEdit[] = [];
Expand All @@ -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(
Expand All @@ -233,7 +249,7 @@ async function formatText(
Position.create(0, 0),
textDocument.positionAt(text.length),
),
formatted_text,
formattedText,
),
);
}
Expand Down

0 comments on commit 804f9d7

Please sign in to comment.