Skip to content

Commit

Permalink
refactored auto validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed Jan 28, 2024
1 parent bdbe497 commit e428728
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 75 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ Configuration depends on your layout of the project but some samples are below:
}
```

```json
{
"css.vaildOnSaveOrChange": "Always"
}
```

### Lit

```json
Expand Down
34 changes: 22 additions & 12 deletions package-lock.json

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

28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@
"description": "List of local or remote style sheets for suggestions.",
"default": []
},
"css.vaildOnSaveOrChange": {
"enum": [
"Always",
"OnChange",
"OnSave",
"Never"
],
"default": "Never",
"css.autoValidation": {
"type": "string",
"scope": "resource",
"description": "Verify label class names when saving files."
"description": "When to validate class selectors.",
"default": "Never",
"enum": [
"Never",
"Save",
"Always"
]
}
}
},
Expand Down Expand Up @@ -103,7 +103,7 @@
"pretest": "npm run build && npm run compile",
"test": "node ./out/test/runTest.js",
"coverage": "c8 -n out/src npm run test",
"update": "ncu -u -x prettier -x @types/vscode",
"update": "npx npm-check-updates -u -x prettier -x @types/vscode",
"vscode:prepublish": "npm run build",
"package": "vsce package",
"publish": "vsce publish"
Expand All @@ -118,11 +118,11 @@
"@rollup/plugin-typescript": "^11.1.6",
"@types/line-column": "^1.0.2",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.5",
"@types/node": "^20.11.9",
"@types/sinon": "^17.0.3",
"@types/vscode": "^1.75.0",
"@vscode/test-electron": "^2.3.8",
"@vscode/vsce": "^2.22.0",
"@vscode/test-electron": "^2.3.9",
"@vscode/vsce": "^2.23.0",
"c8": "^9.1.0",
"fast-glob": "^3.3.2",
"line-column": "^1.0.2",
Expand All @@ -134,4 +134,4 @@
"tslib": "^2.6.2",
"typescript": "^5.3.3"
}
}
}
45 changes: 26 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
window,
workspace,
} from "vscode";
import { getEnabledLanguages, getVaildOnSaveOrChange, VaildOnSaveOrChange } from "./settings";
import {
AutoValidation,
getAutoValidation,
getEnabledLanguages,
} from "./settings";
import { Provider, clear, invalidate } from "./provider";

export function activate(context: ExtensionContext) {
Expand All @@ -21,26 +25,29 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(
languages.registerCompletionItemProvider(enabledLanguages, provider),
languages.registerDefinitionProvider(enabledLanguages, provider),
workspace.onDidSaveTextDocument((document) => {
const vaildOnSaveOrChange = getVaildOnSaveOrChange();
if (vaildOnSaveOrChange == VaildOnSaveOrChange.Always || vaildOnSaveOrChange == VaildOnSaveOrChange.OnSave) {
commands.executeCommand("vscode-html-css.validate")
} else {
invalidate(document.uri.toString())
workspace.onDidSaveTextDocument(async (document) => {
invalidate(document.uri.toString());
if (enabledLanguages.includes(document.languageId)) {
const validation = getAutoValidation(document);
if (validation === AutoValidation.SAVE) {
validations.set(document.uri, await provider.validate(document));
}
}
}),
workspace.onDidCloseTextDocument((document) =>
validations.delete(document.uri)
),
workspace.onDidChangeTextDocument((event) => {
const vaildOnSaveOrChange = getVaildOnSaveOrChange();
if (vaildOnSaveOrChange == VaildOnSaveOrChange.Always || vaildOnSaveOrChange == VaildOnSaveOrChange.OnChange) {
commands.executeCommand("vscode-html-css.validate")
} else {
validations.delete(event.document.uri)
workspace.onDidChangeTextDocument(async (event) => {
const document = event.document;
if (enabledLanguages.includes(document.languageId)) {
const validation = getAutoValidation(document);
if (validation === AutoValidation.ALWAYS) {
validations.set(document.uri, await provider.validate(document));
} else {
validations.delete(document.uri);
}
}
}
),
}),
workspace.onDidCloseTextDocument((document) => {
validations.delete(document.uri);
}),
commands.registerCommand("vscode-html-css.validate", async () => {
const editor = window.activeTextEditor;
if (editor) {
Expand All @@ -54,4 +61,4 @@ export function activate(context: ExtensionContext) {
);
}

export function deactivate() { }
export function deactivate() {}
18 changes: 7 additions & 11 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
} from "vscode";
import { getStyleSheets } from "./settings";
import { Style, StyleType, parse } from "./parser";
import path from "path";

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

for (const [key, value] of styles) {
for (const value of styles.values()) {
for (const style of value) {
if (style.type === type) {
const item = new CompletionItem(
{
label: style.selector,
description: path.basename(key)
},
style.selector,
style.type === StyleType.ID
? CompletionItemKind.Value
: CompletionItemKind.Enum
Expand Down Expand Up @@ -157,12 +153,12 @@ export class Provider implements CompletionItemProvider, DefinitionProvider {
return new Promise((resolve, reject) =>
match && !token.isCancellationRequested
? resolve(
this.getCompletionItems(
document,
position,
match[1] === "id" ? StyleType.ID : StyleType.CLASS
this.getCompletionItems(
document,
position,
match[1] === "id" ? StyleType.ID : StyleType.CLASS
)
)
)
: reject()
);
}
Expand Down
25 changes: 12 additions & 13 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
* Licensed under the MIT License
*/

import { Uri, workspace } from "vscode";
import { ConfigurationScope, workspace } from "vscode";

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

export function getStyleSheets(uri: Uri): string[] {
export function getStyleSheets(scope: ConfigurationScope): string[] {
return workspace
.getConfiguration("css", uri)
.getConfiguration("css", scope)
.get<string[]>("styleSheets", []);
}

export function getVaildOnSaveOrChange(): VaildOnSaveOrChange {
return workspace
.getConfiguration("css")
.get<VaildOnSaveOrChange>("vaildOnSaveOrChange", VaildOnSaveOrChange.Never);
export const enum AutoValidation {
NEVER = "Never",
SAVE = "Save",
ALWAYS = "Always",
}

export enum VaildOnSaveOrChange {
Always = "Always",
OnChange = "OnChange",
OnSave = "OnSave",
Never = "Never"
}
export function getAutoValidation(scope: ConfigurationScope): AutoValidation {
return workspace
.getConfiguration("css", scope)
.get<AutoValidation>("autoValidation", AutoValidation.NEVER);
}

0 comments on commit e428728

Please sign in to comment.