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

Add a command to synchronize the formatter's config file with the workspace settings #60

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
51 changes: 51 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
StatusBarAlignment,
ThemeColor,
StatusBarItem,
Uri,
} from "vscode";

import {
Expand All @@ -17,6 +18,8 @@ import {
ExecuteCommandRequest,
} from "vscode-languageclient/node";

import { objectToSnake } from "ts-case-convert";

let client: LanguageClient;

//拡張機能を立ち上げたときに呼び出す関数
Expand Down Expand Up @@ -74,6 +77,54 @@ export function activate(context: ExtensionContext) {
}),
);

context.subscriptions.push(
commands.registerCommand(
"uroborosql-fmt.sync-config-file-with-extension-config",
async () => {
// uroborosql-fmt の設定を取得
const VSCodeConfig = workspace.getConfiguration("uroborosql-fmt");
lemonadern marked this conversation as resolved.
Show resolved Hide resolved

// Default value of `uroborosql-fmt.configurationFilePath` is "".
const VSCodeConfigPath: string = VSCodeConfig.get(
"configurationFilePath",
);
const configFilePath =
VSCodeConfigPath !== "" ? VSCodeConfigPath : ".uroborosqlfmtrc.json";
tanzaku marked this conversation as resolved.
Show resolved Hide resolved

// VSCodeで開いているディレクトリを取得
// 開いていない場合はエラーを出して終了
const folders = workspace.workspaceFolders;
if (folders === undefined) {
window.showErrorMessage(
"Error: Open the folder before executing this command.",
);
return;
}
const folderPath = folders[0].uri;
const configFileFullPath = Uri.joinPath(folderPath, configFilePath);

// 設定値 `configurationFilePath` の除外・ value が null のエントリを除外・ WorkSpaceConfiguration が持つメソッドと内部オブジェクトを除外
const formattingConfig = Object.fromEntries(
Object.entries(VSCodeConfig).filter(
([key, value]) =>
key !== "configurationFilePath" &&
value !== null &&
typeof value !== "function" &&
typeof value !== "object",
),
);

const content = JSON.stringify(
objectToSnake(formattingConfig),
null,
2,
);
const blob: Uint8Array = Buffer.from(content);
workspace.fs.writeFile(configFileFullPath, blob);
lemonadern marked this conversation as resolved.
Show resolved Hide resolved
},
),
);

// ステータスバーの作成と表示
const statusBar = createStatusBar();
statusBar.show();
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
{
"command": "uroborosql-fmt.uroborosql-format",
"title": "format sql"
},
{
"command": "uroborosql-fmt.sync-config-file-with-extension-config",
"title": "Synchronize config file with Extension config"
lemonadern marked this conversation as resolved.
Show resolved Hide resolved
}
],
"configuration": {
Expand Down