@algosail/lsp 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +43 -4
- package/lib/completion.js +123 -0
- package/lib/validate.js +28 -0
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,8 +1,47 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { createRequire } from 'module'
|
|
3
|
-
import path from 'path'
|
|
4
|
-
import {
|
|
2
|
+
// import { createRequire } from 'module'
|
|
3
|
+
// import path from 'path'
|
|
4
|
+
import {
|
|
5
|
+
createConnection,
|
|
6
|
+
ProposedFeatures,
|
|
7
|
+
TextDocumentSyncKind,
|
|
8
|
+
} from 'vscode-languageserver/node'
|
|
5
9
|
import { createParser } from '@algosail/parser'
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
import { validateDocument } from './lib/validate.js'
|
|
12
|
+
import { getCompletionItems } from './lib/completion.js'
|
|
13
|
+
|
|
8
14
|
const parser = await createParser()
|
|
15
|
+
|
|
16
|
+
const connection = createConnection(ProposedFeatures.all)
|
|
17
|
+
|
|
18
|
+
connection.onInitialize(() => ({
|
|
19
|
+
capabilities: {
|
|
20
|
+
textDocumentSync: TextDocumentSyncKind.Full,
|
|
21
|
+
completionProvider: {
|
|
22
|
+
triggerCharacters: ['/', '#', '_', '~', '*'],
|
|
23
|
+
},
|
|
24
|
+
hoverProvider: true,
|
|
25
|
+
},
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
connection.onDidOpenTextDocument(({ textDocument: { uri, text } }) => {
|
|
29
|
+
validateDocument(connection, parser, uri, text)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
connection.onDidChangeTextDocument(({ textDocument: { uri }, contentChanges }) => {
|
|
33
|
+
// Full-sync mode: the last entry contains the whole new text.
|
|
34
|
+
const text = contentChanges[contentChanges.length - 1].text
|
|
35
|
+
validateDocument(connection, parser, uri, text)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
connection.onDidCloseTextDocument(({ textDocument: { uri } }) => {
|
|
39
|
+
parser.removeAST(uri)
|
|
40
|
+
connection.sendDiagnostics({ uri, diagnostics: [] })
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
connection.onCompletion(({ textDocument: { uri } }) => {
|
|
44
|
+
return getCompletionItems(parser, uri)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
connection.listen()
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { CompletionItemKind } from 'vscode-languageserver/node'
|
|
2
|
+
import { MarkupKind } from 'vscode-languageserver-types'
|
|
3
|
+
import { builtinWords } from '@algosail/builtins'
|
|
4
|
+
|
|
5
|
+
export function getCompletionItems(parser, uri) {
|
|
6
|
+
const items = []
|
|
7
|
+
|
|
8
|
+
for (const b of Object.values(builtinWords)) {
|
|
9
|
+
items.push({
|
|
10
|
+
label: b.name,
|
|
11
|
+
kind: CompletionItemKind.Keyword,
|
|
12
|
+
detail: `( ${b.signature} )`,
|
|
13
|
+
documentation: { kind: MarkupKind.Markdown, value: b.docs },
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const ast = parser.getAST(uri)
|
|
18
|
+
if (!ast) return items
|
|
19
|
+
|
|
20
|
+
for (const [key, module] of Object.entries(ast.modules)) {
|
|
21
|
+
for (const group of module.groups) {
|
|
22
|
+
items.push({
|
|
23
|
+
label: `~${key}&${group.name}`,
|
|
24
|
+
kind: CompletionItemKind.Enum,
|
|
25
|
+
detail: `Group of tags ${group.tags.map((t) => t.name).join(', ')}`,
|
|
26
|
+
documentation: group.doc ? { kind: MarkupKind.Markdown, value: group.doc } : undefined,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const tag of module.tags) {
|
|
31
|
+
items.push({
|
|
32
|
+
label: `~${key}#${tag.name}`,
|
|
33
|
+
kind: CompletionItemKind.EnumMember,
|
|
34
|
+
detail: `Tag constructor in ~${key}&${tag.group}`,
|
|
35
|
+
documentation: tag.doc ? { kind: MarkupKind.Markdown, value: tag.doc } : undefined,
|
|
36
|
+
})
|
|
37
|
+
items.push({
|
|
38
|
+
label: `~${key}_${tag.name}`,
|
|
39
|
+
kind: CompletionItemKind.EnumMember,
|
|
40
|
+
detail: `Tag pattern in ~${key}&${tag.group}`,
|
|
41
|
+
documentation: tag.doc ? { kind: MarkupKind.Markdown, value: tag.doc } : undefined,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const map of module.maps) {
|
|
46
|
+
items.push({
|
|
47
|
+
label: `~${key}$${map.name}`,
|
|
48
|
+
kind: CompletionItemKind.Struct,
|
|
49
|
+
detail: `Map %${map.name}`,
|
|
50
|
+
documentation: map.doc ? { kind: MarkupKind.Markdown, value: map.doc } : undefined,
|
|
51
|
+
})
|
|
52
|
+
for (const field of map.fields) {
|
|
53
|
+
items.push({
|
|
54
|
+
label: `~${key}$${map.name}.${field.name}`,
|
|
55
|
+
kind: CompletionItemKind.Field,
|
|
56
|
+
detail: `Field :${field.name}${field.type ? ` (${field.type.kind})` : ''} of ~${key}$${map.name}`,
|
|
57
|
+
documentation: field.doc ? { kind: MarkupKind.Markdown, value: field.doc } : undefined,
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
for (const word of module.words) {
|
|
63
|
+
items.push({
|
|
64
|
+
label: `~${key}/${word.name}`,
|
|
65
|
+
kind: CompletionItemKind.Function,
|
|
66
|
+
detail: word.sig,
|
|
67
|
+
documentation: word.doc ? { kind: MarkupKind.Markdown, value: word.doc } : undefined,
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (const group of ast.groups) {
|
|
73
|
+
items.push({
|
|
74
|
+
label: `&${group.name}`,
|
|
75
|
+
kind: CompletionItemKind.Enum,
|
|
76
|
+
detail: `Group of tags ${group.tags.map((t) => t.name).join(', ')}`,
|
|
77
|
+
documentation: group.doc ? { kind: MarkupKind.Markdown, value: group.doc } : undefined,
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const tag of ast.tags) {
|
|
82
|
+
items.push({
|
|
83
|
+
label: `#${tag.name}`,
|
|
84
|
+
kind: CompletionItemKind.EnumMember,
|
|
85
|
+
detail: `Tag constructor in &${tag.group}`,
|
|
86
|
+
documentation: tag.doc ? { kind: MarkupKind.Markdown, value: tag.doc } : undefined,
|
|
87
|
+
})
|
|
88
|
+
items.push({
|
|
89
|
+
label: `_${tag.name}`,
|
|
90
|
+
kind: CompletionItemKind.EnumMember,
|
|
91
|
+
detail: `Tag pattern in &${tag.group}`,
|
|
92
|
+
documentation: tag.doc ? { kind: MarkupKind.Markdown, value: tag.doc } : undefined,
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const map of ast.maps) {
|
|
97
|
+
items.push({
|
|
98
|
+
label: `$${map.name}`,
|
|
99
|
+
kind: CompletionItemKind.Struct,
|
|
100
|
+
detail: `Map %${map.name}`,
|
|
101
|
+
documentation: map.doc ? { kind: MarkupKind.Markdown, value: map.doc } : undefined,
|
|
102
|
+
})
|
|
103
|
+
for (const field of map.fields) {
|
|
104
|
+
items.push({
|
|
105
|
+
label: `$${map.name}.${field.name}`,
|
|
106
|
+
kind: CompletionItemKind.Field,
|
|
107
|
+
detail: `Field :${field.name}${field.type ? ` (${field.type.kind})` : ''} of $${map.name}`,
|
|
108
|
+
documentation: field.doc ? { kind: MarkupKind.Markdown, value: field.doc } : undefined,
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
for (const word of ast.words) {
|
|
114
|
+
items.push({
|
|
115
|
+
label: `/${word.name}`,
|
|
116
|
+
kind: CompletionItemKind.Function,
|
|
117
|
+
detail: word.sig,
|
|
118
|
+
documentation: word.doc ? { kind: MarkupKind.Markdown, value: word.doc } : undefined,
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return items
|
|
123
|
+
}
|
package/lib/validate.js
CHANGED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { DiagnosticSeverity } from 'vscode-languageserver/node'
|
|
2
|
+
|
|
3
|
+
export const validateDocument = async (connection, parser, uri, text) => {
|
|
4
|
+
try {
|
|
5
|
+
const res = await parser.parseSail(uri, text)
|
|
6
|
+
|
|
7
|
+
const diagnostics = res.errors.map((err) => ({
|
|
8
|
+
severity: DiagnosticSeverity.Error,
|
|
9
|
+
range: {
|
|
10
|
+
start: {
|
|
11
|
+
line: err.startPosition.row,
|
|
12
|
+
character: err.startPosition.column,
|
|
13
|
+
},
|
|
14
|
+
end: {
|
|
15
|
+
line: err.endPosition.row,
|
|
16
|
+
character: err.endPosition.column,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
message: err.type === 'error' ? `Unexpected token ${err.text}` : `Missing ${err.text}`,
|
|
20
|
+
source: 'sail',
|
|
21
|
+
}))
|
|
22
|
+
|
|
23
|
+
connection.sendDiagnostics({ uri, diagnostics })
|
|
24
|
+
} catch (err) {
|
|
25
|
+
connection.console.error(`[sail-lsp] Failed to parse ${uri}: ${err.message}`)
|
|
26
|
+
connection.sendDiagnostics({ uri, diagnostics: [] })
|
|
27
|
+
}
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@algosail/lsp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@algosail/builtins": "^0.0.1",
|
|
11
|
-
"@algosail/parser": "^0.0.
|
|
11
|
+
"@algosail/parser": "^0.0.4",
|
|
12
12
|
"vscode-languageserver": "^9.0.1",
|
|
13
13
|
"vscode-languageserver-textdocument": "^1.0.11"
|
|
14
14
|
}
|