Yes we can

This commit is contained in:
jazzpi 2015-03-22 14:17:43 +01:00
parent d1295587ee
commit 433b1dd6ac
2 changed files with 28 additions and 27 deletions

View file

@ -10,14 +10,14 @@ class Command
constructor: (@editor, @exState) -> constructor: (@editor, @exState) ->
@viewModel = new ExViewModel(@) @viewModel = new ExViewModel(@)
parseAddr: (str, curLine) -> parseAddr: (str, curPos) ->
if str is '.' if str is '.'
addr = curLine addr = curPos.row
else if str is '$' else if str is '$'
# Lines are 0-indexed in Atom, but 1-indexed in vim. # Lines are 0-indexed in Atom, but 1-indexed in vim.
addr = @editor.getBuffer().lines.length - 1 addr = @editor.getBuffer().lines.length - 1
else if str[0] in ["+", "-"] else if str[0] in ["+", "-"]
addr = curLine + @parseOffset(str) addr = curPos.row + @parseOffset(str)
else if not isNaN(str) else if not isNaN(str)
addr = parseInt(str) - 1 addr = parseInt(str) - 1
else if str[0] is "'" # Parse Mark... else if str[0] is "'" # Parse Mark...
@ -28,11 +28,11 @@ class Command
throw new CommandError('Mark ' + str + ' not set.') throw new CommandError('Mark ' + str + ' not set.')
addr = mark.bufferMarker.range.end.row addr = mark.bufferMarker.range.end.row
else if str[0] is "/" else if str[0] is "/"
addr = Find.findNext(@editor.buffer.lines, str[1...-1], curLine) addr = Find.findNextInBuffer(@editor.buffer, curPos, str[1...-1])
unless addr? unless addr?
throw new CommandError('Pattern not found: ' + str[1...-1]) throw new CommandError('Pattern not found: ' + str[1...-1])
else if str[0] is "?" else if str[0] is "?"
addr = Find.findPrevious(@editor.buffer.lines, str[1...-1], curLine) addr = Find.findPreviousInBuffer(@editor.buffer, curPos, str[1...-1])
unless addr? unless addr?
throw new CommandError('Pattern not found: ' + str[1...-1]) throw new CommandError('Pattern not found: ' + str[1...-1])
@ -95,13 +95,13 @@ class Command
[match, addr1, off1, addr2, off2] = cl.match(addrPattern) [match, addr1, off1, addr2, off2] = cl.match(addrPattern)
curLine = @editor.getCursorBufferPosition().row curPos = @editor.getCursorBufferPosition()
if addr1? if addr1?
address1 = @parseAddr(addr1, curLine) address1 = @parseAddr(addr1, curPos)
else else
# If no addr1 is given (,+3), assume it is '.' # If no addr1 is given (,+3), assume it is '.'
address1 = curLine address1 = curPos.row
if off1? if off1?
address1 += @parseOffset(off1) address1 += @parseOffset(off1)
@ -109,7 +109,7 @@ class Command
throw new CommandError('Invalid range') throw new CommandError('Invalid range')
if addr2? if addr2?
address2 = @parseAddr(addr2, curLine) address2 = @parseAddr(addr2, curPos)
if off2? if off2?
address2 += @parseOffset(off2) address2 += @parseOffset(off2)

View file

@ -1,26 +1,27 @@
module.exports = { module.exports = {
findLines: (lines, pattern) -> findInBuffer : (buffer, pattern) ->
# TODO: There's gotta be a better way to do this. Can we use vim-mode's found = []
# search or find-and-replace maybe? buffer.scan(new RegExp(pattern, 'g'), (obj) -> found.push obj.range)
return (i for line, i in lines when line.match(pattern)) return found
findNext: (lines, pattern, curLine) -> findNextInBuffer : (buffer, curPos, pattern) ->
lines = @findLines(lines, pattern) found = @findInBuffer(buffer, pattern)
if lines.length == 0 more = (i for i in found when i.compare([curPos, curPos]) is 1)
return null
more = (i for i in lines when i > curLine)
if more.length > 0 if more.length > 0
return more[0] return more[0].start.row
else if found.length > 0
return found[0].start.row
else else
return lines[0]
findPrevious: (lines, pattern, curLine) ->
lines = @findLines(lines, pattern)
if lines.length == 0
return null return null
less = (i for i in lines when i < curLine)
findPreviousInBuffer : (buffer, curPos, pattern) ->
found = @findInBuffer(buffer, pattern)
console.log found, curPos
less = (i for i in found when i.compare([curPos, curPos]) is -1)
if less.length > 0 if less.length > 0
return less[0] return less[less.length - 1].start.row
else if found.length > 0
return found[found.length - 1].start.row
else else
return lines[lines.length - 1] return null
} }