From a91f044958f183ac06e97b29a18cb739270c016a Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Sat, 25 Feb 2023 01:51:50 +0100 Subject: [PATCH] add some missing hasCurrent checks --- src/dfmt/formatter.d | 28 +++++++++++++++------------- tests/allman/incomplete_alias.d.ref | 1 + tests/incomplete_alias.d | 1 + tests/otbs/incomplete_alias.d.ref | 1 + 4 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 tests/allman/incomplete_alias.d.ref create mode 100644 tests/incomplete_alias.d create mode 100644 tests/otbs/incomplete_alias.d.ref diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index e5638e1..34d5eaf 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -247,9 +247,9 @@ private: else if (currentIs(tok!"return")) { writeToken(); - if (!currentIs(tok!";") && !currentIs(tok!")") && !currentIs(tok!"{") + if (hasCurrent && (!currentIs(tok!";") && !currentIs(tok!")") && !currentIs(tok!"{") && !currentIs(tok!"in") && !currentIs(tok!"out") && !currentIs(tok!"do") - && (hasCurrent && tokens[index].text != "body")) + && tokens[index].text != "body")) write(" "); } else if (currentIs(tok!"with")) @@ -258,14 +258,14 @@ private: indents.push(tok!"with"); writeToken(); write(" "); - if (currentIs(tok!"(")) + if (hasCurrent && currentIs(tok!"(")) writeParens(false); - if (!currentIs(tok!"switch") && !currentIs(tok!"with") + if (hasCurrent && !currentIs(tok!"switch") && !currentIs(tok!"with") && !currentIs(tok!"{") && !(currentIs(tok!"final") && peekIs(tok!"switch"))) { newline(); } - else if (!currentIs(tok!"{")) + else if (hasCurrent && !currentIs(tok!"{")) write(" "); } else if (currentIs(tok!"switch")) @@ -351,7 +351,7 @@ private: else if (isBasicType(current.type)) { writeToken(); - if (currentIs(tok!"identifier") || isKeyword(current.type) || inAsm) + if (hasCurrent && (currentIs(tok!"identifier") || isKeyword(current.type) || inAsm)) write(" "); } else if (isOperator(current.type)) @@ -1233,7 +1233,7 @@ private: break; case tok!"cast": writeToken(); - if (currentIs(tok!"(")) + if (hasCurrent && currentIs(tok!"(")) writeParens(config.dfmt_space_after_cast == OptionalBoolean.t); break; case tok!"out": @@ -1245,14 +1245,14 @@ private: write(" "); } writeToken(); - if (!currentIs(tok!"{") && !currentIs(tok!"comment")) + if (hasCurrent && !currentIs(tok!"{") && !currentIs(tok!"comment")) write(" "); break; case tok!"try": case tok!"finally": indents.push(current.type); writeToken(); - if (!currentIs(tok!"{")) + if (hasCurrent && !currentIs(tok!"{")) newline(); break; case tok!"identifier": @@ -1277,6 +1277,8 @@ private: write(" "); } writeToken(); + if (!hasCurrent) + return; immutable isFunctionLit = astInformation.funLitStartLocations.canFindIndex( current.index); if (isFunctionLit && config.dfmt_brace_style == BraceStyle.allman) @@ -1289,12 +1291,12 @@ private: tok!"}", tok!"=", tok!"&&", tok!"||") && !peekBackIsKeyword()) write(" "); writeToken(); - if (!currentIs(tok!"(") && !currentIs(tok!"{") && !currentIs(tok!"comment")) + if (hasCurrent && !currentIs(tok!"(") && !currentIs(tok!"{") && !currentIs(tok!"comment")) write(" "); break; case tok!"case": writeToken(); - if (!currentIs(tok!";")) + if (hasCurrent && !currentIs(tok!";")) write(" "); break; case tok!"enum": @@ -1308,7 +1310,7 @@ private: write(" "); indents.push(tok!"enum"); writeToken(); - if (!currentIs(tok!":") && !currentIs(tok!"{")) + if (hasCurrent && !currentIs(tok!":") && !currentIs(tok!"{")) write(" "); } break; @@ -1332,7 +1334,7 @@ private: goto default; case tok!"invariant": writeToken(); - if (currentIs(tok!"(")) + if (hasCurrent && currentIs(tok!"(")) write(" "); break; default: diff --git a/tests/allman/incomplete_alias.d.ref b/tests/allman/incomplete_alias.d.ref new file mode 100644 index 0000000..d63caa7 --- /dev/null +++ b/tests/allman/incomplete_alias.d.ref @@ -0,0 +1 @@ +alias u8 = ubyte \ No newline at end of file diff --git a/tests/incomplete_alias.d b/tests/incomplete_alias.d new file mode 100644 index 0000000..d63caa7 --- /dev/null +++ b/tests/incomplete_alias.d @@ -0,0 +1 @@ +alias u8 = ubyte \ No newline at end of file diff --git a/tests/otbs/incomplete_alias.d.ref b/tests/otbs/incomplete_alias.d.ref new file mode 100644 index 0000000..d63caa7 --- /dev/null +++ b/tests/otbs/incomplete_alias.d.ref @@ -0,0 +1 @@ +alias u8 = ubyte \ No newline at end of file