Skip to content

Commit

Permalink
Add option to count leading as part of line width
Browse files Browse the repository at this point in the history
(and slightly optimise in the process)

Closes #1
  • Loading branch information
mirnovov committed Sep 27, 2022
1 parent d25779f commit 1e2bb8b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
* **Version 0.1**: Initial release
* **Version 0.1.1**: Add repo details
* **Version 0.2**:
* Add option to count leading as part of line width
* Do minor optimisations
48 changes: 31 additions & 17 deletions Scripts/main.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
nova.commands.register("novov.hardwrap.wrap", (editor) => {
editor.insert(hardWrap(editor.getTextInRange(editor.selectedRanges[0]),nova.config.get("novov.hardwrap.breakat")));
nova.commands.register("novov.hardwrap.wrap", editor => {
editor.insert(hardWrap(
editor.getTextInRange(editor.selectedRanges[0]), nova.config.get("novov.hardwrap.breakat"), editor
));
});

function hardWrap(text, max) {
function hardWrap(text, max, editor = null) {
var result = "",
leading = "";

if(nova.config.get("novov.hardwrap.leading")) {
if (nova.config.get("novov.hardwrap.leading")) {
var comment = /^#(\t|\u0020)*/g, space = /^(\t|\u0020)+/g;
var commentMatches = text.match(comment);
var spaceMatches = text.match(space);

if(comment.test(text)) {
leading = text.match(comment)[0];
if (commentMatches) {
leading = commentMatches[0];
text = text.replaceAll(comment,"").replaceAll(/\n#(\t|\u0020)*/g,"\n");

if (nova.config.get("novov.hardwrap.countleading")) {
max -= 1; //for comment character
}
}
else if(space.test(text)) {
leading = text.match(space)[0];
text = text.replaceAll(space,"").replaceAll(/\n(\t|\u0020)+/g,"\n");
}
else if(spaceMatches) {
leading = spaceMatches[0];
text = text.replaceAll(space,"").replaceAll(/\n(\t|\u0020)+/g, "\n");
}

if (nova.config.get("novov.hardwrap.countleading") && (commentMatches || spaceMatches)) {
//reduce the maximum line length by the amount of the leading
var space = " ".repeat(editor ? editor.tabLength : 4);
max -= (leading.replace("\t", space)).length
}
}

if(nova.config.get("novov.hardwrap.remove")) {
text = text.replaceAll(/\n(?!\n)/g," ").replaceAll(/\n\s?/g,"\n\n");
}

while(true) {
if(text.length < max) { result += text; break; }
while (true) {
if (text.length < max) { result += text; break; }

var wrapat = max,
newline = text.lastIndexOf("\n",max),
space = text.lastIndexOf(" ",max);

if(newline != -1 && newline != 0) { wrapat = newline; }
else if(space != -1 && space != 0) { wrapat = space; }
if (newline != -1 && newline != 0) { wrapat = newline; }
else if (space != -1 && space != 0) { wrapat = space; }

var line = text.substring(0,wrapat);
text = text.substring(wrapat);

if(wrapat == space) { text = text.replaceAll(/^\u0020/g,""); }
if(!line.endsWith("\n")) { line += "\n"; }
if (wrapat == space) { text = text.replaceAll(/^\u0020/g,""); }
if (!line.endsWith("\n")) { line += "\n"; }

result += line;
}

if(nova.config.get("novov.hardwrap.leading") && leading != "") {
if (nova.config.get("novov.hardwrap.leading") && leading != "") {
return leading + result.replaceAll("\n","\n" + leading);
}

Expand Down
8 changes: 7 additions & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Hard Wrap",
"organization": "novov",
"description": "Wraps text using line breaks at a specified length.",
"version": "0.1.1",
"version": "0.2",
"categories": ["commands"],
"bugs": "https://github.com/mirnovov/hardwrap/issues",
"repository": "https://github.com/mirnovov/hardwrap",
Expand Down Expand Up @@ -48,6 +48,12 @@
"title": "Auto-detect indents and # comments",
"type": "boolean",
"default": true
},
{
"key": "novov.hardwrap.countleading",
"title": "Count indents as part of line width",
"type": "boolean",
"default": false
}
]

Expand Down

0 comments on commit 1e2bb8b

Please sign in to comment.