Skip to content

Commit 96edd75

Browse files
committed
html language service integration
1 parent a8a36a2 commit 96edd75

File tree

8 files changed

+110
-32
lines changed

8 files changed

+110
-32
lines changed

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
],
3636
"devDependencies": {
3737
"vscode-css-languageservice": "^6.0.0",
38+
"vscode-html-languageservice": "^5.2.0",
3839
"shx": "^0.3.4",
3940
"@babel/core": "^7.18.0",
4041
"@babel/preset-env": "^7.18.0",

src/css-ls.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as cssLangService from 'vscode-css-languageservice';
2+
3+
const service = cssLangService.getCSSLanguageService();
4+
export const CSS_MODES = {
5+
CSS: "css",
6+
LESS: "less",
7+
SCSS: "scss"
8+
};
9+
10+
function getTextDocument(text, languageID, filePath = "file://placeholder.css") {
11+
return cssLangService.TextDocument.create(filePath, languageID, 1, text);
12+
}
13+
14+
/**
15+
* Given a text, returns all the CSS selectors as an array.
16+
* @param {string} text
17+
* @param {string} cssMode
18+
* @param {string} filePath - the path will be used to figure out relative urls.
19+
* @return {Array[string]} all css selectors in the file as an array of strings
20+
*/
21+
export function getAllSymbols(text, cssMode, filePath) {
22+
const textDocument = getTextDocument(text, cssMode, filePath);
23+
const stylesheet = service.parseStylesheet(textDocument);
24+
const output = [];
25+
for(let symbol of service.findDocumentSymbols(textDocument, stylesheet)) {
26+
output.push(symbol.name);
27+
}
28+
return output;
29+
}

src/html-ls.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as htmlLangService from 'vscode-html-languageservice';
2+
3+
const service = htmlLangService.getLanguageService();
4+
export const HTML_MODES = {
5+
HTML: "html",
6+
XHTML: "xhtml",
7+
HTM: "htm",
8+
PHP: "php",
9+
};
10+
11+
function getTextDocument(text, languageID, filePath = "file:///placeholder.html") {
12+
return htmlLangService.TextDocument.create(filePath, languageID, 1, text);
13+
}
14+
15+
// Properly setup DocumentContext with resolveReference function
16+
function getLocalLinkDocumentContext() {
17+
return {
18+
resolveReference: (ref) => { // the second argument base is unused, base is the textDocument url
19+
// we return the links as i, won't resolve relative to the text document base
20+
return ref;
21+
}
22+
};
23+
}
24+
25+
/**
26+
* Given a text, returns all links in the file as an array of strings
27+
* @param {string} text
28+
* @param {string} htmlMode
29+
* @param {string} filePath - the path will be used to figure out relative urls.
30+
* @return {Array[string]} all file links in the file as an array of strings
31+
*/
32+
export function getAllDocumentLinks(text, htmlMode, filePath) {
33+
const textDocument = getTextDocument(text, htmlMode, filePath);
34+
const documentContext = getLocalLinkDocumentContext(); // textDocument.uri
35+
const documentLinks = service.findDocumentLinks(textDocument, documentContext);
36+
const linksArray = [];
37+
for(let link of documentLinks){
38+
if(link.target){
39+
linksArray.push(link.target);
40+
}
41+
}
42+
return linksArray;
43+
}

src/worker.js

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
1-
import * as cssLangService from 'vscode-css-languageservice';
2-
3-
const service = cssLangService.getCSSLanguageService();
4-
const CSS_MODES = {
5-
CSS: "css",
6-
LESS: "less",
7-
SCSS: "scss"
8-
};
9-
10-
function getTextDocument(text, languageID, filePath = "file://placeholder.css") {
11-
return cssLangService.TextDocument.create(filePath, languageID, 1, text);
12-
}
13-
14-
/**
15-
* Given a text, returns all the CSS selectors as an array.
16-
* @param {string} text
17-
* @param {string} cssMode
18-
* @param {string} filePath - the path will be used to figure out relative urls.
19-
* @return {Array[string]} all css selectors in the file as an array of strings
20-
*/
21-
function getAllSymbols(text, cssMode, filePath) {
22-
const textDocument = getTextDocument(text, cssMode, filePath);
23-
const stylesheet = service.parseStylesheet(textDocument);
24-
const output = [];
25-
for(let symbol of service.findDocumentSymbols(textDocument, stylesheet)) {
26-
output.push(symbol.name);
27-
}
28-
return output;
29-
}
1+
import {getAllSymbols, CSS_MODES} from "./css-ls";
2+
import {HTML_MODES, getAllDocumentLinks} from "./html-ls";
303

314
self.CSSLanguageService = {
325
getAllSymbols,
336
CSS_MODES
347
};
8+
9+
self.HTMLLanguageService = {
10+
getAllDocumentLinks,
11+
HTML_MODES
12+
};

test/test-css.worker.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,13 @@ describe(`web worker CSS Language tests`, function () {
6464
const symbols = output.symbols;
6565
expect(symbols).to.deep.equal(["#header",".navigation",".logo","& .phcode"]);
6666
});
67+
68+
it(`Should getAllSymbols get all scss selectors`, async function () {
69+
messageFromWorker = null;
70+
const text = await (await fetch("test-files/css-tests/c.scss")).text();
71+
worker.postMessage({command: `getAllSymbols`, text, cssMode: "SCSS", filePath: "file:///c.scss"});
72+
let output = await waitForWorkerMessage(`getAllSymbols`, 1000);
73+
const symbols = output.symbols;
74+
expect(symbols).to.deep.equal(['.info', '.alert', '#success']);
75+
});
6776
});

test/worker-task.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global*/
22

3-
importScripts("dist/language-worker.js");
4-
// importScripts("dist/language-worker-debug.js"); // uncomment to debug
3+
// importScripts("dist/language-worker.js");
4+
importScripts("dist/language-worker-debug.js"); // uncomment to debug
55

66
function workerOK() {
77
if(self.CSSLanguageService){

webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ const path = require('path');
33

44
module.exports = {
55
entry: './src/worker',
6+
resolve: {
7+
fallback: {
8+
"path": require.resolve('path-browserify')
9+
}
10+
},
611
output: {
712
filename: 'language-worker.js',
813
path: path.resolve(__dirname, 'dist'),

0 commit comments

Comments
 (0)