More changes
This commit is contained in:
parent
32759fafae
commit
96501f7418
37
src/dfmt.d
37
src/dfmt.d
|
@ -851,19 +851,14 @@ struct FormatterConfig
|
||||||
{
|
{
|
||||||
/// Number of spaces used for indentation
|
/// Number of spaces used for indentation
|
||||||
uint indentSize = 4;
|
uint indentSize = 4;
|
||||||
|
|
||||||
/// Use tabs or spaces
|
/// Use tabs or spaces
|
||||||
bool useTabs = false;
|
bool useTabs = false;
|
||||||
|
|
||||||
/// Size of a tab character
|
/// Size of a tab character
|
||||||
uint tabSize = 8;
|
uint tabSize = 8;
|
||||||
|
|
||||||
/// Soft line wrap limit
|
/// Soft line wrap limit
|
||||||
uint columnSoftLimit = 80;
|
uint columnSoftLimit = 80;
|
||||||
|
|
||||||
/// Hard line wrap limit
|
/// Hard line wrap limit
|
||||||
uint columnHardLimit = 120;
|
uint columnHardLimit = 120;
|
||||||
|
|
||||||
/// Use the One True Brace Style
|
/// Use the One True Brace Style
|
||||||
BraceStyle braceStyle = BraceStyle.allman;
|
BraceStyle braceStyle = BraceStyle.allman;
|
||||||
}
|
}
|
||||||
|
@ -1011,6 +1006,18 @@ int tokenLength(ref const Token t) pure @safe @nogc
|
||||||
import std.algorithm : countUntil;
|
import std.algorithm : countUntil;
|
||||||
switch (t.type)
|
switch (t.type)
|
||||||
{
|
{
|
||||||
|
case tok!"doubleLiteral":
|
||||||
|
case tok!"floatLiteral":
|
||||||
|
case tok!"idoubleLiteral":
|
||||||
|
case tok!"ifloatLiteral":
|
||||||
|
case tok!"intLiteral":
|
||||||
|
case tok!"longLiteral":
|
||||||
|
case tok!"realLiteral":
|
||||||
|
case tok!"irealLiteral":
|
||||||
|
case tok!"uintLiteral":
|
||||||
|
case tok!"ulongLiteral":
|
||||||
|
case tok!"characterLiteral":
|
||||||
|
return cast(int) t.text.length;
|
||||||
case tok!"identifier":
|
case tok!"identifier":
|
||||||
case tok!"stringLiteral":
|
case tok!"stringLiteral":
|
||||||
case tok!"wstringLiteral":
|
case tok!"wstringLiteral":
|
||||||
|
@ -1019,6 +1026,8 @@ int tokenLength(ref const Token t) pure @safe @nogc
|
||||||
auto c = cast(int) t.text.countUntil('\n');
|
auto c = cast(int) t.text.countUntil('\n');
|
||||||
if (c == -1)
|
if (c == -1)
|
||||||
return cast(int) t.text.length;
|
return cast(int) t.text.length;
|
||||||
|
else
|
||||||
|
return c;
|
||||||
mixin (generateFixedLengthCases());
|
mixin (generateFixedLengthCases());
|
||||||
default:
|
default:
|
||||||
return INVALID_TOKEN_LENGTH;
|
return INVALID_TOKEN_LENGTH;
|
||||||
|
@ -1072,6 +1081,9 @@ bool isBreakToken(IdType t)
|
||||||
case tok!"%":
|
case tok!"%":
|
||||||
case tok!"+=":
|
case tok!"+=":
|
||||||
case tok!".":
|
case tok!".":
|
||||||
|
case tok!"~":
|
||||||
|
case tok!"+":
|
||||||
|
case tok!"-":
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
@ -1084,10 +1096,10 @@ int breakCost(IdType t)
|
||||||
{
|
{
|
||||||
case tok!"||":
|
case tok!"||":
|
||||||
case tok!"&&":
|
case tok!"&&":
|
||||||
return 21;
|
return 0;
|
||||||
case tok!"(":
|
case tok!"(":
|
||||||
case tok!",":
|
case tok!",":
|
||||||
return 34;
|
return 10;
|
||||||
case tok!"^^":
|
case tok!"^^":
|
||||||
case tok!"^=":
|
case tok!"^=":
|
||||||
case tok!"^":
|
case tok!"^":
|
||||||
|
@ -1125,6 +1137,9 @@ int breakCost(IdType t)
|
||||||
case tok!"&=":
|
case tok!"&=":
|
||||||
case tok!"%=":
|
case tok!"%=":
|
||||||
case tok!"%":
|
case tok!"%":
|
||||||
|
case tok!"+":
|
||||||
|
case tok!"-":
|
||||||
|
case tok!"~":
|
||||||
case tok!"+=":
|
case tok!"+=":
|
||||||
return 55;
|
return 55;
|
||||||
case tok!".":
|
case tok!".":
|
||||||
|
@ -1151,7 +1166,7 @@ struct State
|
||||||
if (breaks.length == 0)
|
if (breaks.length == 0)
|
||||||
{
|
{
|
||||||
_cost = int.max;
|
_cost = int.max;
|
||||||
s = false;
|
s = tokens.map!(a => tokenLength(a)).sum() < formatterConfig.columnSoftLimit;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -1212,8 +1227,7 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens,
|
||||||
|
|
||||||
int depth = 0;
|
int depth = 0;
|
||||||
auto open = new RedBlackTree!State;
|
auto open = new RedBlackTree!State;
|
||||||
open.insert(State(cast(size_t[])[], tokens, depth, formatterConfig,
|
open.insert(State(cast(size_t[])[], tokens, depth, formatterConfig, currentLineLength, indentLevel));
|
||||||
currentLineLength, indentLevel));
|
|
||||||
while (!open.empty)
|
while (!open.empty)
|
||||||
{
|
{
|
||||||
State current = open.front();
|
State current = open.front();
|
||||||
|
@ -1224,8 +1238,7 @@ size_t[] chooseLineBreakTokens(size_t index, const Token[] tokens,
|
||||||
b += index;
|
b += index;
|
||||||
return current.breaks;
|
return current.breaks;
|
||||||
}
|
}
|
||||||
foreach (next; validMoves(tokens, current, formatterConfig,
|
foreach (next; validMoves(tokens, current, formatterConfig, currentLineLength, indentLevel, depth))
|
||||||
currentLineLength, indentLevel, depth))
|
|
||||||
{
|
{
|
||||||
open.insert(next);
|
open.insert(next);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue