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

Blackholable operator by config #902

Merged
merged 6 commits into from
Oct 7, 2017
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
15 changes: 15 additions & 0 deletions lib/operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,14 @@ class Operator extends Base {
}

setTextToRegister(text, selection) {
if (this.vimState.register.isUnnamed() && this.isBlackholeRegisteredOperator()) {
return
}

if (this.target.isLinewise() && !text.endsWith("\n")) {
text += "\n"
}

if (text) {
this.vimState.register.set(null, {text, selection})

Expand All @@ -254,6 +259,16 @@ class Operator extends Base {
}
}

isBlackholeRegisteredOperator() {
const operators = this.getConfig("blackholeRegisteredOperators")
const wildCardOperators = operators.filter(name => name.endsWith("*"))
const commandName = this.getCommandNameWithoutPrefix()
return (
wildCardOperators.some(name => new RegExp("^" + name.replace("*", ".*")).test(commandName)) ||
operators.includes(commandName)
)
}

needSaveToNumberedRegister(target) {
// Used to determine what register to use on change and delete operation.
// Following motion should save to 1-9 register regerdless of content is small or big.
Expand Down
12 changes: 9 additions & 3 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const RENAMED_PARAMS = [
{oldName: "keepColumnOnSelectTextObject", newName: "stayOnSelectTextObject"},
{oldName: "moveToFirstCharacterOnVerticalMotion", newName: "stayOnVerticalMotion", setValueBy: invertValue},
{oldName: "keymapSemicolonToConfirmFind", newName: "keymapSemicolonToConfirmOnFindInput"},
{
oldName: "dontUpdateRegisterOnChangeOrSubstitute",
newName: "blackholeRegisteredOperators",
setValueBy: enabled => (enabled ? ["change*", "substitute*"] : undefined),
},
]

class Settings {
Expand Down Expand Up @@ -290,10 +295,11 @@ module.exports = new Settings("vim-mode-plus", {
},
groupChangesWhenLeavingInsertMode: true,
useClipboardAsDefaultRegister: true,
dontUpdateRegisterOnChangeOrSubstitute: {
default: false,
blackholeRegisteredOperators: {
default: [],
items: {type: "string"},
description:
"When enabled, `change` and `substitute` no longer update register content<br>Affects `c`, `C`, `s`, `S` operator.",
"Comma separated list of operator command name to disable register update.<br>e.g. `delete-right, delete-left, delete, substitute`<br>Also you can use special value(`delete*`, `change*`, `substitute*`) to specify all same-family operators.",
},
startInInsertMode: false,
startInInsertModeScopes: {
Expand Down
110 changes: 110 additions & 0 deletions spec/prefix-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,113 @@ describe "Prefixes", ->
ensure 'd 2 w', text: "222 333 444 555 666 777 888 999"
it "repeat operator and motion respectively", ->
ensure '3 d 2 w', text: "666 777 888 999"
describe "Count modifier", ->
beforeEach ->
set
text: "000 111 222 333 444 555 666 777 888 999"
cursor: [0, 0]

it "repeat operator", ->
ensure '3 d w', text: "333 444 555 666 777 888 999"
it "repeat motion", ->
ensure 'd 2 w', text: "222 333 444 555 666 777 888 999"
it "repeat operator and motion respectively", ->
ensure '3 d 2 w', text: "666 777 888 999"

describe "blackholeRegisteredOperators settings", ->
originalText = "initial clipboard content"
beforeEach ->
set
textC: "a|bc"

describe "when false(default)", ->
it "default", -> ensure register: {'"': text: originalText}
it 'c update', -> ensure 'c l', register: {'"': text: 'b'}
it 'C update', -> ensure 'C', register: {'"': text: 'bc'}
it 'x update', -> ensure 'x', register: {'"': text: 'b'}
it 'X update', -> ensure 'X', register: {'"': text: 'a'}
it 'y update', -> ensure 'y l', register: {'"': text: 'b'}
it 'Y update', -> ensure 'Y', register: {'"': text: "abc\n"}
it 's update', -> ensure 's', register: {'"': text: 'b'}
it 'S update', -> ensure 'S', register: {'"': text: 'abc\n'}
it 'd update', -> ensure 'd l', register: {'"': text: 'b'}
it 'D update', -> ensure 'D', register: {'"': text: 'bc'}

describe "when true(default)", ->
describe "blackhole all", ->
beforeEach ->
settings.set "blackholeRegisteredOperators", [
"change" # c
"change-to-last-character-of-line" # C
"change-line" # C in visual
"change-occurrence"
"change-occurrence-from-search"
"delete" # d
"delete-to-last-character-of-line" # D
"delete-line" # D in visual
"delete-right" # x
"delete-left" # X
"substitute" # s
"substitute-line" # S
"yank" # y
"yank-line" # Y
# "delete*"
# "change*"
# "yank*"
# "substitute*"
]

it "default", -> ensure register: {'"': text: originalText}
it 'c NOT update', -> ensure 'c l', register: {'"': text: originalText}
it 'C NOT update', -> ensure 'C', register: {'"': text: originalText}
it 'x NOT update', -> ensure 'x', register: {'"': text: originalText}
it 'X NOT update', -> ensure 'X', register: {'"': text: originalText}
it 'y NOT update', -> ensure 'y l', register: {'"': text: originalText}
it 'Y NOT update', -> ensure 'Y', register: {'"': text: originalText}
it 's NOT update', -> ensure 's', register: {'"': text: originalText}
it 'S NOT update', -> ensure 'S', register: {'"': text: originalText}
it 'd NOT update', -> ensure 'd l', register: {'"': text: originalText}
it 'D NOT update', -> ensure 'D', register: {'"': text: originalText}

describe "blackhole selectively", ->
beforeEach ->
settings.set "blackholeRegisteredOperators", [
"change-to-last-character-of-line" # C
"delete-right" # x
"substitute" # s
]

it "default", -> ensure register: {'"': text: originalText}
it 'c update', -> ensure 'c l', register: {'"': text: 'b'}
it 'C NOT update', -> ensure 'C', register: {'"': text: originalText}
it 'x NOT update', -> ensure 'x', register: {'"': text: originalText}
it 'X update', -> ensure 'X', register: {'"': text: 'a'}
it 'y update', -> ensure 'y l', register: {'"': text: 'b'}
it 'Y update', -> ensure 'Y', register: {'"': text: "abc\n"}
it 's NOT update', -> ensure 's', register: {'"': text: originalText}
it 'S update', -> ensure 'S', register: {'"': text: 'abc\n'}
it 'd update', -> ensure 'd l', register: {'"': text: 'b'}
it 'D update', -> ensure 'D', register: {'"': text: 'bc'}

describe "blackhole by wildcard", ->
beforeEach ->
settings.set "blackholeRegisteredOperators", [
"change*" # C
"delete*" # x
# "substitute*" # s
# "yank*"
]

it "default", -> ensure register: {'"': text: originalText}
it 'c NOT update', -> ensure 'c l', register: {'"': text: originalText}
it 'c update if specified', -> ensure '" a c l', register: {'a': text: "b"}
it 'c NOT update', -> ensure 'c l', register: {'"': text: originalText}
it 'C NOT update', -> ensure 'C', register: {'"': text: originalText}
it 'x NOT update', -> ensure 'x', register: {'"': text: originalText}
it 'X NOT update', -> ensure 'X', register: {'"': text: originalText}
it 'y update', -> ensure 'y l', register: {'"': text: 'b'}
it 'Y update', -> ensure 'Y', register: {'"': text: "abc\n"}
it 's update', -> ensure 's', register: {'"': text: 'b'}
it 'S update', -> ensure 'S', register: {'"': text: 'abc\n'}
it 'd NOT update', -> ensure 'd l', register: {'"': text: originalText}
it 'D NOT update', -> ensure 'D', register: {'"': text: originalText}