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

Commit

Permalink
Improve UL/OL line implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuochun committed Jul 18, 2019
1 parent 4c79534 commit 595e451
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/commands/style-line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,45 @@ class StyleLine
line = @editor.lineTextForBufferRow(rows[0])
isRemoveStyle = line && @isStyleOn(line) # else add style

lineIdx = 0
rowsToRemove = []

# rows[0] = start of buffer rows, rows[1] = end of buffer rows
for row, i in ([rows[0]..rows[1]])
for row in ([rows[0]..rows[1]])
line = @editor.lineTextForBufferRow(row)
# record lines to be removed
if !line && @style.removeEmptyLine
rowsToRemove.push(row)
continue

lineIdx += 1

indent = @editor.indentationForBufferRow(row)
data =
i: i + 1,
i: lineIdx,
ul: config.get("templateVariables.ulBullet#{indent}") || config.get("templateVariables.ulBullet")

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

line = @editor.lineTextForBufferRow(row)
if line && isRemoveStyle
@removeStyle(selection, line, data)
else if line
@addStyle(selection, line, data)
else if !isRemoveStyle
@insertEmptyStyle(selection, data)

selection.setBufferRange(range) # reselect the previously selected range
# remove deleted line
for row, i in rowsToRemove
@editor.getBuffer().deleteRow(row - i)

# reselect from start of char in range
range.start.column = 0
# to end of last char
range.end.row -= rowsToRemove.length
range.end.column = @editor.lineTextForBufferRow(range.end.row).length

selection.setBufferRange(range) # reselect the spreviously selected range

insertEmptyStyle: (selection, data) ->
selection.insertText(utils.template(@style.before, data))
Expand Down
4 changes: 4 additions & 0 deletions lib/config.cson
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,25 @@ lineStyles:
before: "{ul} "
regexMatchBefore: "(?:-|\\*|\\+|\\.)\\s"
regexBefore: "(?:-|\\*|\\+|\\.|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s"
removeEmptyLine: true
ol:
before: "{i}. "
regexMatchBefore: "(?:\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s"
regexBefore: "(?:-|\\*|\\+|\\.|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s"
removeEmptyLine: true
task:
before: "{captureBefore} [ ] "
regexMatchBefore: "(?:-|\\*|\\+|\\d+[\\.\\)])\\s+\\[ ]\\s"
regexBefore: "(-|\\*|\\+|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s*(?:\\[[xX ]])?\\s"
captureBefore: "ul"
removeEmptyLine: true
taskdone:
before: "{captureBefore} [x] "
regexMatchBefore: "(?:-|\\*|\\+|\\d+[\\.\\)])\\s+\\[[xX]]\\s"
regexBefore: "(-|\\*|\\+|\\d+[\\.\\)]|[a-zA-Z]+[\\.\\)])\\s*(?:\\[[xX ]])?\\s"
captureBefore: "ul"
emptyBefore: "{captureBefore} [ ] "
removeEmptyLine: true
blockquote: before: "> "

# Image tag template, available variables:
Expand Down
34 changes: 34 additions & 0 deletions spec/commands/style-line-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ describe "StyleLine", ->
- list 3
"""

it "apply ordered list on multiple rows (removeEmptyLine)", ->
editor.setText """
list 1
list 2
list 3
"""
editor.setSelectedBufferRange([[0, 0], [4, 3]])

new StyleLine("ol").trigger()
expect(editor.getText()).toBe """
1. list 1
2. list 2
3. list 3
"""

it "apply unordered list on multiple rows (removeEmptyLine)", ->
editor.setText """
list 1
list 2
list 3
"""
editor.setSelectedBufferRange([[0, 0], [4, 3]])

new StyleLine("ul").trigger()
expect(editor.getText()).toBe """
- list 1
- list 2
- list 3
"""

it "apply task list", ->
editor.setText("task")

Expand Down

0 comments on commit 595e451

Please sign in to comment.