Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.

Commit 18fd9c3

Browse files
Markus NilssenMarkus Nilssen
authored andcommitted
Remove comment-delimiting and improve handling of blank lines for the ip/ap text-objects
1 parent 08f4478 commit 18fd9c3

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

lib/text-objects.coffee

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,42 @@ class SelectAWholeWord extends TextObject
160160
selection.selectRight()
161161
true
162162

163-
class SelectInsideParagraph extends TextObject
164-
constructor: (@editor, @inclusive) ->
165-
select: ->
166-
for selection in @editor.getSelections()
167-
range = selection.cursor.getCurrentParagraphBufferRange()
168-
if range?
169-
selection.setBufferRange(range)
170-
selection.selectToBeginningOfNextParagraph()
171-
true
163+
class Paragraph extends TextObject
172164

173-
class SelectAParagraph extends TextObject
174-
constructor: (@editor, @inclusive) ->
175165
select: ->
176166
for selection in @editor.getSelections()
177-
range = selection.cursor.getCurrentParagraphBufferRange()
178-
if range?
179-
selection.setBufferRange(range)
180-
selection.selectToBeginningOfNextParagraph()
181-
selection.selectDown()
182-
true
167+
@selectParagraph(selection)
168+
169+
# Return a range delimted by the start or the end of a paragraph
170+
paragraphDelimitedRange: (startPoint) ->
171+
inParagraph = @isParagraphLine(@editor.lineTextForBufferRow(startPoint.row))
172+
upperRow = @searchLines(startPoint.row, 0, inParagraph)
173+
lowerRow = @searchLines(startPoint.row, @editor.getLineCount(), inParagraph)
174+
new Range([upperRow + 1, 0], [lowerRow, 0])
175+
176+
searchLines: (startRow, rowLimit, startedInParagraph) ->
177+
for currentRow in [startRow..rowLimit]
178+
line = @editor.lineTextForBufferRow(currentRow)
179+
if startedInParagraph isnt @isParagraphLine(line)
180+
return currentRow
181+
rowLimit
182+
183+
isParagraphLine: (line) -> (/\S/.test(line))
184+
185+
class SelectInsideParagraph extends Paragraph
186+
selectParagraph: (selection) ->
187+
startPoint = selection.getBufferRange().start
188+
range = @paragraphDelimitedRange(startPoint)
189+
selection.setBufferRange(range)
190+
true
191+
192+
class SelectAParagraph extends Paragraph
193+
selectParagraph: (selection) ->
194+
startPoint = selection.getBufferRange().start
195+
range = @paragraphDelimitedRange(startPoint)
196+
nextRange = @paragraphDelimitedRange(range.end)
197+
selection.setBufferRange([range.start, nextRange.end])
198+
true
183199

184200
module.exports = {TextObject, SelectInsideWord, SelectInsideWholeWord, SelectInsideQuotes,
185201
SelectInsideBrackets, SelectAWord, SelectAWholeWord, SelectInsideParagraph, SelectAParagraph}

spec/text-objects-spec.coffee

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ describe "TextObjects", ->
190190
describe "the 'ip' text object", ->
191191
beforeEach ->
192192
editor.setText("\nParagraph-1\nParagraph-1\nParagraph-1\n\n")
193-
editor.setCursorScreenPosition([2, 2])
193+
editor.setCursorBufferPosition([2, 2])
194194

195195
it "applies operators inside the current paragraph in operator-pending mode", ->
196196

@@ -205,6 +205,17 @@ describe "TextObjects", ->
205205
expect(editorElement.classList.contains('normal-mode')).toBe(true)
206206

207207
it "selects inside the current paragraph in visual mode", ->
208+
209+
keydown('v')
210+
keydown('i')
211+
keydown('p')
212+
213+
expect(editor.getSelectedScreenRange()).toEqual [[1, 0], [4, 0]]
214+
215+
it "selects between paragraphs in visual mode if invoked on a empty line", ->
216+
editor.setText("text\n\n\n\ntext\n")
217+
editor.setCursorBufferPosition([1, 0])
218+
208219
keydown('v')
209220
keydown('i')
210221
keydown('p')
@@ -213,7 +224,7 @@ describe "TextObjects", ->
213224

214225
describe "the 'ap' text object", ->
215226
beforeEach ->
216-
editor.setText("text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext")
227+
editor.setText("text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\n\nmoretext")
217228
editor.setCursorScreenPosition([3, 2])
218229

219230
it "applies operators around the current paragraph in operator-pending mode", ->
@@ -222,9 +233,9 @@ describe "TextObjects", ->
222233
keydown('a')
223234
keydown('p')
224235

225-
expect(editor.getText()).toBe "text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext"
236+
expect(editor.getText()).toBe "text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\n\nmoretext"
226237
expect(editor.getCursorScreenPosition()).toEqual [2, 0]
227-
expect(vimState.getRegister('"').text).toBe "Paragraph-1\nParagraph-1\nParagraph-1\n\n"
238+
expect(vimState.getRegister('"').text).toBe "Paragraph-1\nParagraph-1\nParagraph-1\n\n\n"
228239
expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
229240
expect(editorElement.classList.contains('normal-mode')).toBe(true)
230241

@@ -233,7 +244,31 @@ describe "TextObjects", ->
233244
keydown('a')
234245
keydown('p')
235246

236-
expect(editor.getSelectedScreenRange()).toEqual [[2, 0], [6, 0]]
247+
expect(editor.getSelectedScreenRange()).toEqual [[2, 0], [7, 0]]
248+
249+
it "applies operators around the next paragraph in operator-pending mode when started from a blank/only-whitespace line", ->
250+
editor.setText("text\n\n\n\nParagraph-1\nParagraph-1\nParagraph-1\n\n\nmoretext")
251+
editor.setCursorBufferPosition([1, 0])
252+
253+
keydown('y')
254+
keydown('a')
255+
keydown('p')
256+
257+
expect(editor.getText()).toBe "text\n\n\n\nParagraph-1\nParagraph-1\nParagraph-1\n\n\nmoretext"
258+
expect(editor.getCursorScreenPosition()).toEqual [1, 0]
259+
expect(vimState.getRegister('"').text).toBe "\n\n\nParagraph-1\nParagraph-1\nParagraph-1\n"
260+
expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
261+
expect(editorElement.classList.contains('normal-mode')).toBe(true)
262+
263+
it "selects around the next paragraph in visual mode when started from a blank/only-whitespace line", ->
264+
editor.setText("text\n\n\n\nparagraph-1\nparagraph-1\nparagraph-1\n\n\nmoretext")
265+
editor.setCursorBufferPosition([1, 0])
266+
267+
keydown('v')
268+
keydown('a')
269+
keydown('p')
270+
271+
expect(editor.getSelectedScreenRange()).toEqual [[1, 0], [7, 0]]
237272

238273
describe "the 'i[' text object", ->
239274
beforeEach ->

0 commit comments

Comments
 (0)