From 12c315ecfd081e26d988398f311c380da0fa36ee Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 21 Mar 2012 16:29:00 -0700 Subject: [PATCH] Use recursion to substitute text --- .../command-interpreter/substitution.coffee | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/atom/command-interpreter/substitution.coffee b/src/atom/command-interpreter/substitution.coffee index b3e47aaed..051711cc3 100644 --- a/src/atom/command-interpreter/substitution.coffee +++ b/src/atom/command-interpreter/substitution.coffee @@ -3,21 +3,29 @@ class Substitution global: false constructor: (@findText, @replaceText, @options) -> - @findRegex = new RegExp(@findText, "g") + @findRegex = new RegExp(@findText) @global = 'g' in @options perform: (editor) -> - { buffer } = editor selectedText = editor.getSelectedText() - selectionStartIndex = buffer.characterIndexForPosition(editor.getSelection().getBufferRange().start) + selectionStartIndex = editor.buffer.characterIndexForPosition(editor.getSelection().getBufferRange().start) + selectionEndIndex = selectionStartIndex + selectedText.length - while match = @findRegex.exec(selectedText) - matchStartIndex = selectionStartIndex + match.index - matchEndIndex = matchStartIndex + match[0].length + @replace(editor, selectedText, selectionStartIndex) - startPosition = buffer.positionForCharacterIndex(matchStartIndex) - endPosition = buffer.positionForCharacterIndex(matchEndIndex) + replace: (editor, text, startIndex) -> + return unless match = text.match(@findRegex) - buffer.change([startPosition, endPosition], @replaceText) - break unless @global - selectedText = editor.getSelectedText() + matchStartIndex = startIndex + match.index + matchEndIndex = matchStartIndex + match[0].length + + buffer = editor.buffer + startPosition = buffer.positionForCharacterIndex(matchStartIndex) + endPosition = buffer.positionForCharacterIndex(matchEndIndex) + + buffer.change([startPosition, endPosition], @replaceText) + + if @global + text = text[(match.index + match[0].length)..] + startIndex = matchStartIndex + @replaceText.length + @replace(editor, text, startIndex)