Created
October 28, 2025 10:34
-
-
Save hyrious/2e6900b4f97d0e9932ee08df706f462e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as vscode from 'vscode'; | |
| import redent from 'redent'; | |
| export function activate(context: vscode.ExtensionContext) { | |
| vscode.commands.registerCommand('selfhost.pasteWithIndent', async () => { | |
| const text = await vscode.env.clipboard.readText() | |
| if (!text) return; | |
| const editor = vscode.window.activeTextEditor | |
| if (editor) { | |
| // Get cursor indentation by calculating the whitespace before the cursor | |
| let cursorLine = editor.document.lineAt(editor.selection.active.line); | |
| let cursorIndent = cursorLine.text.substring(0, editor.selection.active.character).match(/^\s*/)?.[0] || ''; | |
| // Redent the clipboard text to match the cursor indentation | |
| const redentedText = redent(text, cursorIndent.length).trimStart(); | |
| editor.edit(b => b.insert(editor.selection.active, redentedText)) | |
| } | |
| }) | |
| } | |
| /** package.json | |
| "contributes": { | |
| "commands": [ | |
| { | |
| "command": "selfhost.pasteWithIndent", | |
| "title": "Selfhost: Paste With Indent" | |
| } | |
| ], | |
| "keybindings": [ | |
| { | |
| "command": "selfhost.pasteWithIndent", | |
| "key": "cmd+shift+v", | |
| "when": "editorTextFocus" | |
| } | |
| ] | |
| }, | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment