rocambole-indent
Advanced tools
Comparing version 0.1.0 to 0.2.0
{ | ||
"name": "rocambole-indent", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "helpers for rocambole AST indentation", | ||
@@ -5,0 +5,0 @@ "main": "rocambole-indent.js", |
@@ -30,2 +30,5 @@ # rocambole-whitespace | ||
It will not include the start and end tokens on the indentation range, only the | ||
tokens in between them. | ||
```js | ||
@@ -44,5 +47,5 @@ // increase the indent level by 1 | ||
### line(token, level) | ||
### addLevel(token, level) | ||
Increases/decreases the indent level at the begining of the line that includes | ||
Increases/decreases the indent level at the beginning of the line that includes | ||
the given `token`. | ||
@@ -52,7 +55,7 @@ | ||
// adds 2 indents | ||
line(node.startToken, 2); | ||
addLevel(node.startToken, 2); | ||
// decrease indent level in 1 step | ||
line(node.endToken, -1); | ||
addLevel(node.endToken, -1); | ||
// zero does nothing | ||
line(node.endToken, 0); | ||
addLevel(node.endToken, 0); | ||
``` | ||
@@ -68,4 +71,4 @@ | ||
usually the original indentation of the program parsed by rocambole) or that | ||
are not at the begining of the line. It also updates all the `BlockComment` to | ||
align the multiple lines. | ||
are not at the beginning of the line. Also removing `WhiteSpace` tokens that | ||
are at the beginning of the line to avoid mistakes. | ||
@@ -79,2 +82,11 @@ ```js | ||
### updateBlockComment(token) | ||
Updates `BlockComment` `raw` value to make sure all the lines have the same | ||
`Indent` level. | ||
This is called internally by the `addLevel` and `indentInBetween` methods (if | ||
first token of line is a `BlockComment`), so as long as you only use those | ||
methods to edit the indent level you shouldn't need to call this. | ||
## Debug | ||
@@ -81,0 +93,0 @@ |
@@ -38,5 +38,12 @@ 'use strict'; | ||
var token = startToken && startToken.next; | ||
var endsWithBraces = isClosingBrace(endToken); | ||
while (token && token !== endToken) { | ||
if (tk.isBr(token.prev)) { | ||
line(token, level); | ||
// we ignore the last indent (if first token on the line is a ws or | ||
// ident) just because in most cases we don't want to change the indent | ||
// just before "}", ")" and "]" - this allow us to pass | ||
// `node.body.startToken` and `node.body.endToken` as the range | ||
if (token.next !== endToken || !endsWithBraces || !tk.isEmpty(token)) { | ||
addLevel(token, level); | ||
} | ||
} | ||
@@ -48,4 +55,10 @@ token = token.next; | ||
exports.line = line; | ||
function line(token, level) { | ||
function isClosingBrace(token) { | ||
var val = token.value; | ||
return val === ')' || val === '}' || val === ']'; | ||
} | ||
exports.addLevel = addLevel; | ||
function addLevel(token, level) { | ||
if (!level) { | ||
@@ -60,3 +73,3 @@ // zero is a noop | ||
// we never indent empty lines! | ||
debug('[indent.before] can\'t find start of line'); | ||
debug('[indent.addLevel] can\'t find start of line'); | ||
return; | ||
@@ -80,2 +93,5 @@ } | ||
} | ||
if (token.next && token.next.type === 'BlockComment') { | ||
updateBlockComment(token.next); | ||
} | ||
return; | ||
@@ -87,3 +103,3 @@ } | ||
debug( | ||
'[before] we can\'t decrement if line doesn\'t start with Indent. token: %s, level: %s', | ||
'[addLevel] we can\'t decrement if line doesn\'t start with Indent. token: %s, level: %s', | ||
token && token.value, | ||
@@ -117,2 +133,3 @@ level | ||
if (tk.isBr(token) && tk.isBr(token.prev)) { | ||
// empty lines are ignored | ||
return null; | ||
@@ -139,4 +156,2 @@ } | ||
tk.remove(token); | ||
} else if (token.type === 'BlockComment') { | ||
updateBlockComment(token); | ||
} | ||
@@ -143,0 +158,0 @@ token = next; |
8856
142
101