Skip to content

Commit

Permalink
fix(emmet): completions appear inside open tag
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 16, 2023
1 parent 5e04b5d commit 94b5b83
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/emmet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"directory": "packages/emmet"
},
"dependencies": {
"@vscode/emmet-helper": "^2.8.6"
"@vscode/emmet-helper": "^2.8.6",
"vscode-html-languageservice": "^5.0.4"
},
"peerDependencies": {
"@volar/language-service": "*"
Expand Down
32 changes: 32 additions & 0 deletions packages/emmet/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { LanguageServicePlugin } from '@volar/language-service';
import * as emmet from '@vscode/emmet-helper';
import * as html from 'vscode-html-languageservice';

export = (): LanguageServicePlugin => (context) => {

const htmlLs = html.getLanguageService();
const htmlDocuments = new WeakMap<html.TextDocument, [number, html.HTMLDocument]>();

return {

complete: {
Expand All @@ -18,6 +22,18 @@ export = (): LanguageServicePlugin => (context) => {
if (!syntax)
return;

// fix https://github.com/vuejs/language-tools/issues/1329
if (syntax === 'html') {
const htmlDocument = getHtmlDocument(textDocument);
const node = htmlDocument.findNodeAt(textDocument.offsetAt(position));
if (node.tag && node.startTagEnd !== undefined && node.endTagStart !== undefined) {
const insideBlock = textDocument.offsetAt(position) >= node.startTagEnd && textDocument.offsetAt(position) <= node.endTagStart;
if (!insideBlock) {
return;
}
}
}

// monkey fix https://github.com/johnsoncodehk/volar/issues/1105
if (syntax === 'jsx')
return;
Expand Down Expand Up @@ -59,4 +75,20 @@ export = (): LanguageServicePlugin => (context) => {
showSuggestionsAsSnippets: emmetConfig['showSuggestionsAsSnippets']
};
}

function getHtmlDocument(document: html.TextDocument) {

const cache = htmlDocuments.get(document);
if (cache) {
const [cacheVersion, cacheDoc] = cache;
if (cacheVersion === document.version) {
return cacheDoc;
}
}

const doc = htmlLs.parseHTMLDocument(document);
htmlDocuments.set(document, [document.version, doc]);

return doc;
}
};
3 changes: 2 additions & 1 deletion packages/html/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ export = (options: {
updateCustomData(extraData: html.IHTMLDataProvider[]): void,
}> => (context) => {

const htmlDocuments = new WeakMap<TextDocument, [number, html.HTMLDocument]>();

let inited = false;
let customData: html.IHTMLDataProvider[] = [];
let extraData: html.IHTMLDataProvider[] = [];

const htmlLs = html.getLanguageService({ fileSystemProvider: context.env.fileSystemProvider });
const htmlDocuments = new WeakMap<TextDocument, [number, html.HTMLDocument]>();

return {

Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 94b5b83

Please sign in to comment.