Skip to content

Commit

Permalink
feat(html, pug, typescript): update auto insertion confition
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 2, 2024
1 parent 8e79cce commit 0897678
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
20 changes: 9 additions & 11 deletions packages/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,35 +323,33 @@ export function create({
});
},

async provideAutoInsertionEdit(document, position, lastChange) {
async provideAutoInsertionEdit(document, selection, change) {
// selection must at end of change
if (document.offsetAt(selection) !== change.rangeOffset + change.text.length) {
return;
}
return worker(document, async htmlDocument => {

const lastCharacter = lastChange.text[lastChange.text.length - 1];
const rangeLengthIsZero = lastChange.range.start.line === lastChange.range.end.line
&& lastChange.range.start.character === lastChange.range.end.character;

if (rangeLengthIsZero && lastCharacter === '=') {
if (change.rangeLength === 0 && change.text.endsWith('=')) {

const enabled = await isAutoCreateQuotesEnabled(document, context);

if (enabled) {

const completionConfiguration = await getCompletionConfiguration(document, context);
const text = htmlLs.doQuoteComplete(document, position, htmlDocument, completionConfiguration);
const text = htmlLs.doQuoteComplete(document, selection, htmlDocument, completionConfiguration);

if (text) {
return text;
}
}
}

if (rangeLengthIsZero && (lastCharacter === '>' || lastCharacter === '/')) {
if (change.rangeLength === 0 && (change.text.endsWith('>') || change.text.endsWith('/'))) {

const enabled = await isAutoClosingTagsEnabled(document, context);

if (enabled) {

const text = htmlLs.doTagComplete(document, position, htmlDocument);
const text = htmlLs.doTagComplete(document, selection, htmlDocument);

if (text) {
return text;
Expand Down
15 changes: 7 additions & 8 deletions packages/pug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,19 @@ export function create({
});
},

async provideAutoInsertionEdit(document, position, lastChange) {
async provideAutoInsertionEdit(document, selection, change) {
// selection must at end of change
if (document.offsetAt(selection) !== change.rangeOffset + change.text.length) {
return;
}
return worker(document, async pugDocument => {

const lastCharacter = lastChange.text[lastChange.text.length - 1];
const rangeLengthIsZero = lastChange.range.start.line === lastChange.range.end.line
&& lastChange.range.start.character === lastChange.range.end.character;

if (rangeLengthIsZero && lastCharacter === '=') {
if (change.rangeLength === 0 && change.text.endsWith('=')) {

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

if (enabled) {

const text = pugLs.doQuoteComplete(pugDocument, position, await context.env.getConfiguration?.<html.CompletionConfiguration>('html.completion'));
const text = pugLs.doQuoteComplete(pugDocument, selection, await context.env.getConfiguration?.<html.CompletionConfiguration>('html.completion'));

if (text) {
return text;
Expand Down
10 changes: 7 additions & 3 deletions packages/typescript/lib/plugins/syntactic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ export function create(

return {

async provideAutoInsertionEdit(document, position, lastChange) {
async provideAutoInsertionEdit(document, selection, change) {
// selection must at end of change
if (document.offsetAt(selection) !== change.rangeOffset + change.text.length) {
return;
}
if (
(document.languageId === 'javascriptreact' || document.languageId === 'typescriptreact')
&& lastChange.text.endsWith('>')
&& change.text.endsWith('>')
&& await isAutoClosingTagsEnabled(document, context)
) {
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const close = languageService.getJsxClosingTagAtPosition(fileName, document.offsetAt(position));
const close = languageService.getJsxClosingTagAtPosition(fileName, document.offsetAt(selection));
if (close) {
return '$0' + close.newText;
}
Expand Down

0 comments on commit 0897678

Please sign in to comment.