diff --git a/FIGlet Text.novaextension/CHANGELOG.md b/FIGlet Text.novaextension/CHANGELOG.md index 8759511..ae5ff46 100644 --- a/FIGlet Text.novaextension/CHANGELOG.md +++ b/FIGlet Text.novaextension/CHANGELOG.md @@ -1,3 +1,8 @@ +## Version 1.6.1 +- fixed FIGlet converted text output on indented selections +- refactored auto comment code +- added SCSS and Typescript auto comment support + ## Version 1.6 - added option to auto comment FIGlet converted output for CSS, HTML, Javascript, and PHP syntaxes diff --git a/FIGlet Text.novaextension/README.md b/FIGlet Text.novaextension/README.md index 5b96eb3..9b46a6d 100644 --- a/FIGlet Text.novaextension/README.md +++ b/FIGlet Text.novaextension/README.md @@ -2,7 +2,7 @@ - convert selected text to FIGlet text - select FIGlet font - configure FIGlet text output options -- option to auto comment converted text (HTML, Javascript, and PHP syntaxes only) +- option to auto comment converted text; supported syntaxes: CSS, HTML, Javascript, PHP, Typescript, and SCSS - prepend/append new lines to converted text # Requirements diff --git a/FIGlet Text.novaextension/Scripts/main.js b/FIGlet Text.novaextension/Scripts/main.js index 2209c96..da88c99 100644 --- a/FIGlet Text.novaextension/Scripts/main.js +++ b/FIGlet Text.novaextension/Scripts/main.js @@ -44,6 +44,7 @@ nova.commands.register('figlet', (workspace, figletArgs, textToConvert, postConv process.start() }) + // FIGlet convert the selected text in the editor nova.commands.register('figletTextEditor', editor => { let figConfig = { @@ -53,15 +54,23 @@ nova.commands.register('figletTextEditor', editor => { justification: nova.config.get('figlet_text.justification', 'string'), } - let syntax = editor.document.syntax let comment = nova.config.get('figlet_text.comment', 'boolean') let commentPadding = nova.config.get('figlet_text.commentPadding', 'number') let commentPaddingStr = nova.config.get('figlet_text.commentPaddingStr', 'string') - let commentChars = { - css: {start: '/*', end: '*/'}, - html: {start: ''}, - javascript: {start: '//', end: ''}, - php: {start: '//', end: ''} + const getCommentChars = () => { + switch (editor.document.syntax) { + case 'css': + case 'scss': + return {start: '/*', end: '*/'} + case 'html': + return {start: ''} + case 'javascript': + case 'typescript': + case 'php': + return {start: '//', end: ''} + default: + return null + } } let prependNewLines = nova.config.get('figlet_text.prependNewLines', 'number') let appendNewLines = nova.config.get('figlet_text.appendNewLines', 'number') @@ -70,31 +79,53 @@ nova.commands.register('figletTextEditor', editor => { for (let range of selectedRanges) { let text = editor.getTextInRange(range) + let indentRange = new Range(editor.getLineRangeForRange(range).start, range.start) nova.commands.invoke('figlet', figConfig, text, figletText => { // comment each line if the option is selected - if (comment && (syntax === 'css' || syntax === 'html' || syntax === 'javascript' || syntax === 'php')) { + // and the comment structure is defined + if (comment && getCommentChars() !== null) { + // convert the FIGlet string to an array of strings + // to make it easier to comment line by line let lines = figletText.split('\n') + // find the longest line so we can add whitespace to shorter + // lines so closing comments line up if the syntax uses them let longestLine = 0 - lines.map(line => { - if (line.length > longestLine) longestLine = line.length - }) + lines.map(line => { if (line.length > longestLine) longestLine = line.length }) + // add the comment characters, lengthen lines with closing + // comments, and add user configured comment padding let linesCommented = lines.map(line => { let linePadding = 0 - if (line.length < longestLine && (commentChars[syntax].end !== '')) linePadding = longestLine - line.length - return `${commentChars[syntax].start}${commentPaddingStr.repeat(commentPadding)}${line}${' '.repeat(linePadding)}${commentPaddingStr.repeat(commentPadding)}${commentChars[syntax].end}`.trimEnd() + if (line.length < longestLine && (getCommentChars().end !== '')) linePadding = longestLine - line.length + + // return the fully commented and formatted array of strings + return `${getCommentChars().start}${commentPaddingStr.repeat(commentPadding)}${line}${' '.repeat(linePadding)}${commentPaddingStr.repeat(commentPadding)}${getCommentChars().end}`.trimEnd() }) + // convert the array of strings to a single string figletText = linesCommented.join('\n') } + if (!indentRange.empty) { + let lines = figletText.split('\n') + let indentText = editor.getTextInRange(indentRange) + let linesIndented = lines.map((line, index) => { + if (index === 0) { + return `${line}`.trimEnd() + } else { + return `${indentText}${line}`.trimEnd() + } + }) + figletText = linesIndented.join('\n') + } + // prepend/append new lines if (prependNewLines > 0) figletText = `${'\n'.repeat(prependNewLines)}${figletText}` if (appendNewLines > 0) figletText = `${figletText}${'\n'.repeat(appendNewLines)}` - // replace the selection with the converted/transformed FIGlet text + // replace the selection with the fully final FIGlet text editor.edit(e => { e.replace(range, figletText) }) // deselect and position the cursor @@ -103,6 +134,7 @@ nova.commands.register('figletTextEditor', editor => { } }) + // FIGlet convert the preview text in the extension config nova.commands.register('figletTextFontPreview', workspace => { let figConfig = { diff --git a/FIGlet Text.novaextension/extension.json b/FIGlet Text.novaextension/extension.json index 2a8338c..d079a79 100644 --- a/FIGlet Text.novaextension/extension.json +++ b/FIGlet Text.novaextension/extension.json @@ -3,7 +3,7 @@ "name": "FIGlet Text", "organization": "Dan Remollino", "description": "Convert selected text to FIGlet. Great for adding readable text to the Minimap, creating l33t text headers, and organizing files.", - "version": "1.6", + "version": "1.6.1", "categories": ["commands", "formatters"], "entitlements": { @@ -240,7 +240,7 @@ { "key": "figlet_text.comment", "title": "Comment FIGlet Output", - "description": "FIGlet Text will auto line comment the output. Currently supported in CSS, HTML, Javascript, and PHP syntaxes.", + "description": "FIGlet Text will auto line comment the output. Currently supported in CSS, HTML, Javascript, PHP, Typescript, and SCSS syntaxes.", "type": "boolean", "default": true },