diff --git a/compiler/util/adjustIndent.js b/compiler/util/adjustIndent.js index c6421932f..a90ddf28f 100644 --- a/compiler/util/adjustIndent.js +++ b/compiler/util/adjustIndent.js @@ -1,22 +1,52 @@ var splitLinesRegExp = /\r?\n/; var initialIndentationRegExp = /^\s+/; -// var initialSpaceMatches = /^\s+/.exec(text); -// if (initialSpaceMatches) { -// var indentMatches = /\n[^\n]*$/.exec(initialSpaceMatches[0]); -// if (indentMatches) { -// var indentRegExp = new RegExp(indentMatches[0].replace(/\n/g, '\\n'), 'g'); -// text = text.replace(indentRegExp, '\n'); -// } -// text = text.replace(/^\s*/, '').replace(/\s*$/, ''); -// this.setText(text); -// } +function removeInitialEmptyLines(lines) { + var i; + + for (i=0; i=0; i--) { + if (lines[i].trim() !== '') { + break; + } + } + + if (i !== last) { + lines = lines.slice(0, i+1); + } + + return lines; +} + function adjustIndent(str, newIndentation) { if (!str) { return str; } var lines = str.split(splitLinesRegExp); + lines = removeInitialEmptyLines(lines); + lines = removeTrailingEmptyLines(lines); + + if (lines.length === 0) { + return ''; + } + var initialIndentationMatches = initialIndentationRegExp.exec(lines[0]); var indentation = initialIndentationMatches ? initialIndentationMatches[0] : ''; @@ -33,37 +63,8 @@ function adjustIndent(str, newIndentation) { }); return newIndentation ? - newIndentation + lines.join('\n' + newIndentation) : + lines.join('\n' + newIndentation) : lines.join('\n'); } -// function adjustIndent(str, newIndentation) { -// if (!str) { -// return str; -// } -// -// var lines = str.split(splitLinesRegExp); -// var initialIndentationMatches = initialIndentationRegExp.exec(lines[0]); -// -// var indentation = initialIndentationMatches ? initialIndentationMatches[0] : ''; -// if (!indentation && !newIndentation) { -// return str; -// } -// -// var result = ''; -// -// lines.forEach((line, i) => { -// if (line.startsWith(indentation)) { -// line = line.substring(indentation.length); -// } -// -// if (newIndentation) { -// line = newIndentation + line; -// } -// -// result += line + '\n'; -// }); -// -// return result; -// } module.exports = adjustIndent; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt b/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt index 5af2954ba..03b46f0a3 100644 --- a/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt +++ b/test/fixtures/adjustIndent/autotest/bad-indent/expected.txt @@ -1,3 +1,3 @@ - var foo = require('foo'); +var foo = require('foo'); var bar = require('bar'); var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt b/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt index ab84318e5..9e0f7d933 100644 --- a/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt +++ b/test/fixtures/adjustIndent/autotest/increase-indent/expected.txt @@ -1,2 +1,2 @@ - var foo = require('foo'); +var foo = require('foo'); var bar = require('bar'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/initial-empty-lines/expected.txt b/test/fixtures/adjustIndent/autotest/initial-empty-lines/expected.txt new file mode 100644 index 000000000..debde3bbf --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/initial-empty-lines/expected.txt @@ -0,0 +1,3 @@ +var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/initial-empty-lines/input.txt b/test/fixtures/adjustIndent/autotest/initial-empty-lines/input.txt new file mode 100644 index 000000000..499d9d4c7 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/initial-empty-lines/input.txt @@ -0,0 +1,5 @@ + + + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/initial-empty-lines/test.js b/test/fixtures/adjustIndent/autotest/initial-empty-lines/test.js new file mode 100644 index 000000000..91f4d398e --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/initial-empty-lines/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/no-indent/expected.txt b/test/fixtures/adjustIndent/autotest/no-indent/expected.txt index 5af2954ba..03b46f0a3 100644 --- a/test/fixtures/adjustIndent/autotest/no-indent/expected.txt +++ b/test/fixtures/adjustIndent/autotest/no-indent/expected.txt @@ -1,3 +1,3 @@ - var foo = require('foo'); +var foo = require('foo'); var bar = require('bar'); var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/expected.txt b/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/expected.txt new file mode 100644 index 000000000..debde3bbf --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/expected.txt @@ -0,0 +1,3 @@ +var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/input.txt b/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/input.txt new file mode 100644 index 000000000..f42a9b293 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/input.txt @@ -0,0 +1,6 @@ + + + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); + diff --git a/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/test.js b/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/test.js new file mode 100644 index 000000000..91f4d398e --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/surrounding-empty-lines/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt b/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt index 652d77bad..debde3bbf 100644 --- a/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt +++ b/test/fixtures/adjustIndent/autotest/tab-indent/expected.txt @@ -1,3 +1,3 @@ - var foo = require('foo'); +var foo = require('foo'); var bar = require('bar'); var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/trailing-empty-lines/expected.txt b/test/fixtures/adjustIndent/autotest/trailing-empty-lines/expected.txt new file mode 100644 index 000000000..debde3bbf --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/trailing-empty-lines/expected.txt @@ -0,0 +1,3 @@ +var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); \ No newline at end of file diff --git a/test/fixtures/adjustIndent/autotest/trailing-empty-lines/input.txt b/test/fixtures/adjustIndent/autotest/trailing-empty-lines/input.txt new file mode 100644 index 000000000..b48bbe5b7 --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/trailing-empty-lines/input.txt @@ -0,0 +1,4 @@ + var foo = require('foo'); + var bar = require('bar'); + var baz = require('baz'); + diff --git a/test/fixtures/adjustIndent/autotest/trailing-empty-lines/test.js b/test/fixtures/adjustIndent/autotest/trailing-empty-lines/test.js new file mode 100644 index 000000000..91f4d398e --- /dev/null +++ b/test/fixtures/adjustIndent/autotest/trailing-empty-lines/test.js @@ -0,0 +1 @@ +exports.newIndentation = ' '; \ No newline at end of file