From 7cce042f03208e72d8dfd1bfe7e5ef4ad0164bc5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 26 Dec 2012 15:09:07 -0800 Subject: [PATCH] Change case of prefix and suffix to matched word Previously an inserted automcomplete match would not update the case of the prefix or suffix of the match and instead only insert the text from the matched word between the prefix and suffix. Now the entire matched word is inserted as-is replacing the existing prefix and suffix. --- src/app/range.coffee | 5 +++++ .../autocomplete/spec/autocomplete-spec.coffee | 11 +++++++++-- src/packages/autocomplete/src/autocomplete.coffee | 15 ++++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/app/range.coffee b/src/app/range.coffee index 11efe0c94..3464a3bce 100644 --- a/src/app/range.coffee +++ b/src/app/range.coffee @@ -11,6 +11,11 @@ class Range else new Range(object.start, object.end) + @fromPointWithDelta: (point, rowDelta, columnDelta) -> + pointA = Point.fromObject(point) + pointB = new Point(point.row + rowDelta, point.column + columnDelta) + new Range(pointA, pointB) + constructor: (pointA = new Point(0, 0), pointB = new Point(0, 0)) -> pointA = Point.fromObject(pointA) pointB = Point.fromObject(pointB) diff --git a/src/packages/autocomplete/spec/autocomplete-spec.coffee b/src/packages/autocomplete/spec/autocomplete-spec.coffee index c9a22d507..8190f3d31 100644 --- a/src/packages/autocomplete/spec/autocomplete-spec.coffee +++ b/src/packages/autocomplete/spec/autocomplete-spec.coffee @@ -96,6 +96,15 @@ describe "Autocomplete", -> expect(autocomplete.matchesList.find('li').length).toBe 1 expect(autocomplete.matchesList.find('li:eq(0)')).toHaveText "No matches found" + it "autocompletes word and replaces case of prefix with case of word", -> + editor.getBuffer().insert([10,0] ,"extra:SO:extra") + editor.setCursorBufferPosition([10,8]) + autocomplete.attach() + + expect(editor.lineForBufferRow(10)).toBe "extra:sort:extra" + expect(editor.getCursorBufferPosition()).toEqual [10,10] + expect(editor.getSelection().isEmpty()).toBeTruthy() + describe "when text is selected", -> it 'autocompletes word when there is only a prefix', -> editor.getBuffer().insert([10,0] ,"extra:sort:extra") @@ -399,5 +408,3 @@ describe "Autocomplete", -> editor.trigger 'core:move-up' expect(editor.getCursorBufferPosition().row).toBe 0 - - diff --git a/src/packages/autocomplete/src/autocomplete.coffee b/src/packages/autocomplete/src/autocomplete.coffee index 7f9bad470..73d6f66ec 100644 --- a/src/packages/autocomplete/src/autocomplete.coffee +++ b/src/packages/autocomplete/src/autocomplete.coffee @@ -185,11 +185,10 @@ class Autocomplete extends View {prefix, suffix} = @prefixAndSuffixOfSelection(selection) if (prefix.length + suffix.length) > 0 - regex = new RegExp("^#{prefix}(.+)#{suffix}$", "i") + regex = new RegExp("^#{prefix}.+#{suffix}$", "i") currentWord = prefix + @editor.getSelectedText() + suffix for word in @wordList when regex.test(word) and word != currentWord - match = regex.exec(word) - {prefix, suffix, word, infix: match[1]} + {prefix, suffix, word} else [] @@ -197,9 +196,15 @@ class Autocomplete extends View selection = @editor.getSelection() startPosition = selection.getBufferRange().start @isAutocompleting = true - @editor.insertText(match.infix) + buffer = @editor.getBuffer() + @editor.activeEditSession.transact => + selection.deleteSelectedText() + buffer.delete(Range.fromPointWithDelta(@editor.getCursorBufferPosition(), 0, -match.prefix.length)) + buffer.delete(Range.fromPointWithDelta(@editor.getCursorBufferPosition(), 0, match.suffix.length)) + @editor.insertText(match.word) - @currentMatchBufferRange = [startPosition, [startPosition.row, startPosition.column + match.infix.length]] + infixLength = match.word.length - match.prefix.length - match.suffix.length + @currentMatchBufferRange = [startPosition, [startPosition.row, startPosition.column + infixLength]] @editor.setSelectedBufferRange(@currentMatchBufferRange) @isAutocompleting = false