Skip to content

Commit 16caa86

Browse files
committed
Removed validation
1 parent 10831e2 commit 16caa86

File tree

6 files changed

+128
-157
lines changed

6 files changed

+128
-157
lines changed

CHANGELOG.md

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

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

5-
## [1.6.4] -
5+
## [1.7.0] - 2021-01-09
66

7-
- Use nextTick()
7+
- Removed validation.
88

99
## [1.6.3] - 2021-01-07
1010

README.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Visual Studio Code CSS Intellisense for HTML
22

3-
HTML `id` and `class` attribute completion and validation for Visual Studio Code.
3+
HTML `id` and `class` attribute completion for Visual Studio Code.
44

55
## Features
66

7-
- HTML `id` and `class` attribute completion and validation.
7+
- HTML `id` and `class` attribute completion.
88
- Supports linked and embedded style sheets.
99
- Supports template inheritance.
1010
- Supports additional style sheets.
@@ -145,19 +145,6 @@ If it is not possible to specify local or remote styles in HTML or via template
145145

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

148-
## Selector Validation
149-
150-
Validated selectors can be configured with the `css.validation` setting. By default `class` selectors are validated:
151-
152-
```json
153-
{
154-
"css.validation": {
155-
"id": false,
156-
"class": true
157-
}
158-
}
159-
```
160-
161148
## Supported Languages
162149

163150
Supported languages can be configured with the `css.enabledLanguages` setting. By default `html` is enabled:

package.json

