Skip to content
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

(feature) Add hard backspace option #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
Binary file added images/hard-backspace-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
},
Expand All @@ -80,6 +85,11 @@
"key": "ctrl+shift+i",
"mac": "cmd+shift+i",
"when": "editorFocus"
},
{
"key": "backspace",
"command": "hardBackspace",
"when": "editorFocus"
}
]
},
Expand Down
23 changes: 23 additions & 0 deletions src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const loadConfiguration = () => {

return {
paste: overtypeConfiguration.get<boolean>("paste"),
hardBackspace: overtypeConfiguration.get<boolean>("hardBackspace"),
perEditor: overtypeConfiguration.get<boolean>("perEditor") ? true : false,

labelInsertMode: overtypeConfiguration.get<string>("labelInsertMode"),
Expand Down Expand Up @@ -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) {
Expand All @@ -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;

Expand Down
15 changes: 14 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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),

Expand Down Expand Up @@ -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;
Expand Down