Skip to content

Commit 78bf6cc

Browse files
committed
Fixes
1 parent 3e1ae7c commit 78bf6cc

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Missing CSS support for HTML documents.
77
- Html class attribute completion.
88
- Supports `<link rel="stylesheet">` and `<style></style>` tags.
99
- Supports other remote style sheets, see below.
10-
- Local workspace parsing for css files is depreceted.
10+
- Local workspace parsing for css files is deprecated.
1111

1212
## Remote Style Sheets
1313

src/extension.ts

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
walk
1919
} from "css-tree";
2020

21-
import fetch from 'node-fetch';
21+
import fetch from "node-fetch";
2222

2323
class ClassCompletionItemProvider implements CompletionItemProvider {
2424

@@ -37,48 +37,47 @@ class ClassCompletionItemProvider implements CompletionItemProvider {
3737
}
3838

3939
parseRemoteConfig() {
40-
const config = workspace.getConfiguration('css');
41-
const hrefs = config.get('remoteStyleSheets') as string[];
40+
const config = workspace.getConfiguration("css");
41+
const keys = config.get("remoteStyleSheets") as string[];
4242

43-
if (hrefs) {
44-
this.remoteStyles = hrefs;
43+
if (keys) {
44+
this.remoteStyles = keys;
4545
}
4646
}
4747

48-
fetchRemoteStyleSheet(href: string): Thenable<Map<string, CompletionItem>> {
49-
48+
fetchRemoteStyleSheet(key: string): Thenable<Map<string, CompletionItem>> {
5049
return new Promise(resolve => {
51-
const selectors = this.cache.get(href);
50+
const items = this.cache.get(key);
5251

53-
if (selectors) {
54-
resolve(selectors);
52+
if (items) {
53+
resolve(items);
5554
} else {
56-
const selectors = new Map<string, CompletionItem>();
55+
const items = new Map<string, CompletionItem>();
5756

58-
fetch(href).then(res => {
57+
fetch(key).then(res => {
5958
if (res.status === 200) {
6059
res.text().then(text => {
6160
walk(parse(text), (node) => {
6261
if (node.type === "ClassSelector") {
63-
selectors.set(node.name, new CompletionItem(node.name));
62+
items.set(node.name, new CompletionItem(node.name));
6463
};
6564
});
66-
this.cache.set(href, selectors);
67-
resolve(selectors);
65+
this.cache.set(key, items);
66+
resolve(items);
6867
}, () => {
69-
resolve(selectors);
68+
resolve(items);
7069
});
7170
} else {
72-
resolve(selectors);
71+
resolve(items);
7372
}
74-
}, () => resolve(selectors));
73+
}, () => resolve(items));
7574
}
7675
});
7776
}
7877

7978
findDocumentLinks(text: string): Thenable<Map<string, CompletionItem>> {
8079
return new Promise(resolve => {
81-
const links = new Map<string, CompletionItem>();
80+
const items = new Map<string, CompletionItem>();
8281
const findLinks = /<link([^>]+)>/gi;
8382
const promises = [];
8483

@@ -92,31 +91,48 @@ class ClassCompletionItemProvider implements CompletionItemProvider {
9291

9392
if (href && href[2].startsWith("http")) {
9493
promises.push(this.fetchRemoteStyleSheet(href[2]).then(items => {
95-
items.forEach((value, key) => links.set(key, value));
94+
items.forEach((value, key) => items.set(key, value));
9695
}));
9796
}
9897
}
9998
}
10099

101-
Promise.all(promises).then(() => resolve(links));
100+
Promise.all(promises).then(() => resolve(items));
102101
});
103102
}
104103

105104
findRemoteStyles(): Thenable<Map<string, CompletionItem>> {
106105
return new Promise(resolve => {
107-
const links = new Map<string, CompletionItem>();
106+
const items = new Map<string, CompletionItem>();
108107
const promises = [];
109108

110109
for (let i = 0; i < this.remoteStyles.length; i++) {
111-
promises.push(this.fetchRemoteStyleSheet(this.remoteStyles[i]).then(items => {
112-
items.forEach((value, key) => links.set(key, value));
110+
promises.push(this.fetchRemoteStyleSheet(this.remoteStyles[i]).then(found => {
111+
found.forEach((value, key) => items.set(key, value));
113112
}));
114113
}
115114

116-
Promise.all(promises).then(() => resolve(links));
115+
Promise.all(promises).then(() => resolve(items));
117116
});
118117
}
119118

119+
findDocumentStyles(text: string): Map<string, CompletionItem> {
120+
const items = new Map<string, CompletionItem>();
121+
const findStyles = /<style[^>]*>([^<]+)<\/style>/gi;
122+
123+
let style;
124+
125+
while ((style = findStyles.exec(text)) !== null) {
126+
walk(parse(style[1]), (node) => {
127+
if (node.type === "ClassSelector") {
128+
items.set(node.name, new CompletionItem(node.name));
129+
}
130+
});
131+
}
132+
133+
return items;
134+
}
135+
120136
provideCompletionItems(
121137
document: TextDocument,
122138
position: Position,
@@ -129,25 +145,15 @@ class ClassCompletionItemProvider implements CompletionItemProvider {
129145
const canComplete = this.canComplete.test(text);
130146

131147
if (canComplete) {
132-
const styles = new Map<string, CompletionItem>();
133-
const findStyles = /<style[^>]*>([^<]+)<\/style>/gi;
134-
135-
let style;
148+
const styles = this.findDocumentStyles(text);
136149

137-
while ((style = findStyles.exec(text)) !== null) {
138-
walk(parse(style[1]), (node) => {
139-
if (node.type === "ClassSelector") {
140-
styles.set(node.name, new CompletionItem(node.name));
141-
}
142-
});
143-
}
150+
this.findRemoteStyles().then(items => {
151+
styles.forEach((value, key) => items.set(key, value));
144152

145-
this.findDocumentLinks(text).then(links => {
146-
styles.forEach((value, key) => links.set(key, value));
153+
this.findDocumentLinks(text).then(links => {
154+
links.forEach((value, key) => items.set(key, value));
147155

148-
this.findRemoteStyles().then(styles => {
149-
styles.forEach((value, key) => links.set(key, value));
150-
resolve([...links.values()]);
156+
resolve([...items.values()]);
151157
});
152158
});
153159
} else {

0 commit comments

Comments
 (0)