Skip to content

Commit

Permalink
chore(list): support moving by word in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
YongJieYongJie committed Jan 22, 2024
1 parent ddf40ce commit 40165f3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
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

0 comments on commit 40165f3

Please sign in to comment.