tweaking...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-09-14 01:48:57 +03:00
parent a22d408ea8
commit 1f1df751c0
2 changed files with 36 additions and 9 deletions

View File

@ -93,6 +93,11 @@ function(text, tab_size, leading_tabs){
// trim the tail and remove leading blank lines... // trim the tail and remove leading blank lines...
var lines = text.trimEnd().split(/\n/) var lines = text.trimEnd().split(/\n/)
while(lines[0].trim() == ''){ 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() } lines.shift() }
// count common indent... // count common indent...
var l = lines var l = lines
@ -101,17 +106,20 @@ function(text, tab_size, leading_tabs){
return e.trim().length == 0 return e.trim().length == 0
// ignore 0 indent of first line... // ignore 0 indent of first line...
|| (i == 0 && indent == 0) ? || (i == 0 && indent == 0) ?
l || indent l
// last line -- ignore leading_tabs if lower indent... // 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 : i == lines.length-1
&& indent >= l ? && indent >= l ?
Math.max(indent - leading_tabs, 0) Math.min(l, Math.max(indent - leading_tabs, 0))
// initial state... // initial state...
: l < 0 ? : l < 0 ?
indent indent
// min... // min...
: Math.min(l, indent) }, -1) : Math.min(l, indent) }, -1) || 0
// normalize... // normalize...
return lines return lines
.map(function(line, i){ .map(function(line, i){

29
test.js
View File

@ -673,13 +673,30 @@ var cases = test.Cases({
}, },
text_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' // NOTE: there is no way to know what is the indent of 'a'
// relative to the rest of the text... // relative to the rest of the text...
// ...without reading the source file, and that should
// be avoided.
{ input: `a { input: `a
c`, c`,
text: 'a\nc', 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'}, doc: 'a\n c'},
{ input: `a { input: `a
b b
@ -688,13 +705,10 @@ var cases = test.Cases({
doc: 'a\n b\n c' }, doc: 'a\n b\n c' },
// XXX text(..): c is not indented...
{ input: ` { input: `
a a
c`, c`,
all: 'a\n c' }, all: 'a\n c' },
// XXX text(..): losing all lines but 0 and -1...
// XXX text(..): c is not indented...
{ input: ` { input: `
a a
b b
@ -710,6 +724,11 @@ var cases = test.Cases({
b b
c`, c`,
all: `a\n b\nc` }, all: `a\n b\nc` },
{ input: `
a
b
c`,
all: `a\n b\nc` },
], ],
text: function(assert){ text: function(assert){
this.text_cases this.text_cases