@master/css-language-service 2.0.0-rc.8 → 2.0.0-rc.80

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.
Files changed (69) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +129 -0
  3. package/dist/common.d.ts +2 -0
  4. package/dist/common.js +2 -0
  5. package/dist/core.d.ts +39 -0
  6. package/dist/core.js +111 -0
  7. package/dist/features/edit-syntax-colors.d.ts +8 -0
  8. package/dist/features/edit-syntax-colors.js +16 -0
  9. package/dist/features/format-directives.d.ts +7 -0
  10. package/dist/features/format-directives.js +72 -0
  11. package/dist/features/inspect-syntax.d.ts +8 -0
  12. package/dist/features/inspect-syntax.js +37 -0
  13. package/dist/features/render-semantic-tokens.d.ts +9 -0
  14. package/dist/features/render-semantic-tokens.js +10 -0
  15. package/dist/features/render-syntax-colors.d.ts +8 -0
  16. package/dist/features/render-syntax-colors.js +93 -0
  17. package/dist/features/suggest-syntax.d.ts +9 -0
  18. package/dist/features/suggest-syntax.js +11 -0
  19. package/dist/index.d.ts +8 -5
  20. package/dist/index.js +8 -0
  21. package/dist/settings.d.ts +20 -9
  22. package/dist/settings.js +59 -0
  23. package/dist/utils/beautify-css.d.ts +4 -0
  24. package/dist/utils/beautify-css.js +10 -0
  25. package/dist/utils/completion-index.d.ts +43 -0
  26. package/dist/utils/completion-index.js +196 -0
  27. package/dist/utils/convert-color-token.d.ts +8 -0
  28. package/dist/utils/convert-color-token.js +47 -0
  29. package/dist/utils/create-css-markdown-documentation.d.ts +8 -0
  30. package/dist/utils/create-css-markdown-documentation.js +13 -0
  31. package/dist/utils/create-doc.d.ts +20 -0
  32. package/dist/utils/create-doc.js +21 -0
  33. package/dist/utils/get-class-completion-items.d.ts +8 -0
  34. package/dist/utils/get-class-completion-items.js +15 -0
  35. package/dist/utils/get-color-completion-items.d.ts +7 -0
  36. package/dist/utils/get-color-completion-items.js +22 -0
  37. package/dist/utils/get-position.d.ts +4 -0
  38. package/dist/utils/get-position.js +9 -0
  39. package/dist/utils/get-pseudo-class-completion-items.d.ts +8 -0
  40. package/dist/utils/get-pseudo-class-completion-items.js +64 -0
  41. package/dist/utils/get-pseudo-element-completion-items.d.ts +8 -0
  42. package/dist/utils/get-pseudo-element-completion-items.js +39 -0
  43. package/dist/utils/get-query-completion-items.d.ts +8 -0
  44. package/dist/utils/get-query-completion-items.js +62 -0
  45. package/dist/utils/get-range.d.ts +6 -0
  46. package/dist/utils/get-range.js +21 -0
  47. package/dist/utils/get-utility-info.d.ts +8 -0
  48. package/dist/utils/get-utility-info.js +16 -0
  49. package/dist/utils/get-value-completion-items.d.ts +8 -0
  50. package/dist/utils/get-value-completion-items.js +165 -0
  51. package/dist/utils/query-syntax-completions.d.ts +8 -0
  52. package/dist/utils/query-syntax-completions.js +78 -0
  53. package/dist/utils/regex.d.ts +4 -1
  54. package/dist/utils/regex.js +4 -0
  55. package/dist/utils/sort-completion-items.d.ts +6 -0
  56. package/dist/utils/sort-completion-items.js +10 -0
  57. package/package.json +61 -1
  58. package/dist/constant.d.ts +0 -33
  59. package/dist/index.bundle.js +0 -33
  60. package/dist/position-check.d.ts +0 -20
  61. package/dist/providers/color.d.ts +0 -5
  62. package/dist/providers/completion.d.ts +0 -14
  63. package/dist/providers/hover.d.ts +0 -2
  64. package/dist/settings.bundle.js +0 -1
  65. package/dist/utils/get-css-entry-markdown-description.d.ts +0 -1
  66. package/dist/utils/hex-to-rgb.d.ts +0 -2
  67. package/dist/utils/oklab-to-rgb.d.ts +0 -1
  68. package/dist/utils/oklch-to-oklab.d.ts +0 -1
  69. package/dist/utils/to-two-digit-hex.d.ts +0 -1
