-
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(vim) add find support with: "/", "n" and "N"
- Loading branch information
1 parent
3693f6f
commit 2edf7f8
Showing
7 changed files
with
269 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
/* global DOM */ | ||
|
||
const fullstore = require('fullstore'); | ||
const limier = require('limier'); | ||
const Info = DOM.CurrentInfo; | ||
|
||
const searchStore = fullstore([]); | ||
const searchIndex = fullstore(0); | ||
|
||
module.exports.find = (value) => { | ||
const names = Info.files.map(DOM.getCurrentName); | ||
const result = limier(value, names); | ||
|
||
searchStore(result); | ||
searchIndex(0); | ||
|
||
DOM.setCurrentByName(result[0]); | ||
}; | ||
|
||
module.exports.findNext = () => { | ||
const names = searchStore(); | ||
const index = next(searchIndex(), names.length); | ||
|
||
searchIndex(index); | ||
DOM.setCurrentByName(names[searchIndex()]); | ||
}; | ||
|
||
module.exports.findPrevious = () => { | ||
const names = searchStore(); | ||
const index = previous(searchIndex(), names.length); | ||
|
||
searchIndex(index); | ||
DOM.setCurrentByName(names[index]); | ||
}; | ||
|
||
module.exports._next = next; | ||
module.exports._previous = previous; | ||
|
||
function next(index, length) { | ||
if (index === length - 1) | ||
return 0; | ||
|
||
return ++index; | ||
} | ||
|
||
function previous(index, length) { | ||
if (!index) | ||
return length - 1; | ||
|
||
return --index; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict'; | ||
|
||
const test = require('tape'); | ||
const diff = require('sinon-called-with-diff'); | ||
const sinon = diff(require('sinon')); | ||
const dir = '../../../../client/key/vim/'; | ||
|
||
const { | ||
getDOM, | ||
getCloudCmd, | ||
} = require('./globals'); | ||
|
||
global.DOM = global.DOM || getDOM(); | ||
global.CloudCmd = global.CloudCmd || getCloudCmd(); | ||
|
||
const DOM = global.DOM | ||
const CloudCmd = global.CloudCmd; | ||
|
||
const { | ||
find, | ||
findNext, | ||
findPrevious, | ||
_next, | ||
_previous, | ||
} = require(dir + 'find'); | ||
|
||
test('cloudcmd: client: vim: find', (t) => { | ||
const setCurrentByName = sinon.stub(); | ||
DOM.setCurrentByName = setCurrentByName; | ||
DOM.Dialog.prompt = Promise.resolve.bind(Promise); | ||
|
||
find(''); | ||
|
||
t.ok(setCurrentByName.calledWith(), 'should call setCurrentByName'); | ||
t.end(); | ||
}); | ||
|
||
test('cloudcmd: client: vim: findNext', (t) => { | ||
const setCurrentByName = sinon.stub(); | ||
DOM.setCurrentByName = setCurrentByName; | ||
|
||
findNext(); | ||
|
||
t.ok(setCurrentByName.calledWith(), 'should call setCurrentByName'); | ||
t.end(); | ||
}); | ||
|
||
test('cloudcmd: client: vim: findPrevious', (t) => { | ||
const setCurrentByName = sinon.stub(); | ||
DOM.setCurrentByName = setCurrentByName; | ||
|
||
findPrevious(); | ||
|
||
t.ok(setCurrentByName.calledWith(), 'should call setCurrentByName'); | ||
t.end(); | ||
}); | ||
|
||
test('cloudcmd: client: vim: _next', (t) => { | ||
const result = _next(1, 2) | ||
|
||
t.notOk(result, 'should return 0'); | ||
t.end(); | ||
}); | ||
|
||
test('cloudcmd: client: vim: _previous', (t) => { | ||
const result = _previous(0, 2) | ||
|
||
t.equal(result, 1, 'should return 1'); | ||
t.end(); | ||
}); | ||
|
||
function clean(path) { | ||
delete require.cache[require.resolve(path)]; | ||
} | ||
|
||
function stub(name, fn) { | ||
require.cache[require.resolve(name)].exports = fn; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
module.exports.getDOM = () => { | ||
const resolve = Promise.resolve.bind(Promise); | ||
const CurrentInfo = { | ||
element: {}, | ||
files: [], | ||
}; | ||
|
||
const noop = () => {}; | ||
const Buffer = { | ||
copy: noop, | ||
paste: noop, | ||
}; | ||
|
||
const Dialog = { | ||
prompt: resolve | ||
}; | ||
|
||
return { | ||
Buffer, | ||
CurrentInfo, | ||
Dialog, | ||
selectFile: noop, | ||
unselectFile: noop, | ||
unselectFiles: noop, | ||
setCurrentFile: noop, | ||
getCurrentName: noop, | ||
setCurrentByName: noop, | ||
toggleSelectedFile: noop, | ||
}; | ||
}; | ||
|
||
module.exports.getCloudCmd = () => { | ||
const show = () => {}; | ||
|
||
return { | ||
Operation: { | ||
show | ||
} | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters