Skip to content

Commit 64ad46b

Browse files
authored
refactor: revert to implementation using css-tree (yaegassy#5)
1 parent 9c3b43e commit 64ad46b

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
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",
5152
"esbuild": "^0.16.17",
5253
"eslint": "^8.56.0",
5354
"eslint-config-prettier": "^9.1.0",
@@ -97,7 +98,5 @@
9798
}
9899
]
99100
},
100-
"dependencies": {
101-
"fast-glob": "^3.3.2"
102-
}
101+
"dependencies": {}
103102
}

src/completion.ts

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

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

6767
parseTextToItems(path: string, text: string, items: CompletionItem[]) {
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;
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+
}
7481

7582
const resultCompletionItem: CompletionItem = {
76-
label: match[2],
83+
label: node.name,
7784
kind,
7885
detail: 'filename: ' + path,
7986
};
8087

8188
items.push(resultCompletionItem);
82-
}
89+
});
8390
}
8491

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

9299
try {
93100
const content = await workspace.readFile(path);
94-
const text = content.toString();
95-
this.parseTextToItems(basename(path), text, items);
101+
this.parseTextToItems(basename(path), content.toString(), items);
96102
} catch (error) {}
97103

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

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

135137
return path;

yarn.lock

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,14 @@ 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+
521529
debug@^4.1.1:
522530
version "4.3.1"
523531
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"
@@ -793,7 +801,7 @@ fast-glob@^3.2.9:
793801
merge2 "^1.3.0"
794802
micromatch "^4.0.4"
795803

796-
fast-glob@^3.3.0, fast-glob@^3.3.2:
804+
fast-glob@^3.3.0:
797805
version "3.3.2"
798806
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129"
799807
integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==
@@ -1126,6 +1134,11 @@ lru-cache@^6.0.0:
11261134
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.1.0.tgz#2098d41c2dc56500e6c88584aa656c84de7d0484"
11271135
integrity sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==
11281136

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+
11291142
merge-stream@^2.0.0:
11301143
version "2.0.0"
11311144
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
@@ -1427,6 +1440,11 @@ slash@^3.0.0:
14271440
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
14281441
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==
14291442

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+
14301448
"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0:
14311449
name string-width-cjs
14321450
version "4.2.3"

0 commit comments

Comments
 (0)