Skip to content

Commit 66e21f9

Browse files
authored
feat(tolk): add experimental Tolk formatter (#43)
Fixes #44
1 parent 56a064c commit 66e21f9

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,21 @@
493493
"description": "Automatically open decompiled Fift assembly when opening BoC files"
494494
}
495495
}
496+
},
497+
{
498+
"title": "Formatter",
499+
"properties": {
500+
"ton.tolk.formatter.useFormatter": {
501+
"type": "boolean",
502+
"default": true,
503+
"description": "Use experimental Tolk formatter"
504+
},
505+
"ton.tolk.formatter.sortImports": {
506+
"type": "boolean",
507+
"default": true,
508+
"description": "Sort imports on format"
509+
}
510+
}
496511
}
497512
],
498513
"customEditors": [
@@ -530,6 +545,7 @@
530545
"@ton/core": "0.60.1",
531546
"@ton/crypto": "^3.3.0",
532547
"glob": "^11.0.1",
548+
"tolkfmt-test-dev": "0.0.12",
533549
"ton-assembly-test-dev": "0.0.16",
534550
"vscode-languageclient": "^8.0.2",
535551
"vscode-languageserver": "^8.0.2",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {Range} from "vscode-languageserver-textdocument"
2+
import * as lsp from "vscode-languageserver"
3+
import {findTolkFile} from "@server/files"
4+
import {format} from "tolkfmt-test-dev/dist/src"
5+
import {createTolkParser} from "@server/parser"
6+
7+
export async function formatTolkFile(
8+
uri: string,
9+
range: Range | undefined,
10+
options: {
11+
readonly useFormatter: boolean
12+
readonly sortImports: boolean
13+
},
14+
): Promise<lsp.TextEdit[] | null> {
15+
if (!options.useFormatter) return null
16+
17+
const file = await findTolkFile(uri)
18+
19+
const formatted = await format(file.content, {
20+
parser: createTolkParser(),
21+
range,
22+
sortImports: options.sortImports,
23+
})
24+
25+
if (formatted === file.content) {
26+
// already formatted
27+
return null
28+
}
29+
30+
const lines = file.content.split("\n")
31+
return [
32+
{
33+
range: {
34+
start: {
35+
line: 0,
36+
character: 0,
37+
},
38+
end: {
39+
line: lines.length,
40+
character: (lines.at(-1) ?? "").length,
41+
},
42+
},
43+
newText: formatted,
44+
},
45+
]
46+
}

server/src/server.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import {
118118
} from "@server/languages/func/rename/file-renaming"
119119
import {IndexingRootKind} from "@server/indexing/indexing"
120120
import {FuncIndexingRoot} from "@server/languages/func/indexing-root"
121+
import {formatTolkFile} from "@server/languages/tolk/format/format"
121122

122123
/**
123124
* Whenever LS is initialized.
@@ -1021,6 +1022,34 @@ connection.onInitialize(async (initParams: lsp.InitializeParams): Promise<lsp.In
10211022
return [...provideTolkWorkspaceSymbols(), ...provideFuncWorkspaceSymbols()]
10221023
})
10231024

1025+
connection.onRequest(
1026+
lsp.DocumentFormattingRequest.type,
1027+
async (params: lsp.DocumentFormattingParams): Promise<lsp.TextEdit[] | null> => {
1028+
const uri = params.textDocument.uri
1029+
const settings = await getDocumentSettings(uri)
1030+
1031+
if (isTolkFile(uri)) {
1032+
return formatTolkFile(uri, undefined, settings.tolk.formatter)
1033+
}
1034+
1035+
return null
1036+
},
1037+
)
1038+
1039+
connection.onRequest(
1040+
lsp.DocumentRangeFormattingRequest.type,
1041+
async (params: lsp.DocumentRangeFormattingParams): Promise<lsp.TextEdit[] | null> => {
1042+
const uri = params.textDocument.uri
1043+
const settings = await getDocumentSettings(uri)
1044+
1045+
if (isTolkFile(uri)) {
1046+
return formatTolkFile(uri, params.range, settings.tolk.formatter)
1047+
}
1048+
1049+
return null
1050+
},
1051+
)
1052+
10241053
// Custom LSP requests
10251054

10261055
connection.onRequest(
@@ -1045,6 +1074,7 @@ connection.onInitialize(async (initParams: lsp.InitializeParams): Promise<lsp.In
10451074
capabilities: {
10461075
textDocumentSync: lsp.TextDocumentSyncKind.Incremental,
10471076
documentFormattingProvider: true,
1077+
documentRangeFormattingProvider: true,
10481078
documentSymbolProvider: true,
10491079
workspaceSymbolProvider: true,
10501080
definitionProvider: true,

server/src/settings/settings.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export interface TolkSettings {
4444
readonly typeAware: boolean
4545
readonly addImports: boolean
4646
}
47+
readonly formatter: {
48+
readonly useFormatter: boolean
49+
readonly sortImports: boolean
50+
}
4751
}
4852

4953
export interface FuncSettings {
@@ -93,6 +97,10 @@ const tolkDefaultSettings: TolkSettings = {
9397
typeAware: true,
9498
addImports: true,
9599
},
100+
formatter: {
101+
useFormatter: true,
102+
sortImports: true,
103+
},
96104
}
97105

98106
const defaultSettings: ServerSettings = {
@@ -159,6 +167,14 @@ function mergeSettings(vsSettings: Partial<ServerSettings>): ServerSettings {
159167
vsSettings.tolk?.completion.addImports ??
160168
defaultSettings.tolk.completion.addImports,
161169
},
170+
formatter: {
171+
useFormatter:
172+
vsSettings.tolk?.formatter.useFormatter ??
173+
defaultSettings.tolk.formatter.useFormatter,
174+
sortImports:
175+
vsSettings.tolk?.formatter.sortImports ??
176+
defaultSettings.tolk.formatter.sortImports,
177+
},
162178
},
163179
func: {
164180
hints: {

yarn.lock

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7502,6 +7502,19 @@ __metadata:
75027502
languageName: node
75037503
linkType: hard
75047504

7505+
"tolkfmt-test-dev@npm:0.0.12":
7506+
version: 0.0.12
7507+
resolution: "tolkfmt-test-dev@npm:0.0.12"
7508+
dependencies:
7509+
cac: "npm:^6.7.14"
7510+
glob: "npm:^10.3.10"
7511+
web-tree-sitter: "npm:^0.25.6"
7512+
bin:
7513+
tolkfmt: bin/tolkfmt
7514+
checksum: 10c0/3cba34d27ad6bdea60e3d39576d5c7386ed28406e98219f0a01dc59270d8f76d105a1ece90fdaa4f22f5f0dc178af14da92748f7424fa96de415161891fb5113
7515+
languageName: node
7516+
linkType: hard
7517+
75057518
"ton-assembly-test-dev@npm:0.0.16":
75067519
version: 0.0.16
75077520
resolution: "ton-assembly-test-dev@npm:0.0.16"
@@ -8006,6 +8019,7 @@ __metadata:
80068019
jest: "npm:^29.7.0"
80078020
mocha: "npm:^10.3.0"
80088021
prettier: "npm:3.4.2"
8022+
tolkfmt-test-dev: "npm:0.0.12"
80098023
ton-assembly-test-dev: "npm:0.0.16"
80108024
tree-sitter-cli: "npm:^0.25.0"
80118025
ts-jest: "npm:^29.2.6"
@@ -8058,10 +8072,10 @@ __metadata:
80588072
languageName: node
80598073
linkType: hard
80608074

8061-
"web-tree-sitter@npm:^0.25.0":
8062-
version: 0.25.1
8063-
resolution: "web-tree-sitter@npm:0.25.1"
8064-
checksum: 10c0/0b94c7488d8196d9c9019e50903088705b4f659ae182153461d9819e6eec921941c4866dace43e698f09e800ac4de3cfc19ca85caa1dee52db54135213412ded
8075+
"web-tree-sitter@npm:^0.25.0, web-tree-sitter@npm:^0.25.6":
8076+
version: 0.25.7
8077+
resolution: "web-tree-sitter@npm:0.25.7"
8078+
checksum: 10c0/75799ec043455364c1f24a8d4a1bb94b22cbf655b109fdc72038d661a6224ee99f313e44625758dec59a5192aab4ba53315745c3cc86ca4c524e74a247b4d964
80658079
languageName: node
80668080
linkType: hard
80678081

0 commit comments

Comments
 (0)