volar-service-css 0.0.0

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/out/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { Service } from '@volar/language-service';
2
+ declare const _default: () => Service;
3
+ export default _default;
package/out/index.js ADDED
@@ -0,0 +1,223 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const css = __importStar(require("vscode-css-languageservice"));
27
+ const vscode = __importStar(require("vscode-languageserver-protocol"));
28
+ const path = __importStar(require("path"));
29
+ exports.default = () => (context) => {
30
+ // https://github.com/microsoft/vscode/blob/09850876e652688fb142e2e19fd00fd38c0bc4ba/extensions/css-language-features/server/src/cssServer.ts#L97
31
+ const triggerCharacters = ['/', '-', ':'];
32
+ if (!context) {
33
+ return { triggerCharacters };
34
+ }
35
+ let inited = false;
36
+ const stylesheets = new WeakMap();
37
+ const cssLs = css.getCSSLanguageService({ fileSystemProvider: context.env.fileSystemProvider });
38
+ const scssLs = css.getSCSSLanguageService({ fileSystemProvider: context.env.fileSystemProvider });
39
+ const lessLs = css.getLESSLanguageService({ fileSystemProvider: context.env.fileSystemProvider });
40
+ const postcssLs = {
41
+ ...scssLs,
42
+ doValidation: (document, stylesheet, documentSettings) => {
43
+ let errors = scssLs.doValidation(document, stylesheet, documentSettings);
44
+ errors = errors.filter(error => error.code !== 'css-semicolonexpected');
45
+ errors = errors.filter(error => error.code !== 'css-ruleorselectorexpected');
46
+ errors = errors.filter(error => error.code !== 'unknownAtRules');
47
+ return errors;
48
+ },
49
+ };
50
+ return {
51
+ triggerCharacters,
52
+ async resolveRuleContext(context) {
53
+ await worker(context.document, (stylesheet, cssLs) => {
54
+ context.css = {
55
+ stylesheet,
56
+ languageService: cssLs,
57
+ };
58
+ });
59
+ return context;
60
+ },
61
+ async provideCompletionItems(document, position) {
62
+ return worker(document, async (stylesheet, cssLs) => {
63
+ const settings = await context.env.getConfiguration?.(document.languageId);
64
+ const cssResult = context.env.documentContext
65
+ ? await cssLs.doComplete2(document, position, stylesheet, context.env.documentContext, settings?.completion)
66
+ : await cssLs.doComplete(document, position, stylesheet, settings?.completion);
67
+ return cssResult;
68
+ });
69
+ },
70
+ provideRenameRange(document, position) {
71
+ return worker(document, (stylesheet, cssLs) => {
72
+ return cssLs.prepareRename(document, position, stylesheet);
73
+ });
74
+ },
75
+ provideRenameEdits(document, position, newName) {
76
+ return worker(document, (stylesheet, cssLs) => {
77
+ return cssLs.doRename(document, position, newName, stylesheet);
78
+ });
79
+ },
80
+ provideCodeActions(document, range, context) {
81
+ return worker(document, (stylesheet, cssLs) => {
82
+ return cssLs.doCodeActions2(document, range, context, stylesheet);
83
+ });
84
+ },
85
+ provideDefinition(document, position) {
86
+ return worker(document, (stylesheet, cssLs) => {
87
+ const location = cssLs.findDefinition(document, position, stylesheet);
88
+ if (location) {
89
+ return [vscode.LocationLink.create(location.uri, location.range, location.range)];
90
+ }
91
+ });
92
+ },
93
+ async provideDiagnostics(document) {
94
+ return worker(document, async (stylesheet, cssLs) => {
95
+ const settings = await context.env.getConfiguration?.(document.languageId);
96
+ return cssLs.doValidation(document, stylesheet, settings);
97
+ });
98
+ },
99
+ async provideHover(document, position) {
100
+ return worker(document, async (stylesheet, cssLs) => {
101
+ const settings = await context.env.getConfiguration?.(document.languageId);
102
+ return cssLs.doHover(document, position, stylesheet, settings?.hover);
103
+ });
104
+ },
105
+ provideReferences(document, position) {
106
+ return worker(document, (stylesheet, cssLs) => {
107
+ return cssLs.findReferences(document, position, stylesheet);
108
+ });
109
+ },
110
+ provideDocumentHighlights(document, position) {
111
+ return worker(document, (stylesheet, cssLs) => {
112
+ return cssLs.findDocumentHighlights(document, position, stylesheet);
113
+ });
114
+ },
115
+ async provideDocumentLinks(document) {
116
+ return await worker(document, (stylesheet, cssLs) => {
117
+ if (!context.env.documentContext)
118
+ return;
119
+ return cssLs.findDocumentLinks2(document, stylesheet, context.env.documentContext);
120
+ });
121
+ },
122
+ provideDocumentSymbols(document) {
123
+ return worker(document, (stylesheet, cssLs) => {
124
+ return cssLs.findDocumentSymbols2(document, stylesheet);
125
+ });
126
+ },
127
+ provideDocumentColors(document) {
128
+ return worker(document, (stylesheet, cssLs) => {
129
+ return cssLs.findDocumentColors(document, stylesheet);
130
+ });
131
+ },
132
+ provideColorPresentations(document, color, range) {
133
+ return worker(document, (stylesheet, cssLs) => {
134
+ return cssLs.getColorPresentations(document, stylesheet, color, range);
135
+ });
136
+ },
137
+ provideFoldingRanges(document) {
138
+ return worker(document, (stylesheet, cssLs) => {
139
+ return cssLs.getFoldingRanges(document, stylesheet);
140
+ });
141
+ },
142
+ provideSelectionRanges(document, positions) {
143
+ return worker(document, (stylesheet, cssLs) => {
144
+ return cssLs.getSelectionRanges(document, positions, stylesheet);
145
+ });
146
+ },
147
+ async provideDocumentFormattingEdits(document, formatRange, options) {
148
+ return worker(document, async (_stylesheet, cssLs) => {
149
+ const options_2 = await context.env.getConfiguration?.(document.languageId + '.format');
150
+ if (options_2?.enable === false) {
151
+ return;
152
+ }
153
+ return cssLs.format(document, formatRange, {
154
+ ...options_2,
155
+ ...options,
156
+ });
157
+ });
158
+ },
159
+ };
160
+ async function initCustomData() {
161
+ if (!inited) {
162
+ context?.env.onDidChangeConfiguration?.(async () => {
163
+ const customData = await getCustomData();
164
+ cssLs.setDataProviders(true, customData);
165
+ scssLs.setDataProviders(true, customData);
166
+ lessLs.setDataProviders(true, customData);
167
+ });
168
+ const customData = await getCustomData();
169
+ cssLs.setDataProviders(true, customData);
170
+ scssLs.setDataProviders(true, customData);
171
+ lessLs.setDataProviders(true, customData);
172
+ inited = true;
173
+ }
174
+ }
175
+ async function getCustomData() {
176
+ const customData = await context?.env.getConfiguration?.('css.customData') ?? [];
177
+ const newData = [];
178
+ for (const customDataPath of customData) {
179
+ try {
180
+ const jsonPath = path.resolve(customDataPath);
181
+ newData.push(css.newCSSDataProvider(require(jsonPath)));
182
+ }
183
+ catch (error) {
184
+ console.error(error);
185
+ }
186
+ }
187
+ return newData;
188
+ }
189
+ function getCssLs(lang) {
190
+ switch (lang) {
191
+ case 'css': return cssLs;
192
+ case 'scss': return scssLs;
193
+ case 'less': return lessLs;
194
+ case 'postcss': return postcssLs;
195
+ }
196
+ }
197
+ function getStylesheet(document) {
198
+ const cache = stylesheets.get(document);
199
+ if (cache) {
200
+ const [cacheVersion, cacheStylesheet] = cache;
201
+ if (cacheVersion === document.version) {
202
+ return cacheStylesheet;
203
+ }
204
+ }
205
+ const cssLs = getCssLs(document.languageId);
206
+ if (!cssLs)
207
+ return;
208
+ const stylesheet = cssLs.parseStylesheet(document);
209
+ stylesheets.set(document, [document.version, stylesheet]);
210
+ return stylesheet;
211
+ }
212
+ async function worker(document, callback) {
213
+ const stylesheet = getStylesheet(document);
214
+ if (!stylesheet)
215
+ return;
216
+ const cssLs = getCssLs(document.languageId);
217
+ if (!cssLs)
218
+ return;
219
+ await initCustomData();
220
+ return callback(stylesheet, cssLs);
221
+ }
222
+ };
223
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "volar-service-css",
3
+ "version": "0.0.0",
4
+ "main": "out/index.js",
5
+ "license": "MIT",
6
+ "files": [
7
+ "rules.d.ts",
8
+ "out/**/*.js",
9
+ "out/**/*.d.ts"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/volarjs/services.git",
14
+ "directory": "packages/css"
15
+ },
16
+ "dependencies": {
17
+ "vscode-css-languageservice": "^6.2.3",
18
+ "vscode-languageserver-protocol": "^3.17.3",
19
+ "vscode-languageserver-textdocument": "^1.0.8"
20
+ },
21
+ "peerDependencies": {
22
+ "@volar/language-service": "*"
23
+ },
24
+ "peerDependenciesMeta": {
25
+ "@volar/language-service": {
26
+ "optional": true
27
+ }
28
+ },
29
+ "gitHead": "1011561ac4bbf79c53c3b80c27692569bf861519"
30
+ }
package/rules.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import * as css from 'vscode-css-languageservice';
2
+
3
+ declare module '@volar/language-service' {
4
+ interface RuleContext {
5
+ css?: {
6
+ stylesheet: css.Stylesheet;
7
+ languageService: css.LanguageService;
8
+ }
9
+ }
10
+ }