Use output range instead of file for output

This commit is contained in:
Jonas Drewsen 2015-01-18 09:04:04 +01:00
parent 81b17c8f31
commit c1334ab3ec
1 changed files with 14 additions and 14 deletions

View File

@ -75,7 +75,7 @@ int main(string[] args)
else else
break; break;
} }
format("stdin", buffer, output); format("stdin", buffer, output.lockingTextWriter());
} }
else else
{ {
@ -100,14 +100,14 @@ int main(string[] args)
f.rawRead(buffer); f.rawRead(buffer);
if (inplace) if (inplace)
output = File(path, "w"); output = File(path, "w");
format(path, buffer, output); format(path, buffer, output.lockingTextWriter());
} }
} }
return 0; return 0;
} }
void format(Output)(string source_desc, ubyte[] buffer, Output output) void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output)
{ {
LexerConfig config; LexerConfig config;
config.stringBehavior = StringBehavior.source; config.stringBehavior = StringBehavior.source;
@ -124,14 +124,14 @@ void format(Output)(string source_desc, ubyte[] buffer, Output output)
visitor.visit(mod); visitor.visit(mod);
astInformation.cleanup(); astInformation.cleanup();
auto tokens = byToken(buffer, config, &cache).array(); auto tokens = byToken(buffer, config, &cache).array();
auto tokenFormatter = TokenFormatter!Output(tokens, output, &astInformation, auto tokenFormatter = TokenFormatter!OutputRange(tokens, output, &astInformation,
&formatterConfig); &formatterConfig);
tokenFormatter.format(); tokenFormatter.format();
} }
struct TokenFormatter(Output) struct TokenFormatter(OutputRange)
{ {
this(const(Token)[] tokens, Output output, ASTInformation* astInformation, this(const(Token)[] tokens, OutputRange output, ASTInformation* astInformation,
FormatterConfig* config) FormatterConfig* config)
{ {
this.tokens = tokens; this.tokens = tokens;
@ -486,7 +486,7 @@ private:
assumeSorted(astInformation.doubleNewlineLocations) assumeSorted(astInformation.doubleNewlineLocations)
.equalRange(tokens[index].index).length) .equalRange(tokens[index].index).length)
{ {
output.write("\n"); output.put("\n");
} }
if (config.braceStyle == BraceStyle.otbs) if (config.braceStyle == BraceStyle.otbs)
{ {
@ -690,7 +690,7 @@ private:
void newline() void newline()
{ {
output.write("\n"); output.put("\n");
currentLineLength = 0; currentLineLength = 0;
if (index < tokens.length) if (index < tokens.length)
{ {
@ -703,16 +703,16 @@ private:
void write(string str) void write(string str)
{ {
currentLineLength += str.length; currentLineLength += str.length;
output.write(str); output.put(str);
} }
void writeToken() void writeToken()
{ {
currentLineLength += currentTokenLength(); currentLineLength += currentTokenLength();
if (current.text is null) if (current.text is null)
output.write(str(current.type)); output.put(str(current.type));
else else
output.write(current.text); output.put(current.text);
index++; index++;
} }
@ -723,13 +723,13 @@ private:
foreach (i; 0 .. indentLevel + tempIndent) foreach (i; 0 .. indentLevel + tempIndent)
{ {
currentLineLength += config.tabSize; currentLineLength += config.tabSize;
output.write("\t"); output.put("\t");
} }
else else
foreach (i; 0 .. indentLevel + tempIndent) foreach (i; 0 .. indentLevel + tempIndent)
foreach (j; 0 .. config.indentSize) foreach (j; 0 .. config.indentSize)
{ {
output.write(" "); output.put(" ");
currentLineLength++; currentLineLength++;
} }
} }
@ -750,7 +750,7 @@ private:
uint currentLineLength = 0; uint currentLineLength = 0;
/// Output to write output to /// Output to write output to
Output output; OutputRange output;
/// Tokens being formatted /// Tokens being formatted
const(Token)[] tokens; const(Token)[] tokens;