renderExtraNewlineToken
Check if there is an empty line immediately before the given token. If so, render it.
Function parameters
Parameters
- r:*Render
- token_index:Ast.TokenIndex
Type definitions in this namespace
Types
Functions in this namespace
Functions
Error sets in this namespace
Error Sets
Source
Implementation
fn renderExtraNewlineToken(r: *Render, token_index: Ast.TokenIndex) Error!void {
const tree = r.tree;
const ais = r.ais;
const token_start = tree.tokenStart(token_index);
if (token_start == 0) return;
const prev_token_end = if (token_index == 0)
0
else
tree.tokenStart(token_index - 1) + tokenSliceForRender(tree, token_index - 1).len;
// If there is a immediately preceding comment or doc_comment,
// skip it because required extra newline has already been rendered.
if (mem.indexOf(u8, tree.source[prev_token_end..token_start], "//") != null) return;
if (tree.isTokenPrecededByTags(token_index, &.{.doc_comment})) return;
// Iterate backwards to the end of the previous token, stopping if a
// non-whitespace character is encountered or two newlines have been found.
var i = token_start - 1;
var newlines: u2 = 0;
while (std.ascii.isWhitespace(tree.source[i])) : (i -= 1) {
if (tree.source[i] == '\n') newlines += 1;
if (newlines == 2) return ais.insertNewline();
if (i == prev_token_end) break;
}
}