diff --git a/lib/command.coffee b/lib/command.coffee index e22566b..a346b8c 100644 --- a/lib/command.coffee +++ b/lib/command.coffee @@ -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) diff --git a/lib/find.coffee b/lib/find.coffee index 046ef4f..2f0c597 100644 --- a/lib/find.coffee +++ b/lib/find.coffee @@ -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 }