Match $ in command regexes as end of line. Don't infinitely loop on substituting zero-width matches

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-27 15:39:07 -07:00
parent 7d969e145a
commit 5e89c44477
6 changed files with 37 additions and 6 deletions

View File

@@ -3,6 +3,8 @@ Command = require 'command-interpreter/command'
module.exports =
class Address extends Command
execute: (editor) ->
editor.getSelection().setBufferRange(@getRange(editor))
range = @getRange(editor)
editor.clearSelections()
editor.setSelectionBufferRange(range)
isAddress: -> true

View File

@@ -3,3 +3,6 @@ _ = require 'underscore'
module.exports =
class Command
isAddress: -> false
regexForPattern: (pattern) ->
new RegExp(pattern, 'm')

View File

@@ -6,7 +6,7 @@ class RegexAddress extends Address
regex: null
constructor: (pattern) ->
@regex = new RegExp(pattern)
@regex = @regexForPattern(pattern)
getRange: (editor) ->
selectedRange = editor.getSelection().getBufferRange()

View File

@@ -6,7 +6,7 @@ class SelectAllMatches extends Command
@regex: null
constructor: (pattern) ->
@regex = new RegExp(pattern)
@regex = @regexForPattern(pattern)
execute: (editor) ->
rangesToSelect = []

View File

@@ -5,7 +5,7 @@ class Substitution extends Command
global: false
constructor: (@findText, @replaceText, @options) ->
@findRegex = new RegExp(@findText)
@findRegex = @regexForPattern(@findText)
@global = 'g' in @options
execute: (editor) ->
@@ -27,7 +27,10 @@ class Substitution extends Command
buffer.change([startPosition, endPosition], @replaceText)
if @global
text = text[(match.index + match[0].length)..]
startIndex = matchStartIndex + @replaceText.length
offset = if match[0].length then 0 else 1
startNextStringFragmentAt = match.index + match[0].length + offset
return if startNextStringFragmentAt >= text.length
text = text[startNextStringFragmentAt..]
startIndex = matchStartIndex + offset + @replaceText.length
@replace(editor, text, startIndex)