@@ -0,0 +1,39 @@
1
+ import createCSSMarkdownDocumentation from "./create-css-markdown-documentation.js";
2
+ import sortCompletionItems from "./sort-completion-items.js";
3
+ import { createCompletionIndex } from "./completion-index.js";
4
+ import { createDefaultCSS, generateCSS } from "@master/css-language";
5
+ import { CompletionItemKind } from "vscode-languageserver-protocol";
6
+ //#region src/utils/get-pseudo-element-completion-items.ts
7
+ const kind = CompletionItemKind.Function;
8
+ function getPseudoElementCompletionItems(css = createDefaultCSS(), syntax, completionIndex = createCompletionIndex(css)) {
9
+ const completionItems = completionIndex.pseudoElementNames.map((pseudoElementName) => {
10
+ const name = /::(?:part|slotted)/.test(pseudoElementName) ? pseudoElementName + "()" : pseudoElementName;
11
+ let sortText = name.startsWith("::-") ? "zzzz" + name.slice(3) : "zz" + name.replace(/^::/, "");
12
+ if (sortText.endsWith("()")) sortText = "z" + sortText;
13
+ return {
14
+ label: name,
15
+ sortText,
16
+ documentation: createCSSMarkdownDocumentation(generateCSS([syntax + name.slice(2)], css, completionIndex.runtime)),
17
+ kind
18
+ };
19
+ });
20
+ for (const variant of completionIndex.pseudoElementSelectors) {
21
+ const selectorName = variant.token;
22
+ const selectorValue = variant.selector.replace(/&/g, "");
23
+ const name = selectorName.endsWith("(") ? selectorName + ")" : selectorName;
24
+ const value = typeof selectorValue === "string" ? selectorValue.endsWith("(") ? selectorValue + ")" : selectorValue : selectorValue;
25
+ let sortText = name.startsWith("::-") ? "zzzz" + name.slice(3) : "zz" + name.replace(/^::/, "");
26
+ if (sortText.endsWith("()")) sortText = "z" + sortText;
27
+ const completionItem = {
28
+ label: name,
29
+ documentation: createCSSMarkdownDocumentation(generateCSS([syntax + name.slice(2)], css, completionIndex.runtime)),
30
+ sortText,
31
+ kind,
32
+ detail: String(value)
33
+ };
34
+ completionItems.push(completionItem);
35
+ }
36
+ return sortCompletionItems(completionItems);
37
+ }
38
+ //#endregion
39
+ export { getPseudoElementCompletionItems as default };
@@ -0,0 +1,8 @@
1
+ import { MasterCSS } from "@master/css-language";
2
+ import { CompletionItem } from "vscode-languageserver-protocol";
3
+ import { CompletionIndex } from "./completion-index.js";
4
+
5
+ //#region src/utils/get-query-completion-items.d.ts
6
+ declare function getQueryCompletionItems(css: MasterCSS | undefined, triggerCharacter: string | undefined, syntax: string, completionIndex?: CompletionIndex): CompletionItem[];
7
+ //#endregion
8
+ export { getQueryCompletionItems as default };
@@ -0,0 +1,62 @@
1
+ import createCSSMarkdownDocumentation from "./create-css-markdown-documentation.js";
2
+ import sortCompletionItems from "./sort-completion-items.js";
3
+ import { createCompletionIndex } from "./completion-index.js";
4
+ import { AT_SIGN, QUERY_COMPARISON_OPERATORS, QUERY_LOGICAL_OPERATORS, createDefaultCSS, generateAt, generateCSS, getSingleAtNumberRuleNode, parseAt } from "@master/css-language";
5
+ import { CompletionItemKind } from "vscode-languageserver-protocol";
6
+ //#region src/utils/get-query-completion-items.ts
7
+ function getQueryCompletionItems(css = createDefaultCSS(), triggerCharacter = AT_SIGN, syntax, completionIndex = createCompletionIndex(css)) {
8
+ const completionItems = [];
9
+ if (!QUERY_COMPARISON_OPERATORS.includes(triggerCharacter)) [
10
+ "media",
11
+ "container",
12
+ "supports"
13
+ ].forEach((name) => {
14
+ completionItems.push({
15
+ label: triggerCharacter + name + "()",
16
+ filterText: name + "()",
17
+ insertText: name + "()",
18
+ sortText: name + "()",
19
+ documentation: createCSSMarkdownDocumentation(generateCSS([syntax + name + "()"], css, completionIndex.runtime))
20
+ });
21
+ });
22
+ const handleNumberNodes = (atRule, token) => {
23
+ const numberNode = getSingleAtNumberRuleNode(atRule.nodes);
24
+ if (numberNode) {
25
+ const prevComparisonCharacter = syntax.charAt(syntax.length - 2);
26
+ const comparisonCharacter = prevComparisonCharacter === ">" || prevComparisonCharacter === "<" ? prevComparisonCharacter + triggerCharacter : triggerCharacter;
27
+ const text = generateAt(parseAt(comparisonCharacter + token, css));
28
+ completionItems.push({
29
+ label: comparisonCharacter + token,
30
+ sortText: CompletionItemKind.Keyword + comparisonCharacter + String(Math.round(numberNode.value)).padStart(12, "0"),
31
+ filterText: token,
32
+ insertText: token,
33
+ detail: text,
34
+ kind: CompletionItemKind.Keyword,
35
+ documentation: createCSSMarkdownDocumentation(generateCSS([syntax + token], css, completionIndex.runtime))
36
+ });
37
+ return true;
38
+ }
39
+ };
40
+ for (const [token, atRule] of completionIndex.atRules) {
41
+ const completionItem = {
42
+ filterText: token,
43
+ insertText: token,
44
+ documentation: createCSSMarkdownDocumentation(generateCSS([syntax + token], css, completionIndex.runtime))
45
+ };
46
+ if ([AT_SIGN, ...QUERY_LOGICAL_OPERATORS].includes(triggerCharacter)) {
47
+ if (handleNumberNodes(atRule, token)) continue;
48
+ const text = generateAt(parseAt(triggerCharacter + token, css));
49
+ completionItems.push({
50
+ ...completionItem,
51
+ label: triggerCharacter + token,
52
+ detail: text,
53
+ sortText: CompletionItemKind.Keyword + token,
54
+ kind: CompletionItemKind.Keyword,
55
+ documentation: createCSSMarkdownDocumentation(generateCSS([syntax + token], css, completionIndex.runtime))
56
+ });
57
+ } else if (QUERY_COMPARISON_OPERATORS.includes(triggerCharacter)) handleNumberNodes(atRule, token);
58
+ }
59
+ return sortCompletionItems(completionItems);
60
+ }
61
+ //#endregion
62
+ export { getQueryCompletionItems as default };
@@ -0,0 +1,6 @@
1
+ import { Range, TextDocument } from "vscode-languageserver-textdocument";
2
+
3
+ //#region src/utils/get-range.d.ts
4
+ declare function getRange(searchString: string, doc: TextDocument): Range;
5
+ //#endregion
6
+ export { getRange as default };
@@ -0,0 +1,21 @@
1
+ //#region src/utils/get-range.ts
2
+ function getRange(searchString, doc) {
3
+ const text = doc.getText();
4
+ const regex = new RegExp(`(\\b(?:className|class)\\s*=\\s*(['"\`]))(.*?)\\2`, "gs");
5
+ let match;
6
+ while ((match = regex.exec(text)) !== null) {
7
+ match[0];
8
+ const attrStartIndex = match.index + match[1].length;
9
+ const relativeIndex = match[3].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
+ //#endregion
21
+ export { getRange as default };
@@ -0,0 +1,8 @@
1
+ import { CompiledUtility } from "@master/css-language";
2
+
3
+ //#region src/utils/get-utility-info.d.ts
4
+ declare function getUtilityInfo(utility: CompiledUtility): {
5
+ detail: string | undefined;
6
+ };
7
+ //#endregion
8
+ export { getUtilityInfo as default };
@@ -0,0 +1,16 @@
1
+ import { getStaticUtilityDeclarations } from "@master/css-language";
2
+ //#region src/utils/get-utility-info.ts
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
+ * */
12
+ if (propsLength === 1) detail = `${propName}: ${propValue}`;
13
+ return { detail };
14
+ }
15
+ //#endregion
16
+ export { getUtilityInfo as default };
@@ -0,0 +1,8 @@
1
+ import { MasterCSS } from "@master/css-language";
2
+ import { CompletionItem } from "vscode-languageserver-protocol";
3
+ import { CompletionIndex } from "./completion-index.js";
4
+
5
+ //#region src/utils/get-value-completion-items.d.ts
6
+ declare function getValueCompletionItems(css: MasterCSS | undefined, ruleKey: string, valuePrefix?: string, completionIndex?: CompletionIndex): CompletionItem[];
7
+ //#endregion
8
+ export { getValueCompletionItems as default };
@@ -0,0 +1,165 @@
1
+ import createCSSMarkdownDocumentation from "./create-css-markdown-documentation.js";
2
+ import sortCompletionItems from "./sort-completion-items.js";
3
+ import { createCompletionIndex } from "./completion-index.js";
4
+ import { createDefaultCSS, generateCSS, getMdnPropertySyntax, getMdnPropertyValueNames } from "@master/css-language";
5
+ import { CompletionItemKind } from "vscode-languageserver-protocol";
6
+ //#region src/utils/get-value-completion-items.ts
7
+ const SCOPED_VARIABLE_PRIORITY = "aaaa";
8
+ const NATIVE_PRIORITY = "ccccc";
9
+ const GLOBAL_VARIABLE_PRIORITY = "zzzz";
10
+ const ANIMATION_REFERENCE_PROPERTIES = /* @__PURE__ */ new Set(["animation", "animation-name"]);
11
+ function getNumericSortValue(variable, rootSize) {
12
+ if (variable.numeric) switch (variable.numeric.unit) {
13
+ case "rem": return variable.numeric.value * rootSize;
14
+ case void 0:
15
+ case "":
16
+ case "px": return variable.numeric.value;
17
+ }
18
+ return typeof variable.value === "number" ? variable.value : 0;
19
+ }
20
+ function getVariableKeyByNamespace(variableName, namespace) {
21
+ const negative = variableName.startsWith("-");
22
+ const positiveName = negative ? variableName.slice(1) : variableName;
23
+ if (positiveName !== namespace && !positiveName.startsWith(namespace + "-")) return;
24
+ const key = positiveName === namespace ? "" : positiveName.slice(namespace.length + 1);
25
+ return negative ? "-" + key : key;
26
+ }
27
+ function uniqueKeys(...keys) {
28
+ return [...new Set(keys)];
29
+ }
30
+ function getValueCompletionItems(css = createDefaultCSS(), ruleKey, valuePrefix = "", completionIndex = createCompletionIndex(css)) {
31
+ const completionItems = [];
32
+ const addedLabels = /* @__PURE__ */ new Set();
33
+ const canonicalRuleKey = completionIndex.keyAliases[ruleKey] || ruleKey;
34
+ const valueLookupKeys = uniqueKeys(ruleKey, canonicalRuleKey);
35
+ const nativeKey = completionIndex.nativeUtilityKeys.get(canonicalRuleKey) || (getMdnPropertySyntax(canonicalRuleKey) ? canonicalRuleKey : void 0);
36
+ const nativePropertyValues = getMdnPropertyValueNames(nativeKey);
37
+ const pushCompletionItem = (completionItem) => {
38
+ completionItems.push(completionItem);
39
+ addedLabels.add(completionItem.label);
40
+ };
41
+ const generateVariableCompletionItem = (variable, { appliedValue, label, negative, scoped } = {}) => {
42
+ const nativePropertySyntax = getMdnPropertySyntax(variable.namespace) || getMdnPropertySyntax(nativeKey);
43
+ if (variable.type === "number" && variable.name.startsWith("-") && nativePropertySyntax?.includes("absolute")) return;
44
+ const valueToken = appliedValue ?? (scoped ? variable.key : variable.name);
45
+ const documentation = createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + valueToken], css, completionIndex.runtime));
46
+ const completionItemLabel = label ?? variable.name;
47
+ const conflicted = addedLabels.has(completionItemLabel);
48
+ const completionItem = {
49
+ label: conflicted ? "var(--" + completionItemLabel + ")" : completionItemLabel,
50
+ kind: CompletionItemKind.Value
51
+ };
52
+ if (conflicted) completionItem.documentation = createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + completionItem.label], css, completionIndex.runtime));
53
+ else completionItem.documentation = createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + valueToken], css, completionIndex.runtime));
54
+ if (variable.namespace?.startsWith("color")) if (Object.keys(variable.modes || {}).length) {
55
+ completionItem.kind = CompletionItemKind.Color;
56
+ completionItem.detail = variable.name;
57
+ } else {
58
+ const configKey = "variables." + (variable.namespace ? variable.namespace + "." + variable.key : variable.name);
59
+ const valueToken = variable.value ?? variable.name;
60
+ completionItem.detail = String(configKey);
61
+ completionItem.documentation = {
62
+ kind: "markdown",
63
+ value: valueToken + "\n\n" + documentation?.value
64
+ };
65
+ completionItem.kind = CompletionItemKind.Color;
66
+ completionItem.sortText = "color-" + variable.name.replace(/(.+?)-(\d+)/, (match, prefix, num) => prefix + num.padStart(10, "0"));
67
+ }
68
+ else if (variable.type === "number") {
69
+ completionItem.detail = String(variable.name);
70
+ const value = getNumericSortValue(variable, css.settings.rootSize);
71
+ const sortValue = negative ? -Math.abs(value) : value;
72
+ completionItem.sortText = (variable.namespace || "") + (sortValue >= 0 ? String(sortValue).padStart(10, "0") : "-" + String(Math.abs(sortValue)).padStart(10, "0"));
73
+ } else completionItem.detail = String(variable.value || variable.name);
74
+ return completionItem;
75
+ };
76
+ const addScopedVariableCompletionItem = (variable, variableName) => {
77
+ if (addedLabels.has(variableName)) return;
78
+ const completionItem = generateVariableCompletionItem(variable, { scoped: true });
79
+ if (completionItem) {
80
+ completionItem.label = variableName;
81
+ completionItem.sortText = SCOPED_VARIABLE_PRIORITY + (completionItem.sortText || variableName);
82
+ completionItem.detail = "(scope) " + completionItem.detail;
83
+ pushCompletionItem(completionItem);
84
+ }
85
+ const negativeVariableName = "-" + variableName;
86
+ if (valuePrefix.startsWith("-") && variableName[0] !== "-" && variable.type === "number" && !addedLabels.has(negativeVariableName)) {
87
+ const negativeCompletionItem = generateVariableCompletionItem(variable, {
88
+ appliedValue: negativeVariableName,
89
+ label: negativeVariableName,
90
+ negative: true,
91
+ scoped: true
92
+ });
93
+ if (negativeCompletionItem) {
94
+ negativeCompletionItem.sortText = SCOPED_VARIABLE_PRIORITY + (negativeCompletionItem.sortText || negativeVariableName);
95
+ negativeCompletionItem.detail = "(scope) " + negativeCompletionItem.detail;
96
+ pushCompletionItem(negativeCompletionItem);
97
+ }
98
+ }
99
+ };
100
+ /**
101
+ * Scoped variables
102
+ * @example box: + content -> box-sizing:content
103
+ */
104
+ for (const valueLookupKey of valueLookupKeys) for (const { name, variable } of completionIndex.scopedVariablesByKey.get(valueLookupKey) || []) addScopedVariableCompletionItem(variable, name);
105
+ for (const valueLookupKey of valueLookupKeys) for (const { value } of completionIndex.patternValuesByKey.get(valueLookupKey) || []) {
106
+ if (addedLabels.has(value)) continue;
107
+ pushCompletionItem({
108
+ label: value,
109
+ kind: CompletionItemKind.Value,
110
+ documentation: createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + value], css, completionIndex.runtime)),
111
+ detail: valueLookupKey + ": " + value
112
+ });
113
+ }
114
+ /**
115
+ * @example animation:fade|fast animate:fade animation-name:fade
116
+ */
117
+ for (const animationUtility of completionIndex.animationUtilitiesByKey.get(canonicalRuleKey) || []) css.animations.forEach((_, animationName) => {
118
+ pushCompletionItem({
119
+ label: animationName,
120
+ kind: CompletionItemKind.Value,
121
+ documentation: createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + animationName], css, completionIndex.runtime)),
122
+ detail: animationUtility.detailPrefix ? animationUtility.detailPrefix + ": " + animationName : animationName
123
+ });
124
+ });
125
+ if (ANIMATION_REFERENCE_PROPERTIES.has(canonicalRuleKey)) css.animations.forEach((_, animationName) => {
126
+ if (addedLabels.has(animationName)) return;
127
+ pushCompletionItem({
128
+ label: animationName,
129
+ kind: CompletionItemKind.Value,
130
+ documentation: createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + animationName], css, completionIndex.runtime)),
131
+ detail: canonicalRuleKey + ": " + animationName
132
+ });
133
+ });
134
+ const usedKeys = /* @__PURE__ */ new Set();
135
+ for (const valueLookupKey of valueLookupKeys) for (const variableNamespace of completionIndex.nativeVariableNamespacesByProperty.get(valueLookupKey) || []) for (const variable of completionIndex.variables) {
136
+ const variableName = getVariableKeyByNamespace(variable.name, variableNamespace);
137
+ if (variableName === void 0 || usedKeys.has(variableName)) continue;
138
+ usedKeys.add(variableName);
139
+ addScopedVariableCompletionItem(variable, variableName);
140
+ }
141
+ /**
142
+ * Native values
143
+ */
144
+ if (nativeKey) nativePropertyValues.forEach((value) => {
145
+ if (addedLabels.has(value) || nativeKey === "font" && typeof +value === "number" || value.includes(" ")) return;
146
+ pushCompletionItem({
147
+ label: value,
148
+ kind: CompletionItemKind.Value,
149
+ sortText: NATIVE_PRIORITY + (value.startsWith("-") ? "zz" + value.slice(1) : value),
150
+ documentation: createCSSMarkdownDocumentation(generateCSS([ruleKey + ":" + value], css, completionIndex.runtime)),
151
+ detail: nativeKey + ": " + value
152
+ });
153
+ });
154
+ completionIndex.variables.forEach((variable) => {
155
+ const completionItem = generateVariableCompletionItem(variable);
156
+ if (completionItem) {
157
+ completionItem.sortText = GLOBAL_VARIABLE_PRIORITY + (completionItem.sortText || completionItem.label);
158
+ completionItem.detail = "(global) " + completionItem.detail;
159
+ pushCompletionItem(completionItem);
160
+ }
161
+ });
162
+ return sortCompletionItems(completionItems);
163
+ }
164
+ //#endregion
165
+ export { getValueCompletionItems as default };
@@ -0,0 +1,8 @@
1
+ import { MasterCSS } from "@master/css-language";
2
+ import { CompletionItem } from "vscode-languageserver-protocol";
3
+ import { CompletionIndex } from "./completion-index.js";
4
+
5
+ //#region src/utils/query-syntax-completions.d.ts
6
+ declare function querySyntaxCompletions(q?: string, css?: MasterCSS, completionIndex?: CompletionIndex): CompletionItem[] | undefined;
7
+ //#endregion
8
+ export { querySyntaxCompletions as default };
@@ -0,0 +1,78 @@
1
+ import { GROUP_TRIGGER_CHARACTER, SELECTOR_TRIGGER_CHARACTERS } from "../common.js";
2
+ import createCSSMarkdownDocumentation from "./create-css-markdown-documentation.js";
3
+ import { createCompletionIndex, matchesIndexedName } from "./completion-index.js";
4
+ import getPseudoClassCompletionItems from "./get-pseudo-class-completion-items.js";
5
+ import getPseudoElementCompletionItems from "./get-pseudo-element-completion-items.js";
6
+ import getClassCompletionItems from "./get-class-completion-items.js";
7
+ import getValueCompletionItems from "./get-value-completion-items.js";
8
+ import getQueryCompletionItems from "./get-query-completion-items.js";
9
+ import { AT_SIGN, QUERY_COMPARISON_OPERATORS, QUERY_LOGICAL_OPERATORS, SELECTOR_SIGNS, createDefaultCSS, generateCSS } from "@master/css-language";
10
+ import { CompletionItemKind } from "vscode-languageserver-protocol";
11
+ //#region src/utils/query-syntax-completions.ts
12
+ function querySyntaxCompletions(q = "", css = createDefaultCSS(), completionIndex = createCompletionIndex(css)) {
13
+ const fields = q.split(" ");
14
+ let field = fields[fields.length - 1];
15
+ const triggerCharacter = q.charAt(q.length - 1);
16
+ if (triggerCharacter === " " || field.length === 0 || field === GROUP_TRIGGER_CHARACTER || triggerCharacter === ";") return getClassCompletionItems(css, completionIndex);
17
+ const isGroup = field.startsWith(GROUP_TRIGGER_CHARACTER);
18
+ if (isGroup) {
19
+ const declarationSeparatorMatches = Array.from(field.matchAll(/;(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/g));
20
+ if (declarationSeparatorMatches.length) field = field.slice(declarationSeparatorMatches[declarationSeparatorMatches.length - 1].index + 1);
21
+ else field = field.slice(1);
22
+ }
23
+ if (field.startsWith(AT_SIGN) || field.startsWith("~")) return [];
24
+ const keyMatch = field.match(/^[^'":\s]+:/);
25
+ const atMatches = Array.from(field.matchAll(/@(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/g));
26
+ const valueSeparatorMatch = field.match(/\|(?=(?:[^'"]|'[^']*'|"[^"]*")*$)/g);
27
+ const atInvoked = atMatches.findIndex(({ index }) => index !== 0) !== -1;
28
+ const firstColonIndex = keyMatch ? keyMatch[0].length - 1 : -1;
29
+ const selectorInvokedRegex = new RegExp(`[${SELECTOR_SIGNS.join("")}](?=(?:[^'"]|'[^']*'|"[^"]*")*$)`);
30
+ const key = keyMatch ? keyMatch[0].slice(0, firstColonIndex) : void 0;
31
+ const isStyle = completionIndex.componentNames.some((eachStyleName) => matchesIndexedName(field, eachStyleName));
32
+ const isUtility = completionIndex.utilityNames.some((eachUtilityName) => matchesIndexedName(field, eachUtilityName));
33
+ if (!isStyle && !isUtility) {
34
+ if (key === void 0 && !valueSeparatorMatch) return getClassCompletionItems(css, completionIndex);
35
+ if (!atInvoked && !selectorInvokedRegex.test(field.slice(firstColonIndex + 1))) {
36
+ if (key && firstColonIndex !== -1) return getValueCompletionItems(css, key, field.slice(firstColonIndex + 1), completionIndex);
37
+ }
38
+ }
39
+ /**
40
+ * Not support conditional btn:hover and group
41
+ */
42
+ if (isStyle || isGroup) return;
43
+ if (atInvoked && [
44
+ AT_SIGN,
45
+ ...QUERY_COMPARISON_OPERATORS,
46
+ ...QUERY_LOGICAL_OPERATORS
47
+ ].includes(triggerCharacter)) return getQueryCompletionItems(css, triggerCharacter, field, completionIndex);
48
+ if (!atInvoked && SELECTOR_TRIGGER_CHARACTERS.includes(triggerCharacter)) {
49
+ const pseudoElementCompletionItems = getPseudoElementCompletionItems(css, field, completionIndex);
50
+ if (field.endsWith("::")) {
51
+ /**
52
+ * Consider trigger characters and fix insertText.
53
+ * @incorrect class="text-center::" -> class="text-center::::after"
54
+ * @correct class="text-center::" -> class="text-center::after"
55
+ */
56
+ pseudoElementCompletionItems.forEach((completionItem) => completionItem.insertText = completionItem.label.slice(2));
57
+ return pseudoElementCompletionItems;
58
+ } else {
59
+ const completionItems = getPseudoClassCompletionItems(css, field, completionIndex);
60
+ completionItems.push(...pseudoElementCompletionItems);
61
+ /**
62
+ * Consider trigger characters and fix insertText.
63
+ * @incorrect class="text-center:" -> class="text-center::active"
64
+ * @correct class="text-center:" -> class="text-center:active"
65
+ */
66
+ if (triggerCharacter === ":") completionItems.forEach((completionItem) => completionItem.insertText = completionItem.label.slice(1));
67
+ else completionItems.push({
68
+ label: ".<class>",
69
+ insertText: ".",
70
+ documentation: createCSSMarkdownDocumentation(generateCSS([field + ".class"], css, completionIndex.runtime)),
71
+ kind: CompletionItemKind.Class
72
+ });
73
+ return completionItems;
74
+ }
75
+ }
76
+ }
77
+ //#endregion
78
+ export { querySyntaxCompletions as default };
@@ -1 +1,4 @@
1
- export declare const instancePattern: RegExp;
1
+ //#region src/utils/regex.d.ts
2
+ declare const instancePattern: RegExp;
3
+ //#endregion
4
+ export { instancePattern };
@@ -0,0 +1,4 @@
1
+ //#region src/utils/regex.ts
2
+ const instancePattern = /[^\s:]+:\w*\(?((?<!\\)["'`])((?:\\\1|(?:(?!\1))\S)*)((?<!\\)\1)\)?|[^\s"'`]+/g;
3
+ //#endregion
4
+ export { instancePattern };
@@ -0,0 +1,6 @@
1
+ import { CompletionItem } from "vscode-languageserver-protocol";
2
+
3
+ //#region src/utils/sort-completion-items.d.ts
4
+ declare function sortCompletionItems(items: CompletionItem[]): CompletionItem[];
5
+ //#endregion
6
+ export { sortCompletionItems as default };
@@ -0,0 +1,10 @@
1
+ //#region src/utils/sort-completion-items.ts
2
+ function sortCompletionItems(items) {
3
+ return items.sort((a, b) => {
4
+ const sortTextA = a.sortText || a.label;
5
+ const sortTextB = b.sortText || b.label;
6
+ return sortTextA.localeCompare(sortTextB, void 0, { numeric: true });
7
+ });
8
+ }
9
+ //#endregion
10
+ export { sortCompletionItems as default };
package/package.json CHANGED
@@ -1 +1,61 @@
1
- {"name":"@master/css-language-service","scripts":{"build":"techor pack --platform node --declare","dev":"pnpm run build --watch","type-check":"tsc --noEmit","lint":"eslint src"},"description":"Master CSS Language Service","author":"Aoyue Design LLC.","license":"MIT","dependencies":{"@babel/parser":"^7.22.7","@master/css":"^2.0.0-rc.8","color-convert":"^2.0.1","js-beautify":"^1.14.7"},"devDependencies":{"@types/color-convert":"^2.0.0","vscode-css-languageservice":"^6.2.5","vscode-languageserver-types":"^3.17.3"},"main":"./dist/index.bundle.js","types":"./dist/index.d.ts","exports":{".":"./dist/index.bundle.js","./settings":"./dist/settings.bundle.js"},"typesVersions":{"*":{"./":["./dist/index.d.ts"],"./settings":["./dist/settings.d.ts"]}},"files":["dist"],"publishConfig":{"access":"public"},"version":"2.0.0-rc.8"}
1
+ {
2
+ "name": "@master/css-language-service",
3
+ "type": "module",
4
+ "description": "Master CSS Language Service",
5
+ "author": "Aoyue Design LLC.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/master-co/css.git",
10
+ "directory": "packages/language-service"
11
+ },
12
+ "dependencies": {
13
+ "colorjs.io": "^0.6.1",
14
+ "defu": "^6.1.7",
15
+ "js-beautify": "^1.15.4",
16
+ "minimatch": "^10.2.5",
17
+ "vscode-languageserver-protocol": "^3.18.0",
18
+ "vscode-languageserver-textdocument": "^1.0.12",
19
+ "@master/css": "^2.0.0-rc.80",
20
+ "@master/css-engine": "^2.0.0-rc.80",
21
+ "@master/css-language": "^2.0.0-rc.80"
22
+ },
23
+ "devDependencies": {
24
+ "@types/vscode": "^1.125.0",
25
+ "vscode-languageserver": "^10.0.0",
26
+ "@master/css-schema": "^2.0.0-rc.80"
27
+ },
28
+ "sideEffects": false,
29
+ "main": "./dist/index.js",
30
+ "module": "./dist/index.js",
31
+ "jsnext:main": "./dist/index.js",
32
+ "esnext": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "exports": {
35
+ ".": {
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/index.js",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "./common": {
41
+ "types": "./dist/common.d.ts",
42
+ "import": "./dist/common.js",
43
+ "default": "./dist/common.js"
44
+ }
45
+ },
46
+ "files": [
47
+ "dist"
48
+ ],
49
+ "publishConfig": {
50
+ "access": "public",
51
+ "provenance": true
52
+ },
53
+ "version": "2.0.0-rc.80",
54
+ "scripts": {
55
+ "build": "tsdown --config ../../shared/tsdown-package.config.ts",
56
+ "dev": "tsdown --config ../../shared/tsdown-package.config.ts --watch",
57
+ "type-check": "tsc -b tsconfig.typecheck.json",
58
+ "lint": "eslint",
59
+ "test": "vitest"
60
+ }
61
+ }
@@ -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 {};