Make cursor.moveCursorToBeginningOfWord behave like vim

This commit is contained in:
Corey Johnson
2013-01-29 12:15:43 -08:00
parent b66efbe3e7
commit dac92ca6e7
2 changed files with 32 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ describe "EditSession", ->
afterEach ->
fixturesProject.destroy()
fdescribe "cursor", ->
describe "cursor", ->
describe ".getCursor()", ->
it "returns the most recently created cursor", ->
editSession.addCursorAtScreenPosition([1, 0])
@@ -266,7 +266,7 @@ describe "EditSession", ->
editSession.moveCursorToFirstCharacterOfLine()
expect(editSession.getCursorBufferPosition()).toEqual [10, 0]
describe ".moveCursorToBeginningOfWord()", ->
fdescribe ".moveCursorToBeginningOfWord()", ->
it "moves the cursor to the beginning of the word", ->
editSession.setCursorBufferPosition [0, 8]
editSession.addCursorAtBufferPosition [1, 12]
@@ -283,12 +283,17 @@ describe "EditSession", ->
editSession.setCursorBufferPosition([0, 0])
editSession.moveCursorToBeginningOfWord()
it "works when the previous line is blank", ->
it "treats lines with only whitespace as a word", ->
editSession.setCursorBufferPosition([11, 0])
editSession.moveCursorToBeginningOfWord()
expect(editSession.getCursorBufferPosition()).toEqual [10, 0]
describe ".moveCursorToEndOfWord()", ->
it "works when the current line is blank", ->
editSession.setCursorBufferPosition([10, 0])
editSession.moveCursorToBeginningOfWord()
expect(editSession.getCursorBufferPosition()).toEqual [9, 2]
fdescribe ".moveCursorToEndOfWord()", ->
it "moves the cursor to the end of the word", ->
editSession.setCursorBufferPosition [0, 6]
editSession.addCursorAtBufferPosition [1, 10]
@@ -298,8 +303,8 @@ describe "EditSession", ->
editSession.moveCursorToEndOfWord()
expect(cursor1.getBufferPosition()).toEqual [0, 13]
expect(cursor2.getBufferPosition()).toEqual [1, 13]
expect(cursor3.getBufferPosition()).toEqual [3, 4]
expect(cursor2.getBufferPosition()).toEqual [1, 12]
expect(cursor3.getBufferPosition()).toEqual [3, 7]
it "does not blow up when there is no next word", ->
editSession.setCursorBufferPosition [Infinity, Infinity]
@@ -307,6 +312,17 @@ describe "EditSession", ->
editSession.moveCursorToEndOfWord()
expect(editSession.getCursorBufferPosition()).toEqual endPosition
it "treats lines with only whitespace as a word", ->
editSession.setCursorBufferPosition([9, 4])
editSession.moveCursorToEndOfWord()
expect(editSession.getCursorBufferPosition()).toEqual [10, 0]
it "works when the current line is blank", ->
editSession.setCursorBufferPosition([10, 0])
editSession.moveCursorToEndOfWord()
expect(editSession.getCursorBufferPosition()).toEqual [11, 8]
describe ".getCurrentParagraphBufferRange()", ->
it "returns the buffer range of the current paragraph, delimited by blank lines or the beginning / end of the file", ->
buffer.setText """

View File

@@ -142,6 +142,10 @@ class Cursor
if position = @getEndOfCurrentWordBufferPosition()
@setBufferPosition(position)
wordSeparatorsRegExp: ->
wordSeparators = config.get("editor.wordSeparators")
new RegExp("^[\t ]*$|[^\\s#{_.escapeRegExp(wordSeparators)}]+|[#{_.escapeRegExp(wordSeparators)}]+", "mg")
getBeginningOfCurrentWordBufferPosition: (options = {}) ->
allowPrevious = options.allowPrevious ? true
currentBufferPosition = @getBufferPosition()
@@ -150,12 +154,10 @@ class Cursor
beginningOfWordPosition = currentBufferPosition
wordSeparators = config.get("editor.wordSeparators")
wordSeparatorsRegex = new RegExp("^[\t ]*\n|[^\\s#{_.escapeRegExp(wordSeparators)}]+|[#{_.escapeRegExp(wordSeparators)}]+", "m")
@editSession.backwardsScanInRange (options.wordRegex ? wordSeparatorsRegex), previousLinesRange, (match, matchRange, { stop }) =>
@editSession.backwardsScanInRange (options.wordRegex ? @wordSeparatorsRegExp()), previousLinesRange, (match, matchRange, { stop }) =>
if matchRange.end.isGreaterThanOrEqual(currentBufferPosition) or allowPrevious
beginningOfWordPosition = matchRange.start
stop()
stop() unless beginningOfWordPosition.isEqual(currentBufferPosition)
beginningOfWordPosition
@@ -165,8 +167,11 @@ class Cursor
range = [currentBufferPosition, @editSession.getEofBufferPosition()]
endOfWordPosition = null
@editSession.scanInRange (options.wordRegex ? config.get("editor.wordRegex")), range, (match, matchRange, { stop }) =>
@editSession.scanInRange (options.wordRegex ? @wordSeparatorsRegExp()),
range, (match, matchRange, { stop }) =>
endOfWordPosition = matchRange.end
return if endOfWordPosition.isEqual(currentBufferPosition)
if not allowNext and matchRange.start.isGreaterThan(currentBufferPosition)
endOfWordPosition = currentBufferPosition
stop()