Skip to content

Commit

Permalink
Merge pull request #190 from xonixx/more_auto_formatting
Browse files Browse the repository at this point in the history
More auto formatting
  • Loading branch information
xonixx committed May 10, 2023
2 parents c94de18 + 129f67f commit d9c81e3
Show file tree
Hide file tree
Showing 22 changed files with 201 additions and 38 deletions.
17 changes: 10 additions & 7 deletions src/main/java/intellij_awk/Awk.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ private statement_while ::= WHILE LPAREN expr RPAREN newlines_opt statement

private statement_for ::=
FOR LPAREN (
simple_statement_opt SEMICOLON newlines_opt expr_opt SEMICOLON newlines_opt simple_statement_opt RPAREN newlines_opt
statement_for_conditions RPAREN newlines_opt
| var_name IN gawk_var_name RPAREN newlines_opt
) statement
{ pin=2 }
{ pin=2 } // TODO make 1!!!!!

statement_for_conditions ::= simple_statement_opt SEMICOLON newlines_opt expr_opt SEMICOLON newlines_opt simple_statement_opt

private terminator ::= (NEWLINE|SEMICOLON) newlines_opt

Expand All @@ -258,7 +260,8 @@ private recover_switch ::= !RBRACE

case_statement ::= (CASE case_value | DEFAULT) COLON newlines_opt (statement terminator*)*

private case_value ::= ([ADD|SUB] NUMBER) | STRING | ERE | TYPED_ERE
unary_add_sub ::= ADD|SUB
private case_value ::= ([unary_add_sub] NUMBER) | STRING | ERE | TYPED_ERE

private simple_statement_opt ::= simple_statement?

Expand Down Expand Up @@ -296,7 +299,7 @@ expr ::= unary_expr | non_unary_expr
A --> β A'
A' --> ε | α A' <--> A' --> (α A')?
*/
unary_expr ::= (ADD|SUB) expr unary_expr1
unary_expr ::= unary_add_sub expr unary_expr1

