diff --git a/README.md b/README.md index 676dfe7..f58eb5a 100644 --- a/README.md +++ b/README.md @@ -51,14 +51,23 @@ Some additional tips for using overtype paste: - I know this doesn't work like [insert editor here]. Every single freaking editor handles overtype paste differently. It's not my fault. - If you think you have a saner way to handle this, for the love of everything warm and cuddly, [MAKE A PULL REQUEST](https://github.com/DrMerfy/vscode-overtype/pulls). -Without further ado... +### Other options +#### Paste ```json "overtype.paste": true ``` > When in overtype mode, uses overtype behavior when pasting text. +#### Hard Backspace +```json +"overtype.hardBackspace": true +``` +> When in overtype mode, backspaced characters become spaces. + +![Basic demo](images/hard-backspace-demo.gif) + ### Adjusted indicators in status bar (abbreviated, localized or none) Horizontal screen space at a premium? Have too many things in your status bar already? diff --git a/images/hard-backspace-demo.gif b/images/hard-backspace-demo.gif new file mode 100644 index 0000000..103329c Binary files /dev/null and b/images/hard-backspace-demo.gif differ diff --git a/package.json b/package.json index 409bece..8533ec6 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,11 @@ "type": "string", "default": "Overtype", "description": "Label text in the status bar shown during Overtype Mode, may be empty." + }, + "overtype.hardBackspace": { + "type": "boolean", + "default": false, + "description": "When in overtype mode, backspaced characters become spaces." } } }, @@ -80,6 +85,11 @@ "key": "ctrl+shift+i", "mac": "cmd+shift+i", "when": "editorFocus" + }, + { + "key": "backspace", + "command": "hardBackspace", + "when": "editorFocus" } ] }, diff --git a/src/behavior.ts b/src/behavior.ts index 48f729e..2a6aa25 100644 --- a/src/behavior.ts +++ b/src/behavior.ts @@ -55,3 +55,26 @@ export const overtypeBeforePaste = (editor: vscode.TextEditor, text: string, pas } }); } + +export const overtypeBeforeBackspace = (editor: vscode.TextEditor) => { + let selection = editor.selection + if (selection.isEmpty) { + // select the character to the left of the cursor + selection = new vscode.Selection( + selection.start.translate(0, -1), + selection.start + ) + } + + // replace text selection with spaces + const text = editor.document.getText(selection) + editor.edit((editBuilder) => { + editBuilder.replace(selection, " ".repeat(text.length)) + }) + + // move the cursor to the left if the original selection was empty + if (editor.selection.isEmpty) { + const newPos = editor.selection.start.translate(0, -1) + editor.selection = new vscode.Selection(newPos, newPos) + } +} diff --git a/src/configuration.ts b/src/configuration.ts index c06125c..181185a 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -28,6 +28,7 @@ const loadConfiguration = () => { return { paste: overtypeConfiguration.get("paste"), + hardBackspace: overtypeConfiguration.get("hardBackspace"), perEditor: overtypeConfiguration.get("perEditor") ? true : false, labelInsertMode: overtypeConfiguration.get("labelInsertMode"), @@ -57,6 +58,7 @@ export const reloadConfiguration = () => { if (configuration.labelInsertMode === newConfiguration.labelInsertMode && configuration.labelOvertypeMode === newConfiguration.labelOvertypeMode && configuration.paste === newConfiguration.paste && + configuration.hardBackspace === newConfiguration.hardBackspace && configuration.perEditor === newConfiguration.perEditor && configuration.defaultCursorStyle === newConfiguration.defaultCursorStyle && configuration.secondaryCursorStyle === newConfiguration.secondaryCursorStyle) { @@ -66,6 +68,7 @@ export const reloadConfiguration = () => { configuration.labelInsertMode = newConfiguration.labelInsertMode; configuration.labelOvertypeMode = newConfiguration.labelOvertypeMode; configuration.paste = newConfiguration.paste; + configuration.hardBackspace = newConfiguration.hardBackspace; configuration.perEditor = newConfiguration.perEditor; configuration.secondaryCursorStyle = newConfiguration.secondaryCursorStyle; diff --git a/src/extension.ts b/src/extension.ts index 9527ef7..541f9c9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,6 @@ import * as vscode from "vscode"; -import { overtypeBeforePaste, overtypeBeforeType } from "./behavior"; +import { overtypeBeforeBackspace, overtypeBeforePaste, overtypeBeforeType } from "./behavior"; import { configuration, reloadConfiguration } from "./configuration"; import { getMode, resetModes, toggleMode } from "./mode"; import { createStatusBarItem, destroyStatusBarItem, updateStatusBarItem } from "./statusBarItem"; @@ -16,6 +16,7 @@ export const activate = (context: vscode.ExtensionContext) => { vscode.commands.registerCommand("type", typeCommand), vscode.commands.registerCommand("paste", pasteCommand), + vscode.commands.registerCommand("hardBackspace", backspaceCommand), vscode.window.onDidChangeActiveTextEditor(activeTextEditorChanged), @@ -122,6 +123,18 @@ const typeCommand = (args: { text: string }) => { return vscode.commands.executeCommand("default:type", args); } +const backspaceCommand = () => { + if (shouldPerformOvertype() && configuration.hardBackspace) { + const editor = vscode.window.activeTextEditor + if (editor) { + overtypeBeforeBackspace(editor) + } + return + } + + return vscode.commands.executeCommand("deleteLeft"); +} + const pasteCommand = (args: { text: string, pasteOnNewLine: boolean }) => { if (configuration.paste && shouldPerformOvertype()) { const editor = vscode.window.activeTextEditor;