Skip to content

Commit

Permalink
Improved indentation normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Apr 1, 2015
1 parent 668ca33 commit fac8edc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/utils/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ define(function(require, exports, module) {
indentation: resources.getVariable('indentation')
}, options);

var reIndent = /^\t+/;
var indent = function(tabs) {
return utils.repeatString(options.indentation, tabs.length);
};
Expand All @@ -104,7 +103,9 @@ define(function(require, exports, module) {
// normailze indentation if it’s not tabs
if (options.indentation !== '\t') {
lines = lines.map(function(line) {
return line.replace(reIndent, indent);
return line.replace(/^\s+/, function(space) {
return space.replace(/\t/g, indent);
});
});
}

Expand Down
21 changes: 21 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var assert = require('assert');
var utils = require('../lib/utils/common');
var editorUtils = require('../lib/utils/editor');

describe('Utils', function() {
var normalize = function(str, options) {
return editorUtils.normalize(str, utils.extend({
indentation: ' '
}, options || {}));
};

it('normalize simple indentation', function() {
assert.equal(normalize('\ta\n\tb'), ' a\n b');
assert.equal(normalize('\t\t\ta\n\t\tb'), ' a\n b');
});

it('normalize mixed indentation', function() {
assert.equal(normalize(' \ta\n \tb'), ' a\n b');
assert.equal(normalize(' \t a\n \tb'), ' a\n b');
});
});

0 comments on commit fac8edc

Please sign in to comment.