Optimize cursor methods that find the current word

This commit is contained in:
Max Brunsfeld
2017-09-29 16:50:59 -07:00
parent 6c4a9c1987
commit 43aa3c788f
2 changed files with 30 additions and 36 deletions

View File

@@ -514,30 +514,24 @@ class Cursor extends Model {
// Returns a {Range}.
getBeginningOfCurrentWordBufferPosition (options = {}) {
const allowPrevious = options.allowPrevious !== false
const currentBufferPosition = this.getBufferPosition()
const previousNonBlankRow = this.editor.buffer.previousNonBlankRow(currentBufferPosition.row) || 0
const scanRange = [[previousNonBlankRow, 0], currentBufferPosition]
const position = this.getBufferPosition()
let beginningOfWordPosition
this.editor.backwardsScanInBufferRange(options.wordRegex || this.wordRegExp(options), scanRange, ({range, matchText, stop}) => {
// Ignore 'empty line' matches between '\r' and '\n'
if ((matchText === '') && range.start.column !== 0) return
const scanRange = allowPrevious
? new Range(new Point(position.row - 1, 0), position)
: new Range(new Point(position.row, 0), position)
if (range.start.isLessThan(currentBufferPosition)) {
if (range.end.isGreaterThanOrEqual(currentBufferPosition) || allowPrevious) {
beginningOfWordPosition = range.start
}
stop()
}
})
const ranges = this.editor.buffer.buffer.findAllInRangeSync(
options.wordRegex || this.wordRegExp(),
scanRange
)
if (beginningOfWordPosition) {
return beginningOfWordPosition
} else if (allowPrevious) {
return new Point(0, 0)
} else {
return currentBufferPosition
let result
for (let range of ranges) {
if (position.isLessThanOrEqual(range.start)) break
if (allowPrevious || position.isLessThanOrEqual(range.end)) result = range.start
}
return result || (allowPrevious ? new Point(0, 0) : position)
}
// Public: Retrieves the buffer position of where the current word ends.
@@ -552,23 +546,23 @@ class Cursor extends Model {
// Returns a {Range}.
getEndOfCurrentWordBufferPosition (options = {}) {
const allowNext = options.allowNext !== false
const currentBufferPosition = this.getBufferPosition()
const scanRange = [currentBufferPosition, this.editor.getEofBufferPosition()]
const position = this.getBufferPosition()
let endOfWordPosition
this.editor.scanInBufferRange(options.wordRegex || this.wordRegExp(options), scanRange, ({range, matchText, stop}) => {
// Ignore 'empty line' matches between '\r' and '\n'
if (matchText === '' && range.start.column !== 0) return
const scanRange = allowNext
? new Range(position, new Point(position.row + 2, 0))
: new Range(position, new Point(position.row, Infinity))
if (range.end.isGreaterThan(currentBufferPosition)) {
if (allowNext || range.start.isLessThanOrEqual(currentBufferPosition)) {
endOfWordPosition = range.end
}
stop()
}
})
const ranges = this.editor.buffer.buffer.findAllInRangeSync(
options.wordRegex || this.wordRegExp(),
scanRange
)
return endOfWordPosition || currentBufferPosition
for (let range of ranges) {
if (position.isLessThan(range.start) && !allowNext) break
if (position.isLessThan(range.end)) return range.end
}
return allowNext ? this.editor.getEofBufferPosition() : position
}
// Public: Retrieves the buffer position of where the next word starts.
@@ -666,7 +660,7 @@ class Cursor extends Model {
// Returns a {RegExp}.
wordRegExp (options) {
const nonWordCharacters = _.escapeRegExp(this.getNonWordCharacters())
let source = `^[\t ]*$|[^\\s${nonWordCharacters}]+`
let source = `^[\t\r ]*$|[^\\s${nonWordCharacters}]+`
if (!options || options.includeNonWordCharacters !== false) {
source += `|${`[${nonWordCharacters}]+`}`
}