Skip to content

Commit f338b1a

Browse files
committed
refactor: remove css-tree and change to fast-glob implementation
1 parent c5612f4 commit f338b1a

File tree

3 files changed

+22
-39
lines changed

3 files changed

+22
-39
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
"@typescript-eslint/eslint-plugin": "^6.15.0",
4949
"@typescript-eslint/parser": "^6.15.0",
5050
"coc.nvim": "0.0.83-next.17",
51-
"css-tree": "^1.1.2",
5251
"esbuild": "^0.16.17",
5352
"eslint": "^8.56.0",
5453
"eslint-config-prettier": "^9.1.0",
@@ -97,5 +96,8 @@
9796
"title": "Setup `html.customData` to be used in the workspace"
9897
}
9998
]
99+
},
100+
"dependencies": {
101+
"fast-glob": "^3.3.2"
100102
}
101103
}

src/completion.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Uri,
1313
workspace,
1414
} from 'coc.nvim';
15-
import { parse, walk } from 'css-tree';
15+
import { glob } from 'fast-glob';
1616
import fetch from 'node-fetch';
1717
import { basename, dirname, extname, isAbsolute, join } from 'path';
1818

@@ -65,28 +65,21 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
6565
}
6666

6767
parseTextToItems(path: string, text: string, items: CompletionItem[]) {
68-
walk(parse(text), (node) => {
69-
let kind: CompletionItemKind;
70-
71-
switch (node.type) {
72-
case 'ClassSelector':
73-
kind = CompletionItemKind.Enum;
74-
break;
75-
case 'IdSelector':
76-
kind = CompletionItemKind.Value;
77-
break;
78-
default:
79-
return;
80-
}
68+
const regex = /([.#])(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)\s*{([^}]*)}/g;
69+
70+
let match: RegExpExecArray | null = null;
71+
72+
while ((match = regex.exec(text))) {
73+
const kind = match[1] === '.' ? CompletionItemKind.Enum : CompletionItemKind.Value;
8174

8275
const resultCompletionItem: CompletionItem = {
83-
label: node.name,
76+
label: match[2],
8477
kind,
8578
detail: 'filename: ' + path,
8679
};
8780

8881
items.push(resultCompletionItem);
89-
});
82+
}
9083
}
9184

9285
async fetchLocal(path: string): Promise<void> {
@@ -98,7 +91,8 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
9891

9992
try {
10093
const content = await workspace.readFile(path);
101-
this.parseTextToItems(basename(path), content.toString(), items);
94+
const text = content.toString();
95+
this.parseTextToItems(basename(path), text, items);
10296
} catch (error) {}
10397

10498
this.cache.set(path, items);
@@ -128,10 +122,14 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
128122
if (this.isRemote.test(path)) {
129123
await this.fetchRemote(path);
130124
} else {
125+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
131126
const base = basename(uri.fsPath, extname(uri.fsPath));
132127

133-
path = this.getPath(uri, path.replace(/\${\s*fileBasenameNoExtension\s*}/, base));
134-
await this.fetchLocal(path);
128+
const paths = isAbsolute(path) ? glob.sync(path) : [path];
129+
130+
for (const path of paths) {
131+
await this.fetchLocal(path);
132+
}
135133
}
136134

137135
return path;

yarn.lock

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,6 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
518518
shebang-command "^2.0.0"
519519
which "^2.0.1"
520520

521-
css-tree@^1.1.2:
522-
version "1.1.2"
523-
resolved "https://registry.yarnpkg.com/css-tree/-/css-tree-1.1.2.tgz#9ae393b5dafd7dae8a622475caec78d3d8fbd7b5"
524-
integrity sha512-wCoWush5Aeo48GLhfHPbmvZs59Z+M7k5+B1xDnXbdWNcEF423DoFdqSWE0PM5aNk5nI5cp1q7ms36zGApY/sKQ==
525-
dependencies:
526-
mdn-data "2.0.14"
527-
source-map "^0.6.1"
528-
529521
debug@^4.1.1:
530522
version "4.3.1"
531523
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
@@ -801,7 +793,7 @@ fast-glob@^3.2.9:
801793
merge2 "^1.3.0"
802794
micromatch "^4.0.4"
803795

804-
fast-glob@^3.3.0:
796+
fast-glob@^3.3.0, fast-glob@^3.3.2:
805797
version "3.3.2"
806798
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129"
807799
integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==
@@ -1134,11 +1126,6 @@ lru-cache@^6.0.0:
11341126
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.1.0.tgz#2098d41c2dc56500e6c88584aa656c84de7d0484"
11351127
integrity sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==
11361128

1137-
mdn-data@2.0.14:
1138-
version "2.0.14"
1139-
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.14.tgz#7113fc4281917d63ce29b43446f701e68c25ba50"
1140-
integrity sha512-dn6wd0uw5GsdswPFfsgMp5NSB0/aDe6fK94YJV/AJDYXL6HVLWBsxeq7js7Ad+mU2K9LAlwpk6kN2D5mwCPVow==
1141-
11421129
merge-stream@^2.0.0:
11431130
version "2.0.0"
11441131
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
@@ -1440,12 +1427,8 @@ slash@^3.0.0:
14401427
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
14411428
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
14421429

1443-
source-map@^0.6.1:
1444-
version "0.6.1"
1445-
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
1446-
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
1447-
14481430
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0:
1431+
name string-width-cjs
14491432
version "4.2.3"
14501433
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
14511434
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==

0 commit comments

Comments
 (0)