Skip to content

Commit

Permalink
Merge pull request #470 from dlang-community/issue-469
Browse files Browse the repository at this point in the history
Fix Issue #469
merged-on-behalf-of: Brian Schott <[email protected]>
  • Loading branch information
dlang-bot authored Mar 9, 2020
2 parents c41c034 + e9034f4 commit 66faac4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"targetType": "autodetect",
"license": "BSL-1.0",
"dependencies": {
"libdparse": "~>0.13.0"
"libdparse": "~>0.14.0"
},
"targetPath" : "bin/",
"targetName" : "dfmt",
Expand Down
21 changes: 19 additions & 2 deletions src/dfmt/formatter.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@ import dfmt.tokens;
import dfmt.wrapping;
import std.array;

void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
/**
* Formats the code contained in `buffer` into `output`.
* Params:
* source_desc = A description of where `buffer` came from. Usually a file name.
* buffer = The raw source code.
* output = The output range that will have the formatted code written to it.
* formatterConfig = Formatter configuration.
* Returns: `true` if the formatting succeeded, `false` of a lexing error. This
* function can return `true` if parsing failed.
*/
bool format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
Config* formatterConfig)
{
LexerConfig config;
Expand All @@ -33,11 +43,18 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
auto visitor = new FormatVisitor(&astInformation);
visitor.visit(mod);
astInformation.cleanup();
auto tokens = byToken(buffer, config, &cache).array();
auto tokenRange = byToken(buffer, config, &cache);
auto app = appender!(Token[])();
for (; !tokenRange.empty(); tokenRange.popFront())
app.put(tokenRange.front());
auto tokens = app.data;
if (!tokenRange.messages.empty)
return false;
auto depths = generateDepthInfo(tokens);
auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths,
output, &astInformation, formatterConfig);
tokenFormatter.format();
return true;
}

immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted
Expand Down
21 changes: 13 additions & 8 deletions src/dfmt/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ static immutable VERSION = () {

version (built_with_dub)
{
enum DFMT_VERSION = import("dubhash.txt").strip;
enum DFMT_VERSION = import("dubhash.txt").strip;
}
else
{
/**
* Current build's Git commit hash
*/
enum DFMT_VERSION = import("githash.txt").strip;
/**
* Current build's Git commit hash
*/
enum DFMT_VERSION = import("githash.txt").strip;
}

return DFMT_VERSION ~ DEBUG_SUFFIX;
Expand Down Expand Up @@ -215,14 +215,17 @@ else
else
break;
}
format("stdin", buffer, output.lockingTextWriter(), &config);
immutable bool formatSuccess = format("stdin", buffer,
output.lockingTextWriter(), &config);
return formatSuccess ? 0 : 1;
}
else
{
import std.file : dirEntries, isDir, SpanMode;

if (args.length >= 2)
inplace = true;
int retVal;
while (args.length > 0)
{
const path = args.front;
Expand Down Expand Up @@ -257,11 +260,13 @@ else
f.rawRead(buffer);
if (inplace)
output = File(path, "wb");
format(path, buffer, output.lockingTextWriter(), &config);
immutable bool formatSuccess = format(path, buffer, output.lockingTextWriter(), &config);
if (!formatSuccess)
retVal = 1;
}
}
return retVal;
}
return 0;
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/expected_failures/issue0469.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import std.stdio; void main() { writeln("\eee8Hello"); int a = 5; }
16 changes: 13 additions & 3 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ do
for source in *.d
do
echo "${source}.ref" "${braceStyle}/${source}.out"
argsFile=$(basename ${source} .d).args
if [ -e ${argsFile} ]; then
args=$(cat ${argsFile})
argsFile=$(basename "${source}" .d).args
if [ -e "${argsFile}" ]; then
args=$(cat "${argsFile}")
else
args=
fi
../bin/dfmt --brace_style=${braceStyle} ${args} "${source}" > "${braceStyle}/${source}.out"
diff -u "${braceStyle}/${source}.ref" "${braceStyle}/${source}.out"
done
done

set +e

for source in expected_failures/*.d
do
if ../bin/dfmt "${source}" > /dev/null; then
echo "Expected failure on test ${source} but passed"
exit 1
fi
done

0 comments on commit 66faac4

Please sign in to comment.