Skip to content

example to stop lockup #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 49 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
{
"name": "html-css-class-completion",
"displayName": "HTML CSS Class Completion",
"description": "Provides CSS class name completion for the HTML class attribute based on the CSS files on your workspace.",
"version": "1.0.3",
"publisher": "Zignd",
"engines": {
"vscode": "^1.4.0"
},
"categories": [
"Languages",
"Other"
],
"activationEvents": [
"*"
],
"contributes": {
"commands": [
{
"command": "html-css-class-completion.cache",
"title": "Cache CSS class definitions"
}
]
},
"icon": "images/icon.png",
"repository": {
"url": "https://github.com/Zignd/HTML-CSS-Class-Completion"
},
"main": "./out/src/extension",
"scripts": {
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"typescript": "^1.8.5",
"vscode": "^0.11.0"
},
"dependencies": {
"async": "^2.0.1",
"css": "^2.2.1"
}
}
"name": "html-css-class-completion",
"displayName": "HTML CSS Class Completion",
"description": "Provides CSS class name completion for the HTML class attribute based on the CSS files on your workspace.",
"version": "1.0.3",
"publisher": "Zignd",
"engines": {
"vscode": "^1.4.0"
},
"categories": [
"Languages",
"Other"
],
"activationEvents": [
"*"
],
"contributes": {
"commands": [
{
"command": "html-css-class-completion.cache",
"title": "Cache CSS class definitions"
}
]
},
"icon": "images/icon.png",
"repository": {
"url": "https://github.com/Zignd/HTML-CSS-Class-Completion"
},
"main": "./out/src/extension",
"scripts": {
"vscode:prepublish": "tsc -p ./",
"compile": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"devDependencies": {
"@types/css": "^0.0.30",
"@types/lodash": "^4.14.37",
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.40",
"@types/verror": "^1.6.28",
"typescript": "^2.0.3",
"vscode": "^1.0.3"
},
"dependencies": {
"css": "^2.2.1",
"mocha": "^3.1.2",
"verror": "^1.8.1",
"lodash": "^4.16.4"
}
}
82 changes: 38 additions & 44 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

import * as async from 'async';
import * as _ from 'lodash';
import * as vscode from 'vscode';
import CssClassDefinition from './common/css-class-definition';
Expand All @@ -12,61 +11,56 @@ import ParseEngineGateway from './parse-engine-gateway';
let notifier: Notifier = new Notifier('html-css-class-completion.cache');
let uniqueDefinitions: CssClassDefinition[];

