Skip to content

Commit

Permalink
Fixe sindresorhus#121 by making insert_final_newline more lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Breisch committed Oct 27, 2016
1 parent 8231a7d commit 3aaf29e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ function initializeTextBuffer(buffer) {
const lastRow = buffer.getLineCount() - 1;

if (buffer.isRowBlank(lastRow)) {
const previousNonBlankRow = buffer.previousNonBlankRow(lastRow);
let stripStart = buffer.previousNonBlankRow(lastRow);

if (settings.insert_final_newline === true) {
stripStart += 1;
}
// Strip empty lines from the end
if (previousNonBlankRow < lastRow) {
buffer.deleteRows(previousNonBlankRow + 1, lastRow);
if (stripStart < lastRow) {
buffer.deleteRows(stripStart + 1, lastRow);
}
}
if (settings.insert_final_newline === true) {
} else if (settings.insert_final_newline === true) {
buffer.append('\n');
}
}
Expand Down
78 changes: 78 additions & 0 deletions spec/iss121-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/** @babel */
/* eslint-env jasmine, atomtest */

/*
This file contains verifying specs for:
https://github.com/sindresorhus/atom-editorconfig/issues/121
Due to a bad implementation of the `insert_final_newline`-logic the final newline
is unecessarily stripped and appended even if a single final newline already exists.
This messes the undo-history up.
*/

import fs from 'fs';
import path from 'path';

const projectRoot = path.join(__dirname, 'fixtures');
const filePath = path.join(
projectRoot,
`test.${path.basename(__filename).split('-').shift()}`
);

describe('editorconfig', () => {
let textEditor;
const textWithOneFinalNewline = `
This is a text
with a lot of lines,
which have a lot of
trailing spaces.\n`;

beforeEach(() => {
waitsForPromise(() =>
Promise.all([
atom.packages.activatePackage('editorconfig'),
atom.workspace.open(filePath)
]).then(results => {
textEditor = results[1];
})
);
});

afterEach(() => {
// remove the created fixture, if it exists
runs(() => {
fs.stat(filePath, (err, stats) => {
if (!err && stats.isFile()) {
fs.unlink(filePath);
}
});
});

waitsFor(() => {
try {
return fs.statSync(filePath).isFile() === false;
} catch (err) {
return true;
}
}, 5000, `removed ${filePath}`);
});

describe('Atom being set to insert a final newline', () => {
beforeEach(() => {
Object.assign(textEditor.getBuffer().editorconfig.settings, {
trim_trailing_whitespace: true, // eslint-disable-line camelcase
insert_final_newline: true // eslint-disable-line camelcase
});
});

it('should strip not extend the undo-history if a final newline already exists', () => {
const buffer = textEditor.getBuffer();

textEditor.setText(textWithOneFinalNewline);

const checkpoint = buffer.createCheckpoint();
textEditor.save();
expect(buffer.getChangesSinceCheckpoint(checkpoint).length).toEqual(0);
});
});
});

0 comments on commit 3aaf29e

Please sign in to comment.