Skip to content

Commit

Permalink
feat: add support defaultFormatter (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Sep 26, 2023
1 parent 93e5ada commit 7e9c6f2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ out
node_modules
client/server
.vscode-test
uroborosql-fmt-1.0.0.vsix
uroborosql-fmt-*.vsix
/server/uroborosql-fmt-napi-0.0.0.tgz
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,36 @@

A Visual Studio Code extension for [uroboroSQL-fmt](https://github.com/future-architect/uroborosql-fmt) that is a tool that formats SQL statements according to [SQL coding standards created by Future Corporation](https://future-architect.github.io/coding-standards/documents/forSQL/SQL%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0%E8%A6%8F%E7%B4%84%EF%BC%88PostgreSQL%EF%BC%89.html) (Japanese only).

## Usage

Once installed in Visual Studio Code, "uroborosql-fmt" will be available as a formatter for SQL files. Please select "uroborosql-fmt" (extension id:`Future.uroborosql-fmt`) as the default formatter. You can do this either by using the context menu (right click on a open SQL file in the editor) and select "Format Document With...", or you can add the following to your settings:

```json
{
"[sql]": {
"editor.defaultFormatter": "Future.uroborosql-fmt"
}
}
```

### Format on save

You can enable format on save for SQL by having the following values in your settings:

```json
{
"[sql]": {
"editor.defaultFormatter": "Future.uroborosql-fmt",
"editor.formatOnSave": true
}
}
```

## Settings

| Settings | Defaults | Description |
| Settings | Defaults | Description |
| -------- | -------- | ----------- |
| uroborosql-fmt.configurationFilePath | null | The path of configuration file. File extension must be `.json`. If you don't specify the path and `./.uroborosqlfmtrc.json` exists, formatter will use `./.uroborosqlfmtrc.json`. If you doesn't specify and `.uroborosqlfmtrc.json` doesn't exist, formatter will use formatters default configurations. |
| uroborosql-fmt.configurationFilePath | null | The path of configuration file. File extension must be `.json`. If you don't specify the path and `./.uroborosqlfmtrc.json` exists, formatter will use `./.uroborosqlfmtrc.json`. If you doesn't specify and `.uroborosqlfmtrc.json` doesn't exist, formatter will use formatters default configurations. |

## License

Expand Down
57 changes: 39 additions & 18 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
TextDocumentEdit,
Position,
Range,
DocumentFormattingRequest,
DocumentFilter,
DocumentFormattingRegistrationOptions,
} from "vscode-languageserver/node";

import { TextDocument } from "vscode-languageserver-textdocument";
Expand Down Expand Up @@ -80,6 +83,12 @@ connection.onInitialized(() => {
connection.console.log("Workspace folder change event received.");
});
}

const filter: DocumentFilter = { language: "sql" };
const options: DocumentFormattingRegistrationOptions = {
documentSelector: [filter],
};
connection.client.register(DocumentFormattingRequest.type, options);
});

type ConfigurationSettings = {
Expand Down Expand Up @@ -122,18 +131,18 @@ async function formatText(
textDocument: TextDocument,
version: number,
selections: Range[]
): Promise<void> {
): Promise<TextEdit[]> {
const settings: ConfigurationSettings = await getSettings(uri);

const workspaceFolder: string | undefined = await getWorkspaceFolder(textDocument);
if (!workspaceFolder) {
connection.window.showErrorMessage("The workspace folder is undefined");
return;
return [];
}

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

// remove scheme
Expand All @@ -160,7 +169,7 @@ async function formatText(
connection.window.showErrorMessage(
`${specifiedConfigPath} doesn't exist.`
);
return;
return [];
}
}

Expand All @@ -180,7 +189,7 @@ async function formatText(
formatted_text = runfmt(text, configPath);
} catch (e) {
console.error(e);
return;
return [];
}

// フォーマット
Expand All @@ -200,7 +209,7 @@ async function formatText(
connection.window.showErrorMessage(
`Formatter error. src:${textDocument.uri}, config:${configPath} msg: ${e}`
);
return;
return [];
}
//タイマーストップ
const endTime = performance.now();
Expand All @@ -218,19 +227,11 @@ async function formatText(
);
}

// 変更を適用
connection.workspace.applyEdit({
documentChanges: [
TextDocumentEdit.create(
{ uri: textDocument.uri, version: textDocument.version },
changes
),
],
});
return changes;
}

// コマンド実行時に行う処理
connection.onExecuteCommand((params) => {
connection.onExecuteCommand(async (params) => {
if (
params.command !== "uroborosql-fmt.executeFormat" ||
params.arguments == null
Expand All @@ -247,7 +248,27 @@ connection.onExecuteCommand((params) => {
const version = params.arguments[1];
const selections = params.arguments[2];

formatText(uri, textDocument, version, selections);
const changes = await formatText(uri, textDocument, version, selections);

// 変更を適用
connection.workspace.applyEdit({
documentChanges: [
TextDocumentEdit.create(
{ uri: textDocument.uri, version: textDocument.version },
changes
),
],
});
});

connection.onDocumentFormatting(async (params): Promise<TextEdit[]> => {
const uri = params.textDocument.uri;
const textDocument = documents.get(uri);
if (textDocument == null) {
return [];
}

return formatText(uri, textDocument, textDocument.version, []);
});

// Make the text document manager listen on the connection
Expand Down

0 comments on commit 7e9c6f2

Please sign in to comment.