1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
_ = require 'underscore-plus'
{MotionWithInput} = require './general-motions'
SearchViewModel = require '../view-models/search-view-model'
{Input} = require '../view-models/view-model'
{Point, Range} = require 'atom'
settings = require '../settings'
class SearchBase extends MotionWithInput
constructor: (@editor, @vimState, options = {}) ->
super(@editor, @vimState)
@reverse = @initiallyReversed = false
@updateCurrentSearch() unless options.dontUpdateCurrentSearch
reversed: =>
@initiallyReversed = @reverse = true
@updateCurrentSearch()
this
moveCursor: (cursor, count=1) ->
ranges = @scan(cursor)
if ranges.length > 0
range = ranges[(count - 1) % ranges.length]
cursor.setBufferPosition(range.start)
else
atom.beep()
scan: (cursor) ->
return [] if @input.characters is ""
currentPosition = cursor.getBufferPosition()
[rangesBefore, rangesAfter] = [[], []]
@editor.scan @getSearchTerm(@input.characters), ({range}) =>
isBefore = if @reverse
range.start.compare(currentPosition) < 0
else
range.start.compare(currentPosition) <= 0
if isBefore
rangesBefore.push(range)
else
rangesAfter.push(range)
if @reverse
rangesAfter.concat(rangesBefore).reverse()
else
rangesAfter.concat(rangesBefore)
getSearchTerm: (term) ->
modifiers = {'g': true}
if not term.match('[A-Z]') and settings.useSmartcaseForSearch()
modifiers['i'] = true
if term.indexOf('\\c') >= 0
term = term.replace('\\c', '')
modifiers['i'] = true
modFlags = Object.keys(modifiers).join('')
try
new RegExp(term, modFlags)
catch
new RegExp(_.escapeRegExp(term), modFlags)
updateCurrentSearch: ->
@vimState.globalVimState.currentSearch.reverse = @reverse
@vimState.globalVimState.currentSearch.initiallyReversed = @initiallyReversed
replicateCurrentSearch: ->
@reverse = @vimState.globalVimState.currentSearch.reverse
@initiallyReversed = @vimState.globalVimState.currentSearch.initiallyReversed
class Search extends SearchBase
constructor: (@editor, @vimState) ->
super(@editor, @vimState)
@viewModel = new SearchViewModel(this)
@updateViewModel()
reversed: =>
@initiallyReversed = @reverse = true
@updateCurrentSearch()
@updateViewModel()
this
updateViewModel: ->
@viewModel.update(@initiallyReversed)
class SearchCurrentWord extends SearchBase
@keywordRegex: null
constructor: (@editor, @vimState) ->
super(@editor, @vimState)
# FIXME: This must depend on the current language
defaultIsKeyword = "[@a-zA-Z0-9_\-]+"
userIsKeyword = atom.config.get('vim-mode.iskeyword')
@keywordRegex = new RegExp(userIsKeyword or defaultIsKeyword)
searchString = @getCurrentWordMatch()
@input = new Input(searchString)
@vimState.pushSearchHistory(searchString) unless searchString is @vimState.getSearchHistoryItem()
getCurrentWord: ->
cursor = @editor.getLastCursor()
wordStart = cursor.getBeginningOfCurrentWordBufferPosition(wordRegex: @keywordRegex, allowPrevious: false)
wordEnd = cursor.getEndOfCurrentWordBufferPosition (wordRegex: @keywordRegex, allowNext: false)
cursorPosition = cursor.getBufferPosition()
if wordEnd.column is cursorPosition.column
# either we don't have a current word, or it ends on cursor, i.e. precedes it, so look for the next one
wordEnd = cursor.getEndOfCurrentWordBufferPosition (wordRegex: @keywordRegex, allowNext: true)
return "" if wordEnd.row isnt cursorPosition.row # don't look beyond the current line
cursor.setBufferPosition wordEnd
wordStart = cursor.getBeginningOfCurrentWordBufferPosition(wordRegex: @keywordRegex, allowPrevious: false)
cursor.setBufferPosition wordStart
@editor.getTextInBufferRange([wordStart, wordEnd])
cursorIsOnEOF: (cursor) ->
pos = cursor.getNextWordBoundaryBufferPosition(wordRegex: @keywordRegex)
eofPos = @editor.getEofBufferPosition()
pos.row is eofPos.row and pos.column is eofPos.column
getCurrentWordMatch: ->
characters = @getCurrentWord()
if characters.length > 0
if /\W/.test(characters) then "#{characters}\\b" else "\\b#{characters}\\b"
else
characters
isComplete: -> true
execute: (count=1) ->
super(count) if @input.characters.length > 0
OpenBrackets = ['(', '{', '[']
CloseBrackets = [')', '}', ']']
AnyBracket = new RegExp(OpenBrackets.concat(CloseBrackets).map(_.escapeRegExp).join("|"))
class BracketMatchingMotion extends SearchBase
operatesInclusively: true
isComplete: -> true
searchForMatch: (startPosition, reverse, inCharacter, outCharacter) ->
depth = 0
point = startPosition.copy()
lineLength = @editor.lineTextForBufferRow(point.row).length
eofPosition = @editor.getEofBufferPosition().translate([0, 1])
increment = if reverse then -1 else 1
loop
character = @characterAt(point)
depth++ if character is inCharacter
depth-- if character is outCharacter
return point if depth is 0
point.column += increment
return null if depth < 0
return null if point.isEqual([0, -1])
return null if point.isEqual(eofPosition)
if point.column < 0
point.row--
lineLength = @editor.lineTextForBufferRow(point.row).length
point.column = lineLength - 1
else if point.column >= lineLength
point.row++
lineLength = @editor.lineTextForBufferRow(point.row).length
point.column = 0
characterAt: (position) ->
@editor.getTextInBufferRange([position, position.translate([0, 1])])
getSearchData: (position) ->
character = @characterAt(position)
if (index = OpenBrackets.indexOf(character)) >= 0
[character, CloseBrackets[index], false]
else if (index = CloseBrackets.indexOf(character)) >= 0
[character, OpenBrackets[index], true]
else
[]
moveCursor: (cursor) ->
startPosition = cursor.getBufferPosition()
[inCharacter, outCharacter, reverse] = @getSearchData(startPosition)
unless inCharacter?
restOfLine = [startPosition, [startPosition.row, Infinity]]
@editor.scanInBufferRange AnyBracket, restOfLine, ({range, stop}) ->
startPosition = range.start
stop()
[inCharacter, outCharacter, reverse] = @getSearchData(startPosition)
return unless inCharacter?
if matchPosition = @searchForMatch(startPosition, reverse, inCharacter, outCharacter)
cursor.setBufferPosition(matchPosition)
class RepeatSearch extends SearchBase
constructor: (@editor, @vimState) ->
super(@editor, @vimState, dontUpdateCurrentSearch: true)
@input = new Input(@vimState.getSearchHistoryItem(0) ? "")
@replicateCurrentSearch()
isComplete: -> true
reversed: ->
@reverse = not @initiallyReversed
this
module.exports = {Search, SearchCurrentWord, BracketMatchingMotion, RepeatSearch}
|