From 595e451070fb7735cc1cf1c6813cf224ddbde0de Mon Sep 17 00:00:00 2001 From: Zhuochun Date: Thu, 18 Jul 2019 22:23:23 +0800 Subject: [PATCH] Improve UL/OL line implementation #273 --- lib/commands/style-line.coffee | 28 +++++++++++++++++++---- lib/config.cson | 4 ++++ spec/commands/style-line-spec.coffee | 34 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/commands/style-line.coffee b/lib/commands/style-line.coffee index 2b02a5db..6740fe73 100644 --- a/lib/commands/style-line.coffee +++ b/lib/commands/style-line.coffee @@ -58,17 +58,27 @@ 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 @@ -76,7 +86,17 @@ class StyleLine 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)) diff --git a/lib/config.cson b/lib/config.cson index 36fbfc51..ffbb8e09 100644 --- a/lib/config.cson +++ b/lib/config.cson @@ -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: diff --git a/spec/commands/style-line-spec.coffee b/spec/commands/style-line-spec.coffee index 9e740dff..e46dc8d0 100644 --- a/spec/commands/style-line-spec.coffee +++ b/spec/commands/style-line-spec.coffee @@ -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")