From 1f1df751c04aec46efb03c64a35f440a57aa653a Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 14 Sep 2020 01:48:57 +0300 Subject: [PATCH] tweaking... Signed-off-by: Alex A. Naanou --- object.js | 16 ++++++++++++---- test.js | 29 ++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/object.js b/object.js index 6761a53..77d4109 100755 --- a/object.js +++ b/object.js @@ -93,6 +93,11 @@ function(text, tab_size, leading_tabs){ // trim the tail and remove leading blank lines... var lines = text.trimEnd().split(/\n/) while(lines[0].trim() == ''){ + // XXX we have two options here: + // - indent everyline including the first non-blank + // - do not indent anything (current) + // ...not sure which is best... + leading_tabs = 0 lines.shift() } // count common indent... var l = lines @@ -101,17 +106,20 @@ function(text, tab_size, leading_tabs){ return e.trim().length == 0 // ignore 0 indent of first line... || (i == 0 && indent == 0) ? - l || indent + l // last line -- ignore leading_tabs if lower indent... - // XXX BUG this does messes things up for 2 liners... + // XXX does not work correctly when: + // - two lines + // - l is 0 + // - non-zero leading_tabs... : i == lines.length-1 && indent >= l ? - Math.max(indent - leading_tabs, 0) + Math.min(l, Math.max(indent - leading_tabs, 0)) // initial state... : l < 0 ? indent // min... - : Math.min(l, indent) }, -1) + : Math.min(l, indent) }, -1) || 0 // normalize... return lines .map(function(line, i){ diff --git a/test.js b/test.js index c9ef5f5..5d6bd91 100755 --- a/test.js +++ b/test.js @@ -673,13 +673,30 @@ var cases = test.Cases({ }, text_cases: [ + // sanity checks... + { input: 'abc', + all: 'abc'}, + { input: '\n\t\tabc', + all: 'abc'}, + // NOTE: there is no way to know what is the indent of 'a' // relative to the rest of the text... - // ...without reading the source file, and that should - // be avoided. { input: `a c`, text: 'a\nc', + // XXX fail... + doc: 'a\n c'}, + { input: `a\nc`, + text: 'a\nc', + doc: 'a\nc'}, + { input: `\ + a + c`, + all: 'a\n c'}, + { input: ` + a + c`, + text: 'a\n c', doc: 'a\n c'}, { input: `a b @@ -688,13 +705,10 @@ var cases = test.Cases({ doc: 'a\n b\n c' }, - // XXX text(..): c is not indented... { input: ` a c`, all: 'a\n c' }, - // XXX text(..): losing all lines but 0 and -1... - // XXX text(..): c is not indented... { input: ` a b @@ -710,6 +724,11 @@ var cases = test.Cases({ b c`, all: `a\n b\nc` }, + { input: ` + a + b + c`, + all: `a\n b\nc` }, ], text: function(assert){ this.text_cases