From 23bad79c82c3d70d4340963b3c703a3dbdbddce2 Mon Sep 17 00:00:00 2001 From: xonix Date: Sun, 17 Dec 2023 17:06:26 +0200 Subject: [PATCH] Auto-format commas #187 --- .../java/intellij_awk/AwkFormattingModelBuilder.java | 10 ++++++---- src/test/java/intellij_awk/AwkAutoFormatTests.java | 4 ++++ src/test/testData/auto_format/commaInArrIndex.awk | 3 +++ src/test/testData/auto_format/commaInArrIndexAfter.awk | 3 +++ src/test/testData/auto_format/commaInFuncCall.awk | 3 +++ src/test/testData/auto_format/commaInFuncCallAfter.awk | 3 +++ .../testData/auto_format/commaInFuncParamsNoChange.awk | 2 ++ .../auto_format/commaInFuncParamsNoChangeAfter.awk | 2 ++ src/test/testData/auto_format/commaInIfIn.awk | 3 +++ src/test/testData/auto_format/commaInIfInAfter.awk | 3 +++ .../testData/inspection/enforceGlobalVarNaming3.awk | 2 +- .../inspection/enforceGlobalVarNaming3After.awk | 2 +- 12 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/test/testData/auto_format/commaInArrIndex.awk create mode 100644 src/test/testData/auto_format/commaInArrIndexAfter.awk create mode 100644 src/test/testData/auto_format/commaInFuncCall.awk create mode 100644 src/test/testData/auto_format/commaInFuncCallAfter.awk create mode 100644 src/test/testData/auto_format/commaInFuncParamsNoChange.awk create mode 100644 src/test/testData/auto_format/commaInFuncParamsNoChangeAfter.awk create mode 100644 src/test/testData/auto_format/commaInIfIn.awk create mode 100644 src/test/testData/auto_format/commaInIfInAfter.awk diff --git a/src/main/java/intellij_awk/AwkFormattingModelBuilder.java b/src/main/java/intellij_awk/AwkFormattingModelBuilder.java index a9ba33c..6559db5 100644 --- a/src/main/java/intellij_awk/AwkFormattingModelBuilder.java +++ b/src/main/java/intellij_awk/AwkFormattingModelBuilder.java @@ -9,6 +9,7 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleSettings; import com.intellij.psi.tree.TokenSet; +import intellij_awk.psi.AwkTypes; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -43,6 +44,7 @@ public class AwkFormattingModelBuilder implements FormattingModelBuilder { private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) { TokenSet ternaryExpr = TokenSet.create(TERNARY_EXPR, TERNARY_PRINT_EXPR); + TokenSet commaParents = TokenSet.create(GAWK_FUNC_CALL_LIST, EXPR_LST); return new SpacingBuilder(settings, AwkLanguage.INSTANCE) .around(binaryOps) .spaces(1) @@ -84,10 +86,10 @@ private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) { .spaces(1) .between(RPAREN, TokenSet.create(STATEMENT, ACTION)) // ) { .spaces(1) - // .before(COMMA) // TODO taking into account function local params - // .none() - // .after(COMMA) - // .spaces(1) + .beforeInside(COMMA, commaParents) + .none() + .afterInside(COMMA, commaParents) + .spaces(1) .after(TokenSet.create(PRINT, PRINTF)) .spaces(1) .between(SIMPLE_PRINT_STATEMENT, OUTPUT_REDIRECTION) diff --git a/src/test/java/intellij_awk/AwkAutoFormatTests.java b/src/test/java/intellij_awk/AwkAutoFormatTests.java index b7787ba..5e7c123 100644 --- a/src/test/java/intellij_awk/AwkAutoFormatTests.java +++ b/src/test/java/intellij_awk/AwkAutoFormatTests.java @@ -29,6 +29,10 @@ public class AwkAutoFormatTests extends BasePlatformTestCase { public void testFile13_2(){ checkByFile(); } public void testGawk_switch1(){ checkByFile(); } public void testGawk_switch2(){ checkByFile(); } + public void testCommaInArrIndex(){ checkByFile(); } + public void testCommaInFuncCall(){ checkByFile(); } + public void testCommaInIfIn(){ checkByFile(); } + public void testCommaInFuncParamsNoChange(){ checkByFile(); } public void testIssue100(){ checkByFile(); } @Override diff --git a/src/test/testData/auto_format/commaInArrIndex.awk b/src/test/testData/auto_format/commaInArrIndex.awk new file mode 100644 index 0000000..7a0772a --- /dev/null +++ b/src/test/testData/auto_format/commaInArrIndex.awk @@ -0,0 +1,3 @@ +{ + A[a ,b, c] +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInArrIndexAfter.awk b/src/test/testData/auto_format/commaInArrIndexAfter.awk new file mode 100644 index 0000000..58f4f8b --- /dev/null +++ b/src/test/testData/auto_format/commaInArrIndexAfter.awk @@ -0,0 +1,3 @@ +{ + A[a, b, c] +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInFuncCall.awk b/src/test/testData/auto_format/commaInFuncCall.awk new file mode 100644 index 0000000..fa7b158 --- /dev/null +++ b/src/test/testData/auto_format/commaInFuncCall.awk @@ -0,0 +1,3 @@ +{ + ff(a ,b, c) +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInFuncCallAfter.awk b/src/test/testData/auto_format/commaInFuncCallAfter.awk new file mode 100644 index 0000000..882bf09 --- /dev/null +++ b/src/test/testData/auto_format/commaInFuncCallAfter.awk @@ -0,0 +1,3 @@ +{ + ff(a, b, c) +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInFuncParamsNoChange.awk b/src/test/testData/auto_format/commaInFuncParamsNoChange.awk new file mode 100644 index 0000000..88cc08f --- /dev/null +++ b/src/test/testData/auto_format/commaInFuncParamsNoChange.awk @@ -0,0 +1,2 @@ +function f(a ,b, c) { +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInFuncParamsNoChangeAfter.awk b/src/test/testData/auto_format/commaInFuncParamsNoChangeAfter.awk new file mode 100644 index 0000000..88cc08f --- /dev/null +++ b/src/test/testData/auto_format/commaInFuncParamsNoChangeAfter.awk @@ -0,0 +1,2 @@ +function f(a ,b, c) { +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInIfIn.awk b/src/test/testData/auto_format/commaInIfIn.awk new file mode 100644 index 0000000..e22ff12 --- /dev/null +++ b/src/test/testData/auto_format/commaInIfIn.awk @@ -0,0 +1,3 @@ +{ + if ((a ,b, c) in A) {} +} \ No newline at end of file diff --git a/src/test/testData/auto_format/commaInIfInAfter.awk b/src/test/testData/auto_format/commaInIfInAfter.awk new file mode 100644 index 0000000..4ce7764 --- /dev/null +++ b/src/test/testData/auto_format/commaInIfInAfter.awk @@ -0,0 +1,3 @@ +{ + if ((a, b, c) in A) {} +} \ No newline at end of file diff --git a/src/test/testData/inspection/enforceGlobalVarNaming3.awk b/src/test/testData/inspection/enforceGlobalVarNaming3.awk index 0b38281..dc3a99d 100644 --- a/src/test/testData/inspection/enforceGlobalVarNaming3.awk +++ b/src/test/testData/inspection/enforceGlobalVarNaming3.awk @@ -4,5 +4,5 @@ BEGIN { arr["key3"] = "val3" } function f() { - split("",arr) + split("", arr) } \ No newline at end of file diff --git a/src/test/testData/inspection/enforceGlobalVarNaming3After.awk b/src/test/testData/inspection/enforceGlobalVarNaming3After.awk index 7ab5994..31636a6 100644 --- a/src/test/testData/inspection/enforceGlobalVarNaming3After.awk +++ b/src/test/testData/inspection/enforceGlobalVarNaming3After.awk @@ -4,5 +4,5 @@ BEGIN { Arr["key3"] = "val3" } function f() { - split("",Arr) + split("", Arr) } \ No newline at end of file