Skip to content

Commit b70b51c

Browse files
committed
regex refactoring
1 parent b31a2ae commit b70b51c

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

src/parser.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
import lineColumn from "line-column";
77

8-
const regex =
9-
/([.#])(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[#.,()\s\[\]\^:*"'>=_a-zA-Z0-9-]*{[^}]*})/g;
10-
118
export const enum StyleType {
129
ID = "#",
1310
CLASS = ".",
@@ -22,14 +19,16 @@ export interface Style {
2219
}
2320

2421
export function parse(text: string) {
22+
const selector =
23+
/([.#])(-?[_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[#.,()\s\[\]\^:*"'>=_a-zA-Z0-9-]*{[^}]*})/g;
2524
const styles: Style[] = [];
2625
const lc = lineColumn(text, { origin: 0 });
2726
let match,
2827
lci,
2928
index,
3029
line = 0,
3130
col = 0;
32-
while ((match = regex.exec(text))) {
31+
while ((match = selector.exec(text))) {
3332
index = match.index;
3433
lci = lc.fromIndex(index);
3534
if (lci) {

src/provider.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ import { Style, StyleType, parse } from "./parser";
3131

3232
const start = new Position(0, 0);
3333
const cache = new Map<string, Style[]>();
34-
const isRemote = /^https?:\/\//i;
35-
const wordRange = /[_a-zA-Z0-9-]+/;
36-
const findSelector = /([^(\[{}\])\s]+)(?![^(\[{]*[}\])])/gi;
37-
const findAttribute = /(class|className)\s*[=:]\s*(["'])(.*?)\2/gis;
38-
const canComplete = /(id|class|className)\s*[=:]\s*(["'])(?:.(?!\2))*$/is;
3934

4035
export class Provider implements CompletionItemProvider, DefinitionProvider {
36+
private get isRemote() {
37+
return /^https?:\/\//i;
38+
}
39+
40+
private get wordRange() {
41+
return /[_a-zA-Z0-9-]+/;
42+
}
43+
44+
private get canComplete() {
45+
return /(id|class|className)\s*[=:]\s*(["'])(?:.(?!\2))*$/is;
46+
}
47+
4148
private async fetch(url: string) {
4249
try {
4350
const res = await fetch(url);
@@ -82,7 +89,7 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
8289
const globs = getStyleSheets(document.uri);
8390

8491
for (const glob of globs) {
85-
if (isRemote.test(glob)) {
92+
if (this.isRemote.test(glob)) {
8693
styles.set(glob, await this.getRemote(glob));
8794
} else if (folder) {
8895
const files = await workspace.findFiles(
@@ -123,7 +130,7 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
123130
type: StyleType
124131
) {
125132
const map = await this.getCompletionMap(document, type);
126-
const range = document.getWordRangeAtPosition(position, wordRange);
133+
const range = document.getWordRangeAtPosition(position, this.wordRange);
127134
const items = [];
128135

129136
for (const item of map.values()) {
@@ -141,7 +148,7 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
141148
): ProviderResult<CompletionItem[] | CompletionList<CompletionItem>> {
142149
const range = new Range(start, position);
143150
const text = document.getText(range);
144-
const match = canComplete.exec(text);
151+
const match = this.canComplete.exec(text);
145152

146153
return new Promise((resolve, reject) =>
147154
match && !token.isCancellationRequested
@@ -158,12 +165,12 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
158165

159166
private async getDefinitions(document: TextDocument, position: Position) {
160167
const styles = await this.getStyles(document);
161-
const range = document.getWordRangeAtPosition(position, wordRange);
168+
const range = document.getWordRangeAtPosition(position, this.wordRange);
162169
const selector = document.getText(range);
163170
const locations: Location[] = [];
164171

165172
for (const entry of styles) {
166-
if (!isRemote.test(entry[0])) {
173+
if (!this.isRemote.test(entry[0])) {
167174
entry[1]
168175
.filter((style) => style.selector === selector)
169176
.forEach((style) =>
@@ -186,7 +193,7 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
186193
): ProviderResult<Definition | LocationLink[]> {
187194
const range = new Range(start, position);
188195
const text = document.getText(range);
189-
const match = canComplete.exec(text);
196+
const match = this.canComplete.exec(text);
190197

191198
return new Promise((resolve, reject) =>
192199
match && !token.isCancellationRequested
@@ -196,6 +203,8 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
196203
}
197204

198205
async validate(document: TextDocument) {
206+
const findSelector = /([^(\[{}\])\s]+)(?![^(\[{]*[}\])])/gi;
207+
const findAttribute = /(class|className)\s*[=:]\s*(["'])(.*?)\2/gis;
199208
const diagnostics: Diagnostic[] = [];
200209
const map = await this.getCompletionMap(document, StyleType.CLASS);
201210
const text = document.getText();

0 commit comments

Comments
 (0)