diff --git a/package.json b/package.json index 9e9f1c4..26a9f14 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,10 @@ { "command": "migrateRequireToImports", "title": "Migrate Require to Imports" + }, + { + "command": "wrapIntoNewTag", + "title": "Wrap Into New Tag" } ], "keybindings": [ @@ -169,7 +173,7 @@ "@vue/language-server": "latest", "@vue/language-service": "latest", "@zardoy/utils": "^0.0.9", - "@zardoy/vscode-utils": "^0.0.47", + "@zardoy/vscode-utils": "^0.0.52", "chai": "^4.3.6", "change-case": "^4.1.2", "chokidar": "^3.5.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 39c742e..d0991fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -45,8 +45,8 @@ importers: specifier: ^0.0.9 version: 0.0.9 '@zardoy/vscode-utils': - specifier: ^0.0.47 - version: 0.0.47(@types/vscode@1.72.0)(esbuild@0.15.18)(vscode-framework@0.0.18) + specifier: ^0.0.52 + version: 0.0.52(@types/vscode@1.72.0)(esbuild@0.15.18)(vscode-framework@0.0.18) chai: specifier: ^4.3.6 version: 4.3.6 @@ -1090,8 +1090,8 @@ packages: type-fest: 2.19.0 dev: false - /@zardoy/vscode-utils@0.0.47(@types/vscode@1.72.0)(esbuild@0.15.18)(vscode-framework@0.0.18): - resolution: {integrity: sha512-0xsdTonXFxcBdsHcWgkSfJSpP9VOvOiT6oo/LuA5FOGJJI/5W6e6EDwN6Y9FahRWdrYmOfliekEMGZGk/eEWcQ==} + /@zardoy/vscode-utils@0.0.52(@types/vscode@1.72.0)(esbuild@0.15.18)(vscode-framework@0.0.18): + resolution: {integrity: sha512-e+HvIdzALhuFtnub6jzNARge78GPnYuW/+b1jMO0p7A/LjjYCPhFIPjhj/1tf5SHtpbiy06mDb6z8HpGGUhFpg==} engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0} hasBin: true peerDependencies: @@ -1126,7 +1126,7 @@ packages: untildify: 4.0.0 vscode-framework: 0.0.18(@types/vscode@1.72.0)(typescript@5.3.3) vscode-manifest: 0.0.8 - vscode-uri: 3.0.6 + vscode-uri: 3.0.8 dev: false /accepts@1.3.7: diff --git a/src/specialCommands.ts b/src/specialCommands.ts index 9b4d9dd..0e8a55a 100644 --- a/src/specialCommands.ts +++ b/src/specialCommands.ts @@ -1,10 +1,12 @@ import * as vscode from 'vscode' import { getActiveRegularEditor } from '@zardoy/vscode-utils' import { getExtensionCommandId, getExtensionSetting, registerExtensionCommand, VSCodeQuickPickItem } from 'vscode-framework' +import { registerTextEditorCommand } from '@zardoy/vscode-utils/build/commands' import { showQuickPick } from '@zardoy/vscode-utils/build/quickPick' import _ from 'lodash' import { compact } from '@zardoy/utils' import { offsetPosition } from '@zardoy/vscode-utils/build/position' +import { defaultJsSupersetLangs } from '@zardoy/vscode-utils/build/langs' import { RequestInputTypes, RequestOutputTypes } from '../typescript/src/ipcTypes' import { sendCommand } from './sendCommand' import { tsRangeToVscode, tsRangeToVscodeSelection, tsTextChangesToVscodeTextEdits } from './util' @@ -319,6 +321,37 @@ export default () => { await vscode.workspace.applyEdit(edit) }) + registerTextEditorCommand('wrapIntoNewTag', async (editor, edit, fallbackCommand = 'editor.emmet.action.wrapWithAbbreviation') => { + const { selection } = editor + if (selection.start.isEqual(selection.end)) { + const range = editor.selection + const selectedText = '$TM_SELECTED_TEXT' + + if (!defaultJsSupersetLangs.includes(editor.document.languageId)) { + if (fallbackCommand) { + await vscode.commands.executeCommand(fallbackCommand, ...fallbackCommand.split(' ').slice(1)) + } + + return + } + + const data = (await sendCommand('getNodePath', {})) ?? [] + const jsxElem = [...data].reverse().find(d => ['JsxElement', 'JsxSelfClosingElement', 'JsxFragment'].includes(d.kindName)) + if (!jsxElem) return + const { start, end } = jsxElem + const startPos = editor.document.positionAt(start) + const startRange = new vscode.Range(startPos, startPos) + const endPos = editor.document.positionAt(end) + const endRange = new vscode.Range(endPos, endPos) + editor.selection = new vscode.Selection(startRange.start, endRange.end) + } + + const line = editor.document.lineAt(editor.selection.start.line) + const currentIndent = line.text.slice(0, line.firstNonWhitespaceCharacterIndex) + await editor.insertSnippet(new vscode.SnippetString(`<\${1:div}$0>\n\t$TM_SELECTED_TEXT\n`), editor.selection) + return + }) + // registerExtensionCommand('insertImportFlatten', () => { // // got -> default, got // type A = ts.Type diff --git a/typescript/src/ipcTypes.ts b/typescript/src/ipcTypes.ts index 2bd55d9..c45fc52 100644 --- a/typescript/src/ipcTypes.ts +++ b/typescript/src/ipcTypes.ts @@ -77,6 +77,8 @@ export type RequestInputTypes = { * @keysSuggestions TriggerCharacterCommand */ export type RequestOutputTypes = { + getNodePath: NodeAtPositionResponse[] + getNodeAtPosition: NodeAtPositionResponse removeFunctionArgumentsTypesInSelection: { ranges: TsRange[] }