Skip to content

Commit cba11d4

Browse files
committed
added variable substitution
1 parent 46d33aa commit cba11d4

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

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

5-
## [2.0.8] - 2024-02-01
5+
## [2.0.9] - 2024-02-01
66

77
- Added optional auto validation
8+
- Added variable substitution for local style sheets
89
- Added feedback for clear cache
910
- Updated documentation
1011
- Updated dependencies

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This setting is application scoped and changing the setting requires restarting
2929

3030
## Specifying Style Sheets
3131

32-
Remote and local style sheets with optional glob patterns can be specified in VS Code settings per workspace folder in `.vscode/settings.json` and will suggest in all configured languages within that workspace folder.
32+
Remote and local style sheets with optional glob patterns and variable substitutions can be specified in VS Code settings per workspace folder in `.vscode/settings.json` and will suggest in all configured languages within that workspace folder.
3333

3434
Glob patterns for local style sheets can have the following syntax:
3535

@@ -41,9 +41,16 @@ Glob patterns for local style sheets can have the following syntax:
4141
| `{}` | group conditions like `**​/*.{css,scss}` |
4242
| `[]` | a range of characters like `[0-9]` or negate `[!0-9]` |
4343

44+
The following variable substitutions are supported for local style sheets as well:
45+
46+
| Variable | Substitution |
47+
| ---------------------------- | ----------------------------------------- |
48+
| `${fileBasename}` | Current file's basename |
49+
| `${fileBasenameNoExtension}` | Current file's basename with no extension |
50+
4451
## Examples
4552

46-
Configuration depends on your layout of the project. The following most basic setting will suggest from all `css` files in project's `src` folder:
53+
Configuration depends on your layout of the project. The following most basic setup will suggest from all `css` files in project's `src` folder:
4754

4855
**`.vscode/settings.json`**
4956

@@ -103,6 +110,16 @@ Component's [static styles](https://lit.dev/docs/components/styles/) will be ava
103110
}
104111
```
105112

113+
Variable substitution can be used to refer to a related `css` file. If you are working on `my-component.ts` and your `css` is in `my-component-css.ts` then a suitable setup can be:
114+
115+
**`.vscode/settings.json`**
116+
117+
```json
118+
{
119+
"css.styleSheets": ["src/${fileBasenameNoExtension}-css.ts"]
120+
}
121+
```
122+
106123
### [Vue](https://vuejs.org/)
107124

108125
Install your favorite Vue language extension from [Marketplace](https://marketplace.visualstudio.com/search?term=tag%3Avue&target=VSCode&category=All%20categories&sortBy=Relevance) which registers `vue` language id then enable `vue` in global settings and restart VS Code:
@@ -115,6 +132,20 @@ Install your favorite Vue language extension from [Marketplace](https://marketpl
115132

116133
Styles defined in component's `<style>` section will be available for completion in component's `<template>` section.
117134

135+
### [Angular](https://angular.io/)
136+
137+
Variable substitution can be used for Angular development:
138+
139+
**`.vscode/settings.json`**
140+
141+
```json
142+
{
143+
"css.styleSheets": ["app/${fileBasenameNoExtension}.css"]
144+
}
145+
```
146+
147+
With this setup, styles defined in e.g. `app.component.css` will be available in `app.component.html`.
148+
118149
## Go to Definition
119150

120151
Go to definition for `id` and `class` selectors for local style sheets are supported. Selecting `Go to Definition` from context menu (`F12` or `⌘ click`) on a selector will open the local style sheet which the selector is defined.

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
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": "2.0.8",
5+
"version": "2.0.9",
66
"license": "MIT",
77
"publisher": "ecmel",
88
"author": {
@@ -120,7 +120,7 @@
120120
"@rollup/plugin-typescript": "^11.1.6",
121121
"@types/line-column": "^1.0.2",
122122
"@types/mocha": "^10.0.6",
123-
"@types/node": "^20.11.14",
123+
"@types/node": "^20.11.16",
124124
"@types/sinon": "^17.0.3",
125125
"@types/vscode": "^1.75.0",
126126
"@vscode/test-electron": "^2.3.9",

src/settings.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,28 @@
33
* Licensed under the MIT License
44
*/
55

6-
import { ConfigurationScope, workspace } from "vscode";
6+
import Path from "path";
7+
import { ConfigurationScope, TextDocument, workspace } from "vscode";
78

89
export function getEnabledLanguages(): string[] {
910
return workspace
1011
.getConfiguration("css")
1112
.get<string[]>("enabledLanguages", ["html"]);
1213
}
1314

14-
export function getStyleSheets(scope: ConfigurationScope): string[] {
15+
export function getStyleSheets(scope: TextDocument): string[] {
16+
const path = Path.parse(scope.fileName);
17+
1518
return workspace
1619
.getConfiguration("css", scope)
17-
.get<string[]>("styleSheets", []);
20+
.get<string[]>("styleSheets", [])
21+
.map((glob) =>
22+
glob.replace(
23+
/\$\s*{\s*(fileBasenameNoExtension|fileBasename)\s*}/g,
24+
(match, variable) =>
25+
variable === "fileBasename" ? path.base : path.name
26+
)
27+
);
1828
}
1929

2030
export const enum AutoValidation {

0 commit comments

Comments
 (0)