1 {Range} = require 'atom'
5 constructor: (@editor, @state) ->
10 class SelectInsideWord extends TextObject
12 @editor.selectWordsContainingCursors()
15 # SelectInsideQuotes and the next class defined (SelectInsideBrackets) are
16 # almost-but-not-quite-repeated code. They are different because of the depth
17 # checks in the bracket matcher.
19 class SelectInsideQuotes extends TextObject
20 constructor: (@editor, @char, @includeQuotes) ->
22 findOpeningQuote: (pos) ->
26 line = @editor.lineTextForBufferRow(pos.row)
27 pos.column = line.length - 1 if pos.column is -1
29 if line[pos.column] is @char
30 if pos.column is 0 or line[pos.column - 1] isnt '\\'
34 return @lookForwardOnLine(start)
38 @lookForwardOnLine(start)
40 isStartQuote: (end) ->
41 line = @editor.lineTextForBufferRow(end.row)
42 numQuotes = line.substring(0, end.column + 1).replace( "'#{@char}", '').split(@char).length - 1
45 lookForwardOnLine: (pos) ->
46 line = @editor.lineTextForBufferRow(pos.row)
48 index = line.substring(pos.column).indexOf(@char)
54 findClosingQuote: (start) ->
58 while end.row < @editor.getLineCount()
59 endLine = @editor.lineTextForBufferRow(end.row)
60 while end.column < endLine.length
61 if endLine[end.column] is '\\'
63 else if endLine[end.column] is @char
64 -- start.column if @includeQuotes
65 ++ end.column if @includeQuotes
73 for selection in @editor.getSelections()
74 start = @findOpeningQuote(selection.cursor.getBufferPosition())
76 ++ start.column # skip the opening quote
77 end = @findClosingQuote(start)
79 selection.setBufferRange([start, end])
80 not selection.isEmpty()
82 # SelectInsideBrackets and the previous class defined (SelectInsideQuotes) are
83 # almost-but-not-quite-repeated code. They are different because of the depth
84 # checks in the bracket matcher.
86 class SelectInsideBrackets extends TextObject
87 constructor: (@editor, @beginChar, @endChar, @includeBrackets) ->
89 findOpeningBracket: (pos) ->
93 line = @editor.lineTextForBufferRow(pos.row)
94 pos.column = line.length - 1 if pos.column is -1
96 switch line[pos.column]
97 when @endChar then ++ depth
99 return pos if -- depth < 0
104 findClosingBracket: (start) ->
107 while end.row < @editor.getLineCount()
108 endLine = @editor.lineTextForBufferRow(end.row)
109 while end.column < endLine.length
110 switch endLine[end.column]
111 when @beginChar then ++ depth
114 -- start.column if @includeBrackets
115 ++ end.column if @includeBrackets
123 for selection in @editor.getSelections()
124 start = @findOpeningBracket(selection.cursor.getBufferPosition())
126 ++ start.column # skip the opening quote
127 end = @findClosingBracket(start)
129 selection.setBufferRange([start, end])
130 not selection.isEmpty()
132 class SelectAWord extends TextObject
134 for selection in @editor.getSelections()
135 selection.selectWord()
137 endPoint = selection.getBufferRange().end
138 char = @editor.getTextInRange(Range.fromPointWithDelta(endPoint, 0, 1))
139 break unless AllWhitespace.test(char)
140 selection.selectRight()
143 class SelectInsideParagraph extends TextObject
144 constructor: (@editor, @inclusive) ->
146 for selection in @editor.getSelections()
147 range = selection.cursor.getCurrentParagraphBufferRange()
149 selection.setBufferRange(range)
150 selection.selectToBeginningOfNextParagraph()
153 class SelectAParagraph extends TextObject
154 constructor: (@editor, @inclusive) ->
156 for selection in @editor.getSelections()
157 range = selection.cursor.getCurrentParagraphBufferRange()
159 selection.setBufferRange(range)
160 selection.selectToBeginningOfNextParagraph()
161 selection.selectDown()
164 module.exports = {TextObject, SelectInsideWord, SelectInsideQuotes, SelectInsideBrackets, SelectAWord, SelectInsideParagraph, SelectAParagraph}