Skip to content

Commit

Permalink
refactor(language-service): decoupled from `vscode-languageserver-pro…
Browse files Browse the repository at this point in the history
…tocol`
  • Loading branch information
johnsoncodehk committed May 20, 2023
1 parent 7d1edf9 commit de08939
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 118 deletions.
2 changes: 1 addition & 1 deletion packages/vue-language-service/package.json
Expand Up @@ -32,11 +32,11 @@
"volar-service-typescript": "0.0.3",
"volar-service-typescript-twoslash-queries": "0.0.3",
"vscode-html-languageservice": "^5.0.4",
"vscode-languageserver-protocol": "^3.17.3",
"vscode-languageserver-textdocument": "^1.0.8"
},
"devDependencies": {
"@volar/kit": "1.6.7",
"vscode-languageserver-protocol": "^3.17.3",
"vscode-uri": "^3.0.7"
}
}
14 changes: 7 additions & 7 deletions packages/vue-language-service/src/ideFeatures/nameCasing.ts
Expand Up @@ -2,7 +2,7 @@ import { hyphenate } from '@vue/shared';
import { ServiceContext, VirtualFile } from '@volar/language-service';
import { checkComponentNames, getTemplateTagsAndAttrs, checkPropsOfTag, checkNativeTags } from '../helpers';
import * as vue from '@vue/language-core';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';
import { AttrNameCasing, TagNameCasing } from '../types';

