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

View file

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