Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format even if workspace folder cannot be obtained #36

Merged
merged 4 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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