Merge pull request #470 from dlang-community/issue-469

Fix Issue #469
merged-on-behalf-of: Brian Schott <Hackerpilot@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2020-03-09 10:03:20 +01:00 committed by GitHub
commit 66faac49f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 15 deletions

View File

@ -4,7 +4,7 @@
"targetType": "autodetect", "targetType": "autodetect",
"license": "BSL-1.0", "license": "BSL-1.0",
"dependencies": { "dependencies": {
"libdparse": "~>0.13.0" "libdparse": "~>0.14.0"
}, },
"targetPath" : "bin/", "targetPath" : "bin/",
"targetName" : "dfmt", "targetName" : "dfmt",

@ -1 +1 @@
Subproject commit f512cb0b4bc11ce64eb64a710163495e425a7ad8 Subproject commit 597d9a697b1f8a51fb2f441c61d0c6cc4eadc6d1

View File

@ -16,7 +16,17 @@ import dfmt.tokens;
import dfmt.wrapping; import dfmt.wrapping;
import std.array; 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) Config* formatterConfig)
{ {
LexerConfig config; LexerConfig config;
@ -33,11 +43,18 @@ void format(OutputRange)(string source_desc, ubyte[] buffer, OutputRange output,
auto visitor = new FormatVisitor(&astInformation); auto visitor = new FormatVisitor(&astInformation);
visitor.visit(mod); visitor.visit(mod);
astInformation.cleanup(); 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 depths = generateDepthInfo(tokens);
auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths, auto tokenFormatter = TokenFormatter!OutputRange(buffer, tokens, depths,
output, &astInformation, formatterConfig); output, &astInformation, formatterConfig);
tokenFormatter.format(); tokenFormatter.format();
return true;
} }
immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted immutable(short[]) generateDepthInfo(const Token[] tokens) pure nothrow @trusted

View File

@ -19,14 +19,14 @@ static immutable VERSION = () {
version (built_with_dub) version (built_with_dub)
{ {
enum DFMT_VERSION = import("dubhash.txt").strip; enum DFMT_VERSION = import("dubhash.txt").strip;
} }
else else
{ {
/** /**
* Current build's Git commit hash * Current build's Git commit hash
*/ */
enum DFMT_VERSION = import("githash.txt").strip; enum DFMT_VERSION = import("githash.txt").strip;
} }
return DFMT_VERSION ~ DEBUG_SUFFIX; return DFMT_VERSION ~ DEBUG_SUFFIX;
@ -215,7 +215,9 @@ else
else else
break; break;
} }
format("stdin", buffer, output.lockingTextWriter(), &config); immutable bool formatSuccess = format("stdin", buffer,
output.lockingTextWriter(), &config);
return formatSuccess ? 0 : 1;
} }
else else
{ {
@ -223,6 +225,7 @@ else
if (args.length >= 2) if (args.length >= 2)
inplace = true; inplace = true;
int retVal;
while (args.length > 0) while (args.length > 0)
{ {
const path = args.front; const path = args.front;
@ -257,11 +260,13 @@ else
f.rawRead(buffer); f.rawRead(buffer);
if (inplace) if (inplace)
output = File(path, "wb"); 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;
} }
} }

View File

@ -0,0 +1 @@
import std.stdio; void main() { writeln("\eee8Hello"); int a = 5; }

View File

@ -6,9 +6,9 @@ do
for source in *.d for source in *.d
do do
echo "${source}.ref" "${braceStyle}/${source}.out" echo "${source}.ref" "${braceStyle}/${source}.out"
argsFile=$(basename ${source} .d).args argsFile=$(basename "${source}" .d).args
if [ -e ${argsFile} ]; then if [ -e "${argsFile}" ]; then
args=$(cat ${argsFile}) args=$(cat "${argsFile}")
else else
args= args=
fi fi
@ -16,3 +16,13 @@ do
diff -u "${braceStyle}/${source}.ref" "${braceStyle}/${source}.out" diff -u "${braceStyle}/${source}.ref" "${braceStyle}/${source}.out"
done done
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