From 557750c3f8518af2bdec6b5940920544986a2ce1 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 18 Jul 2019 10:31:22 +0900 Subject: [PATCH] Added indenter, fixes #34 --- desktop/sources/index.html | 1 + desktop/sources/links/main.css | 2 +- desktop/sources/scripts/commander.js | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/desktop/sources/index.html b/desktop/sources/index.html index 6a57f64..d3a5353 100644 --- a/desktop/sources/index.html +++ b/desktop/sources/index.html @@ -40,6 +40,7 @@ ronin.controller.addRole('default', 'Edit', 'paste') ronin.controller.addRole('default', 'Edit', 'delete') ronin.controller.addRole('default', 'Edit', 'selectall') + ronin.controller.add("default","Edit","Re-Indent",() => { ronin.commander.reindent() },"CmdOrCtrl+Shift+I") ronin.controller.add("default","View","Zoom In",() => { ronin.modZoom(0.25) },"CmdOrCtrl+=") ronin.controller.add("default","View","Zoom Out",() => { ronin.modZoom(-0.25) },"CmdOrCtrl+-") ronin.controller.add("default","View","Zoom Reset",() => { ronin.modZoom(1,true) },"CmdOrCtrl+0") diff --git a/desktop/sources/links/main.css b/desktop/sources/links/main.css index 350ac06..edf46f1 100644 --- a/desktop/sources/links/main.css +++ b/desktop/sources/links/main.css @@ -13,4 +13,4 @@ body { margin:0px; padding:0px; overflow:hidden; font-family:"input_mono_regular #ronin canvas#guide { background:none; } #ronin canvas#surface { border-radius: 2px } -#ronin.hidden canvas#surface, #ronin.hidden canvas#guide { left:0px; } \ No newline at end of file +#ronin.hidden canvas#surface, #ronin.hidden canvas#guide { left:10px; } \ No newline at end of file diff --git a/desktop/sources/scripts/commander.js b/desktop/sources/scripts/commander.js index cab77f4..4f9d441 100644 --- a/desktop/sources/scripts/commander.js +++ b/desktop/sources/scripts/commander.js @@ -33,6 +33,26 @@ function Commander (ronin) { this.run() } + this.reindent = function () { + let val = this._input.value.replace(/\n/g, '').replace(/\( \(/g, '((').replace(/\) \)/g, '))').replace(/ {2}/g, ' ').trim() + let depth = 0 + for (let i = 0; i < val.length; i++) { + const c = val.charAt(i) + if (c === '(') { depth++ } else if (c === ')') { depth-- } + if (c === ';') { + const indent = '\n' + (' '.repeat(depth)) + val = val.insert(indent, i) + i += indent.length + } + if (c === '(') { + const indent = '\n' + (' '.repeat(depth - 1)) + val = val.insert(indent, i) + i += indent.length + } + } + this._input.value = val.trim() + } + this.setStatus = function (msg) { if (!msg) { return } this._status.textContent = `${(msg + '').substr(0, 40)}` @@ -196,4 +216,6 @@ function Commander (ronin) { }, '') } } + + String.prototype.insert = function (s, i) { return [this.slice(0, i), `${s}`, this.slice(i)].join('') } }