Skip to content

Commit bf456db

Browse files
committed
Added block selection support
Fixed bug with numbered list
1 parent e6b7974 commit bf456db

File tree

5 files changed

+80
-36
lines changed

5 files changed

+80
-36
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
**v0.5.0**
2+
* Added strikethrough shortcut. Thanks to [@seanmft](https://github.com/seanmft) for the contribution!
3+
* Added support for block selection. This allows you to select a subset of a block of text,
4+
and it will automatically find the start and end of the block. This applies to:
5+
* Bullet, number, and checkbox lists
6+
* Code blocks
7+
* Tables
8+
* Fixed bug where numbered list was adding "1" twice
9+
110
**v0.4.1**
211
* Added bullets icon to title menu
312
* Improved ordering of menu items

lib/commands.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ function showCommandPalette() {
4545
})
4646
}
4747

48-
function toggleStrikethrough() {
49-
editorHelpers.surroundSelection('~~');
50-
}
51-
5248
function toggleBold() {
5349
editorHelpers.surroundSelection('**')
5450
}
@@ -57,8 +53,12 @@ function toggleItalic() {
5753
editorHelpers.surroundSelection('_')
5854
}
5955

56+
function toggleStrikethrough() {
57+
editorHelpers.surroundSelection('~~');
58+
}
59+
6060
function toggleCodeBlock() {
61-
editorHelpers.surroundSelection('```\n', '\n```')
61+
editorHelpers.surroundBlockSelection('```\n', '```')
6262
}
6363

6464
function toggleInlineCode() {
@@ -99,15 +99,15 @@ function toggleBullets() {
9999
}
100100

101101
if (editorHelpers.isMatch(HasBullets)) {
102-
editorHelpers.replaceSelection((text) => text.replace(HasBullets, "$1$2"))
102+
editorHelpers.replaceBlockSelection((text) => text.replace(HasBullets, "$1$2"))
103103
}
104104
else {
105-
editorHelpers.replaceSelection((text) => text.replace(AddBullets, "$1* $2"))
105+
editorHelpers.replaceBlockSelection((text) => text.replace(AddBullets, "$1* $2"))
106106
}
107107
}
108108

