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

chore(list): support moving by word in prompt #4867

Merged
merged 1 commit into from
Jan 22, 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
2 changes: 2 additions & 0 deletions doc/coc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3760,6 +3760,8 @@ The mapping expression should be `command:arguments`, available commands:
'end' - move cursor to end.
'left' - move cursor left.
'right' - move cursor right.
'leftword' - move cursor left by a word.
'rightword' - move cursor right by a word.
'deleteforward' - remove previous character.
'deletebackward' - remove next character.
'removetail' - remove characters afterwards.
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/list/mappings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,26 @@ describe('list insert mappings', () => {
expect(input).toBe('afc')
})

it('should move cursor by leftword and rightword', async () => {
let revert = helper.updateConfiguration('list.insertMappings', {
'<A-b>': 'prompt:leftword',
'<A-f>': 'prompt:rightword',
})
await manager.start(['location'])
await manager.session.ui.ready
await helper.listInput('aaa bbb ccc') // -> aaa bbb ccc|
await helper.listInput('<A-b>') // -> aaa bbb |ccc
await helper.listInput('<A-b>') // -> aaa |bbb ccc
await helper.listInput('ddd ') // -> aaa ddd |bbb ccc
await helper.listInput('<A-f>') // -> aaa ddd bbb |ccc
await helper.listInput('eee ') // -> aaa ddd bbb eee |ccc
expect(manager.mappings.hasUserMapping('insert', '<A-b>')).toBe(true)
expect(manager.mappings.hasUserMapping('insert', '<A-f>')).toBe(true)
let input = manager.prompt.input
revert()
expect(input).toBe('aaa ddd bbb eee ccc')
})

it('should move cursor by <end> and <home>', async () => {
await manager.start(['location'])
await manager.session.ui.ready
Expand Down
6 changes: 6 additions & 0 deletions src/list/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ export default class Mappings {
this.addAction('prompt:right', () => {
prompt.moveRight()
})
this.addAction('prompt:leftword', () => {
prompt.moveLeftWord()
})
this.addAction('prompt:rightword', () => {
prompt.moveRightWord()
})
this.addAction('prompt:deleteforward', () => {
prompt.onBackspace()
})
Expand Down
22 changes: 22 additions & 0 deletions src/list/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ export default class Prompt {
this.drawPrompt()
}

public moveLeftWord(): void {
// Reuses logic from removeWord(), except that we only update the
// cursorIndex and don't actually remove the word.
let { cusorIndex, input } = this
if (cusorIndex == 0) return
let pre = input.slice(0, cusorIndex)
let remain = pre.replace(/[\w$]+([^\w$]+)?$/, '')
this.cusorIndex = cusorIndex - (pre.length - remain.length)
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}

public moveRightWord(): void {
let { cusorIndex, input } = this
if (cusorIndex == input.length) return
let post = input.slice(cusorIndex)
let nextWord = post.match(/[\w$]+ */).at(0) ?? post
this.cusorIndex = cusorIndex + nextWord.length
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}

public moveToEnd(): void {
if (this.cusorIndex == this._input.length) return
this.cusorIndex = this._input.length
Expand Down
Loading