Lines changed: 2 additions & 20 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": "1.6.4",
5+
"version": "1.7.0",
66
"publisher": "ecmel",
77
"license": "MIT",
88
"homepage": "https://github.com/ecmel/vscode-html-css",
@@ -42,29 +42,11 @@
4242
"css.enabledLanguages": {
4343
"type": "array",
4444
"scope": "application",
45-
"description": "List of languages where suggestions are desired.",
45+
"description": "List of languages which suggestions are desired.",
4646
"default": [
4747
"html"
4848
]
4949
},
50-
"css.validation": {
51-
"type": "object",
52-
"scope": "resource",
53-
"description": "Map of selectors to validate.",
54-
"default": {},
55-
"properties": {
56-
"id": {
57-
"type": "boolean",
58-
"description": "Whether to validate 'id' selectors.",
59-
"default": false
60-
},
61-
"class": {
62-
"type": "boolean",
63-
"description": "Whether to validate 'class' selectors.",
64-
"default": true
65-
}
66-
}
67-
},
6850
"css.styleSheets": {
6951
"type": "array",
7052
"scope": "resource",

src/completion.ts

Lines changed: 22 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import {
99
CompletionItemKind,
1010
CompletionItemProvider,
1111
CompletionList,
12-
Diagnostic,
13-
DiagnosticSeverity,
1412
Disposable,
15-
languages,
1613
Position,
1714
ProviderResult,
1815
Range,
@@ -21,11 +18,6 @@ import {
2118
workspace
2219
} from "vscode";
2320

24-
export type Validation = {
25-
id: boolean,
26-
class: boolean
27-
};
28-
2921
export type Selector = {
3022
ids: Map<string, CompletionItem>,
3123
classes: Map<string, CompletionItem>,
@@ -38,19 +30,16 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
3830
readonly start = new Position(0, 0);
3931
readonly cache = new Map<string, Map<string, CompletionItem>>();
4032
readonly watchers = new Map<string, Disposable>();
41-
readonly selectors = new Map<string, Selector>();
42-
readonly warnings = languages.createDiagnosticCollection();
4333
readonly isRemote = /^https?:\/\//i;
34+
readonly canComplete = /(id|class|className)\s*=\s*("|')(?:(?!\2).)*$/si;
4435
readonly findLinkRel = /rel\s*=\s*("|')((?:(?!\1).)+)\1/si;
4536
readonly findLinkHref = /href\s*=\s*("|')((?:(?!\1).)+)\1/si;
4637
readonly findExtended = /(?:{{<|{{>|{%\s*extends|@extends\s*\()\s*("|')?([./A-Za-z_0-9\\\-]+)\1\s*(?:\)|%}|}})/i;
4738

4839
dispose() {
4940
this.watchers.forEach(v => v.dispose());
50-
this.cache.clear();
5141
this.watchers.clear();
52-
this.selectors.clear();
53-
this.warnings.dispose();
42+
this.cache.clear();
5443
}
5544

5645
watchFile(uri: Uri, listener: (e: Uri) => any) {
@@ -71,15 +60,6 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
7160
return workspace.getConfiguration("css", uri).get<string[]>("styleSheets", []);
7261
}
7362

74-
getValidation(uri: Uri): Validation {
75-
const config = workspace.getConfiguration("css", uri);
76-
77-
return {
78-
id: config.get<boolean>("validation.id", false),
79-
class: config.get<boolean>("validation.class", true)
80-
};
81-
}
82-
8363
getRelativePath(uri: Uri, spec: string, ext?: string): string {
8464
const folder = workspace.getWorkspaceFolder(uri);
8565
const name = ext ? join(dirname(spec), basename(spec, ext) + ext) : spec;
@@ -214,100 +194,52 @@ export class SelectorCompletionItemProvider implements CompletionItemProvider, D
214194
}
215195
}
216196

217-
async validate(document: TextDocument): Promise<void> {
197+
async findAll(document: TextDocument, kind: CompletionItemKind): Promise<Map<string, CompletionItem>> {
218198
const keys = new Set<string>();
219199
const uri = document.uri;
220200
const text = document.getText();
221201

222202
this.findDocumentStyles(uri, keys, text);
203+
223204
await this.findStyleSheets(uri, keys);
224205
await this.findDocumentLinks(uri, keys, text);
225206
await this.findExtendedStyles(uri, keys, text);
226207

227-
const ids = new Map<string, CompletionItem>();
228-
const classes = new Map<string, CompletionItem>();
229-
const rangesId: Range[] = [];
230-
const rangesClass: Range[] = [];
231-
232-
keys.forEach(key => this.cache.get(key)?.forEach((v, k) =>
233-
(v.kind === CompletionItemKind.Value ? ids : classes).set(k, v)));
234-
235-
const validation = this.getValidation(uri);
236-
const diagnostics: Diagnostic[] = [];
237-
const findAttribute = /(id|class|className)\s*=\s*("|')(.*?)\2/gsi;
238-
239-
let attribute;
240-
241-
while ((attribute = findAttribute.exec(text)) !== null) {
242-
const offset = findAttribute.lastIndex
243-
- attribute[3].length
244-
+ attribute[3].indexOf(attribute[2]);
245-
246-
(attribute[1] === "id" ? rangesId : rangesClass).push(new Range(
247-
document.positionAt(offset),
248-
document.positionAt(findAttribute.lastIndex - 1)));
249-
250-
const findSelector = /([^(\[{}\])\s]+)(?![^(\[{]*[}\])])/gi;
251-
252-
let value;
253-
254-
while ((value = findSelector.exec(attribute[3])) !== null) {
255-
const anchor = findSelector.lastIndex + offset;
256-
const end = document.positionAt(anchor);
257-
const start = document.positionAt(anchor - value[1].length);
208+
const items = new Map<string, CompletionItem>();
258209

259-
if (attribute[1] === "id") {
260-
if (validation.id && !ids.has(value[1])) {
261-
diagnostics.push(new Diagnostic(new Range(start, end),
262-
`CSS id selector '${value[1]}' not found.`,
263-
DiagnosticSeverity.Information));
264-
}
265-
} else {
266-
if (validation.class && !classes.has(value[1])) {
267-
diagnostics.push(new Diagnostic(new Range(start, end),
268-
`CSS class selector '${value[1]}' not found.`,
269-
DiagnosticSeverity.Warning));
270-
}
271-
}
210+
keys.forEach(key => this.cache.get(key)?.forEach((v, k) => {
211+
if (v.kind === kind) {
212+
items.set(k, v);
272213
}
273-
}
214+
}));
274215

275-
this.warnings.set(uri, diagnostics);
276-
this.selectors.set(uri.toString(), { ids, classes, rangesId, rangesClass });
216+
return items;
277217
}
278218

279219
provideCompletionItems(
280220
document: TextDocument,
281221
position: Position,
282222
token: CancellationToken,
283-
context: CompletionContext)
284-
: ProviderResult<CompletionItem[] | CompletionList<CompletionItem>> {
223+
context: CompletionContext): ProviderResult<CompletionItem[] | CompletionList<CompletionItem>> {
285224

286225
return new Promise((resolve, reject) => nextTick(() => {
287226
if (token.isCancellationRequested) {
288227
reject();
289-
return;
290-
}
291-
292-
const selector = this.selectors.get(document.uri.toString());
228+
} else {
229+
const range = new Range(this.start, position);
230+
const text = document.getText(range);
231+
const canComplete = this.canComplete.exec(text);
293232

294-
if (selector) {
295-
for (const range of selector.rangesClass) {
296-
if (range.contains(position)) {
297-
resolve([...selector.classes.values()]);
298-
return;
299-
}
300-
}
233+
if (canComplete) {
234+
const kind = canComplete[1] === "id"
235+
? CompletionItemKind.Value
236+
: CompletionItemKind.Enum;
301237

302-
for (const range of selector.rangesId) {
303-
if (range.contains(position)) {
304-
resolve([...selector.ids.values()]);
305-
return;
306-
}
238+
this.findAll(document, kind).then((items => resolve([...items.values()])));
239+
} else {
240+
reject();
307241
}
308242
}
309-
310-
reject();
311243
}));
312244
}
313245
}

src/extension.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
import { SelectorCompletionItemProvider } from "./completion";
2-
import {
3-
ExtensionContext,
4-
languages,
5-
TextDocument,
6-
TextDocumentChangeEvent,
7-
workspace
8-
} from "vscode";
2+
import { ExtensionContext, languages, workspace } from "vscode";
93

104
export function activate(context: ExtensionContext) {
115
const config = workspace.getConfiguration("css");
126
const enabledLanguages = config.get<string[]>("enabledLanguages", ["html"]);
13-
const timeouts = new Map<string, NodeJS.Timeout>();
147
const provider = new SelectorCompletionItemProvider();
158

16-
const validate = (e: TextDocumentChangeEvent | TextDocument) => {
17-
const document = (e as TextDocumentChangeEvent).document || e;
18-
19-
if (enabledLanguages.includes(document.languageId)) {
20-
const uri = document.uri.toString();
21-
const timeout = timeouts.get(uri);
22-
23-
if (timeout) {
24-
clearTimeout(timeout);
25-
}
26-
27-
timeouts.set(uri, setTimeout(() => {
28-
timeouts.delete(uri);
29-
provider.validate(document);
30-
}, 300));
31-
}
32-
};
33-
34-
workspace.textDocuments.forEach(validate);
35-
369
context.subscriptions.push(
37-
workspace.onDidOpenTextDocument(validate),
38-
workspace.onDidChangeTextDocument(validate),
3910
languages.registerCompletionItemProvider(enabledLanguages, provider),
4011
provider
4112
);

0 commit comments

Comments
 (0)