-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage-server.ts
More file actions
169 lines (141 loc) · 5.69 KB
/
language-server.ts
File metadata and controls
169 lines (141 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
createConnection,
TextDocuments,
ProposedFeatures,
CompletionItem,
CompletionItemKind,
TextDocumentPositionParams,
InitializeResult,
Hover,
TextDocumentSyncKind,
} from 'vscode-languageserver/node'
import {TextDocument} from 'vscode-languageserver-textdocument'
import postcss, {type Root as ASTRoot} from 'postcss'
// import {properties, stories} from '@primer/primitives/dist/js/intellisense'
import camelCase from 'lodash.camelcase'
import {isColor} from './utils/is-color'
import {getSuggestions, getSuggestionsLikeVariable, type SuggestionWithSortText} from './suggestions'
import {getCssVariable} from './utils/get-css-variable'
import {getDocumentation} from './documentation'
import {getCurrentWord} from './utils/get-current-word'
// Create a connection for the server, using Node's IPC as a transport.
// Also include all preview / proposed LSP features.
const connection = createConnection(ProposedFeatures.all)
// Create a simple text document manager.
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument)
connection.onInitialize(async () => {
const result: InitializeResult = {
capabilities: {
// Tell the client that this server supports code completion.
completionProvider: {resolveProvider: true, triggerCharacters: [':']},
hoverProvider: true,
textDocumentSync: TextDocumentSyncKind.Incremental,
},
}
return result
})
connection.onInitialized(() => {
// nothing to do here, i suppose?
})
// This handler provides the initial list of the completion items.
connection.onCompletion((params: TextDocumentPositionParams): CompletionItem[] => {
const doc = documents.get(params.textDocument.uri)
if (!doc) return []
const currentLine = doc.getText({
start: {line: params.position.line, character: 0},
end: {line: params.position.line + 1, character: 0},
})
let ast: ASTRoot
try {
ast = postcss.parse(currentLine)
} catch {
// do nothing
}
if (!ast || ast.nodes.length === 0) return []
let property: string = ast.nodes[0].type === 'decl' ? camelCase(ast.nodes[0].prop) : undefined
if (!property) return []
const value: string =
ast.nodes[0].type === 'decl' ? ast.nodes[0].value.replace(');', '').replace('\n', '').trim() : undefined
const suggestedVariablesWithSortText: SuggestionWithSortText[] = []
// if the cursor is at the css variable, get getSuggestionsLikeVariable instead of getSuggestions(property)
const offset = doc.offsetAt(params.position)
const currentWord = getCurrentWord(doc, offset)
if (currentWord.includes('--') && currentWord.length > 2 /* checking it's not just -- */) {
const variableSuggestions = getSuggestionsLikeVariable(currentWord.replace('--', ''))
suggestedVariablesWithSortText.push(...variableSuggestions)
}
// TODO: for shorthands, property might be the second property like borderColor or paddingInline
// we can be smarter about this
if (property === 'padding') {
try {
// padding: block inline
const [blockValue] = value.split(' ')
const blockValuePositionEnd = currentLine.indexOf(blockValue) + blockValue.length
if (params.position.character > blockValuePositionEnd) {
property = 'paddingInline'
} else if (params.position.character <= blockValuePositionEnd) {
property = 'paddingBlock'
}
} catch {
// do nothing
}
} else if (property === 'border') {
try {
// border: width style color
const [borderWidth] = value.split(' ')
const borderWidthPositionEnd = currentLine.indexOf(borderWidth) + borderWidth.length
if (params.position.character > borderWidthPositionEnd) {
property = 'borderColor'
} else if (params.position.character <= borderWidthPositionEnd) {
property = 'borderWidth'
}
} catch {
// do nothing
}
}
suggestedVariablesWithSortText.push(...getSuggestions(property))
const items = suggestedVariablesWithSortText.map(variable => {
const documentation = getDocumentation(variable.name)
const item: CompletionItem = {
label: variable.name,
detail: String(variable.value),
// using kind only for the icon
kind:
typeof variable.value === 'string' && isColor(variable.value)
? CompletionItemKind.Color
: variable.kind === 'functional'
? CompletionItemKind.Field
: CompletionItemKind.Constructor,
sortText: variable.sortText,
// this is slightly silly because what about multiple variables in one line
// like shorthands or fallbacks
insertText: currentLine.includes('var') ? variable.name : `var(${variable.name});`,
documentation: {kind: 'markdown', value: documentation},
}
return item
})
return items
})
// This handler resolves additional information for the item selected in
// the completion list.
connection.onCompletionResolve((item: CompletionItem): CompletionItem => {
// const variableInfo = getVariableInfo(item.label as `--${string}`)
// if (!variableInfo) return null
// TODO: this works but reloads the page every time, commenting this out for now
// connection.sendRequest('open-docs', {variable: variableInfo})
return item
})
connection.onHover(params => {
const doc = documents.get(params.textDocument.uri)
if (!doc) return null
const offset = doc.offsetAt(params.position)
const variableName = getCssVariable(doc, offset)
if (!variableName) return null
const documentation = getDocumentation(variableName)
return {contents: {kind: 'markdown', value: documentation}} as Hover
})
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection)
// Listen on the connection
connection.listen()