109109
var HasNumbers = /^(\s*)[0-9]\.+ (.*)$/gm
110-
var AddNumbers = /^(\s*)(.+)$/gm
110+
var AddNumbers = /^(\n?)(\s*)(.+)$/gm
111111
function toggleNumberList() {
112112

113113
if (!editorHelpers.isAnythingSelected()) {
@@ -116,15 +116,15 @@ function toggleNumberList() {
116116
}
117117

118118
if (editorHelpers.isMatch(HasNumbers)) {
119-
editorHelpers.replaceSelection((text) => text.replace(HasNumbers, "$1$2"))
119+
editorHelpers.replaceBlockSelection((text) => text.replace(HasNumbers, "$1$2"))
120120
}
121121
else {
122122
var lineNums = {};
123-
editorHelpers.replaceSelection((text) => text.replace(AddNumbers, (match, whitespace, line) => {
123+
editorHelpers.replaceBlockSelection((text) => text.replace(AddNumbers, (match, newline, whitespace, line) => {
124124
if (!lineNums[whitespace]) {
125125
lineNums[whitespace] = 1
126126
}
127-
return whitespace + lineNums[whitespace]++ + ". " + line
127+
return newline + whitespace + lineNums[whitespace]++ + ". " + line
128128
}))
129129
}
130130
}
@@ -139,10 +139,10 @@ function toggleCheckboxes() {
139139
}
140140

141141
if (editorHelpers.isMatch(HasCheckboxes)) {
142-
editorHelpers.replaceSelection((text) => text.replace(HasCheckboxes, "$1$2"))
142+
editorHelpers.replaceBlockSelection((text) => text.replace(HasCheckboxes, "$1$2"))
143143
}
144144
else {
145-
editorHelpers.replaceSelection((text) => text.replace(AddCheckboxes, "$1- [ ] $2"))
145+
editorHelpers.replaceBlockSelection((text) => text.replace(AddCheckboxes, "$1- [ ] $2"))
146146
}
147147
}
148148

lib/editorHelpers.js

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ var vscode = require("vscode");
33
module.exports = {
44
isAnythingSelected: isAnythingSelected,
55
replaceSelection: replaceSelection,
6+
replaceBlockSelection: replaceBlockSelection,
67
surroundSelection: surroundSelection,
8+
surroundBlockSelection: surroundBlockSelection,
79
isMatch: isMatch
810
}
911

@@ -17,6 +19,16 @@ function replaceSelection(replaceFunc) {
1719
})
1820
}
1921

22+
function replaceBlockSelection(replaceFunc) {
23+
var editor = vscode.window.activeTextEditor;
24+
var selection = getBlockSelection();
25+
26+
var newText = replaceFunc(editor.document.getText(selection));
27+
return editor.edit((edit) => {
28+
edit.replace(selection, newText)
29+
})
30+
}
31+
2032
function isAnythingSelected() {
2133
return !vscode.window.activeTextEditor.selection.isEmpty;
2234
}
@@ -38,7 +50,7 @@ function surroundSelection(startPattern, endPattern)
3850
})
3951
}
4052
else {
41-
if (isMatch(startPattern, endPattern)) {
53+
if (isSelectionMatch(selection, startPattern, endPattern)) {
4254
replaceSelection((text) => text.substr(startPattern.length, text.length - startPattern.length - endPattern.length))
4355
}
4456
else {
@@ -47,11 +59,45 @@ function surroundSelection(startPattern, endPattern)
4759
}
4860
}
4961

50-
function isMatch(startPattern, endPattern) {
62+
function surroundBlockSelection(startPattern, endPattern)
63+
{
64+
if (endPattern == undefined || endPattern == null) endPattern = startPattern;
5165

5266
var editor = vscode.window.activeTextEditor;
53-
var selection = editor.selection;
67+
var selection = getBlockSelection();
5468

69+
if (!isAnythingSelected()) {
70+
var position = selection.active;
71+
var newPosition = position.with(position.line, position.character + startPattern.length)
72+
editor.edit((edit) => {
73+
edit.insert(selection.start, startPattern + endPattern);
74+
}).then(() => {
75+
editor.selection = new vscode.Selection(newPosition, newPosition)
76+
})
77+
}
78+
else {
79+
if (isSelectionMatch(selection, startPattern, endPattern)) {
80+
replaceBlockSelection((text) => text.substr(startPattern.length, text.length - startPattern.length - endPattern.length))
81+
}
82+
else {
83+
replaceBlockSelection((text) => startPattern + text + endPattern)
84+
}
85+
}
86+
}
87+
88+
function getBlockSelection() {
89+
var selection = vscode.window.activeTextEditor.selection;
90+
return selection
91+
.with(selection.start.with(undefined, 0),
92+
selection.end.with(selection.end.line + 1, 0));
93+
}
94+
95+
function isMatch(startPattern, endPattern) {
96+
return isSelectionMatch(vscode.window.activeTextEditor.selection, startPattern, endPattern);
97+
}
98+
99+
function isSelectionMatch(selection, startPattern, endPattern) {
100+
var editor = vscode.window.activeTextEditor;
55101
var text = editor.document.getText(selection)
56102
if (startPattern.constructor === RegExp) {
57103
return startPattern.test(text);

lib/tables.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ var sampleTable = [
1313
" A1 | B1 | C1",
1414
" A2 | B2 | C2",
1515
" A3 | B3 | C3"
16-
].join("\r\n");
16+
].join("\n");
1717

1818
function addTable(addHeader) {
19-
//var editor = vscode.window.activeTextEditor;
20-
2119
var editFunc;
2220
if (!editorHelpers.isAnythingSelected()) {
2321
editFunc = () => sampleTable;
@@ -28,7 +26,7 @@ function addTable(addHeader) {
2826
else {
2927
editFunc = convertToTableWithoutHeader;
3028
}
31-
editorHelpers.replaceSelection(editFunc);
29+
editorHelpers.replaceBlockSelection(editFunc);
3230
}
3331

3432
var tableColumnSeparator = /([ ]{2,}|[\t])/gi;
@@ -41,7 +39,7 @@ function convertToTableWithoutHeader(text) {
4139
for (var i = 0; i < columnCount + 1; i++) {
4240
line1.push("column" + i);
4341
}
44-
var tableHeader = line1.join(" | ") + "\r\n";
42+
var tableHeader = line1.join(" | ") + "\n";
4543
tableHeader = tableHeader + tableHeader.replace(/[a-z0-9]/gi, "-");
4644

4745
return tableHeader + text.replace(tableColumnSeparator, " | ");
@@ -54,5 +52,5 @@ function convertToTableWithHeader(text) {
5452

5553
var headerLine = firstRow.replace(/[^\|]/gi, "-");
5654

57-
return firstRow + "\r\n" + headerLine + textAsTable.substring(firstRow.length);
55+
return firstRow + "\n" + headerLine + textAsTable.substring(firstRow.length);
5856
}

package.json

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "markdown-shortcuts",
33
"displayName": "Markdown Shortcuts",
44
"description": "Shortcuts for Markdown editing",
5-
"version": "0.4.1",
5+
"version": "0.5.0",
66
"publisher": "mdickin",
77
"engines": {
88
"vscode": "^0.10.10"
@@ -21,15 +21,6 @@
2121
"contributes": {
2222
"commands": [
2323

24-
{
25-
"command": "md-shortcut.toggleStrikethrough",
26-
"title": "Toggle strikethrough",
27-
"icon": {
28-
"dark": "./media/icons/strikethrough_white.svg",
29-
"light": "./media/icons/strikethrough_black.svg"
30-
},
31-
"category": "Markdown Shortcuts"
32-
},
3324
{
3425
"command": "md-shortcut.toggleBold",
3526
"title": "Toggle bold",
@@ -49,11 +40,11 @@
4940
"category": "Markdown Shortcuts"
5041
},
5142
{
52-
"command": "md-shortcut.toggleBullets",
53-
"title": "Toggle bullet points",
43+
"command": "md-shortcut.toggleStrikethrough",
44+
"title": "Toggle strikethrough",
5445
"icon": {
55-
"dark": "./media/icons/bullet_white.svg",
56-
"light": "./media/icons/bullet_black.svg"
46+
"dark": "./media/icons/strikethrough_white.svg",
47+
"light": "./media/icons/strikethrough_black.svg"
5748
},
5849
"category": "Markdown Shortcuts"
5950
},

0 commit comments

Comments
 (0)