diff --git a/CHANGELOG.md b/CHANGELOG.md index 1835ef0..216a0ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +**v0.5.0** +* Added strikethrough shortcut. Thanks to [@seanmft](https://github.com/seanmft) for the contribution! +* Added support for block selection. This allows you to select a subset of a block of text, +and it will automatically find the start and end of the block. This applies to: + * Bullet, number, and checkbox lists + * Code blocks + * Tables +* Fixed bug where numbered list was adding "1" twice + **v0.4.1** * Added bullets icon to title menu * Improved ordering of menu items diff --git a/lib/commands.js b/lib/commands.js index b9fc928..c62bc33 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -45,10 +45,6 @@ function showCommandPalette() { }) } -function toggleStrikethrough() { - editorHelpers.surroundSelection('~~'); -} - function toggleBold() { editorHelpers.surroundSelection('**') } @@ -57,8 +53,12 @@ function toggleItalic() { editorHelpers.surroundSelection('_') } +function toggleStrikethrough() { + editorHelpers.surroundSelection('~~'); +} + function toggleCodeBlock() { - editorHelpers.surroundSelection('```\n', '\n```') + editorHelpers.surroundBlockSelection('```\n', '```') } function toggleInlineCode() { @@ -99,15 +99,15 @@ function toggleBullets() { } if (editorHelpers.isMatch(HasBullets)) { - editorHelpers.replaceSelection((text) => text.replace(HasBullets, "$1$2")) + editorHelpers.replaceBlockSelection((text) => text.replace(HasBullets, "$1$2")) } else { - editorHelpers.replaceSelection((text) => text.replace(AddBullets, "$1* $2")) + editorHelpers.replaceBlockSelection((text) => text.replace(AddBullets, "$1* $2")) } } var HasNumbers = /^(\s*)[0-9]\.+ (.*)$/gm -var AddNumbers = /^(\s*)(.+)$/gm +var AddNumbers = /^(\n?)(\s*)(.+)$/gm function toggleNumberList() { if (!editorHelpers.isAnythingSelected()) { @@ -116,15 +116,15 @@ function toggleNumberList() { } if (editorHelpers.isMatch(HasNumbers)) { - editorHelpers.replaceSelection((text) => text.replace(HasNumbers, "$1$2")) + editorHelpers.replaceBlockSelection((text) => text.replace(HasNumbers, "$1$2")) } else { var lineNums = {}; - editorHelpers.replaceSelection((text) => text.replace(AddNumbers, (match, whitespace, line) => { + editorHelpers.replaceBlockSelection((text) => text.replace(AddNumbers, (match, newline, whitespace, line) => { if (!lineNums[whitespace]) { lineNums[whitespace] = 1 } - return whitespace + lineNums[whitespace]++ + ". " + line + return newline + whitespace + lineNums[whitespace]++ + ". " + line })) } } @@ -139,10 +139,10 @@ function toggleCheckboxes() { } if (editorHelpers.isMatch(HasCheckboxes)) { - editorHelpers.replaceSelection((text) => text.replace(HasCheckboxes, "$1$2")) + editorHelpers.replaceBlockSelection((text) => text.replace(HasCheckboxes, "$1$2")) } else { - editorHelpers.replaceSelection((text) => text.replace(AddCheckboxes, "$1- [ ] $2")) + editorHelpers.replaceBlockSelection((text) => text.replace(AddCheckboxes, "$1- [ ] $2")) } } diff --git a/lib/editorHelpers.js b/lib/editorHelpers.js index c329685..4117af6 100644 --- a/lib/editorHelpers.js +++ b/lib/editorHelpers.js @@ -3,7 +3,9 @@ var vscode = require("vscode"); module.exports = { isAnythingSelected: isAnythingSelected, replaceSelection: replaceSelection, + replaceBlockSelection: replaceBlockSelection, surroundSelection: surroundSelection, + surroundBlockSelection: surroundBlockSelection, isMatch: isMatch } @@ -17,6 +19,16 @@ function replaceSelection(replaceFunc) { }) } +function replaceBlockSelection(replaceFunc) { + var editor = vscode.window.activeTextEditor; + var selection = getBlockSelection(); + + var newText = replaceFunc(editor.document.getText(selection)); + return editor.edit((edit) => { + edit.replace(selection, newText) + }) +} + function isAnythingSelected() { return !vscode.window.activeTextEditor.selection.isEmpty; } @@ -38,7 +50,7 @@ function surroundSelection(startPattern, endPattern) }) } else { - if (isMatch(startPattern, endPattern)) { + if (isSelectionMatch(selection, startPattern, endPattern)) { replaceSelection((text) => text.substr(startPattern.length, text.length - startPattern.length - endPattern.length)) } else { @@ -47,11 +59,45 @@ function surroundSelection(startPattern, endPattern) } } -function isMatch(startPattern, endPattern) { +function surroundBlockSelection(startPattern, endPattern) +{ + if (endPattern == undefined || endPattern == null) endPattern = startPattern; var editor = vscode.window.activeTextEditor; - var selection = editor.selection; + var selection = getBlockSelection(); + if (!isAnythingSelected()) { + var position = selection.active; + var newPosition = position.with(position.line, position.character + startPattern.length) + editor.edit((edit) => { + edit.insert(selection.start, startPattern + endPattern); + }).then(() => { + editor.selection = new vscode.Selection(newPosition, newPosition) + }) + } + else { + if (isSelectionMatch(selection, startPattern, endPattern)) { + replaceBlockSelection((text) => text.substr(startPattern.length, text.length - startPattern.length - endPattern.length)) + } + else { + replaceBlockSelection((text) => startPattern + text + endPattern) + } + } +} + +function getBlockSelection() { + var selection = vscode.window.activeTextEditor.selection; + return selection + .with(selection.start.with(undefined, 0), + selection.end.with(selection.end.line + 1, 0)); +} + +function isMatch(startPattern, endPattern) { + return isSelectionMatch(vscode.window.activeTextEditor.selection, startPattern, endPattern); +} + +function isSelectionMatch(selection, startPattern, endPattern) { + var editor = vscode.window.activeTextEditor; var text = editor.document.getText(selection) if (startPattern.constructor === RegExp) { return startPattern.test(text); diff --git a/lib/tables.js b/lib/tables.js index 6a80d29..7ec9d77 100644 --- a/lib/tables.js +++ b/lib/tables.js @@ -13,11 +13,9 @@ var sampleTable = [ " A1 | B1 | C1", " A2 | B2 | C2", " A3 | B3 | C3" - ].join("\r\n"); + ].join("\n"); function addTable(addHeader) { - //var editor = vscode.window.activeTextEditor; - var editFunc; if (!editorHelpers.isAnythingSelected()) { editFunc = () => sampleTable; @@ -28,7 +26,7 @@ function addTable(addHeader) { else { editFunc = convertToTableWithoutHeader; } - editorHelpers.replaceSelection(editFunc); + editorHelpers.replaceBlockSelection(editFunc); } var tableColumnSeparator = /([ ]{2,}|[\t])/gi; @@ -41,7 +39,7 @@ function convertToTableWithoutHeader(text) { for (var i = 0; i < columnCount + 1; i++) { line1.push("column" + i); } - var tableHeader = line1.join(" | ") + "\r\n"; + var tableHeader = line1.join(" | ") + "\n"; tableHeader = tableHeader + tableHeader.replace(/[a-z0-9]/gi, "-"); return tableHeader + text.replace(tableColumnSeparator, " | "); @@ -54,5 +52,5 @@ function convertToTableWithHeader(text) { var headerLine = firstRow.replace(/[^\|]/gi, "-"); - return firstRow + "\r\n" + headerLine + textAsTable.substring(firstRow.length); + return firstRow + "\n" + headerLine + textAsTable.substring(firstRow.length); } \ No newline at end of file diff --git a/package.json b/package.json index 384af9a..2fd73bc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "markdown-shortcuts", "displayName": "Markdown Shortcuts", "description": "Shortcuts for Markdown editing", - "version": "0.4.1", + "version": "0.5.0", "publisher": "mdickin", "engines": { "vscode": "^0.10.10" @@ -21,15 +21,6 @@ "contributes": { "commands": [ - { - "command": "md-shortcut.toggleStrikethrough", - "title": "Toggle strikethrough", - "icon": { - "dark": "./media/icons/strikethrough_white.svg", - "light": "./media/icons/strikethrough_black.svg" - }, - "category": "Markdown Shortcuts" - }, { "command": "md-shortcut.toggleBold", "title": "Toggle bold", @@ -49,11 +40,11 @@ "category": "Markdown Shortcuts" }, { - "command": "md-shortcut.toggleBullets", - "title": "Toggle bullet points", + "command": "md-shortcut.toggleStrikethrough", + "title": "Toggle strikethrough", "icon": { - "dark": "./media/icons/bullet_white.svg", - "light": "./media/icons/bullet_black.svg" + "dark": "./media/icons/strikethrough_white.svg", + "light": "./media/icons/strikethrough_black.svg" }, "category": "Markdown Shortcuts" },