From 24654d5bd6653a2834fe4e054e6c76b052ec149a Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 27 Mar 2012 15:55:23 -0700 Subject: [PATCH] :lipstick: clarify substitution regex recursion --- .../command-interpreter/substitution.coffee | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/atom/command-interpreter/substitution.coffee b/src/atom/command-interpreter/substitution.coffee index c08535321..a19336fb0 100644 --- a/src/atom/command-interpreter/substitution.coffee +++ b/src/atom/command-interpreter/substitution.coffee @@ -14,23 +14,27 @@ class Substitution extends Command @replace(editor, selectedText, selectionStartIndex) - replace: (editor, text, startIndex) -> + replace: (editor, text, globalStartIndex) -> return unless match = text.match(@findRegex) - matchStartIndex = startIndex + match.index - matchEndIndex = matchStartIndex + match[0].length + localMatchStartIndex = match.index + localMatchEndIndex = localMatchStartIndex + match[0].length + + globalMatchStartIndex = globalStartIndex + localMatchStartIndex + globalMatchEndIndex = globalStartIndex + localMatchEndIndex buffer = editor.buffer - startPosition = buffer.positionForCharacterIndex(matchStartIndex) - endPosition = buffer.positionForCharacterIndex(matchEndIndex) - + startPosition = buffer.positionForCharacterIndex(globalMatchStartIndex) + endPosition = buffer.positionForCharacterIndex(globalMatchEndIndex) buffer.change([startPosition, endPosition], @replaceText) - if @global - 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) + if match[0].length is 0 + localMatchEndIndex++ + globalMatchStartIndex++ + + if @global + return if localMatchStartIndex >= text.length + text = text[localMatchEndIndex..] + nextGlobalStartIndex = globalMatchStartIndex + @replaceText.length + @replace(editor, text, nextGlobalStartIndex)