Skip to content

Commit

Permalink
feat: support autoCreateQuotes for pug
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 4, 2022
1 parent c0bc95c commit 9d6f5a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/pug-language-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { register as registerHover } from './services/hover';
import { register as registerScanner } from './services/scanner';
import { register as registerSelectRanges } from './services/selectionRanges';
import { register as registerFoldingRanges } from './services/foldingRanges';
import { register as registerQuoteComplete } from './services/quoteComplete';

export { PugDocument } from './pugDocument';
export { baseParse } from './baseParse';
Expand All @@ -24,6 +25,7 @@ export function getLanguageService(htmlLs: html.LanguageService) {
doHover: registerHover(htmlLs),
createScanner: registerScanner(htmlLs),
getSelectionRanges: registerSelectRanges(htmlLs),
doQuoteComplete: registerQuoteComplete(htmlLs),
getFoldingRanges: registerFoldingRanges(),
};
}
19 changes: 19 additions & 0 deletions packages/pug-language-service/src/services/quoteComplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type * as html from 'vscode-html-languageservice';
import type { PugDocument } from '../pugDocument';

export function register(htmlLs: html.LanguageService) {
return (pugDoc: PugDocument, pos: html.Position, options?: html.CompletionConfiguration | undefined) => {

const htmlRange = pugDoc.sourceMap.getMappedRange(pos)?.[0];
if (!htmlRange) return;

const text = htmlLs.doQuoteComplete(
pugDoc.htmlTextDocument,
htmlRange.start,
pugDoc.htmlDocument,
options,
);

return text;
};
}
23 changes: 22 additions & 1 deletion packages/vue-language-service/src/plugins/pug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbeddedLanguageServicePlugin } from '@volar/vue-language-service-types';
import { EmbeddedLanguageServicePlugin, useConfigurationHost } from '@volar/vue-language-service-types';
import type * as html from 'vscode-html-languageservice';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as pug from '@volar/pug-language-service';
Expand Down Expand Up @@ -99,6 +99,27 @@ export default function (options: {
return pugLs.getSelectionRanges(pugDocument, positions);
});
},

async doAutoInsert(document, position, context) {
return worker(document, async (pugDocument) => {

const lastCharacter = context.lastChange.text[context.lastChange.text.length - 1];

if (context.lastChange.rangeLength === 0 && lastCharacter === '=') {

const enabled = (await useConfigurationHost()?.getConfiguration<boolean>('html.autoCreateQuotes')) ?? true;

if (enabled) {

const text = pugLs.doQuoteComplete(pugDocument, position, await useConfigurationHost()?.getConfiguration<html.CompletionConfiguration>('html.completion', document.uri));

if (text) {
return text;
}
}
}
});
},
};

function worker<T>(document: TextDocument, callback: (pugDocument: pug.PugDocument) => T) {
Expand Down

0 comments on commit 9d6f5a8

Please sign in to comment.