@master/css-language-service 2.0.0-rc.15 → 2.0.0-rc.16

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/dist/index.cjs ADDED
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var color = require('./providers/color.cjs');
4
+ var completion = require('./providers/completion.cjs');
5
+ var hover = require('./providers/hover.cjs');
6
+ var positionCheck = require('./position-check.cjs');
7
+ var settings = require('./settings.cjs');
8
+
9
+
10
+
11
+ exports.getColorPresentation = color.getColorPresentation;
12
+ exports.getColorValue = color.getColorValue;
13
+ exports.getDocumentColors = color.getDocumentColors;
14
+ exports.checkConfigColorsBlock = completion.checkConfigColorsBlock;
15
+ exports.getCompletionItem = completion.getCompletionItem;
16
+ exports.getConfigColorsCompletionItem = completion.getConfigColorsCompletionItem;
17
+ exports.getLastInstance = completion.getLastInstance;
18
+ exports.getReturnItem = completion.getReturnItem;
19
+ exports.doHover = hover.doHover;
20
+ exports.positionCheck = positionCheck.positionCheck;
21
+ exports.settings = settings.settings;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { getDocumentColors, getColorValue, getColorPresentation } from './providers/color';
2
- export { checkConfigColorsBlock, getLastInstance, getConfigColorsCompletionItem, getCompletionItem, getReturnItem } from './providers/completion';
3
- export { doHover } from './providers/hover';
4
- export { positionCheck } from './position-check';
5
- export { settings } from './settings';
1
+ export * from './providers/color';
2
+ export * from './providers/completion';
3
+ export * from './providers/hover';
4
+ export * from './position-check';
5
+ export * from './settings';
package/dist/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ export { getColorPresentation, getColorValue, getDocumentColors } from './providers/color.mjs';
2
+ export { checkConfigColorsBlock, getCompletionItem, getConfigColorsCompletionItem, getLastInstance, getReturnItem } from './providers/completion.mjs';
3
+ export { doHover } from './providers/hover.mjs';
4
+ export { positionCheck } from './position-check.mjs';
5
+ export { settings } from './settings.mjs';
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ var regex = require('./utils/regex.cjs');
4
+
5
+ function positionCheck(text, positionIndex, startIndex, RegExpList) {
6
+ const result = {
7
+ IsMatch: false,
8
+ classStartIndex: 0,
9
+ classEndIndex: 0,
10
+ classString: '',
11
+ instance: {
12
+ index: {
13
+ start: 0,
14
+ end: 0
15
+ },
16
+ instanceString: ''
17
+ },
18
+ instanceList: []
19
+ };
20
+ let instanceMatch;
21
+ let classMatch;
22
+ RegExpList.forEach((x)=>{
23
+ const classPattern = new RegExp(x, 'g');
24
+ while((classMatch = classPattern.exec(text)) !== null){
25
+ if ((classMatch.index <= positionIndex - startIndex && classMatch.index + classMatch[0].length - 1 >= positionIndex - startIndex) == true) {
26
+ result.IsMatch = true;
27
+ result.classStartIndex = classMatch.index;
28
+ result.classEndIndex = classMatch.index + classMatch[0].length - 1;
29
+ result.classString = classMatch[0];
30
+ while((instanceMatch = regex.instancePattern.exec(classMatch[2])) !== null){
31
+ result.instanceList.push({
32
+ index: {
33
+ start: classMatch.index + classMatch[1].length + instanceMatch.index,
34
+ end: classMatch.index + classMatch[1].length + instanceMatch.index + instanceMatch[0].length
35
+ },
36
+ instanceString: instanceMatch[0]
37
+ });
38
+ if ((classMatch.index + classMatch[1].length + instanceMatch.index <= positionIndex && classMatch.index + classMatch[1].length + instanceMatch.index + instanceMatch[0].length >= positionIndex) == true) {
39
+ result.instance = {
40
+ index: {
41
+ start: classMatch.index + classMatch[1].length + instanceMatch.index,
42
+ end: classMatch.index + classMatch[1].length + instanceMatch.index + instanceMatch[0].length
43
+ },
44
+ instanceString: instanceMatch[0]
45
+ };
46
+ }
47
+ }
48
+ return result;
49
+ } else if (classMatch.index > positionIndex - startIndex) {
50
+ break;
51
+ }
52
+ }
53
+ });
54
+ return result;
55
+ }
56
+
57
+ exports.positionCheck = positionCheck;
@@ -0,0 +1,55 @@
1
+ import { instancePattern } from './utils/regex.mjs';
2
+
3
+ function positionCheck(text, positionIndex, startIndex, RegExpList) {
4
+ const result = {
5
+ IsMatch: false,
6
+ classStartIndex: 0,
7
+ classEndIndex: 0,
8
+ classString: '',
9
+ instance: {
10
+ index: {
11
+ start: 0,
12
+ end: 0
13
+ },
14
+ instanceString: ''
15
+ },
16
+ instanceList: []
17
+ };
18
+ let instanceMatch;
19
+ let classMatch;
20
+ RegExpList.forEach((x)=>{
21
+ const classPattern = new RegExp(x, 'g');
22
+ while((classMatch = classPattern.exec(text)) !== null){
23
+ if ((classMatch.index <= positionIndex - startIndex && classMatch.index + classMatch[0].length - 1 >= positionIndex - startIndex) == true) {
24
+ result.IsMatch = true;
25
+ result.classStartIndex = classMatch.index;
26
+ result.classEndIndex = classMatch.index + classMatch[0].length - 1;
27
+ result.classString = classMatch[0];
28
+ while((instanceMatch = instancePattern.exec(classMatch[2])) !== null){
29
+ result.instanceList.push({
30
+ index: {
31
+ start: classMatch.index + classMatch[1].length + instanceMatch.index,
32
+ end: classMatch.index + classMatch[1].length + instanceMatch.index + instanceMatch[0].length
33
+ },
34
+ instanceString: instanceMatch[0]
35
+ });
36
+ if ((classMatch.index + classMatch[1].length + instanceMatch.index <= positionIndex && classMatch.index + classMatch[1].length + instanceMatch.index + instanceMatch[0].length >= positionIndex) == true) {
37
+ result.instance = {
38
+ index: {
39
+ start: classMatch.index + classMatch[1].length + instanceMatch.index,
40
+ end: classMatch.index + classMatch[1].length + instanceMatch.index + instanceMatch[0].length
41
+ },
42
+ instanceString: instanceMatch[0]
43
+ };
44
+ }
45
+ }
46
+ return result;
47
+ } else if (classMatch.index > positionIndex - startIndex) {
48
+ break;
49
+ }
50
+ }
51
+ });
52
+ return result;
53
+ }
54
+
55
+ export { positionCheck };
@@ -0,0 +1,319 @@
1
+ 'use strict';
2
+
3
+ var css = require('@master/css');
4
+ var hexToRgb = require('../utils/hex-to-rgb.cjs');
5
+ var regex = require('../utils/regex.cjs');
6
+ var toTwoDigitHex = require('../utils/to-two-digit-hex.cjs');
7
+ var colorConvert = require('color-convert');
8
+ var oklabToRgb = require('../utils/oklab-to-rgb.cjs');
9
+ var oklchToOklab = require('../utils/oklch-to-oklab.cjs');
10
+
11
+ async function getDocumentColors(text, css$1 = new css.MasterCSS()) {
12
+ const colors = [];
13
+ let instanceMatch;
14
+ while((instanceMatch = regex.instancePattern.exec(text)) !== null){
15
+ const instanceStartIndex = instanceMatch.index;
16
+ // todo: refactor theme
17
+ // const theme = css.themeNames.find(x => instanceMatch?.[0]?.endsWith(`@${x}`)) ?? ''
18
+ const theme = '';
19
+ const colorPattern = /(?<=[|:\s"'`]|^)(?:#?[\w-]+(?:-[\d]{1,2})?(?:\/.?[\d]*)?(?:\([^\s)]+\))?(?![:]))/g;
20
+ let colorMatch;
21
+ //check color
22
+ while((colorMatch = colorPattern.exec(instanceMatch[0])) !== null){
23
+ const colorValue = parseColorString(colorMatch[0], theme, css$1);
24
+ if (colorValue) {
25
+ const colorIndex = {
26
+ index: {
27
+ start: instanceStartIndex + colorMatch.index,
28
+ end: instanceStartIndex + colorMatch.index + colorMatch[0].length
29
+ },
30
+ color: colorValue
31
+ };
32
+ colors.push(colorIndex);
33
+ }
34
+ }
35
+ }
36
+ return colors;
37
+ }
38
+ function percentageConverter(value, max = 1) {
39
+ if (value === 'none') {
40
+ return 0;
41
+ } else if (value.endsWith('%')) {
42
+ return max * Number(value.replace('%', '')) / 100;
43
+ } else {
44
+ return Number(value);
45
+ }
46
+ }
47
+ function degConverter(value) {
48
+ if (value === 'none') {
49
+ return 0;
50
+ } else if (value.endsWith('turn')) {
51
+ return Number(value.replace('turn', '')) * 360;
52
+ } else if (value.endsWith('grad')) {
53
+ return Number(value.replace('grad', '')) * 0.9;
54
+ } else if (value.endsWith('rad')) {
55
+ return Number(value.replace('rad', '')) * 57.2957795;
56
+ } else if (value.endsWith('deg')) {
57
+ return Number(value.replace('deg', ''));
58
+ } else {
59
+ return Number(value);
60
+ }
61
+ }
62
+ function parseColorString(colorString, theme, css$1 = new css.MasterCSS()) {
63
+ const rgbaColorPattern = /rgb(?:a)?\(([\d.]+)[,|]([\d.]+)[,|]([\d.]+)(?:[,/]([\d.]+(?:%)?))?\)/g;
64
+ const hslaColorPattern = /hsl(?:a)?\(([\d.]+(?:deg|turn|grad|rad)?|none)[,|]([\d.]+%|none)[,|]([\d.]+%|none)(?:[,/]([\d.]+(?:%)?))?\)/g;
65
+ const hwbColorPattern = /hwb\(([\d.]+(?:deg|turn|grad|rad)?|none)[|]([\d.]+%|none)[|]([\d.]+%|none)(?:[/]([\d.]+(?:%)?))?\)/g;
66
+ const oklchColorPattern = /oklch\(([\d.]+(?:%)?|none)[|]([\d.]+(?:%)?|none)[|]([\d.]+(?:deg|turn|grad|rad)?|none)(?:[/]([\d.]+(?:%)?))?\)/g;
67
+ const lchColorPattern = /lch\(([\d.]+(?:%)?|none)[|]([\d.]+(?:%)?|none)[|]([\d.]+(?:deg|turn|grad|rad)?|none)(?:[/]([\d.]+(?:%)?))?\)/g;
68
+ const oklabColorPattern = /oklab\(([\d.]+(?:%)?|none)[|](-?[\d.]+(?:%)?|none)[|](-?[\d.]+(?:%)?|none)(?:[/]([\d.]+(?:%)?))?\)/g;
69
+ const labColorPattern = /lab\(([\d.]+(?:%)?|none)[|](-?[\d.]+(?:%)?|none)[|](-?[\d.]+(?:%)?|none)(?:[/]([\d.]+(?:%)?))?\)/g;
70
+ const cmykColorPattern = /device-cmyk\(([\d.]+(?:%)?|none)[|]([\d.]+(?:%)?|none)[|]([\d.]+(?:%)?|none)[|]([\d.]+(?:%)?|none)(?:[/]([\d.]+(?:%)?))?(?:,\w+\(.*\))?\)/g;
71
+ const hexColorPattern = /#([0-9a-fA-F]{6,8})/g;
72
+ const colorFunctionPattern = /color?\((?:srgb|srgb-linear|display-p3|a98-rgb|prophoto-rgb|rec2020|xyz|xyz-d50|xyz-d65)[|]([\d.]+)[|]([\d.]+)[|]([\d.]+)(?:[/]([\d.]+(?:%)?))?\)/g;
73
+ let colorMatch2;
74
+ //#region for rgb、hls
75
+ if (colorMatch2 = rgbaColorPattern.exec(colorString)) {
76
+ return getColorValue({
77
+ red: Number(colorMatch2[1]),
78
+ green: Number(colorMatch2[2]),
79
+ blue: Number(colorMatch2[3]),
80
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
81
+ });
82
+ } else if (colorMatch2 = hslaColorPattern.exec(colorString)) {
83
+ const rgb = colorConvert.hsl.rgb([
84
+ degConverter(colorMatch2[1]),
85
+ percentageConverter(colorMatch2[2], 100),
86
+ percentageConverter(colorMatch2[3], 100)
87
+ ]);
88
+ return getColorValue({
89
+ red: rgb[0],
90
+ green: rgb[1],
91
+ blue: rgb[2],
92
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
93
+ });
94
+ } else if (colorMatch2 = hwbColorPattern.exec(colorString)) {
95
+ const rgb = colorConvert.hwb.rgb([
96
+ degConverter(colorMatch2[1]),
97
+ percentageConverter(colorMatch2[2], 100),
98
+ percentageConverter(colorMatch2[3], 100)
99
+ ]);
100
+ return getColorValue({
101
+ red: rgb[0],
102
+ green: rgb[1],
103
+ blue: rgb[2],
104
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
105
+ });
106
+ } else if (colorMatch2 = oklchColorPattern.exec(colorString)) {
107
+ const oklab = oklchToOklab.oklchToOklab([
108
+ percentageConverter(colorMatch2[1]),
109
+ percentageConverter(colorMatch2[2], 0.4),
110
+ degConverter(colorMatch2[3])
111
+ ]);
112
+ const rgb = oklabToRgb.oklabToRgb(oklab);
113
+ return getColorValue({
114
+ red: rgb[0],
115
+ green: rgb[1],
116
+ blue: rgb[2],
117
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
118
+ });
119
+ } else if (colorMatch2 = lchColorPattern.exec(colorString)) {
120
+ const rgb = colorConvert.lch.rgb([
121
+ percentageConverter(colorMatch2[1], 100),
122
+ percentageConverter(colorMatch2[2], 150),
123
+ degConverter(colorMatch2[3])
124
+ ]);
125
+ return getColorValue({
126
+ red: rgb[0],
127
+ green: rgb[1],
128
+ blue: rgb[2],
129
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
130
+ });
131
+ } else if (colorMatch2 = oklabColorPattern.exec(colorString)) {
132
+ const rgb = oklabToRgb.oklabToRgb([
133
+ percentageConverter(colorMatch2[1]),
134
+ percentageConverter(colorMatch2[2], 0.4),
135
+ percentageConverter(colorMatch2[3], 0.4)
136
+ ]);
137
+ return getColorValue({
138
+ red: rgb[0],
139
+ green: rgb[1],
140
+ blue: rgb[2],
141
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
142
+ });
143
+ } else if (colorMatch2 = labColorPattern.exec(colorString)) {
144
+ const rgb = colorConvert.lab.rgb([
145
+ percentageConverter(colorMatch2[1], 100),
146
+ percentageConverter(colorMatch2[2], 125),
147
+ percentageConverter(colorMatch2[3], 125)
148
+ ]);
149
+ return getColorValue({
150
+ red: rgb[0],
151
+ green: rgb[1],
152
+ blue: rgb[2],
153
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
154
+ });
155
+ } else if (colorMatch2 = cmykColorPattern.exec(colorString)) {
156
+ const rgb = colorConvert.cmyk.rgb([
157
+ percentageConverter(colorMatch2[1], 100),
158
+ percentageConverter(colorMatch2[2], 100),
159
+ percentageConverter(colorMatch2[3], 100),
160
+ percentageConverter(colorMatch2[4], 100)
161
+ ]);
162
+ return getColorValue({
163
+ red: rgb[0],
164
+ green: rgb[1],
165
+ blue: rgb[2],
166
+ alpha: colorMatch2[5] == undefined ? 1 : percentageConverter(colorMatch2[5])
167
+ });
168
+ } else if (colorMatch2 = hexColorPattern.exec(colorString)) {
169
+ return getColorValue(hexToRgb.hexToRgb(colorMatch2[1]));
170
+ } else if (colorMatch2 = colorFunctionPattern.exec(colorString)) {
171
+ return getColorValue({
172
+ red: Number(colorMatch2[1]) * 255,
173
+ green: Number(colorMatch2[2]) * 255,
174
+ blue: Number(colorMatch2[3]) * 255,
175
+ alpha: colorMatch2[4] == undefined ? 1 : percentageConverter(colorMatch2[4])
176
+ });
177
+ }
178
+ //#endregion for rgb、hls
179
+ //#region for mastercss color
180
+ let colorAlpha = 1;
181
+ let colorName = colorString;
182
+ const allCSSColorNames = [];
183
+ // todo: refactor css.variables
184
+ for(const eachVariableName in css$1.variables){
185
+ const eachVariable = css$1.variables[eachVariableName];
186
+ if (eachVariable.type === 'color') allCSSColorNames.push(eachVariableName);
187
+ }
188
+ if (colorString.split('/').length == 2) {
189
+ colorAlpha = Number('0' + colorString.split('/')[1]);
190
+ }
191
+ if (colorString.split('-').length == 1) {
192
+ colorName = colorString.split('/')[0];
193
+ colorName = colorName.endsWith('_') ? colorName.replace('_', '') : colorName;
194
+ }
195
+ if (allCSSColorNames.find((x)=>x == colorName)) {
196
+ return getColorsRGBA(colorName, colorAlpha, theme, css$1);
197
+ }
198
+ //#endregion for mastercss color
199
+ }
200
+ function getColorsRGBA(colorName, colorAlpha = 1, theme = '', css$1 = new css.MasterCSS()) {
201
+ try {
202
+ // todo: refactor colorNumberMap
203
+ const colorNumberMap = css$1.variables[colorName];
204
+ const levelRgb = hexToRgb.hexToRgb(colorNumberMap[theme] ?? colorNumberMap[''] ?? Object.values(colorNumberMap)[0]);
205
+ return {
206
+ red: levelRgb.red / 255,
207
+ green: levelRgb.green / 255,
208
+ blue: levelRgb.blue / 255,
209
+ alpha: colorAlpha
210
+ };
211
+ } catch (ex) {
212
+ return {
213
+ red: 0,
214
+ green: 0,
215
+ blue: 0,
216
+ alpha: 1
217
+ };
218
+ }
219
+ }
220
+ function getColorValue(color) {
221
+ return {
222
+ red: color.red / 255.0,
223
+ green: color.green / 255.0,
224
+ blue: color.blue / 255.0,
225
+ alpha: color.alpha
226
+ };
227
+ }
228
+ function getColorPresentation(params, isColorRender = false) {
229
+ const result = [];
230
+ const color = params.color;
231
+ const range = params.range;
232
+ const red256 = Math.round(color.red * 255), green256 = Math.round(color.green * 255), blue256 = Math.round(color.blue * 255);
233
+ let label;
234
+ if (isColorRender) {
235
+ if (color.alpha === 1) {
236
+ label = `'#${toTwoDigitHex.toTwoDigitHex(red256)}${toTwoDigitHex.toTwoDigitHex(green256)}${toTwoDigitHex.toTwoDigitHex(blue256)}`;
237
+ } else {
238
+ label = `'#${toTwoDigitHex.toTwoDigitHex(red256)}${toTwoDigitHex.toTwoDigitHex(green256)}${toTwoDigitHex.toTwoDigitHex(blue256)}${toTwoDigitHex.toTwoDigitHex(Math.round(color.alpha * 255))}`;
239
+ }
240
+ result.push({
241
+ label: label,
242
+ textEdit: {
243
+ range: range,
244
+ newText: label
245
+ }
246
+ });
247
+ return result;
248
+ }
249
+ if (color.alpha === 1) {
250
+ label = `rgb(${red256},${green256},${blue256})`;
251
+ } else {
252
+ label = `rgba(${red256},${green256},${blue256},${color.alpha})`;
253
+ }
254
+ result.push({
255
+ label: label,
256
+ textEdit: {
257
+ range: range,
258
+ newText: label
259
+ }
260
+ });
261
+ if (color.alpha === 1) {
262
+ label = `#${toTwoDigitHex.toTwoDigitHex(red256)}${toTwoDigitHex.toTwoDigitHex(green256)}${toTwoDigitHex.toTwoDigitHex(blue256)}`;
263
+ } else {
264
+ label = `#${toTwoDigitHex.toTwoDigitHex(red256)}${toTwoDigitHex.toTwoDigitHex(green256)}${toTwoDigitHex.toTwoDigitHex(blue256)}${toTwoDigitHex.toTwoDigitHex(Math.round(color.alpha * 255))}`;
265
+ }
266
+ result.push({
267
+ label: label,
268
+ textEdit: {
269
+ range: range,
270
+ newText: label
271
+ }
272
+ });
273
+ const hsl = colorConvert.rgb.hsl(red256, green256, blue256);
274
+ if (color.alpha === 1) {
275
+ label = `hsl(${hsl[0]},${Math.round(hsl[1])}%,${Math.round(hsl[2])}%)`;
276
+ } else {
277
+ label = `hsla(${hsl[0]},${Math.round(hsl[1])}%,${Math.round(hsl[2])}%,${color.alpha})`;
278
+ }
279
+ result.push({
280
+ label: label,
281
+ textEdit: {
282
+ range: range,
283
+ newText: label
284
+ }
285
+ });
286
+ // const hwb = rgb.hwb(red256, green256, blue256)
287
+ // if (color.alpha === 1) {
288
+ // label = `hwb(${hwb[0]}|${hwb[1]}%|${hwb[2]}%)`
289
+ // } else {
290
+ // label = `hwb(${hwb[0]}|${hwb[1]}%|${hwb[2]}%/${color.alpha})`
291
+ // }
292
+ // result.push({ label: label, textEdit: { range: range, newText: label } })
293
+ // const lab = rgb.lab(red256, green256, blue256)
294
+ // if (color.alpha === 1) {
295
+ // label = `lab(${lab[0]}%|${lab[1]}|${lab[2]})`
296
+ // } else {
297
+ // label = `lab(${lab[0]}%|${lab[1]}|${lab[2]}/${color.alpha})`
298
+ // }
299
+ // result.push({ label: label, textEdit: { range: range, newText: label } })
300
+ // const lch = rgb.lch(red256, green256, blue256)
301
+ // if (color.alpha === 1) {
302
+ // label = `lch(${lch[0]}%|${lch[1]}|${lch[2]})`
303
+ // } else {
304
+ // label = `lch(${lch[0]}%|${lch[1]}|${lch[2]}/${color.alpha})`
305
+ // }
306
+ // result.push({ label: label, textEdit: { range: range, newText: label } })
307
+ // const cmyk = rgb.cmyk(red256, green256, blue256)
308
+ // if (color.alpha === 1) {
309
+ // label = `device-cmyk(${cmyk[0]}%|${cmyk[1]}%|${cmyk[2]}%|${cmyk[3]}%)`
310
+ // } else {
311
+ // label = `device-cmyk(${cmyk[0]}%|${cmyk[1]}%|${cmyk[2]}%|${cmyk[3]}%/${color.alpha})`
312
+ // }
313
+ // result.push({ label: label, textEdit: { range: range, newText: label } })
314
+ return result;
315
+ }
316
+
317
+ exports.getColorPresentation = getColorPresentation;
318
+ exports.getColorValue = getColorValue;
319
+ exports.getDocumentColors = getDocumentColors;