Skip to content

Commit

Permalink
respect casing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Oct 31, 2023
1 parent 9094c97 commit 650d065
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
20 changes: 12 additions & 8 deletions extensions/vscode/src/features/dragImport.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GetDragAndDragImportEditsRequest } from '@vue/language-server';
import { GetDragAndDragImportEditsRequest, TagNameCasing } from '@vue/language-server';
import * as vscode from 'vscode';
import type { BaseLanguageClient } from 'vscode-languageclient';
import type { BaseLanguageClient, InsertTextFormat } from 'vscode-languageclient';
import { tagNameCasings } from './nameCasing';

export async function register(_context: vscode.ExtensionContext, client: BaseLanguageClient) {
vscode.languages.registerDocumentDropEditProvider(
Expand All @@ -11,15 +12,16 @@ export async function register(_context: vscode.ExtensionContext, client: BaseLa
if (mimeType === 'text/uri-list') {
const uri = item.value as string;
if (uri.endsWith('.vue')) {
let tagName = uri.substring(uri.lastIndexOf('/') + 1);
tagName = tagName.substring(0, tagName.lastIndexOf('.'));
const edits = await client.sendRequest(GetDragAndDragImportEditsRequest.type, {
const response = await client.sendRequest(GetDragAndDragImportEditsRequest.type, {
uri: document.uri.toString(),
importUri: uri,
tagName,
casing: tagNameCasings.get(document.uri.toString()) ?? TagNameCasing.Pascal,
});
if (!response) {
return;
}
const additionalEdit = new vscode.WorkspaceEdit();
for (const edit of edits ?? []) {
for (const edit of response.additionalEdits) {
additionalEdit.replace(
document.uri,
new vscode.Range(
Expand All @@ -32,7 +34,9 @@ export async function register(_context: vscode.ExtensionContext, client: BaseLa
);
}
return {
insertText: new vscode.SnippetString(`<${tagName}$0 />`),
insertText: response.insertTextFormat === 2 satisfies typeof InsertTextFormat.Snippet
? new vscode.SnippetString(response.insertText)
: response.insertText,
additionalEdit,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/language-server/src/languageServerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function createServerPlugin(connection: Connection) {
connection.onRequest(GetDragAndDragImportEditsRequest.type, async params => {
const languageService = await getService(params.uri);
if (languageService) {
return nameCasing.getDragImportEdits(ts, languageService.context, params.uri, params.importUri, params.tagName);
return nameCasing.getDragImportEdits(ts, languageService.context, params.uri, params.importUri, params.casing);
}
});

Expand Down
8 changes: 6 additions & 2 deletions packages/language-server/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ export namespace GetDragAndDragImportEditsRequest {
export type ParamsType = {
uri: string,
importUri: string,
tagName: string,
casing: TagNameCasing,
};
export type ResponseType = vscode.TextEdit[] | null | undefined;
export type ResponseType = {
insertText: string;
insertTextFormat: vscode.InsertTextFormat;
additionalEdits: vscode.TextEdit[];
} | null | undefined;
export type ErrorType = never;
export const type = new vscode.RequestType<ParamsType, ResponseType, ErrorType>('vue/dragImportEdits');
}
Expand Down
24 changes: 18 additions & 6 deletions packages/language-service/src/ideFeatures/dragImport.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { ServiceContext } from '@volar/language-service';
import { VueFile } from '@vue/language-core';
import { camelize, capitalize } from '@vue/shared';
import { camelize, capitalize, hyphenate } from '@vue/shared';
import { posix as path } from 'path';
import type * as vscode from 'vscode-languageserver-protocol';
import { createAddComponentToOptionEdit, getLastImportNode } from '../plugins/vue-extract-file';
import { TagNameCasing } from '../types';

export function getDragImportEdits(
ts: typeof import('typescript/lib/tsserverlibrary'),
ctx: ServiceContext,
uri: string,
importUri: string,
tagName: string
) {
casing: TagNameCasing
): {
insertText: string;
insertTextFormat: vscode.InsertTextFormat;
additionalEdits: vscode.TextEdit[];
} | undefined {

const newName = capitalize(camelize(tagName));
let baseName = importUri.substring(importUri.lastIndexOf('/') + 1);
baseName = baseName.substring(0, baseName.lastIndexOf('.'));

const newName = capitalize(camelize(baseName));
const document = ctx!.getTextDocument(uri)!;
const [vueFile] = ctx!.documents.getVirtualFileByUri(document.uri) as [VueFile, any];
const { sfc } = vueFile;
const script = sfc.scriptSetup ?? sfc.script;

if (!sfc.template || !script)
return [];
return;

const lastImportNode = getLastImportNode(ts, script.ast);
const edits: vscode.TextEdit[] = [
Expand Down Expand Up @@ -49,5 +57,9 @@ export function getDragImportEdits(
}
}

return edits;
return {
insertText: `<${casing === TagNameCasing.Kebab ? hyphenate(newName) : newName}$0 />`,
insertTextFormat: 2 satisfies typeof vscode.InsertTextFormat.Snippet,
additionalEdits: edits,
};
}

0 comments on commit 650d065

Please sign in to comment.