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

Commit d63aed1

Browse files
author
Max Brunsfeld
committed
Merge pull request #696 from MarkusSN/fix-#689
Fix for #689 and improve blank line handling for the ip/ap text-objects
2 parents b4d5165 + ad3da9e commit d63aed1

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
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, -1, 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: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,9 @@ 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", ->
196-
197196
keydown('y')
198197
keydown('i')
199198
keydown('p')
@@ -211,20 +210,39 @@ describe "TextObjects", ->
211210

212211
expect(editor.getSelectedScreenRange()).toEqual [[1, 0], [4, 0]]
213212

213+
it "selects between paragraphs in visual mode if invoked on a empty line", ->
214+
editor.setText("text\n\n\n\ntext\n")
215+
editor.setCursorBufferPosition([1, 0])
216+
217+
keydown('v')
218+
keydown('i')
219+
keydown('p')
220+
221+
expect(editor.getSelectedScreenRange()).toEqual [[1, 0], [4, 0]]
222+
223+
it "selects all the lines", ->
224+
editor.setText("text\ntext\ntext\n")
225+
editor.setCursorBufferPosition([0, 0])
226+
227+
keydown('v')
228+
keydown('i')
229+
keydown('p')
230+
231+
expect(editor.getSelectedScreenRange()).toEqual [[0, 0], [3, 0]]
232+
214233
describe "the 'ap' text object", ->
215234
beforeEach ->
216-
editor.setText("text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext")
235+
editor.setText("text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\n\nmoretext")
217236
editor.setCursorScreenPosition([3, 2])
218237

219238
it "applies operators around the current paragraph in operator-pending mode", ->
220-
221239
keydown('y')
222240
keydown('a')
223241
keydown('p')
224242

225-
expect(editor.getText()).toBe "text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\nmoretext"
243+
expect(editor.getText()).toBe "text\n\nParagraph-1\nParagraph-1\nParagraph-1\n\n\nmoretext"
226244
expect(editor.getCursorScreenPosition()).toEqual [2, 0]
227-
expect(vimState.getRegister('"').text).toBe "Paragraph-1\nParagraph-1\nParagraph-1\n\n"
245+
expect(vimState.getRegister('"').text).toBe "Paragraph-1\nParagraph-1\nParagraph-1\n\n\n"
228246
expect(editorElement.classList.contains('operator-pending-mode')).toBe(false)
229247
expect(editorElement.classList.contains('normal-mode')).toBe(true)
230248

@@ -233,7 +251,31 @@ describe "TextObjects", ->
233251
keydown('a')
234252
keydown('p')
235253

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

238280
describe "the 'i[' text object", ->
239281
beforeEach ->

0 commit comments

Comments
 (0)