Skip to content

feat: New command: Wrap Into New Tag #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
{
"command": "migrateRequireToImports",
"title": "Migrate Require to Imports"
},
{
"command": "wrapIntoNewTag",
"title": "Wrap Into New Tag"
}
],
"keybindings": [
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions src/specialCommands.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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</\${1:div}>`), editor.selection)
return
})

// registerExtensionCommand('insertImportFlatten', () => {
// // got -> default, got
// type A = ts.Type
Expand Down
2 changes: 2 additions & 0 deletions typescript/src/ipcTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export type RequestInputTypes = {
* @keysSuggestions TriggerCharacterCommand
*/
export type RequestOutputTypes = {
getNodePath: NodeAtPositionResponse[]
getNodeAtPosition: NodeAtPositionResponse
removeFunctionArgumentsTypesInSelection: {
ranges: TsRange[]
}
Expand Down