Skip to content

Commit

Permalink
Added block selection support
Browse files Browse the repository at this point in the history
Fixed bug with numbered list
  • Loading branch information
mdickin committed Dec 3, 2016
1 parent e6b7974 commit bf456db
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 36 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 13 additions & 13 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ function showCommandPalette() {
})
}

function toggleStrikethrough() {
editorHelpers.surroundSelection('~~');
}

function toggleBold() {
editorHelpers.surroundSelection('**')
}
Expand All @@ -57,8 +53,12 @@ function toggleItalic() {
editorHelpers.surroundSelection('_')
}

function toggleStrikethrough() {
editorHelpers.surroundSelection('~~');
}

function toggleCodeBlock() {
editorHelpers.surroundSelection('```\n', '\n```')
editorHelpers.surroundBlockSelection('```\n', '```')
}

function toggleInlineCode() {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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
}))
}
}
Expand All @@ -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"))
}
}

Expand Down
52 changes: 49 additions & 3 deletions lib/editorHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var vscode = require("vscode");
module.exports = {
isAnythingSelected: isAnythingSelected,
replaceSelection: replaceSelection,
replaceBlockSelection: replaceBlockSelection,
surroundSelection: surroundSelection,
surroundBlockSelection: surroundBlockSelection,
isMatch: isMatch
}

Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions lib/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +26,7 @@ function addTable(addHeader) {
else {
editFunc = convertToTableWithoutHeader;
}
editorHelpers.replaceSelection(editFunc);
editorHelpers.replaceBlockSelection(editFunc);
}

var tableColumnSeparator = /([ ]{2,}|[\t])/gi;
Expand All @@ -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, " | ");
Expand All @@ -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);
}
19 changes: 5 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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"
},
Expand Down

0 comments on commit bf456db

Please sign in to comment.