-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.ts
169 lines (144 loc) · 4.94 KB
/
index.ts
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
#!/usr/bin/env node
import {
FileType,
doComplete,
expandAbbreviation,
getEmmetMode,
updateExtensionsPath,
type FileService,
type VSCodeEmmetConfig,
} from '@vscode/emmet-helper'
import fs from 'fs'
import path from 'path'
import util from 'util'
import { TextDocument } from 'vscode-languageserver-textdocument'
import {
ProposedFeatures,
TextDocumentSyncKind,
TextDocuments,
createConnection,
} from 'vscode-languageserver/node'
const connection = createConnection(ProposedFeatures.all)
const documents = new TextDocuments(TextDocument)
interface GlobalConfig extends VSCodeEmmetConfig {
extensionsPath?: string[]
includeLanguages?: Record<string, string>
}
let globalConfig: GlobalConfig = {}
/**
* @see {@link https://github.com/microsoft/vscode-emmet-helper/blob/ea184b3b7d6d7ffbc1721b5ce986c8477d420127/src/test/emmetHelper.test.ts#L40-L79}
*/
const fileService: FileService = {
async readFile(uri) {
if (uri.scheme !== 'file') {
throw new Error(`schema ${uri.scheme} is not supported`)
}
return await util.promisify(fs.readFile)(uri.fsPath)
},
async stat(uri) {
if (uri.scheme !== 'file') {
throw new Error(`schema ${uri.scheme} is not supported`)
}
return new Promise((c, e) => {
fs.stat(uri.fsPath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
return c({ type: FileType.Unknown, ctime: -1, mtime: -1, size: -1 })
} else {
return e(err)
}
}
let type = FileType.Unknown
if (stats.isFile()) {
type = FileType.File
} else if (stats.isDirectory()) {
type = FileType.Directory
} else if (stats.isSymbolicLink()) {
type = FileType.SymbolicLink
}
c({
type,
ctime: stats.ctime.getTime(),
mtime: stats.mtime.getTime(),
size: stats.size,
})
})
})
},
}
connection.onInitialize((params) => {
globalConfig = params.initializationOptions || {}
if (globalConfig.extensionsPath?.length) {
const absolutePaths = globalConfig.extensionsPath.map((extensionPath) =>
path.isAbsolute(extensionPath)
? extensionPath
: path.resolve(extensionPath),
)
updateExtensionsPath(absolutePaths, fileService)
}
return {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
completionProvider: {
resolveProvider: false,
triggerCharacters: [
// NOTE: For cases where is valid to expand emmet abbreviations with
// special characters
'!', // eg. `!` and `!!!` snippets in html or `!important` in css
':', // eg. `w:` should expand to `width: |;`
'>', // https://docs.emmet.io/abbreviations/syntax/#child-gt
'+', // https://docs.emmet.io/abbreviations/syntax/#sibling
'^', // https://docs.emmet.io/abbreviations/syntax/#climb-up
'*', // https://docs.emmet.io/abbreviations/syntax/#multiplication
')', // https://docs.emmet.io/abbreviations/syntax/#grouping
'.', // https://docs.emmet.io/abbreviations/syntax/#id-and-class
']', // https://docs.emmet.io/abbreviations/syntax/#custom-attributes
'@', // https://docs.emmet.io/abbreviations/syntax/#changing-numbering-base-and-direction
'}', // https://docs.emmet.io/abbreviations/syntax/#text
'/', // for self-closing tags, eg. `div/` should expand to `<div />|`
// NOTE: For cases where completion is not triggered by typing a
// single character
...'abcdefghijklmnopqrstuvwxyz',
// NOTE: For cases where completion is not triggered by typing a
// single character or because numbers cannot be used to trigger
// completion
...'0123456789',
],
},
},
}
})
connection.onCompletion((textDocumentPosition) => {
const document = documents.get(textDocumentPosition.textDocument.uri)
if (!document) {
return
}
const editorLanguage = document.languageId
const emmetLanguage = getEmmetMode(editorLanguage) ?? 'html'
const syntax = !!globalConfig.includeLanguages?.[editorLanguage]
? (getEmmetMode(globalConfig.includeLanguages[editorLanguage]) ??
emmetLanguage)
: emmetLanguage
const position = textDocumentPosition.position
return doComplete(document, position, syntax, globalConfig)
})
connection.onRequest(
'emmet/expandAbbreviation',
(params: {
abbreviation: string
language: string
options: Parameters<typeof expandAbbreviation>[1]
}) => {
const emmetLanguage = getEmmetMode(params.language) ?? 'html'
const syntax = !!globalConfig.includeLanguages?.[params.language]
? (getEmmetMode(globalConfig.includeLanguages[params.language]) ??
emmetLanguage)
: emmetLanguage
return expandAbbreviation(params.abbreviation, {
syntax,
...params.options,
})
},
)
documents.listen(connection)
connection.listen()