Skip to content

Commit dd1b86d

Browse files
committed
Added ${fileBasenameNoExtension}
1 parent 81dd177 commit dd1b86d

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to the extension will be documented in this file.
44

5+
## [1.8.1] - 2021-01-15
6+
7+
- Added ${fileBasenameNoExtension} setting variable.
8+
59
## [1.8.0] - 2021-01-11
610

711
- Added validate command.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ If it is not possible to specify local or remote styles in HTML or via template
136136
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css",
137137
"/style.css",
138138
"style.css",
139-
"./style.css"
139+
"./style.css",
140+
"./${fileBasenameNoExtension}.css"
140141
]
141142
}
142143
```
143144

144-
This configuration is same as the [first](#linked-and-embedded-style-sheets) example. All relative paths will be evaluated relative to the HTML file being edited.
145+
All relative paths will be evaluated relative to the HTML file being edited. `${fileBasenameNoExtension}` will be replaced with the base name of the file being edited.
145146

146147
## Supported Languages
147148

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-html-css",
33
"displayName": "HTML CSS Support",
44
"description": "CSS Intellisense for HTML",
5-
"version": "1.8.0",
5+
"version": "1.8.1",
66
"publisher": "ecmel",
77
"license": "MIT",
88
"homepage": "https://github.com/ecmel/vscode-html-css",

src/completion.ts

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,25 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
4141
this.cache.clear();
4242
}
4343

44-
watchFile(uri: Uri, listener: (e: Uri) => any) {
45-
const key = uri.toString();
44+
watchFile(path: string, listener: (e: Uri) => any) {
45+
if (this.watchers.has(path)) {
46+
return;
47+
}
4648

47-
if (!this.watchers.has(key)) {
48-
const watcher = workspace.createFileSystemWatcher(uri.fsPath);
49+
const watcher = workspace.createFileSystemWatcher(path);
4950

50-
watcher.onDidCreate(listener);
51-
watcher.onDidChange(listener);
52-
watcher.onDidDelete(listener);
51+
watcher.onDidCreate(listener);
52+
watcher.onDidChange(listener);
53+
watcher.onDidDelete(listener);
5354

54-
this.watchers.set(key, watcher);
55-
}
55+
this.watchers.set(path, watcher);
5656
}
5757

5858
getStyleSheets(uri: Uri): string[] {
5959
return workspace.getConfiguration("css", uri).get<string[]>("styleSheets", []);
6060
}
6161

62-
getRelativePath(uri: Uri, spec: string, ext?: string): string {
62+
getPath(uri: Uri, spec: string, ext?: string): string {
6363
const folder = workspace.getWorkspaceFolder(uri);
6464
const name = ext ? join(dirname(spec), basename(spec, ext) + ext) : spec;
6565

@@ -90,25 +90,32 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
9090
});
9191
}
9292

93-
async fetchLocal(key: string, uri: Uri): Promise<void> {
94-
const file = Uri.file(this.getRelativePath(uri, key));
93+
async fetchLocal(path: string): Promise<void> {
94+
if (this.cache.has(path)) {
95+
return;
96+
}
97+
9598
const items: CompletionItem[] = [];
9699

97100
try {
98-
const content = await workspace.fs.readFile(file);
101+
const content = await workspace.fs.readFile(Uri.file(path));
99102
this.parseTextToItems(content.toString(), items);
100103
} catch (error) {
101104
}
102105

103-
this.cache.set(key, items);
104-
this.watchFile(file, e => this.cache.delete(key));
106+
this.cache.set(path, items);
107+
this.watchFile(path, () => this.cache.delete(path));
105108
}
106109

107-
async fetchRemote(key: string): Promise<void> {
110+
async fetchRemote(path: string): Promise<void> {
111+
if (this.cache.has(path)) {
112+
return;
113+
}
114+
108115
const items: CompletionItem[] = [];
109116

110117
try {
111-
const res = await fetch(key);
118+
const res = await fetch(path);
112119

113120
if (res.ok) {
114121
const text = await res.text();
@@ -117,20 +124,23 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
117124
} catch (error) {
118125
}
119126

120-
this.cache.set(key, items);
127+
this.cache.set(path, items);
121128
}
122129

123-
async fetchStyleSheet(key: string, uri: Uri): Promise<void> {
124-
if (!this.cache.has(key)) {
125-
if (this.isRemote.test(key)) {
126-
await this.fetchRemote(key);
127-
} else {
128-
await this.fetchLocal(key, uri);
129-
}
130+
async fetch(uri: Uri, path: string): Promise<string> {
131+
if (this.isRemote.test(path)) {
132+
await this.fetchRemote(path);
133+
} else {
134+
const base = basename(uri.fsPath, extname(uri.fsPath));
135+
136+
path = this.getPath(uri, path.replace(/\${\s*fileBasenameNoExtension\s*}/, base));
137+
await this.fetchLocal(path);
130138
}
139+
140+
return path;
131141
}
132142

133-
findDocumentStyles(uri: Uri, keys: Set<string>, text: string) {
143+
findEmbedded(uri: Uri, keys: Set<string>, text: string) {
134144
const key = uri.toString();
135145
const items: CompletionItem[] = [];
136146
const findStyles = /<style[^>]*>([^<]+)<\/style>/gi;
@@ -145,14 +155,13 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
145155
keys.add(key);
146156
}
147157

148-
async findStyleSheets(uri: Uri, keys: Set<string>): Promise<void> {
149-
for (const key of this.getStyleSheets(uri)) {
150-
await this.fetchStyleSheet(key, uri);
151-
keys.add(key);
158+
async findFixed(uri: Uri, keys: Set<string>): Promise<void> {
159+
for (const sheet of this.getStyleSheets(uri)) {
160+
keys.add(await this.fetch(uri, sheet));
152161
}
153162
}
154163

155-
async findDocumentLinks(uri: Uri, keys: Set<string>, text: string): Promise<void> {
164+
async findLinks(uri: Uri, keys: Set<string>, text: string): Promise<void> {
156165
const findLinks = /<link([^>]+)>/gi;
157166

158167
let link;
@@ -164,34 +173,31 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
164173
const href = this.findLinkHref.exec(link[1]);
165174

166175
if (href) {
167-
const key = href[2];
168-
169-
await this.fetchStyleSheet(key, uri);
170-
keys.add(key);
176+
keys.add(await this.fetch(uri, href[2]));
171177
}
172178
}
173179
}
174180
}
175181

176-
async findExtendedStyles(uri: Uri, keys: Set<string>, text: string, level: number = 0): Promise<void> {
182+
async findInherited(uri: Uri, keys: Set<string>, text: string, level: number = 0): Promise<void> {
177183
const extended = this.findExtended.exec(text);
178184

179185
if (extended && level < 3) {
180186
level++;
181187

182188
const name = extended[2];
183189
const ext = extname(name) || extname(uri.fsPath);
184-
const key = this.getRelativePath(uri, name, ext);
190+
const key = this.getPath(uri, name, ext);
185191
const file = Uri.file(key);
186192

187193
try {
188194
const content = await workspace.fs.readFile(file);
189195
const text = content.toString();
190196

191-
this.findDocumentStyles(file, keys, text);
197+
this.findEmbedded(file, keys, text);
192198

193-
await this.findDocumentLinks(file, keys, text);
194-
await this.findExtendedStyles(file, keys, text, level);
199+
await this.findLinks(file, keys, text);
200+
await this.findInherited(file, keys, text, level);
195201
} catch (error) {
196202
}
197203
}
@@ -202,11 +208,11 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
202208
const uri = document.uri;
203209
const text = document.getText();
204210

205-
this.findDocumentStyles(uri, keys, text);
211+
this.findEmbedded(uri, keys, text);
206212

207-
await this.findStyleSheets(uri, keys);
208-
await this.findDocumentLinks(uri, keys, text);
209-
await this.findExtendedStyles(uri, keys, text);
213+
await this.findFixed(uri, keys);
214+
await this.findLinks(uri, keys, text);
215+
await this.findInherited(uri, keys, text);
210216

211217
const ids = new Map<string, CompletionItem>();
212218
const classes = new Map<string, CompletionItem>();

0 commit comments

Comments
 (0)