From afe521141e0de80eec04bb28d29442d589ca5df2 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Mon, 29 Jan 2024 20:25:49 +0530 Subject: [PATCH 1/2] wip --- src/specialCommands.ts | 22 ++++++++++++++++++++++ typescript/src/ipcTypes.ts | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/specialCommands.ts b/src/specialCommands.ts index 0d72413..f43c26e 100644 --- a/src/specialCommands.ts +++ b/src/specialCommands.ts @@ -1,6 +1,7 @@ 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' @@ -319,6 +320,27 @@ export default () => { await vscode.workspace.applyEdit(edit) }) + registerTextEditorCommand('wrapIntoNewTag', async (editor, edit, fallbackCommand = 'editor.emmet.action.wrapWithAbbreviation') => { + const range = editor.selection + const selectedText = '$TM_SELECTED_TEXT' + if (editor.selection) { + 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 + } + + const data = await sendCommand('getNodePath', {}) + const jsxElem = data?.find(d => d.kindName === 'JsxElement') + 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) + }) + // registerExtensionCommand('insertImportFlatten', () => { // // got -> default, got // type A = ts.Type diff --git a/typescript/src/ipcTypes.ts b/typescript/src/ipcTypes.ts index 91dcadc..b9726eb 100644 --- a/typescript/src/ipcTypes.ts +++ b/typescript/src/ipcTypes.ts @@ -76,6 +76,8 @@ export type RequestInputTypes = { * @keysSuggestions TriggerCharacterCommand */ export type RequestOutputTypes = { + getNodePath: NodeAtPositionResponse[] + getNodeAtPosition: NodeAtPositionResponse removeFunctionArgumentsTypesInSelection: { ranges: TsRange[] } From 238dd23c5a2cf4c75bed49debf1d3422082a45ad Mon Sep 17 00:00:00 2001 From: Vitaly Date: Sat, 3 Feb 2024 19:37:27 +0300 Subject: [PATCH 2/2] done --- package.json | 6 +++++- pnpm-lock.yaml | 10 +++++----- src/specialCommands.ts | 43 ++++++++++++++++++++++++++---------------- 3 files changed, 37 insertions(+), 22 deletions(-) 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 0b1584f..0e8a55a 100644 --- a/src/specialCommands.ts +++ b/src/specialCommands.ts @@ -6,6 +6,7 @@ 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' @@ -321,24 +322,34 @@ export default () => { }) registerTextEditorCommand('wrapIntoNewTag', async (editor, edit, fallbackCommand = 'editor.emmet.action.wrapWithAbbreviation') => { - const range = editor.selection - const selectedText = '$TM_SELECTED_TEXT' - if (editor.selection) { - 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 + 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 data = await sendCommand('getNodePath', {}) - const jsxElem = data?.find(d => d.kindName === 'JsxElement') - 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', () => {