private unary_expr1 ::= [
( POW expr
Expand Down Expand Up @@ -324,8 +327,8 @@ private unary_expr1 ::= [
]

// newlines in ternary are Gawk-only feature
private ternary_expr ::= QUESTION newlines_opt expr COLON newlines_opt expr
private ternary_print_expr ::= QUESTION newlines_opt print_expr COLON newlines_opt print_expr
ternary_expr ::= QUESTION newlines_opt expr COLON newlines_opt expr
ternary_print_expr ::= QUESTION newlines_opt print_expr COLON newlines_opt print_expr

non_unary_expr ::= (
LPAREN ( expr RPAREN
Expand Down Expand Up @@ -408,7 +411,7 @@ print_expr ::= unary_print_expr | non_unary_print_expr

private gawk_print_expr ::= TYPED_ERE | print_expr

private unary_print_expr ::= (ADD|SUB) print_expr unary_print_expr1
private unary_print_expr ::= unary_add_sub print_expr unary_print_expr1

private unary_print_expr1 ::= [
(
Expand Down
83 changes: 82 additions & 1 deletion src/main/java/intellij_awk/AwkFormattingModelBuilder.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,99 @@
package intellij_awk;

import static intellij_awk.psi.AwkTypes.*;

import com.intellij.formatting.*;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class AwkFormattingModelBuilder implements FormattingModelBuilder {
public static final TokenSet binaryOps =
TokenSet.create(
MUL,
MUL_ASSIGN,
DIV,
DIV_ASSIGN,
POW,
POW_ASSIGN,
ADD,
ADD_ASSIGN,
SUB,
SUB_ASSIGN,
MOD,
MOD_ASSIGN,
AND,
OR,
MATCH,
NO_MATCH,
GT,
LT,
GE,
LE,
EQ,
NE,
QUESTION,
ASSIGN,
PIPE);

private static SpacingBuilder createSpaceBuilder(CodeStyleSettings settings) {
return new SpacingBuilder(settings, AwkLanguage.INSTANCE);
TokenSet ternaryExpr = TokenSet.create(TERNARY_EXPR, TERNARY_PRINT_EXPR);
return new SpacingBuilder(settings, AwkLanguage.INSTANCE)
.around(binaryOps)
.spaces(1)
.aroundInside(COLON, ternaryExpr)
.spaces(1)
.before(ternaryExpr)
.spaces(1)
.around(TokenSet.create(INCR, DECR))
.none()
.after(UNARY_ADD_SUB)
.none()
.afterInside(SEMICOLON, STATEMENT_FOR_CONDITIONS)
.spaces(1)
.beforeInside(SEMICOLON, STATEMENT_FOR_CONDITIONS)
.none()
.after(TokenSet.create(DOLLAR, AT))
.none()
.after(TokenSet.create(LOAD, NAMESPACE, INCLUDE))
.spaces(1)
.after(LBRACKET)
.none()
.afterInside(
LPAREN,
TokenSet.create(FUNCTION_CALL_USER, FUNCTION_CALL_BUILT_IN, STATEMENT, NON_UNARY_EXPR))
.none()
.before(TokenSet.create(RPAREN, RBRACKET, LBRACKET))
.none()
.after(NOT)
.none()
.between(TokenSet.create(FOR, IF, WHILE), LPAREN)
.spaces(1)
.between(DO, STATEMENT) // do {
.spaces(1)
.between(STATEMENT, WHILE) // } while
.spaces(1)
.between(STATEMENT, ELSE) // } else
.spaces(1)
.between(ELSE, STATEMENT) // else if
.spaces(1)
.between(RPAREN, TokenSet.create(STATEMENT, ACTION)) // ) {
.spaces(1)
// .before(COMMA) // TODO taking into account function local params
// .none()
// .after(COMMA)
// .spaces(1)
.after(TokenSet.create(PRINT, PRINTF))
.spaces(1)
.between(SIMPLE_PRINT_STATEMENT, OUTPUT_REDIRECTION)
.spaces(1)
.after(TokenSet.create(APPEND, PIPE_AMP))
.spaces(1);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/test/java/intellij_awk/AwkAutoFormatTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AwkAutoFormatTests extends BasePlatformTestCase {
public void testFile13_2(){ checkByFile(); }
public void testGawk_switch1(){ checkByFile(); }
public void testGawk_switch2(){ checkByFile(); }
public void testIssue100(){ checkByFile(); }

@Override
protected String getTestDataPath() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/testData/auto_format/file1After.awk
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
BEGIN {
if (5>2) {
if (5 > 2) {
print "Hello world"
}
}

function inc(i) {
return i+1
return i + 1
}
2 changes: 1 addition & 1 deletion src/test/testData/auto_format/file2_1After.awk
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
a=1
a = 1
}
4 changes: 2 additions & 2 deletions src/test/testData/auto_format/file4_0After.awk
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
BEGIN {
if (5>2)
if (5 > 2)
return
else if (1)
exit
else if (2)

a=1
a = 1
else
print
}
2 changes: 1 addition & 1 deletion src/test/testData/auto_format/file5After.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BEGIN {
for(i=0;i<10;i++)
for (i = 0; i < 10; i++)
print i
}
4 changes: 2 additions & 2 deletions src/test/testData/auto_format/file8After.awk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BEGIN {
while (1)
for(i=1; i<10; i++)
if (i%2==0)
for (i = 1; i < 10; i++)
if (i % 2 == 0)
print "even"
else
print "odd"
Expand Down
2 changes: 1 addition & 1 deletion src/test/testData/auto_format/file9After.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function a(\
\
a, b, \
c){}
c) {}
39 changes: 39 additions & 0 deletions src/test/testData/auto_format/issue100.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@ namespace "x"
@ load "x"
@ include "x"
BEGIN {
for( i=0;i<7;i ++ )print -- i
if( 1 ){}else if( 2 ){}else{}
while( 1 ){}
do{}while( 1 )
print - 7
f(- 7)
f( a+b*c/d%f^j-h )
f( a [ b [ 1 ] ] )
a+=1
a-=1
a*=1
a/=1
a%=1
a^=1
v=a||b&&c
v=a>7
v=a<7
v=a>=7
v=a<=7
v=a?b:c
print a?b:c
printf""
v=a~b
v=a!~b
v=( 1 + 2 )
v=$ NF
v=! v
v=a!=b
"cat file"|getline
print data | "subprogram"
print data|&"subprogram"
print "123" > "file"
print"123">>"file"
}
function f(a, i){}
39 changes: 39 additions & 0 deletions src/test/testData/auto_format/issue100After.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@namespace "x"
@load "x"
@include "x"
BEGIN {
for (i = 0; i < 7; i++) print --i
if (1) {} else if (2) {} else {}
while (1) {}
do {} while (1)
print -7
f(-7)
f(a + b * c / d % f ^ j - h)
f(a[b[1]])
a += 1
a -= 1
a *= 1
a /= 1
a %= 1
a ^= 1
v = a || b && c
v = a > 7
v = a < 7
v = a >= 7
v = a <= 7
v = a ? b : c
print a ? b : c
printf ""
v = a ~ b
v = a !~ b
v = (1 + 2)
v = $NF
v = !v
v = a != b
"cat file" | getline
print data | "subprogram"
print data |& "subprogram"
print "123" > "file"
print "123" >> "file"
}
function f(a, i) {}
4 changes: 2 additions & 2 deletions src/test/testData/inspection/unusedFunctionParam1After.awk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function f() {
a( 2)
a( 2222)
a(2)
a(2222)
a( 2222 )
a()
a()
Expand Down
4 changes: 2 additions & 2 deletions src/test/testData/inspection/unusedFunctionParam2_0After.awk
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function f() {
a(1)
a(111)
a( 111 )
a( 111)
a(1111)
a()
}

function a(unused1 ) {
function a(unused1) {
print "hello"
}
4 changes: 2 additions & 2 deletions src/test/testData/inspection/unusedFunctionParam2_1After.awk
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function f() {
a(1)
a(111)
a( 111 )
a( 111)
a(1111)
a()
}

function a(used ) {
function a(used) {
print "hello" used
}
4 changes: 2 additions & 2 deletions src/test/testData/inspection/unusedFunctionParam3_0After.awk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function f() {
a( 2)
a( 2222)
a(2)
a(2222)
a( 2222 )
a()
a()
Expand Down
4 changes: 2 additions & 2 deletions src/test/testData/inspection/unusedFunctionParam3_1After.awk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function f() {
a( 2)
a( 2222)
a(2)
a(2222)
a( 2222 )
a()
a()
Expand Down
4 changes: 2 additions & 2 deletions src/test/testData/inspection/unusedFunctionParam4After.awk
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function f() {
a(1)
a(111)
a( 111 )
a( 111)
a(1111)
a()
}

function a( unused1\
) {
) {
print "hello"
}
2 changes: 1 addition & 1 deletion src/test/testData/rename/indirect1After.awk
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ function f2() {

END {
print toupper(++newName)
@ newName()
@newName()
}
4 changes: 2 additions & 2 deletions src/test/testData/rename/vars1After.awk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN {
newName=1
newName =1
newName++
print newName+1
print newName +1
}
Loading

0 comments on commit d9c81e3

Please sign in to comment.