function cache(): Promise<void> {
return new Promise<void>(async (resolve, reject): Promise<void> => {
try {
notifier.notify('eye', 'Looking for CSS classes on the workspace...');
async function cache(): Promise<void> {
try {
notifier.notify('eye', 'Looking for CSS classes on the workspace...');

console.log('Looking for parseable documents...');
let uris: vscode.Uri[] = await Fetcher.findAllParseableDocuments();
console.log('Looking for parseable documents...');
let uris: vscode.Uri[] = await Fetcher.findAllParseableDocuments();

if (!uris) {
console.log("Found no documents");
notifier.statusBarItem.hide();
return;
}
if (!uris) {
console.log("Found no documents");
notifier.statusBarItem.hide();
return;
}

console.log('Found all parseable documents.');
let definitions: CssClassDefinition[] = [];
console.log('Found all parseable documents.');
let definitions: CssClassDefinition[] = [];

let failedLogs: string = '';
let failedLogsCount: number = 0;
let failedLogs: string = '';
let failedLogsCount: number = 0;

console.log('Parsing documents and looking for CSS class definitions...');
return async.each(uris, async (uri, callback) => {
console.log('Parsing documents and looking for CSS class definitions...');

try {
await Promise.all(uris.map(async (uri: vscode.Uri) => {
try {
Array.prototype.push.apply(definitions, await ParseEngineGateway.callParser(uri));
callback();
definitions.push(...await ParseEngineGateway.callParser(uri))
} catch (error) {
failedLogs += `${uri.path}\n`;
failedLogsCount++;
callback();
}
}, (error) => {
if (error) {
console.error('Failed to parse the documents: ', error);
return reject(error);
}

uniqueDefinitions = _.uniqBy(definitions, (x) => x.className);

console.log('Sumary:');
console.log(uris.length, 'parseable documents found');
console.log(definitions.length, 'CSS class definitions found');
console.log(uniqueDefinitions.length, 'unique CSS class definitions found');
console.log(failedLogsCount, 'failed attempts to parse. List of the documents:');
console.log(failedLogs);

notifier.notify('zap', 'CSS classes cached (click to cache again)');

return resolve();
});
}));
} catch (error) {
console.error('Failed while looping through the documents to cache the classes definitions:', error);
notifier.notify('alert', 'Failed to cache the CSS classes on the workspace (click for another attempt)');
return reject(error);
console.error('Failed to parse the documents: ', error);
throw error;
}
});

uniqueDefinitions = _.uniqBy(definitions, (x: CssClassDefinition) => x.className);

console.log('Sumary:');
console.log(uris.length, 'parseable documents found');
console.log(definitions.length, 'CSS class definitions found');
console.log(uniqueDefinitions.length, 'unique CSS class definitions found');
console.log(failedLogsCount, 'failed attempts to parse. List of the documents:');
console.log(failedLogs);

notifier.notify('zap', 'CSS classes cached (click to cache again)');
} catch (error) {
console.error('Failed while looping through the documents to cache the classes definitions:', error);
notifier.notify('alert', 'Failed to cache the CSS classes on the workspace (click for another attempt)');
throw error;
}
}

export async function activate(context: vscode.ExtensionContext): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as vscode from 'vscode';

class Notifier {
private _timeoutId: number;
private _timeoutId: any;

public statusBarItem: vscode.StatusBarItem;

Expand Down
6 changes: 3 additions & 3 deletions src/parse-engine-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import ParseEngineRegistry from './parse-engines/parse-engine-registry';

class ParseEngineGateway {
public static async callParser(uri: vscode.Uri): Promise<CssClassDefinition[]> {
let textDocument = await vscode.workspace.openTextDocument(uri);
let parseEngine: ParseEngine = ParseEngineRegistry.getParseEngine(textDocument.languageId);
let cssClassDefinitions: CssClassDefinition[] = parseEngine.parse(textDocument);
// let textDocument = await vscode.workspace.openTextDocument(uri);
let parseEngine: ParseEngine = ParseEngineRegistry.getParseEngine('css');
let cssClassDefinitions: CssClassDefinition[] = parseEngine.parse(uri);
return cssClassDefinitions;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parse-engines/common/parse-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CssClassDefinition from './../../common/css-class-definition';

interface ParseEngine {
languageId: string;
parse(textDocument: vscode.TextDocument): CssClassDefinition[];
parse(uri: vscode.Uri): CssClassDefinition[];
}

export default ParseEngine;
43 changes: 23 additions & 20 deletions src/parse-engines/types/css-parse-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,35 @@ import * as css from 'css';
import * as vscode from 'vscode';
import CssClassDefinition from '../../common/css-class-definition';
import ParseEngine from '../common/parse-engine';
import fs = require('fs');

class CssParseEngine implements ParseEngine {
public languageId: string = 'css';

public parse(textDocument: vscode.TextDocument): CssClassDefinition[] {
public parse(textDocument: vscode.Uri): CssClassDefinition[] {
let definitions: CssClassDefinition[] = [];

let code: string = textDocument.getText();
let codeAst: css.Stylesheet = css.parse(code);

// go through each of the rules...
codeAst.stylesheet.rules.forEach((rule: css.Rule) => {
// ...of type rule
if (rule.type === 'rule') {
// go through each of the selectors of the current rule
rule.selectors.forEach((selector: string) => {
let classNameRegex: RegExp = /[.]([\w-]+)/g;
var item: RegExpExecArray = null;

while (item = classNameRegex.exec(selector)) {
definitions.push(new CssClassDefinition(item[1]));
}
});
}
});

try {
let code: string = fs.readFileSync(textDocument.fsPath).toString();// textDocument.getText();
let codeAst: css.Stylesheet = css.parse(code);

// go through each of the rules...
codeAst.stylesheet.rules.forEach((rule: css.Rule) => {
// ...of type rule
if (rule.type === 'rule') {
// go through each of the selectors of the current rule
rule.selectors.forEach((selector: string) => {
let classNameRegex: RegExp = /[.]([\w-]+)/g;
var item: RegExpExecArray = null;

while (item = classNameRegex.exec(selector)) {
definitions.push(new CssClassDefinition(item[1]));
}
});
}
});

} catch (e) { }
return definitions;
}
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"module": "commonjs",
"target": "es6",
"outDir": "out",
"noLib": true,
"lib": [
"es6"
],
"sourceMap": true,
"rootDir": ".",
"noImplicitAny": true,
Expand Down
9 changes: 0 additions & 9 deletions typings.json

This file was deleted.

Loading