Skip to content

Commit

Permalink
Account for blank newlines in citation blocks
Browse files Browse the repository at this point in the history
Resolves #43
  • Loading branch information
mdickin committed Feb 3, 2019
1 parent 94b5283 commit fe2fab5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function toggleCheckboxes() {
}

var HasCitations = /^(\s*)> (.*)$/gmi
var AddCitations = /^(\s*)(.+)$/gm
var AddCitations = /^(\s*)(.*)$/gm
function toggleCitations() {

if (!editorHelpers.isAnythingSelected()) {
Expand All @@ -176,6 +176,7 @@ function toggleCitations() {
return editorHelpers.replaceBlockSelection((text) => text.replace(HasCitations, "$1$2"))
}
else {
return editorHelpers.prefixLines("> ");
return editorHelpers.replaceBlockSelection((text) => text.replace(AddCitations, "$1> $2"))
}
}
Expand Down
14 changes: 13 additions & 1 deletion lib/editorHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module.exports = {
surroundBlockSelection: surroundBlockSelection,
getSurroundingWord: getSurroundingWord,
isMatch: isMatch,
isBlockMatch: isBlockMatch
isBlockMatch: isBlockMatch,
prefixLines: prefixLines
}

function replaceSelection(replaceFunc) {
Expand Down Expand Up @@ -134,4 +135,15 @@ function isSelectionMatch(selection, startPattern, endPattern) {
}

return text.startsWith(startPattern) && text.endsWith(endPattern)
}

function prefixLines(text) {
var selection = vscode.window.activeTextEditor.selection;

return vscode.window.activeTextEditor.edit(builder => {
for (let line = selection.start.line; line <= selection.end.line; line++) {
builder.insert(selection.start.with(line, 0), text);
}
}
);
}
9 changes: 8 additions & 1 deletion test/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,14 @@ suite("Citations", function () {
return TestCommand(
'toggleCitations',
'A li[st\nOf Citatio}ns',
'> A [list\n> Of Cit}ations');
'> A li[st\n> Of Citatio}ns');
})

test("Ranged selection with blank lines", function () {
return TestCommand(
'toggleCitations',
'A li[st\n\n\nOf Citatio}ns',
'> A li[st\n> \n> \n> Of Citatio}ns');
})

test("Toggles with collapsed selection", function () {
Expand Down

0 comments on commit fe2fab5

Please sign in to comment.