feat: track line length
Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
parent
16028a8f62
commit
6e97ea81d1
|
@ -23,7 +23,8 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
|
||||||
File.LockingTextWriter buf;
|
File.LockingTextWriter buf;
|
||||||
const Config* config;
|
const Config* config;
|
||||||
string eol;
|
string eol;
|
||||||
uint depth;
|
uint depth; // the current indentation level
|
||||||
|
uint length; // the length of the current line of code
|
||||||
bool declString; // set while declaring alias for string,wstring or dstring
|
bool declString; // set while declaring alias for string,wstring or dstring
|
||||||
bool isNewline; // used to indent before writing the line
|
bool isNewline; // used to indent before writing the line
|
||||||
bool insideCase; // true if the node is a child of a CaseStatement
|
bool insideCase; // true if the node is a child of a CaseStatement
|
||||||
|
@ -69,17 +70,20 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
|
||||||
auto indent = config.indent_style == IndentStyle.space ? ' '.repeat()
|
auto indent = config.indent_style == IndentStyle.space ? ' '.repeat()
|
||||||
.take(depth * 4) : '\t'.repeat().take(depth);
|
.take(depth * 4) : '\t'.repeat().take(depth);
|
||||||
buf.put(indent.array);
|
buf.put(indent.array);
|
||||||
|
length += indent.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void newline()
|
void newline()
|
||||||
{
|
{
|
||||||
buf.put(eol);
|
buf.put(eol);
|
||||||
|
length = 0;
|
||||||
// Indicate that the next write should be indented
|
// Indicate that the next write should be indented
|
||||||
isNewline = true;
|
isNewline = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern (D) void write(T)(T data)
|
void write(T)(T data)
|
||||||
|
if (is(T : char) || is(T : dchar))
|
||||||
{
|
{
|
||||||
if (isNewline)
|
if (isNewline)
|
||||||
{
|
{
|
||||||
|
@ -87,8 +91,22 @@ extern (C++) class FormatVisitor : SemanticTimeTransitiveVisitor
|
||||||
isNewline = false;
|
isNewline = false;
|
||||||
}
|
}
|
||||||
buf.put(data);
|
buf.put(data);
|
||||||
|
length += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern (D) void write(T)(T data)
|
||||||
|
if (!(is(T : char) || is(T : dchar)))
|
||||||
|
{
|
||||||
|
if (isNewline)
|
||||||
|
{
|
||||||
|
indent();
|
||||||
|
isNewline = false;
|
||||||
|
}
|
||||||
|
buf.put(data);
|
||||||
|
length += data.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*******************************************
|
/*******************************************
|
||||||
* Helpers to write different AST nodes to buffer
|
* Helpers to write different AST nodes to buffer
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue