Skip to content

Commit

Permalink
added variable substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed Feb 1, 2024
1 parent 46d33aa commit cba11d4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

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

## [2.0.8] - 2024-02-01
## [2.0.9] - 2024-02-01

- Added optional auto validation
- Added variable substitution for local style sheets
- Added feedback for clear cache
- Updated documentation
- Updated dependencies
Expand Down
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This setting is application scoped and changing the setting requires restarting

## Specifying Style Sheets

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.
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.

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

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

The following variable substitutions are supported for local style sheets as well:

| Variable | Substitution |
| ---------------------------- | ----------------------------------------- |
| `${fileBasename}` | Current file's basename |
| `${fileBasenameNoExtension}` | Current file's basename with no extension |

## Examples

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

**`.vscode/settings.json`**

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

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:

**`.vscode/settings.json`**

```json
{
"css.styleSheets": ["src/${fileBasenameNoExtension}-css.ts"]
}
```

### [Vue](https://vuejs.org/)

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:
Expand All @@ -115,6 +132,20 @@ Install your favorite Vue language extension from [Marketplace](https://marketpl

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

### [Angular](https://angular.io/)

Variable substitution can be used for Angular development:

**`.vscode/settings.json`**

```json
{
"css.styleSheets": ["app/${fileBasenameNoExtension}.css"]
}
```

With this setup, styles defined in e.g. `app.component.css` will be available in `app.component.html`.

## Go to Definition

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.
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-html-css",
"displayName": "HTML CSS Support",
"description": "CSS Intellisense for HTML",
"version": "2.0.8",
"version": "2.0.9",
"license": "MIT",
"publisher": "ecmel",
"author": {
Expand Down Expand Up @@ -120,7 +120,7 @@
"@rollup/plugin-typescript": "^11.1.6",
"@types/line-column": "^1.0.2",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.14",
"@types/node": "^20.11.16",
"@types/sinon": "^17.0.3",
"@types/vscode": "^1.75.0",
"@vscode/test-electron": "^2.3.9",
Expand Down
16 changes: 13 additions & 3 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@
* Licensed under the MIT License
*/

import { ConfigurationScope, workspace } from "vscode";
import Path from "path";
import { ConfigurationScope, TextDocument, workspace } from "vscode";

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

export function getStyleSheets(scope: ConfigurationScope): string[] {
export function getStyleSheets(scope: TextDocument): string[] {
const path = Path.parse(scope.fileName);

return workspace
.getConfiguration("css", scope)
.get<string[]>("styleSheets", []);
.get<string[]>("styleSheets", [])
.map((glob) =>
glob.replace(
/\$\s*{\s*(fileBasenameNoExtension|fileBasename)\s*}/g,
(match, variable) =>
variable === "fileBasename" ? path.base : path.name
)
);
}

export const enum AutoValidation {
Expand Down

0 comments on commit cba11d4

Please sign in to comment.