diff --git a/src/util.js b/src/util.js index 4216a9ec..d124a686 100644 --- a/src/util.js +++ b/src/util.js @@ -234,9 +234,20 @@ util.getLast = function(arr) { return null; }; +function skipNewLineForward(text, index) { + if (text.charAt(index) === "\n") { + return index + 1; + } + if (text.charAt(index) === "\r" && text.charAt(index + 1) === "\n") { + return index + 2; + } + // Note: this is incorrect, but makes the current tests pass for now. + return index + 1; +} + function _findNewline(text, index, backwards) { const length = text.length; - let cursor = backwards ? index - 1 : index + 1; + let cursor = backwards ? index - 1 : skipNewLineForward(text, index); // Look forward and see if there is a newline after/before this code // by scanning up/back to the next non-indentation character. while (cursor > 0 && cursor < length) { diff --git a/tests/line-endings/__snapshots__/jsfmt.spec.js.snap b/tests/line-endings/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..b88e44e4 --- /dev/null +++ b/tests/line-endings/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,14 @@ +exports[`test windows.js 1`] = ` +"const vscode = require(\"vscode\"); +const {getDir, getActiveFile, uint8arrayToString} = require(\"./utils\"); + +let outChannel; +let _commands; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +const vscode = require(\"vscode\"); +const { getDir, getActiveFile, uint8arrayToString } = require(\"./utils\"); + +let outChannel; +let _commands; +" +`; diff --git a/tests/line-endings/jsfmt.spec.js b/tests/line-endings/jsfmt.spec.js new file mode 100644 index 00000000..989047bc --- /dev/null +++ b/tests/line-endings/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname); diff --git a/tests/line-endings/windows.js b/tests/line-endings/windows.js new file mode 100644 index 00000000..3779d12d --- /dev/null +++ b/tests/line-endings/windows.js @@ -0,0 +1,5 @@ +const vscode = require("vscode"); +const {getDir, getActiveFile, uint8arrayToString} = require("./utils"); + +let outChannel; +let _commands;