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

Commit 595e451

Browse files
committed
Improve UL/OL line implementation
#273
1 parent 4c79534 commit 595e451

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

lib/commands/style-line.coffee

+24-4
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,45 @@ class StyleLine
5858
line = @editor.lineTextForBufferRow(rows[0])
5959
isRemoveStyle = line && @isStyleOn(line) # else add style
6060

61+
lineIdx = 0
62+
rowsToRemove = []
63+
6164
# rows[0] = start of buffer rows, rows[1] = end of buffer rows
62-
for row, i in ([rows[0]..rows[1]])
65+
for row in ([rows[0]..rows[1]])
66+
line = @editor.lineTextForBufferRow(row)
67+
# record lines to be removed
68+
if !line && @style.removeEmptyLine
69+
rowsToRemove.push(row)
70+
continue
71+
72+
lineIdx += 1
73+
6374
indent = @editor.indentationForBufferRow(row)
6475
data =
65-
i: i + 1,
76+
i: lineIdx,
6677
ul: config.get("templateVariables.ulBullet#{indent}") || config.get("templateVariables.ulBullet")
6778

6879
# we need to move cursor to each row start to perform action on line
6980
selection.cursor.setBufferPosition([row, 0])
7081

71-
line = @editor.lineTextForBufferRow(row)
7282
if line && isRemoveStyle
7383
@removeStyle(selection, line, data)
7484
else if line
7585
@addStyle(selection, line, data)
7686
else if !isRemoveStyle
7787
@insertEmptyStyle(selection, data)
7888

79-
selection.setBufferRange(range) # reselect the previously selected range
89+
# remove deleted line
90+
for row, i in rowsToRemove
91+
@editor.getBuffer().deleteRow(row - i)
92+
93+
# reselect from start of char in range
94+
range.start.column = 0
95+
# to end of last char
96+
range.end.row -= rowsToRemove.length
97+
range.end.column = @editor.lineTextForBufferRow(range.end.row).length
98+
99+
selection.setBufferRange(range) # reselect the spreviously selected range
80100

81101
insertEmptyStyle: (selection, data) ->
82102
selection.insertText(utils.template(@style.before, data))

lib/config.cson

+4
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,25 @@ lineStyles:
183183
before: "{ul} "
184184
regexMatchBefore: "(?:-|\\*|\\+|\\.)\\s"
185185
regexBefore: "(?:-|\\*|\\+|\\.|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s"
186+
removeEmptyLine: true
186187
ol:
187188
before: "{i}. "
188189
regexMatchBefore: "(?:\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s"
189190
regexBefore: "(?:-|\\*|\\+|\\.|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s"
191+
removeEmptyLine: true
190192
task:
191193
before: "{captureBefore} [ ] "
192194
regexMatchBefore: "(?:-|\\*|\\+|\\d+[\\.\\)])\\s+\\[ ]\\s"
193195
regexBefore: "(-|\\*|\\+|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s*(?:\\[[xX ]])?\\s"
194196
captureBefore: "ul"
197+
removeEmptyLine: true
195198
taskdone:
196199
before: "{captureBefore} [x] "
197200
regexMatchBefore: "(?:-|\\*|\\+|\\d+[\\.\\)])\\s+\\[[xX]]\\s"
198201
regexBefore: "(-|\\*|\\+|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s*(?:\\[[xX ]])?\\s"
199202
captureBefore: "ul"
200203
emptyBefore: "{captureBefore} [ ] "
204+
removeEmptyLine: true
201205
blockquote: before: "> "
202206

203207
# Image tag template, available variables:

spec/commands/style-line-spec.coffee

+34
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ describe "StyleLine", ->
9797
- list 3
9898
"""
9999

100+
it "apply ordered list on multiple rows (removeEmptyLine)", ->
101+
editor.setText """
102+
list 1
103+
104+
list 2
105+
106+
list 3
107+
"""
108+
editor.setSelectedBufferRange([[0, 0], [4, 3]])
109+
110+
new StyleLine("ol").trigger()
111+
expect(editor.getText()).toBe """
112+
1. list 1
113+
2. list 2
114+
3. list 3
115+
"""
116+
117+
it "apply unordered list on multiple rows (removeEmptyLine)", ->
118+
editor.setText """
119+
list 1
120+
121+
list 2
122+
123+
list 3
124+
"""
125+
editor.setSelectedBufferRange([[0, 0], [4, 3]])
126+
127+
new StyleLine("ul").trigger()
128+
expect(editor.getText()).toBe """
129+
- list 1
130+
- list 2
131+
- list 3
132+
"""
133+
100134
it "apply task list", ->
101135
editor.setText("task")
102136

0 commit comments

Comments
 (0)