Skip to content

Commit e428728

Browse files
committed
refactored auto validation
1 parent bdbe497 commit e428728

File tree

6 files changed

+81
-75
lines changed

6 files changed

+81
-75
lines changed

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ Configuration depends on your layout of the project but some samples are below:
7272
}
7373
```
7474

75-
```json
76-
{
77-
"css.vaildOnSaveOrChange": "Always"
78-
}
79-
```
80-
8175
### Lit
8276

8377
```json

package-lock.json

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

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@
6060
"description": "List of local or remote style sheets for suggestions.",
6161
"default": []
6262
},
63-
"css.vaildOnSaveOrChange": {
64-
"enum": [
65-
"Always",
66-
"OnChange",
67-
"OnSave",
68-
"Never"
69-
],
70-
"default": "Never",
63+
"css.autoValidation": {
64+
"type": "string",
7165
"scope": "resource",
72-
"description": "Verify label class names when saving files."
66+
"description": "When to validate class selectors.",
67+
"default": "Never",
68+
"enum": [
69+
"Never",
70+
"Save",
71+
"Always"
72+
]
7373
}
7474
}
7575
},
@@ -103,7 +103,7 @@
103103
"pretest": "npm run build && npm run compile",
104104
"test": "node ./out/test/runTest.js",
105105
"coverage": "c8 -n out/src npm run test",
106-
"update": "ncu -u -x prettier -x @types/vscode",
106+
"update": "npx npm-check-updates -u -x prettier -x @types/vscode",
107107
"vscode:prepublish": "npm run build",
108108
"package": "vsce package",
109109
"publish": "vsce publish"
@@ -118,11 +118,11 @@
118118
"@rollup/plugin-typescript": "^11.1.6",
119119
"@types/line-column": "^1.0.2",
120120
"@types/mocha": "^10.0.6",
121-
"@types/node": "^20.11.5",
121+
"@types/node": "^20.11.9",
122122
"@types/sinon": "^17.0.3",
123123
"@types/vscode": "^1.75.0",
124-
"@vscode/test-electron": "^2.3.8",
125-
"@vscode/vsce": "^2.22.0",
124+
"@vscode/test-electron": "^2.3.9",
125+
"@vscode/vsce": "^2.23.0",
126126
"c8": "^9.1.0",
127127
"fast-glob": "^3.3.2",
128128
"line-column": "^1.0.2",
@@ -134,4 +134,4 @@
134134
"tslib": "^2.6.2",
135135
"typescript": "^5.3.3"
136136
}
137-
}
137+
}

src/extension.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
window,
1111
workspace,
1212
} from "vscode";
13-
import { getEnabledLanguages, getVaildOnSaveOrChange, VaildOnSaveOrChange } from "./settings";
13+
import {
14+
AutoValidation,
15+
getAutoValidation,
16+
getEnabledLanguages,
17+
} from "./settings";
1418
import { Provider, clear, invalidate } from "./provider";
1519

