返回 slidev
volar-service-yaml.ts
根目录 / packages / vscode / language-server / volar-service-yaml.ts
1 // Vendored from https://github.com/volarjs/services/tree/master/packages/yaml
2 // Await https://github.com/volarjs/services/pull/103
3
4 import type { Disposable, DocumentSelector, LanguageServiceContext, LanguageServicePlugin, LanguageServicePluginInstance, ProviderResult } from '@volar/language-service'
5 import type { TextDocument } from 'vscode-languageserver-textdocument'
6 import { URI, Utils } from 'vscode-uri'
7 import * as yaml from 'yaml-language-server'
8
9 export interface Provide {
10 'yaml/languageService': () => yaml.LanguageService
11 }
12
13 function noop(): undefined { }
14
15 /**
16 * Create a Volar language service for YAML documents.
17 */
18 export function create({
19 documentSelector = ['yaml'],
20 getWorkspaceContextService = (context) => {
21 return {
22 resolveRelativePath(relativePath, resource) {
23 const base = resource.substring(0, resource.lastIndexOf('/') + 1)
24 let baseUri = URI.parse(base)
25 const decoded = context.decodeEmbeddedDocumentUri(baseUri)
26 if (decoded) {
27 baseUri = decoded[0]
28 }
29 return Utils.resolvePath(baseUri, relativePath).toString()
30 },
31 }
32 },
33 getLanguageSettings = () => {
34 return {
35 completion: true,
36 customTags: [],
37 format: true,
38 hover: true,
39 isKubernetes: false,
40 validate: true,
41 yamlVersion: '1.2',
42 }
43 },
44 onDidChangeLanguageSettings = () => {
45 return {
46 dispose() { },
47 }
48 },
49 }: {
50 documentSelector?: DocumentSelector
51 getWorkspaceContextService?: (context: LanguageServiceContext) => yaml.WorkspaceContextService
52 getLanguageSettings?: (context: LanguageServiceContext) => ProviderResult<yaml.LanguageSettings>
53 onDidChangeLanguageSettings?: (listener: () => void, context: LanguageServiceContext) => Disposable
54 } = {}): LanguageServicePlugin {
55 return {
56 name: 'yaml',
57 capabilities: {
58 codeActionProvider: {},
59 codeLensProvider: {
60 resolveProvider: false,
61 },
62 completionProvider: {
63 triggerCharacters: [' ', ':'],
64 },
65 definitionProvider: true,
66 diagnosticProvider: {
67 interFileDependencies: true,
68 workspaceDiagnostics: false,
69 },
70 documentOnTypeFormattingProvider: {
71 triggerCharacters: ['\n'],
72 },
73 documentSymbolProvider: true,
74 hoverProvider: true,
75 documentLinkProvider: {},
76 foldingRangeProvider: true,
77 selectionRangeProvider: true,
78 },
79 create(context): LanguageServicePluginInstance<Provide> {
80 const ls = yaml.getLanguageService({
81 schemaRequestService: async uri => await context.env.fs?.readFile(URI.parse(uri)) ?? '',
82 telemetry: {
83 send: noop,
84 sendError: noop,
85 sendTrack: noop,
86 },
87 clientCapabilities: context.env?.clientCapabilities,
88 workspaceContext: getWorkspaceContextService(context),
89 })
90 let initializing: Promise<void> | undefined
91
92 const disposable = onDidChangeLanguageSettings(() => initializing = undefined, context)
93
94 return {
95 dispose() {
96 disposable.dispose()
97 },
98
99 provide: {
100 'yaml/languageService': () => ls,
101 },
102
103 provideCodeActions(document, range, context) {
104 return worker(document, () => {
105 return ls.getCodeAction(document, {
106 context,
107 range,
108 textDocument: document,
109 })
110 })
111 },
112
113 // provideCodeLenses(document) {
114 // return worker(document, () => {
115 // return ls.getCodeLens(document)
116 // })
117 // },
118
119 provideCompletionItems(document, position) {
120 return worker(document, () => {
121 return ls.doComplete(document, position, false)
122 })
123 },
124
125 provideDefinition(document, position) {
126 return worker(document, () => {
127 return ls.doDefinition(document, { position, textDocument: document })
128 })
129 },
130
131 provideDiagnostics(document) {
132 return worker(document, () => {
133 return ls.doValidation(document, false)
134 })
135 },
136
137 provideDocumentSymbols(document) {
138 return worker(document, () => {
139 return ls.findDocumentSymbols2(document, {})
140 })
141 },
142
143 provideHover(document, position) {
144 return worker(document, () => {
145 return ls.doHover(document, position)
146 })
147 },
148
149 provideDocumentLinks(document) {
150 return worker(document, () => {
151 return ls.findLinks(document)
152 })
153 },
154
155 provideFoldingRanges(document) {
156 return worker(document, () => {
157 return ls.getFoldingRanges(document, context.env.clientCapabilities?.textDocument?.foldingRange ?? {})
158 })
159 },
160
161 provideOnTypeFormattingEdits(document, position, key, options) {
162 return worker(document, () => {
163 return ls.doDocumentOnTypeFormatting(document, {
164 ch: key,
165 options,
166 position,
167 textDocument: document,
168 })
169 })
170 },
171
172 provideSelectionRanges(document, positions) {
173 return worker(document, () => {
174 return ls.getSelectionRanges(document, positions)
175 })
176 },
177
178 // resolveCodeLens(codeLens) {
179 // return ls.resolveCodeLens(codeLens)
180 // },
181 }
182
183 async function worker<T>(document: TextDocument, callback: () => T): Promise<Awaited<T> | undefined> {
184 if (!matchDocument(documentSelector, document)) {
185 return
186 }
187
188 await (initializing ??= initialize())
189
190 return await callback()
191 }
192
193 async function initialize() {
194 const settings = await getLanguageSettings(context)
195 ls.configure(settings)
196 }
197 },
198 }
199 }
200
201 function matchDocument(selector: DocumentSelector, document: TextDocument) {
202 for (const sel of selector) {
203 if (sel === document.languageId || (typeof sel === 'object' && sel.language === document.languageId)) {
204 return true
205 }
206 }
207 return false
208 }
209
209 lines TYPESCRIPT