@master/css-language-service 2.0.0-rc.7 → 2.0.0-rc.70
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/README.md +114 -0
- package/dist/common.d.ts +1 -0
- package/dist/common.mjs +1 -0
- package/dist/core.d.ts +30 -0
- package/dist/core.mjs +114 -0
- package/dist/features/edit-syntax-colors.d.ts +4 -0
- package/dist/features/edit-syntax-colors.mjs +17 -0
- package/dist/features/inspect-syntax.d.ts +4 -0
- package/dist/features/inspect-syntax.mjs +54 -0
- package/dist/features/render-semantic-tokens.d.ts +5 -0
- package/dist/features/render-semantic-tokens.mjs +10 -0
- package/dist/features/render-syntax-colors.d.ts +4 -0
- package/dist/features/render-syntax-colors.mjs +102 -0
- package/dist/features/suggest-syntax.d.ts +5 -0
- package/dist/features/suggest-syntax.mjs +18 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.mjs +7 -0
- package/dist/settings.d.ts +16 -9
- package/dist/settings.mjs +53 -0
- package/dist/utils/beautify-css.d.ts +1 -0
- package/dist/utils/beautify-css.mjs +11 -0
- package/dist/utils/completion-index.d.ts +38 -0
- package/dist/utils/completion-index.mjs +268 -0
- package/dist/utils/convert-color-token.d.ts +4 -0
- package/dist/utils/convert-color-token.mjs +55 -0
- package/dist/utils/create-css-markdown-documentation.d.ts +5 -0
- package/dist/utils/create-css-markdown-documentation.mjs +15 -0
- package/dist/utils/create-doc.d.ts +16 -0
- package/dist/utils/create-doc.mjs +21 -0
- package/dist/utils/get-class-completion-items.d.ts +4 -0
- package/dist/utils/get-class-completion-items.mjs +20 -0
- package/dist/utils/get-color-completion-items.d.ts +3 -0
- package/dist/utils/get-color-completion-items.mjs +24 -0
- package/dist/utils/get-position.d.ts +1 -0
- package/dist/utils/get-position.mjs +10 -0
- package/dist/utils/get-pseudo-class-completion-items.d.ts +4 -0
- package/dist/utils/get-pseudo-class-completion-items.mjs +68 -0
- package/dist/utils/get-pseudo-element-completion-items.d.ts +4 -0
- package/dist/utils/get-pseudo-element-completion-items.mjs +43 -0
- package/dist/utils/get-query-completion-items.d.ts +4 -0
- package/dist/utils/get-query-completion-items.mjs +77 -0
- package/dist/utils/get-range.d.ts +2 -0
- package/dist/utils/get-range.mjs +21 -0
- package/dist/utils/get-utility-info.d.ts +4 -0
- package/dist/utils/get-utility-info.mjs +19 -0
- package/dist/utils/get-value-completion-items.d.ts +4 -0
- package/dist/utils/get-value-completion-items.mjs +203 -0
- package/dist/utils/query-syntax-completions.d.ts +4 -0
- package/dist/utils/query-syntax-completions.mjs +94 -0
- package/dist/utils/regex.mjs +3 -0
- package/dist/utils/sort-completion-items.d.ts +2 -0
- package/dist/utils/sort-completion-items.mjs +11 -0
- package/package.json +1 -1
- package/dist/constant.d.ts +0 -33
- package/dist/index.bundle.js +0 -33
- package/dist/position-check.d.ts +0 -20
- package/dist/providers/color.d.ts +0 -5
- package/dist/providers/completion.d.ts +0 -14
- package/dist/providers/hover.d.ts +0 -2
- package/dist/settings.bundle.js +0 -1
- package/dist/utils/get-css-entry-markdown-description.d.ts +0 -1
- package/dist/utils/hex-to-rgb.d.ts +0 -2
- package/dist/utils/oklab-to-rgb.d.ts +0 -1
- package/dist/utils/oklch-to-oklab.d.ts +0 -1
- package/dist/utils/to-two-digit-hex.d.ts +0 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AT_SIGN, QUERY_COMPARISON_OPERATORS, generateCSS, QUERY_LOGICAL_OPERATORS, generateAt, parseAt, createDefaultCSS, getSingleAtNumberRuleNode } from '@master/css-language';
|
|
2
|
+
import { CompletionItemKind } from 'vscode-languageserver-protocol';
|
|
3
|
+
import sortCompletionItems from './sort-completion-items.mjs';
|
|
4
|
+
import createCSSMarkdownDocumentation from './create-css-markdown-documentation.mjs';
|
|
5
|
+
import { createCompletionIndex } from './completion-index.mjs';
|
|
6
|
+
|
|
7
|
+
function getQueryCompletionItems(css = createDefaultCSS(), triggerCharacter = AT_SIGN, syntax, completionIndex = createCompletionIndex(css)) {
|
|
8
|
+
const completionItems = [];
|
|
9
|
+
if (!QUERY_COMPARISON_OPERATORS.includes(triggerCharacter)) {
|
|
10
|
+
[
|
|
11
|
+
'media',
|
|
12
|
+
'container',
|
|
13
|
+
'supports'
|
|
14
|
+
].forEach((name)=>{
|
|
15
|
+
completionItems.push({
|
|
16
|
+
label: triggerCharacter + name + '()',
|
|
17
|
+
filterText: name + '()',
|
|
18
|
+
insertText: name + '()',
|
|
19
|
+
sortText: name + '()',
|
|
20
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
21
|
+
syntax + name + '()'
|
|
22
|
+
], css))
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
const handleNumberNodes = (atRule, token)=>{
|
|
27
|
+
const numberNode = getSingleAtNumberRuleNode(atRule.nodes);
|
|
28
|
+
if (numberNode) {
|
|
29
|
+
const prevComparisonCharacter = syntax.charAt(syntax.length - 2);
|
|
30
|
+
const comparisonCharacter = prevComparisonCharacter === '>' || prevComparisonCharacter === '<' ? prevComparisonCharacter + triggerCharacter : triggerCharacter;
|
|
31
|
+
const text = generateAt(parseAt(comparisonCharacter + token, css));
|
|
32
|
+
completionItems.push({
|
|
33
|
+
label: comparisonCharacter + token,
|
|
34
|
+
sortText: CompletionItemKind.Keyword + comparisonCharacter + String(Math.round(numberNode.value)).padStart(12, '0'),
|
|
35
|
+
filterText: token,
|
|
36
|
+
insertText: token,
|
|
37
|
+
detail: text,
|
|
38
|
+
kind: CompletionItemKind.Keyword,
|
|
39
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
40
|
+
syntax + token
|
|
41
|
+
], css))
|
|
42
|
+
});
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
for (const [token, atRule] of completionIndex.atRules){
|
|
47
|
+
const completionItem = {
|
|
48
|
+
filterText: token,
|
|
49
|
+
insertText: token,
|
|
50
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
51
|
+
syntax + token
|
|
52
|
+
], css))
|
|
53
|
+
};
|
|
54
|
+
if ([
|
|
55
|
+
AT_SIGN,
|
|
56
|
+
...QUERY_LOGICAL_OPERATORS
|
|
57
|
+
].includes(triggerCharacter)) {
|
|
58
|
+
if (handleNumberNodes(atRule, token)) continue;
|
|
59
|
+
const text = generateAt(parseAt(triggerCharacter + token, css));
|
|
60
|
+
completionItems.push({
|
|
61
|
+
...completionItem,
|
|
62
|
+
label: triggerCharacter + token,
|
|
63
|
+
detail: text,
|
|
64
|
+
sortText: CompletionItemKind.Keyword + token,
|
|
65
|
+
kind: CompletionItemKind.Keyword,
|
|
66
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
67
|
+
syntax + token
|
|
68
|
+
], css))
|
|
69
|
+
});
|
|
70
|
+
} else if (QUERY_COMPARISON_OPERATORS.includes(triggerCharacter)) {
|
|
71
|
+
handleNumberNodes(atRule, token);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return sortCompletionItems(completionItems);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { getQueryCompletionItems as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function getRange(searchString, doc) {
|
|
2
|
+
const text = doc.getText();
|
|
3
|
+
const regex = new RegExp(`(\\b(?:className|class)\\s*=\\s*(['"\`]))(.*?)\\2`, 'gs');
|
|
4
|
+
let match;
|
|
5
|
+
while((match = regex.exec(text)) !== null){
|
|
6
|
+
match[0];
|
|
7
|
+
const attrStartIndex = match.index + match[1].length;
|
|
8
|
+
const className = match[3];
|
|
9
|
+
const relativeIndex = className.indexOf(searchString);
|
|
10
|
+
if (relativeIndex !== -1) {
|
|
11
|
+
const absoluteIndex = attrStartIndex + relativeIndex;
|
|
12
|
+
return {
|
|
13
|
+
start: doc.positionAt(absoluteIndex),
|
|
14
|
+
end: doc.positionAt(absoluteIndex + searchString.length)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
throw new Error(`Cannot find "${searchString}" in any class attribute.`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { getRange as default };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getStaticUtilityDeclarations } from '@master/css-language';
|
|
2
|
+
|
|
3
|
+
function getUtilityInfo(utility) {
|
|
4
|
+
const declarations = getStaticUtilityDeclarations(utility);
|
|
5
|
+
const propsLength = Object.keys(declarations || {}).length;
|
|
6
|
+
const propName = Object.keys(declarations || {})[0];
|
|
7
|
+
const propValue = declarations?.[propName];
|
|
8
|
+
let detail;
|
|
9
|
+
/**
|
|
10
|
+
* Remaps to native CSS properties when only one property is declared
|
|
11
|
+
* */ if (propsLength === 1) {
|
|
12
|
+
detail = `${propName}: ${propValue}`;
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
detail
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { getUtilityInfo as default };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type CompletionItem } from 'vscode-languageserver-protocol';
|
|
2
|
+
import { MasterCSS } from '@master/css-language';
|
|
3
|
+
import { type CompletionIndex } from './completion-index';
|
|
4
|
+
export default function getValueCompletionItems(css: MasterCSS | undefined, ruleKey: string, valuePrefix?: string, completionIndex?: CompletionIndex): CompletionItem[];
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { CompletionItemKind } from 'vscode-languageserver-protocol';
|
|
2
|
+
import { getMdnPropertySyntax, getMdnPropertyValueNames, generateCSS, createDefaultCSS } from '@master/css-language';
|
|
3
|
+
import { builtinKeyAliases } from '@master/css-engine';
|
|
4
|
+
import createCSSMarkdownDocumentation from './create-css-markdown-documentation.mjs';
|
|
5
|
+
import sortCompletionItems from './sort-completion-items.mjs';
|
|
6
|
+
import { createCompletionIndex } from './completion-index.mjs';
|
|
7
|
+
|
|
8
|
+
const SCOPED_VARIABLE_PRIORITY = 'aaaa';
|
|
9
|
+
const NATIVE_PRIORITY = 'ccccc';
|
|
10
|
+
const GLOBAL_VARIABLE_PRIORITY = 'zzzz';
|
|
11
|
+
const ANIMATION_REFERENCE_PROPERTIES = new Set([
|
|
12
|
+
'animation',
|
|
13
|
+
'animation-name'
|
|
14
|
+
]);
|
|
15
|
+
function getNumericSortValue(variable, rootSize) {
|
|
16
|
+
if (variable.numeric) {
|
|
17
|
+
switch(variable.numeric.unit){
|
|
18
|
+
case 'rem':
|
|
19
|
+
return variable.numeric.value * rootSize;
|
|
20
|
+
case undefined:
|
|
21
|
+
case '':
|
|
22
|
+
case 'px':
|
|
23
|
+
return variable.numeric.value;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return typeof variable.value === 'number' ? variable.value : 0;
|
|
27
|
+
}
|
|
28
|
+
function getVariableKeyByNamespace(variableName, namespace) {
|
|
29
|
+
const negative = variableName.startsWith('-');
|
|
30
|
+
const positiveName = negative ? variableName.slice(1) : variableName;
|
|
31
|
+
if (positiveName !== namespace && !positiveName.startsWith(namespace + '-')) return;
|
|
32
|
+
const key = positiveName === namespace ? '' : positiveName.slice(namespace.length + 1);
|
|
33
|
+
return negative ? '-' + key : key;
|
|
34
|
+
}
|
|
35
|
+
function getValueCompletionItems(css = createDefaultCSS(), ruleKey, valuePrefix = '', completionIndex = createCompletionIndex(css)) {
|
|
36
|
+
const completionItems = [];
|
|
37
|
+
const addedLabels = new Set();
|
|
38
|
+
const canonicalRuleKey = builtinKeyAliases[ruleKey] || ruleKey;
|
|
39
|
+
const nativeUtilityKey = completionIndex.nativeUtilityKeys.get(canonicalRuleKey);
|
|
40
|
+
const nativeKey = nativeUtilityKey || (getMdnPropertySyntax(canonicalRuleKey) ? canonicalRuleKey : undefined);
|
|
41
|
+
const nativePropertyValues = getMdnPropertyValueNames(nativeKey);
|
|
42
|
+
const pushCompletionItem = (completionItem)=>{
|
|
43
|
+
completionItems.push(completionItem);
|
|
44
|
+
addedLabels.add(completionItem.label);
|
|
45
|
+
};
|
|
46
|
+
const generateVariableCompletionItem = (variable, { appliedValue, label, negative, scoped } = {})=>{
|
|
47
|
+
const nativePropertySyntax = getMdnPropertySyntax(variable.namespace) || getMdnPropertySyntax(nativeKey);
|
|
48
|
+
if (variable.type === 'number' && variable.name.startsWith('-') && nativePropertySyntax?.includes('absolute')) return;
|
|
49
|
+
const valueToken = appliedValue ?? (scoped ? variable.key : variable.name);
|
|
50
|
+
const documentation = createCSSMarkdownDocumentation(generateCSS([
|
|
51
|
+
ruleKey + ':' + valueToken
|
|
52
|
+
], css));
|
|
53
|
+
const completionItemLabel = label ?? variable.name;
|
|
54
|
+
const conflicted = addedLabels.has(completionItemLabel);
|
|
55
|
+
const completionItem = {
|
|
56
|
+
label: conflicted ? 'var(--' + completionItemLabel + ')' : completionItemLabel,
|
|
57
|
+
kind: CompletionItemKind.Value
|
|
58
|
+
};
|
|
59
|
+
if (conflicted) {
|
|
60
|
+
completionItem.documentation = createCSSMarkdownDocumentation(generateCSS([
|
|
61
|
+
ruleKey + ':' + completionItem.label
|
|
62
|
+
], css));
|
|
63
|
+
} else {
|
|
64
|
+
completionItem.documentation = createCSSMarkdownDocumentation(generateCSS([
|
|
65
|
+
ruleKey + ':' + valueToken
|
|
66
|
+
], css));
|
|
67
|
+
}
|
|
68
|
+
if (variable.namespace?.startsWith('color')) {
|
|
69
|
+
if (Object.keys(variable.modes || {}).length) {
|
|
70
|
+
completionItem.kind = CompletionItemKind.Color;
|
|
71
|
+
completionItem.detail = variable.name;
|
|
72
|
+
} else {
|
|
73
|
+
// todo: packages/core should support getTextByVariable(variable)
|
|
74
|
+
const configKey = 'variables.' + (variable.namespace ? variable.namespace + '.' + variable.key : variable.name);
|
|
75
|
+
const valueToken = variable.value ?? variable.name;
|
|
76
|
+
// detail is shown in the detail pane
|
|
77
|
+
// todo: variable.token should be recorded as original config variable
|
|
78
|
+
completionItem.detail = String(configKey);
|
|
79
|
+
completionItem.documentation = {
|
|
80
|
+
kind: 'markdown',
|
|
81
|
+
value: valueToken + '\n\n' + documentation?.value
|
|
82
|
+
};
|
|
83
|
+
completionItem.kind = CompletionItemKind.Color;
|
|
84
|
+
completionItem.sortText = 'color-' + variable.name.replace(/(.+?)-(\d+)/, (match, prefix, num)=>prefix + num.padStart(10, '0'));
|
|
85
|
+
}
|
|
86
|
+
} else if (variable.type === 'number') {
|
|
87
|
+
completionItem.detail = String(variable.name);
|
|
88
|
+
const value = getNumericSortValue(variable, css.settings.rootSize);
|
|
89
|
+
const sortValue = negative ? -Math.abs(value) : value;
|
|
90
|
+
completionItem.sortText = (variable.namespace || '') + (sortValue >= 0 ? String(sortValue).padStart(10, '0') : '-' + String(Math.abs(sortValue)).padStart(10, '0'));
|
|
91
|
+
} else {
|
|
92
|
+
completionItem.detail = String(variable.value || variable.name);
|
|
93
|
+
}
|
|
94
|
+
return completionItem;
|
|
95
|
+
};
|
|
96
|
+
const addScopedVariableCompletionItem = (variable, variableName)=>{
|
|
97
|
+
if (addedLabels.has(variableName)) return;
|
|
98
|
+
const completionItem = generateVariableCompletionItem(variable, {
|
|
99
|
+
scoped: true
|
|
100
|
+
});
|
|
101
|
+
if (completionItem) {
|
|
102
|
+
completionItem.label = variableName;
|
|
103
|
+
completionItem.sortText = SCOPED_VARIABLE_PRIORITY + (completionItem.sortText || variableName);
|
|
104
|
+
completionItem.detail = '(scope) ' + completionItem.detail;
|
|
105
|
+
pushCompletionItem(completionItem);
|
|
106
|
+
}
|
|
107
|
+
const negativeVariableName = '-' + variableName;
|
|
108
|
+
if (valuePrefix.startsWith('-') && variableName[0] !== '-' && variable.type === 'number' && !addedLabels.has(negativeVariableName)) {
|
|
109
|
+
const negativeCompletionItem = generateVariableCompletionItem(variable, {
|
|
110
|
+
appliedValue: negativeVariableName,
|
|
111
|
+
label: negativeVariableName,
|
|
112
|
+
negative: true,
|
|
113
|
+
scoped: true
|
|
114
|
+
});
|
|
115
|
+
if (negativeCompletionItem) {
|
|
116
|
+
negativeCompletionItem.sortText = SCOPED_VARIABLE_PRIORITY + (negativeCompletionItem.sortText || negativeVariableName);
|
|
117
|
+
negativeCompletionItem.detail = '(scope) ' + negativeCompletionItem.detail;
|
|
118
|
+
pushCompletionItem(negativeCompletionItem);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Scoped variables
|
|
124
|
+
* @example box: + content -> box-sizing:content
|
|
125
|
+
*/ for (const { name, variable } of completionIndex.scopedVariablesByKey.get(canonicalRuleKey) || []){
|
|
126
|
+
addScopedVariableCompletionItem(variable, name);
|
|
127
|
+
}
|
|
128
|
+
for (const { value } of completionIndex.patternValuesByKey.get(canonicalRuleKey) || []){
|
|
129
|
+
if (addedLabels.has(value)) continue;
|
|
130
|
+
pushCompletionItem({
|
|
131
|
+
label: value,
|
|
132
|
+
kind: CompletionItemKind.Value,
|
|
133
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
134
|
+
ruleKey + ':' + value
|
|
135
|
+
], css)),
|
|
136
|
+
detail: canonicalRuleKey + ': ' + value
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* @example animation:fade|fast animate:fade animation-name:fade
|
|
141
|
+
*/ for (const animationUtility of completionIndex.animationUtilitiesByKey.get(canonicalRuleKey) || []){
|
|
142
|
+
css.animations.forEach((_, animationName)=>{
|
|
143
|
+
pushCompletionItem({
|
|
144
|
+
label: animationName,
|
|
145
|
+
kind: CompletionItemKind.Value,
|
|
146
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
147
|
+
ruleKey + ':' + animationName
|
|
148
|
+
], css)),
|
|
149
|
+
detail: animationUtility.detailPrefix ? animationUtility.detailPrefix + ': ' + animationName : animationName
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (ANIMATION_REFERENCE_PROPERTIES.has(canonicalRuleKey)) {
|
|
154
|
+
css.animations.forEach((_, animationName)=>{
|
|
155
|
+
if (addedLabels.has(animationName)) return;
|
|
156
|
+
pushCompletionItem({
|
|
157
|
+
label: animationName,
|
|
158
|
+
kind: CompletionItemKind.Value,
|
|
159
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
160
|
+
ruleKey + ':' + animationName
|
|
161
|
+
], css)),
|
|
162
|
+
detail: canonicalRuleKey + ': ' + animationName
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
const usedKeys = new Set();
|
|
167
|
+
for (const variableNamespace of completionIndex.nativeVariableNamespacesByProperty.get(canonicalRuleKey) || []){
|
|
168
|
+
for (const variable of completionIndex.variables){
|
|
169
|
+
const variableName = getVariableKeyByNamespace(variable.name, variableNamespace);
|
|
170
|
+
if (variableName === undefined || usedKeys.has(variableName)) continue;
|
|
171
|
+
usedKeys.add(variableName);
|
|
172
|
+
addScopedVariableCompletionItem(variable, variableName);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Native values
|
|
177
|
+
*/ if (nativeKey) {
|
|
178
|
+
nativePropertyValues.forEach((value)=>{
|
|
179
|
+
if (addedLabels.has(value) || nativeKey === 'font' && typeof +value === 'number' || value.includes(' ')) return;
|
|
180
|
+
pushCompletionItem({
|
|
181
|
+
label: value,
|
|
182
|
+
kind: CompletionItemKind.Value,
|
|
183
|
+
sortText: NATIVE_PRIORITY + (value.startsWith('-') ? 'zz' + value.slice(1) : value),
|
|
184
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
185
|
+
ruleKey + ':' + value
|
|
186
|
+
], css)),
|
|
187
|
+
detail: nativeKey + ': ' + value
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
// global variables
|
|
192
|
+
completionIndex.variables.forEach((variable)=>{
|
|
193
|
+
const completionItem = generateVariableCompletionItem(variable);
|
|
194
|
+
if (completionItem) {
|
|
195
|
+
completionItem.sortText = GLOBAL_VARIABLE_PRIORITY + (completionItem.sortText || completionItem.label);
|
|
196
|
+
completionItem.detail = '(global) ' + completionItem.detail;
|
|
197
|
+
pushCompletionItem(completionItem);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
return sortCompletionItems(completionItems);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export { getValueCompletionItems as default };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type CompletionItem } from 'vscode-languageserver-protocol';
|
|
2
|
+
import { MasterCSS } from '@master/css-language';
|
|
3
|
+
import { type CompletionIndex } from './completion-index';
|
|
4
|
+
export default function querySyntaxCompletions(q?: string, css?: MasterCSS, completionIndex?: CompletionIndex): CompletionItem[] | undefined;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { CompletionItemKind } from 'vscode-languageserver-protocol';
|
|
2
|
+
import getPseudoClassCompletionItems from './get-pseudo-class-completion-items.mjs';
|
|
3
|
+
import getPseudoElementCompletionItems from './get-pseudo-element-completion-items.mjs';
|
|
4
|
+
import { GROUP_TRIGGER_CHARACTER, AT_SIGN, SELECTOR_SIGNS, QUERY_COMPARISON_OPERATORS, QUERY_LOGICAL_OPERATORS, SELECTOR_TRIGGER_CHARACTERS, generateCSS, createDefaultCSS } from '@master/css-language';
|
|
5
|
+
import getClassCompletionItems from './get-class-completion-items.mjs';
|
|
6
|
+
import getValueCompletionItems from './get-value-completion-items.mjs';
|
|
7
|
+
import getQueryCompletionItems from './get-query-completion-items.mjs';
|
|
8
|
+
import createCSSMarkdownDocumentation from './create-css-markdown-documentation.mjs';
|
|
9
|
+
import { matchesIndexedName, createCompletionIndex } from './completion-index.mjs';
|
|
10
|
+
|
|
11
|
+
function querySyntaxCompletions(q = '', css = createDefaultCSS(), completionIndex = createCompletionIndex(css)) {
|
|
12
|
+
const fields = q.split(' ');
|
|
13
|
+
let field = fields[fields.length - 1];
|
|
14
|
+
const triggerCharacter = q.charAt(q.length - 1);
|
|
15
|
+
const invoked = triggerCharacter === ' ' || field.length === 0;
|
|
16
|
+
if (invoked || field === GROUP_TRIGGER_CHARACTER || triggerCharacter === ';') {
|
|
17
|
+
return getClassCompletionItems(css, completionIndex);
|
|
18
|
+
}
|
|
19
|
+
const isGroup = field.startsWith(GROUP_TRIGGER_CHARACTER);
|
|
20
|
+
if (isGroup) {
|
|
21
|
+
const declarationSeparatorMatches = Array.from(field.matchAll(/;(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/g));
|
|
22
|
+
if (declarationSeparatorMatches.length) {
|
|
23
|
+
field = field.slice(declarationSeparatorMatches[declarationSeparatorMatches.length - 1].index + 1);
|
|
24
|
+
} else {
|
|
25
|
+
field = field.slice(1);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (field.startsWith(AT_SIGN) || field.startsWith('~')) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const keyMatch = field.match(/^[^'":\s]+:/);
|
|
32
|
+
const atMatches = Array.from(field.matchAll(/@(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/g));
|
|
33
|
+
const valueSeparatorMatch = field.match(/\|(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/g);
|
|
34
|
+
const atInvoked = atMatches.findIndex(({ index })=>index !== 0) !== -1;
|
|
35
|
+
const firstColonIndex = keyMatch ? keyMatch[0].length - 1 : -1;
|
|
36
|
+
const selectorInvokedRegex = new RegExp(`[${SELECTOR_SIGNS.join('')}](?=(?:[^'"]|'[^']*'|"[^"]*")*$)`);
|
|
37
|
+
const key = keyMatch ? keyMatch[0].slice(0, firstColonIndex) : undefined;
|
|
38
|
+
const isStyle = completionIndex.componentNames.some((eachStyleName)=>matchesIndexedName(field, eachStyleName));
|
|
39
|
+
const isUtility = completionIndex.utilityNames.some((eachUtilityName)=>matchesIndexedName(field, eachUtilityName));
|
|
40
|
+
// check by utilities and components
|
|
41
|
+
if (!isStyle && !isUtility) {
|
|
42
|
+
if (key === undefined && !valueSeparatorMatch) {
|
|
43
|
+
return getClassCompletionItems(css, completionIndex);
|
|
44
|
+
}
|
|
45
|
+
if (!atInvoked && !selectorInvokedRegex.test(field.slice(firstColonIndex + 1))) {
|
|
46
|
+
if (key && firstColonIndex !== -1) {
|
|
47
|
+
return getValueCompletionItems(css, key, field.slice(firstColonIndex + 1), completionIndex);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Not support conditional btn:hover and group
|
|
53
|
+
*/ if (isStyle || isGroup) return;
|
|
54
|
+
if (atInvoked && [
|
|
55
|
+
AT_SIGN,
|
|
56
|
+
...QUERY_COMPARISON_OPERATORS,
|
|
57
|
+
...QUERY_LOGICAL_OPERATORS
|
|
58
|
+
].includes(triggerCharacter)) {
|
|
59
|
+
return getQueryCompletionItems(css, triggerCharacter, field, completionIndex);
|
|
60
|
+
}
|
|
61
|
+
if (!atInvoked && SELECTOR_TRIGGER_CHARACTERS.includes(triggerCharacter)) {
|
|
62
|
+
const pseudoElementCompletionItems = getPseudoElementCompletionItems(css, field, completionIndex);
|
|
63
|
+
if (field.endsWith('::')) {
|
|
64
|
+
/**
|
|
65
|
+
* Consider trigger characters and fix insertText.
|
|
66
|
+
* @incorrect class="text-center::" -> class="text-center::::after"
|
|
67
|
+
* @correct class="text-center::" -> class="text-center::after"
|
|
68
|
+
*/ pseudoElementCompletionItems.forEach((completionItem)=>completionItem.insertText = completionItem.label.slice(2));
|
|
69
|
+
return pseudoElementCompletionItems;
|
|
70
|
+
} else {
|
|
71
|
+
const completionItems = getPseudoClassCompletionItems(css, field, completionIndex);
|
|
72
|
+
completionItems.push(...pseudoElementCompletionItems);
|
|
73
|
+
/**
|
|
74
|
+
* Consider trigger characters and fix insertText.
|
|
75
|
+
* @incorrect class="text-center:" -> class="text-center::active"
|
|
76
|
+
* @correct class="text-center:" -> class="text-center:active"
|
|
77
|
+
*/ if (triggerCharacter === ':') {
|
|
78
|
+
completionItems.forEach((completionItem)=>completionItem.insertText = completionItem.label.slice(1));
|
|
79
|
+
} else {
|
|
80
|
+
completionItems.push({
|
|
81
|
+
label: '.<class>',
|
|
82
|
+
insertText: '.',
|
|
83
|
+
documentation: createCSSMarkdownDocumentation(generateCSS([
|
|
84
|
+
field + '.class'
|
|
85
|
+
], css)),
|
|
86
|
+
kind: CompletionItemKind.Class
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return completionItems;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { querySyntaxCompletions as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* localeCompare equals to the real vscode completion items sorting */ function sortCompletionItems(items) {
|
|
2
|
+
return items.sort((a, b)=>{
|
|
3
|
+
const sortTextA = a.sortText || a.label;
|
|
4
|
+
const sortTextB = b.sortText || b.label;
|
|
5
|
+
return sortTextA.localeCompare(sortTextB, undefined, {
|
|
6
|
+
numeric: true
|
|
7
|
+
});
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { sortCompletionItems as default };
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@master/css-language-service","scripts":{"build":"techor
|
|
1
|
+
{"name":"@master/css-language-service","type":"module","scripts":{"build":"techor build \"src/**/*.ts\" --formats esm","dev":"pnpm build --watch","type-check":"tsc --noEmit","lint":"eslint","test":"vitest"},"description":"Master CSS Language Service","author":"Aoyue Design LLC.","license":"MIT","repository":{"type":"git","url":"https://github.com/master-co/css.git","directory":"packages/language-service"},"dependencies":{"@master/css":"^2.0.0-rc.70","@master/css-engine":"^2.0.0-rc.70","@master/css-language":"^2.0.0-rc.70","@techor/extend":"^3.2.5","colorjs.io":"^0.6.1","js-beautify":"^1.15.4","minimatch":"^10.2.5","vscode-languageserver-protocol":"^3.18.0","vscode-languageserver-textdocument":"^1.0.12"},"devDependencies":{"@types/vscode":"^1.125.0","vscode-languageserver":"^10.0.0","@master/css-schema":"^2.0.0-rc.70"},"sideEffects":false,"main":"./dist/index.mjs","module":"./dist/index.mjs","jsnext:main":"./dist/index.mjs","esnext":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","default":"./dist/index.mjs"},"./common":{"types":"./dist/common.d.ts","import":"./dist/common.mjs","default":"./dist/common.mjs"}},"files":["dist"],"publishConfig":{"access":"public","provenance":true},"version":"2.0.0-rc.70"}
|
package/dist/constant.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { CompletionItem } from 'vscode-languageserver-types';
|
|
2
|
-
interface MasterCssKey {
|
|
3
|
-
key: string[];
|
|
4
|
-
colored: boolean;
|
|
5
|
-
values: Array<string | CompletionItem>;
|
|
6
|
-
}
|
|
7
|
-
export declare const masterCssSelectors: (string | {
|
|
8
|
-
label: string;
|
|
9
|
-
kind: number;
|
|
10
|
-
})[];
|
|
11
|
-
export declare const masterCssElements: (string | {
|
|
12
|
-
label: string;
|
|
13
|
-
kind: number;
|
|
14
|
-
})[];
|
|
15
|
-
export declare const masterCssMedia: (string | {
|
|
16
|
-
label: string;
|
|
17
|
-
kind: number;
|
|
18
|
-
})[];
|
|
19
|
-
export declare const masterCssOtherKeys: string[];
|
|
20
|
-
export declare const masterCssCommonValues: (string | {
|
|
21
|
-
label: string;
|
|
22
|
-
kind: number;
|
|
23
|
-
})[];
|
|
24
|
-
export declare const commonUnit: any;
|
|
25
|
-
export declare const masterCssKeyValues: MasterCssKey[];
|
|
26
|
-
export declare const masterCssType: {
|
|
27
|
-
type: string;
|
|
28
|
-
values: (string | {
|
|
29
|
-
label: string;
|
|
30
|
-
kind: number;
|
|
31
|
-
})[];
|
|
32
|
-
}[];
|
|
33
|
-
export {};
|