1620
export function activate(context: ExtensionContext) {
@@ -21,26 +25,29 @@ export function activate(context: ExtensionContext) {
2125
context.subscriptions.push(
2226
languages.registerCompletionItemProvider(enabledLanguages, provider),
2327
languages.registerDefinitionProvider(enabledLanguages, provider),
24-
workspace.onDidSaveTextDocument((document) => {
25-
const vaildOnSaveOrChange = getVaildOnSaveOrChange();
26-
if (vaildOnSaveOrChange == VaildOnSaveOrChange.Always || vaildOnSaveOrChange == VaildOnSaveOrChange.OnSave) {
27-
commands.executeCommand("vscode-html-css.validate")
28-
} else {
29-
invalidate(document.uri.toString())
28+
workspace.onDidSaveTextDocument(async (document) => {
29+
invalidate(document.uri.toString());
30+
if (enabledLanguages.includes(document.languageId)) {
31+
const validation = getAutoValidation(document);
32+
if (validation === AutoValidation.SAVE) {
33+
validations.set(document.uri, await provider.validate(document));
34+
}
3035
}
3136
}),
32-
workspace.onDidCloseTextDocument((document) =>
33-
validations.delete(document.uri)
34-
),
35-
workspace.onDidChangeTextDocument((event) => {
36-
const vaildOnSaveOrChange = getVaildOnSaveOrChange();
37-
if (vaildOnSaveOrChange == VaildOnSaveOrChange.Always || vaildOnSaveOrChange == VaildOnSaveOrChange.OnChange) {
38-
commands.executeCommand("vscode-html-css.validate")
39-
} else {
40-
validations.delete(event.document.uri)
37+
workspace.onDidChangeTextDocument(async (event) => {
38+
const document = event.document;
39+
if (enabledLanguages.includes(document.languageId)) {
40+
const validation = getAutoValidation(document);
41+
if (validation === AutoValidation.ALWAYS) {
42+
validations.set(document.uri, await provider.validate(document));
43+
} else {
44+
validations.delete(document.uri);
45+
}
4146
}
42-
}
43-
),
47+
}),
48+
workspace.onDidCloseTextDocument((document) => {
49+
validations.delete(document.uri);
50+
}),
4451
commands.registerCommand("vscode-html-css.validate", async () => {
4552
const editor = window.activeTextEditor;
4653
if (editor) {
@@ -54,4 +61,4 @@ export function activate(context: ExtensionContext) {
5461
);
5562
}
5663

57-
export function deactivate() { }
64+
export function deactivate() {}

src/provider.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
} from "vscode";
2929
import { getStyleSheets } from "./settings";
3030
import { Style, StyleType, parse } from "./parser";
31-
import path from "path";
3231

3332
const start = new Position(0, 0);
3433
const cache = new Map<string, Style[]>();
@@ -109,14 +108,11 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
109108
const map = new Map<string, CompletionItem>();
110109
const styles = await this.getStyles(document);
111110

112-
for (const [key, value] of styles) {
111+
for (const value of styles.values()) {
113112
for (const style of value) {
114113
if (style.type === type) {
115114
const item = new CompletionItem(
116-
{
117-
label: style.selector,
118-
description: path.basename(key)
119-
},
115+
style.selector,
120116
style.type === StyleType.ID
121117
? CompletionItemKind.Value
122118
: CompletionItemKind.Enum
@@ -157,12 +153,12 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
157153
return new Promise((resolve, reject) =>
158154
match && !token.isCancellationRequested
159155
? resolve(
160-
this.getCompletionItems(
161-
document,
162-
position,
163-
match[1] === "id" ? StyleType.ID : StyleType.CLASS
156+
this.getCompletionItems(
157+
document,
158+
position,
159+
match[1] === "id" ? StyleType.ID : StyleType.CLASS
160+
)
164161
)
165-
)
166162
: reject()
167163
);
168164
}

src/settings.ts

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

6-
import { Uri, workspace } from "vscode";
6+
import { ConfigurationScope, workspace } from "vscode";
77

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

14-
export function getStyleSheets(uri: Uri): string[] {
14+
export function getStyleSheets(scope: ConfigurationScope): string[] {
1515
return workspace
16-
.getConfiguration("css", uri)
16+
.getConfiguration("css", scope)
1717
.get<string[]>("styleSheets", []);
1818
}
1919

20-
export function getVaildOnSaveOrChange(): VaildOnSaveOrChange {
21-
return workspace
22-
.getConfiguration("css")
23-
.get<VaildOnSaveOrChange>("vaildOnSaveOrChange", VaildOnSaveOrChange.Never);
20+
export const enum AutoValidation {
21+
NEVER = "Never",
22+
SAVE = "Save",
23+
ALWAYS = "Always",
2424
}
2525

26-
export enum VaildOnSaveOrChange {
27-
Always = "Always",
28-
OnChange = "OnChange",
29-
OnSave = "OnSave",
30-
Never = "Never"
31-
}
26+
export function getAutoValidation(scope: ConfigurationScope): AutoValidation {
27+
return workspace
28+
.getConfiguration("css", scope)
29+
.get<AutoValidation>("autoValidation", AutoValidation.NEVER);
30+
}

0 commit comments

Comments
 (0)