From 72403396272612595c0864648e4ef521f34c8b2d Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Tue, 31 Oct 2023 12:39:08 +0800 Subject: [PATCH] support for vitepress, petite-vue --- extensions/vscode/src/features/dragImport.ts | 94 ++++++++++++-------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/extensions/vscode/src/features/dragImport.ts b/extensions/vscode/src/features/dragImport.ts index 978df5b653..46878bd4ed 100644 --- a/extensions/vscode/src/features/dragImport.ts +++ b/extensions/vscode/src/features/dragImport.ts @@ -1,48 +1,64 @@ import { GetDragAndDragImportEditsRequest, TagNameCasing } from '@vue/language-server'; import * as vscode from 'vscode'; -import type { BaseLanguageClient, InsertTextFormat } from 'vscode-languageclient'; +import type { BaseLanguageClient, DocumentFilter, InsertTextFormat } from 'vscode-languageclient'; import { tagNameCasings } from './nameCasing'; +import { config } from '../config'; -export async function register(_context: vscode.ExtensionContext, client: BaseLanguageClient) { - vscode.languages.registerDocumentDropEditProvider( - { language: 'vue' }, - { - async provideDocumentDropEdits(document, _position, dataTransfer) { - for (const [mimeType, item] of dataTransfer) { - if (mimeType === 'text/uri-list') { - const uri = item.value as string; - if (uri.endsWith('.vue')) { - const response = await client.sendRequest(GetDragAndDragImportEditsRequest.type, { - uri: document.uri.toString(), - importUri: uri, - casing: tagNameCasings.get(document.uri.toString()) ?? TagNameCasing.Pascal, - }); - if (!response) { - return; - } - const additionalEdit = new vscode.WorkspaceEdit(); - for (const edit of response.additionalEdits) { - additionalEdit.replace( - document.uri, - new vscode.Range( - edit.range.start.line, - edit.range.start.character, - edit.range.end.line, - edit.range.end.character, - ), - edit.newText - ); +export async function register(context: vscode.ExtensionContext, client: BaseLanguageClient) { + + const selectors: DocumentFilter[] = [{ language: 'vue' }]; + + if (config.server.petiteVue.supportHtmlFile) { + selectors.push({ language: 'html' }); + } + if (config.server.vitePress.supportMdFile) { + selectors.push({ language: 'markdown' }); + } + + context.subscriptions.push( + vscode.languages.registerDocumentDropEditProvider( + selectors, + { + async provideDocumentDropEdits(document, _position, dataTransfer) { + for (const [mimeType, item] of dataTransfer) { + if (mimeType === 'text/uri-list') { + const uri = item.value as string; + if ( + uri.endsWith('.vue') + || (uri.endsWith('.md') && config.server.vitePress.supportMdFile) + ) { + const response = await client.sendRequest(GetDragAndDragImportEditsRequest.type, { + uri: document.uri.toString(), + importUri: uri, + casing: tagNameCasings.get(document.uri.toString()) ?? TagNameCasing.Pascal, + }); + if (!response) { + return; + } + const additionalEdit = new vscode.WorkspaceEdit(); + for (const edit of response.additionalEdits) { + additionalEdit.replace( + document.uri, + new vscode.Range( + edit.range.start.line, + edit.range.start.character, + edit.range.end.line, + edit.range.end.character, + ), + edit.newText + ); + } + return { + insertText: response.insertTextFormat === 2 satisfies typeof InsertTextFormat.Snippet + ? new vscode.SnippetString(response.insertText) + : response.insertText, + additionalEdit, + }; } - return { - insertText: response.insertTextFormat === 2 satisfies typeof InsertTextFormat.Snippet - ? new vscode.SnippetString(response.insertText) - : response.insertText, - additionalEdit, - }; } } - } - }, - } + }, + } + ), ); }