diff --git a/config/commands.json b/config/commands.json index bb60870..9f164a3 100644 --- a/config/commands.json +++ b/config/commands.json @@ -21,6 +21,7 @@ { "label": "Select Line", "command": "sublime:expand-to-line" }, { "label": "Select Paragraph", "command": "sublime:expand-to-paragraph" }, { "label": "Select to Matching", "command": "sublime:expand-to-matching" }, + { "label": "Wrap Paragraph to Print Margin", "command": "sublime:wrap" }, { "label": "Word Count", "command": "editor:word-count" }, { "label": "Find", "command": "ace:command", "argument": "find" }, diff --git a/config/keys.json b/config/keys.json index dc6099d..d9be37a 100644 --- a/config/keys.json +++ b/config/keys.json @@ -27,6 +27,7 @@ //Sublime compatibility "Ctrl-L": "sublime:expand-to-line", + "Alt-Q": "sublime:wrap", "Ctrl-D": { "ace": "selectMoreAfter" }, "Ctrl-P": { "command": "palette:open" }, "Ctrl-Shift-P": { "command": "palette:open", "argument": "command" }, diff --git a/config/menus.json b/config/menus.json index b9b6d1b..1030927 100644 --- a/config/menus.json +++ b/config/menus.json @@ -23,6 +23,7 @@ { "label": "Select Line", "command": "sublime:expand-to-line" }, { "label": "Select Paragraph", "command": "sublime:expand-to-paragraph" }, { "label": "Select to Matching", "command": "sublime:expand-to-matching" }, + { "label": "Wrap Paragraph to Print Margin", "command": "sublime:wrap" }, "divider", { "label": "Find", "command": "ace:command", "argument": "find" }, { "label": "Find/Replace", "command": "ace:command", "argument": "replace" }, diff --git a/js/aceBindings.js b/js/aceBindings.js index 6d8ce56..a6c83f8 100644 --- a/js/aceBindings.js +++ b/js/aceBindings.js @@ -151,6 +151,79 @@ define([ if (c) c(); }); + command.on("sublime:wrap", function(c) { + var Range = ace.require("ace/range").Range; + var lang = ace.require("ace/lib/lang"); + var session = editor.getSession(); + var selection = editor.getSelection(); + var isBackwards = editor.selection.isBackwards(); + var selectionLead = isBackwards ? editor.selection.getSelectionLead() : editor.selection.getSelectionAnchor(); + var selectionAnchor = isBackwards ? editor.selection.getSelectionAnchor() : editor.selection.getSelectionLead(); + var startLine = selectionLead.row; + var endLine = selectionAnchor.row; + while (startLine > 0) { + startLine--; + var line = session.getLine(startLine).replace(/\s+/, ""); + if (line == "") { + //we'll skip the preceding space + startLine += 1; + break; + } + } + var length = session.getLength(); + while (endLine < length) { + endLine++; + var line = session.getLine(endLine).replace(/\s+/, ""); + if (line == "") { + break; + } + } + editor.clearSelection(); + editor.moveCursorTo(startLine, 0); + selection.selectTo(endLine, 0); + var indentStartCol = session.getLine(startLine).length - lang.stringTrimLeft(session.getLine(startLine)).length; + var selectedText = lang.stringTrimLeft(session.doc.getTextRange(new Range(startLine, 0, endLine, 0)).replace(/\n/g, " ")); + var selectedTextParts = selectedText.split(" "); + var partCount = 0; + var rulerColumn = editor.renderer.getPrintMarginColumn() - 1; + var textToAdd = ""; + var indentValue = indentStartCol > 0 ? new Array(indentStartCol + 1).join(' ') : ""; + var lineToAdd = indentValue; + while (partCount < selectedTextParts.length) { + if (selectedTextParts[partCount].length + lineToAdd.length + 1 < rulerColumn) { + lineToAdd += (partCount === 0 ? "" : " ") + selectedTextParts[partCount]; + } else { + lineToAdd = lang.stringTrimRight(lineToAdd); + if (lineToAdd.length > 0) { + lineToAdd += session.doc.getNewLineCharacter(); + textToAdd += lineToAdd; + } + if (selectedTextParts[partCount].length + indentValue.length >= rulerColumn) { + var tmpLine = selectedTextParts[partCount]; + while (tmpLine.length + indentValue.length >= rulerColumn) { + lineToAdd = indentValue + tmpLine.slice(0, rulerColumn - (indentValue.length + 1)); + lineToAdd = lang.stringTrimRight(lineToAdd); + lineToAdd += session.doc.getNewLineCharacter(); + textToAdd += lineToAdd; + tmpLine = tmpLine.slice(rulerColumn - (indentValue.length + 1)); + } + lineToAdd = indentValue + tmpLine; + } else { + lineToAdd = indentValue + selectedTextParts[partCount]; + } + } + partCount++; + } + textToAdd += lang.stringTrimRight(lineToAdd); + var theNewText = lang.stringTrimLeft(textToAdd.replace(/\n/g, " ")); + if (theNewText !== selectedText) { + // Add newline if the text has changed + textToAdd += session.doc.getNewLineCharacter(); + } + editor.session.doc.replace(new Range(startLine, 0, endLine, 0), textToAdd); + if (c) c(); + }); + //we also add a command redirect for firing Ace commands via regular command attributes command.on("ace:command", editor.execCommand.bind(editor));