Skip to content

Commit fe2fab5

Browse files
committed
Account for blank newlines in citation blocks
Resolves #43
1 parent 94b5283 commit fe2fab5

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/commands.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ function toggleCheckboxes() {
165165
}
166166

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

171171
if (!editorHelpers.isAnythingSelected()) {
@@ -176,6 +176,7 @@ function toggleCitations() {
176176
return editorHelpers.replaceBlockSelection((text) => text.replace(HasCitations, "$1$2"))
177177
}
178178
else {
179+
return editorHelpers.prefixLines("> ");
179180
return editorHelpers.replaceBlockSelection((text) => text.replace(AddCitations, "$1> $2"))
180181
}
181182
}

lib/editorHelpers.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = {
88
surroundBlockSelection: surroundBlockSelection,
99
getSurroundingWord: getSurroundingWord,
1010
isMatch: isMatch,
11-
isBlockMatch: isBlockMatch
11+
isBlockMatch: isBlockMatch,
12+
prefixLines: prefixLines
1213
}
1314

1415
function replaceSelection(replaceFunc) {
@@ -134,4 +135,15 @@ function isSelectionMatch(selection, startPattern, endPattern) {
134135
}
135136

136137
return text.startsWith(startPattern) && text.endsWith(endPattern)
138+
}
139+
140+
function prefixLines(text) {
141+
var selection = vscode.window.activeTextEditor.selection;
142+
143+
return vscode.window.activeTextEditor.edit(builder => {
144+
for (let line = selection.start.line; line <= selection.end.line; line++) {
145+
builder.insert(selection.start.with(line, 0), text);
146+
}
147+
}
148+
);
137149
}

test/extension.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,14 @@ suite("Citations", function () {
251251
return TestCommand(
252252
'toggleCitations',
253253
'A li[st\nOf Citatio}ns',
254-
'> A [list\n> Of Cit}ations');
254+
'> A li[st\n> Of Citatio}ns');
255+
})
256+
257+
test("Ranged selection with blank lines", function () {
258+
return TestCommand(
259+
'toggleCitations',
260+
'A li[st\n\n\nOf Citatio}ns',
261+
'> A li[st\n> \n> \n> Of Citatio}ns');
255262
})
256263

257264
test("Toggles with collapsed selection", function () {

0 commit comments

Comments
 (0)