export async function convertTagName(
Expand Down Expand Up @@ -36,12 +36,12 @@ export async function convertTagName(
for (const offset of offsets) {
const start = document.positionAt(template.startTagEnd + offset);
const end = document.positionAt(template.startTagEnd + offset + tagName.length);
const range = vscode.Range.create(start, end);
const range: vscode.Range = { start, end };
if (casing === TagNameCasing.Kebab && tagName !== hyphenate(componentName)) {
edits.push(vscode.TextEdit.replace(range, hyphenate(componentName)));
edits.push({ range, newText: hyphenate(componentName) });
}
if (casing === TagNameCasing.Pascal && tagName !== componentName) {
edits.push(vscode.TextEdit.replace(range, componentName));
edits.push({ range, newText: componentName });
}
}
}
Expand Down Expand Up @@ -85,12 +85,12 @@ export async function convertAttrName(
for (const offset of offsets) {
const start = document.positionAt(template.startTagEnd + offset);
const end = document.positionAt(template.startTagEnd + offset + attrName.length);
const range = vscode.Range.create(start, end);
const range: vscode.Range = { start, end };
if (casing === AttrNameCasing.Kebab && attrName !== hyphenate(propName)) {
edits.push(vscode.TextEdit.replace(range, hyphenate(propName)));
edits.push({ range, newText: hyphenate(propName) });
}
if (casing === AttrNameCasing.Camel && attrName !== propName) {
edits.push(vscode.TextEdit.replace(range, propName));
edits.push({ range, newText: propName });
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions packages/vue-language-service/src/languageService.ts
Expand Up @@ -12,7 +12,7 @@ import createTsService from 'volar-service-typescript';
import createTsTqService from 'volar-service-typescript-twoslash-queries';
import type { Data } from 'volar-service-typescript/out/services/completions/basic';
import type * as html from 'vscode-html-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';
import { getNameCasing } from './ideFeatures/nameCasing';
import createVueService from './plugins/vue';
import createAutoDotValueService from './plugins/vue-autoinsert-dotvalue';
Expand Down Expand Up @@ -184,16 +184,16 @@ function resolvePlugins(
] as any as ts.NodeArray<ts.ObjectLiteralElementLike>,
};
const printText = printer.printNode(ts.EmitHint.Expression, newNode, sfc.scriptAst);
const editRange = vscode.Range.create(
textDoc.positionAt(sfc.script.startTagEnd + exportDefault.componentsOption.start),
textDoc.positionAt(sfc.script.startTagEnd + exportDefault.componentsOption.end),
);
const editRange: vscode.Range = {
start: textDoc.positionAt(sfc.script.startTagEnd + exportDefault.componentsOption.start),
end: textDoc.positionAt(sfc.script.startTagEnd + exportDefault.componentsOption.end),
};
autoImportPositions.add(editRange.start);
autoImportPositions.add(editRange.end);
item.additionalTextEdits.push(vscode.TextEdit.replace(
editRange,
unescape(printText.replace(/\\u/g, '%u')),
));
item.additionalTextEdits.push({
range: editRange,
newText: unescape(printText.replace(/\\u/g, '%u')),
});
}
else if (exportDefault.args && exportDefault.argsNode) {
const newNode: typeof exportDefault.argsNode = {
Expand All @@ -204,16 +204,16 @@ function resolvePlugins(
] as any as ts.NodeArray<ts.ObjectLiteralElementLike>,
};
const printText = printer.printNode(ts.EmitHint.Expression, newNode, sfc.scriptAst);
const editRange = vscode.Range.create(
textDoc.positionAt(sfc.script.startTagEnd + exportDefault.args.start),
textDoc.positionAt(sfc.script.startTagEnd + exportDefault.args.end),
);
const editRange: vscode.Range = {
start: textDoc.positionAt(sfc.script.startTagEnd + exportDefault.args.start),
end: textDoc.positionAt(sfc.script.startTagEnd + exportDefault.args.end),
};
autoImportPositions.add(editRange.start);
autoImportPositions.add(editRange.end);
item.additionalTextEdits.push(vscode.TextEdit.replace(
editRange,
unescape(printText.replace(/\\u/g, '%u')),
));
item.additionalTextEdits.push({
range: editRange,
newText: unescape(printText.replace(/\\u/g, '%u')),
});
}
}
}
Expand Down Expand Up @@ -241,7 +241,7 @@ function resolvePlugins(
}
if (result?.edit?.documentChanges) {
for (const documentChange of result.edit.documentChanges) {
if (vscode.TextDocumentEdit.is(documentChange)) {
if ('textDocument' in documentChange) {
patchAdditionalTextEdits(documentChange.textDocument.uri, documentChange.edits);
}
}
Expand Down
@@ -1,7 +1,7 @@
import { AutoInsertionContext, Service } from '@volar/language-service';
import { hyphenate } from '@vue/shared';
import type * as ts from 'typescript/lib/tsserverlibrary';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';
import type { TextDocument } from 'vscode-languageserver-textdocument';

const plugin: Service = (context, modules) => {
Expand Down Expand Up @@ -94,8 +94,14 @@ export function isCharacterTyping(document: TextDocument, options: AutoInsertion

const lastCharacter = options.lastChange.text[options.lastChange.text.length - 1];
const rangeStart = options.lastChange.range.start;
const position = vscode.Position.create(rangeStart.line, rangeStart.character + options.lastChange.text.length);
const nextCharacter = document.getText(vscode.Range.create(position, document.positionAt(document.offsetAt(position) + 1)));
const position: vscode.Position = {
line: rangeStart.line,
character: rangeStart.character + options.lastChange.text.length,
};
const nextCharacter = document.getText({
start: position,
end: { line: position.line, character: position.character + 1 },
});

if (lastCharacter === undefined) { // delete text
return false;
Expand Down
Expand Up @@ -2,7 +2,6 @@ import * as embedded from '@volar/language-core';
import { VirtualFile } from '@volar/language-core';
import { Service } from '@volar/language-service';
import { VueFile } from '@vue/language-core';
import * as vscode from 'vscode-languageserver-protocol';
import { isCharacterTyping } from './vue-autoinsert-dotvalue';

const plugin: Service = (context, modules) => {
Expand Down Expand Up @@ -76,13 +75,13 @@ const plugin: Service = (context, modules) => {
.replaceAll('\\', '\\\\')
.replaceAll('$', '\\$')
.replaceAll('}', '\\}');
return vscode.TextEdit.replace(
{
start: document.positionAt(mappedRange.sourceRange[0]),
return {
range: {
start: document.positionAt(mappedRange.sourceRange[1]),
end: document.positionAt(mappedRange.sourceRange[1]),
},
'(' + escapedText + '$0' + ')',
);
newText: '(' + escapedText + '$0' + ')',
};
}
}
}
Expand Down
@@ -1,6 +1,6 @@
import { Service } from '@volar/language-service';
import { VueFile } from '@vue/language-core';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';

export default function (): Service {

Expand Down
24 changes: 12 additions & 12 deletions packages/vue-language-service/src/plugins/vue-template.ts
Expand Up @@ -2,7 +2,7 @@ import { FileRangeCapabilities, Service, SourceMapWithDocuments } from '@volar/l
import * as vue from '@vue/language-core';
import { hyphenate, capitalize, camelize } from '@vue/shared';
import * as html from 'vscode-html-languageservice';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { checkComponentNames, checkEventsOfTag, checkPropsOfTag, getElementAttrs, checkNativeTags } from '../helpers';
import { getNameCasing } from '../ideFeatures/nameCasing';
Expand Down Expand Up @@ -184,7 +184,7 @@ export default <S extends Service>(options: {
label: `${requiredProp}!`,
paddingLeft: true,
position: document.positionAt(current.labelOffset),
kind: vscode.InlayHintKind.Parameter,
kind: 2 satisfies typeof vscode.InlayHintKind.Parameter,
textEdits: [{
range: {
start: document.positionAt(current.insertOffset),
Expand Down Expand Up @@ -239,11 +239,11 @@ export default <S extends Service>(options: {
if (sfcVueTemplateCompiled) {

for (const error of sfcVueTemplateCompiled.errors) {
onCompilerError(error, vscode.DiagnosticSeverity.Error);
onCompilerError(error, 1 satisfies typeof vscode.DiagnosticSeverity.Error);
}

for (const warning of sfcVueTemplateCompiled.warnings) {
onCompilerError(warning, vscode.DiagnosticSeverity.Warning);
onCompilerError(warning, 2 satisfies typeof vscode.DiagnosticSeverity.Warning);
}

function onCompilerError(error: NonNullable<typeof sfcVueTemplateCompiled>['errors'][number], severity: vscode.DiagnosticSeverity) {
Expand Down Expand Up @@ -551,7 +551,7 @@ export default <S extends Service>(options: {
range: replacement.textEdit.range,
newText: insertText,
},
kind: vscode.CompletionItemKind.EnumMember,
kind: 20 satisfies typeof vscode.CompletionItemKind.EnumMember,
};

completionList.items.push(newItem);
Expand All @@ -577,7 +577,7 @@ export default <S extends Service>(options: {
range: replacement.textEdit.range,
newText: insertText,
},
kind: vscode.CompletionItemKind.EnumMember,
kind: 20 satisfies typeof vscode.CompletionItemKind.EnumMember,
};

completionList.items.push(newItem);
Expand Down Expand Up @@ -606,11 +606,11 @@ export default <S extends Service>(options: {

if (itemId.type === 'componentProp') {
if (componentName !== '*') {
item.kind = vscode.CompletionItemKind.Field;
item.kind = 5 satisfies typeof vscode.CompletionItemKind.Field;
}
}
else {
item.kind = componentName !== '*' ? vscode.CompletionItemKind.Function : vscode.CompletionItemKind.Event;
item.kind = componentName !== '*' ? 3 satisfies typeof vscode.CompletionItemKind.Function : 23 satisfies typeof vscode.CompletionItemKind.Event;
}
}
else if (
Expand All @@ -619,19 +619,19 @@ export default <S extends Service>(options: {
|| item.label === 'v-else'
|| item.label === 'v-for'
) {
item.kind = vscode.CompletionItemKind.Method;
item.kind = 2 satisfies typeof vscode.CompletionItemKind.Method;
item.sortText = '\u0003' + (item.sortText ?? item.label);
}
else if (item.label.startsWith('v-')) {
item.kind = vscode.CompletionItemKind.Function;
item.kind = 3 satisfies typeof vscode.CompletionItemKind.Function;
item.sortText = '\u0002' + (item.sortText ?? item.label);
}
else {
item.sortText = '\u0001' + (item.sortText ?? item.label);
}
}
else if (item.kind === vscode.CompletionItemKind.Property && componentNames.has(hyphenate(item.label))) {
item.kind = vscode.CompletionItemKind.Variable;
else if (item.kind === 10 satisfies typeof vscode.CompletionItemKind.Property && componentNames.has(hyphenate(item.label))) {
item.kind = 6 satisfies typeof vscode.CompletionItemKind.Variable;
item.sortText = '\u0000' + (item.sortText ?? item.label);
}
}
Expand Down
@@ -1,6 +1,6 @@
import { FileKind, forEachEmbeddedFile, Service } from '@volar/language-service';
import * as vue from '@vue/language-core';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';

const plugin: Service = (context, modules) => {

Expand Down
@@ -1,5 +1,5 @@
import { Service } from '@volar/language-service';
import * as vscode from 'vscode-languageserver-protocol';
import type * as vscode from 'vscode-languageserver-protocol';

const plugin: Service = (context) => {

Expand Down Expand Up @@ -42,7 +42,7 @@ const plugin: Service = (context) => {
paddingRight: hint.paddingRight,
paddingLeft: hint.paddingLeft,
position: document.positionAt(mapping.generatedRange[0]),
kind: vscode.InlayHintKind.Parameter,
kind: 2 satisfies typeof vscode.InlayHintKind.Parameter,
tooltip: {
kind: 'markdown',
value: hint.tooltip,
Expand Down

0 comments on commit de08939

Please sign in to comment.