src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/edit/highlight/regions_test.go (about)

     1  package highlight
     2  
     3  import (
     4  	"testing"
     5  
     6  	"src.elv.sh/pkg/parse"
     7  	"src.elv.sh/pkg/tt"
     8  )
     9  
    10  var Args = tt.Args
    11  
    12  func TestGetRegions(t *testing.T) {
    13  	lsCommand := region{0, 2, semanticRegion, commandRegion}
    14  
    15  	tt.Test(t, getRegionsFromString,
    16  		Args("").Rets([]region(nil)),
    17  		Args("ls").Rets([]region{
    18  			lsCommand,
    19  		}),
    20  
    21  		// Lexical regions.
    22  
    23  		Args("ls a").Rets([]region{
    24  			lsCommand,
    25  			{3, 4, lexicalRegion, barewordRegion}, // a
    26  		}),
    27  		Args("ls 'a'").Rets([]region{
    28  			lsCommand,
    29  			{3, 6, lexicalRegion, singleQuotedRegion}, // 'a'
    30  		}),
    31  		Args(`ls "a"`).Rets([]region{
    32  			lsCommand,
    33  			{3, 6, lexicalRegion, doubleQuotedRegion}, // 'a'
    34  		}),
    35  		Args("ls $x").Rets([]region{
    36  			lsCommand,
    37  			{3, 5, lexicalRegion, variableRegion}, // $x
    38  		}),
    39  		Args("ls x*y").Rets([]region{
    40  			lsCommand,
    41  			{3, 4, lexicalRegion, barewordRegion}, // x
    42  			{4, 5, lexicalRegion, wildcardRegion}, // *
    43  			{5, 6, lexicalRegion, barewordRegion}, // y
    44  		}),
    45  		Args("ls ~user/x").Rets([]region{
    46  			lsCommand,
    47  			{3, 4, lexicalRegion, tildeRegion},     // ~
    48  			{4, 10, lexicalRegion, barewordRegion}, // user/x
    49  		}),
    50  		Args("ls # comment").Rets([]region{
    51  			lsCommand,
    52  			{2, 12, lexicalRegion, commentRegion}, // # comment
    53  		}),
    54  
    55  		// LHS of temporary assignments.
    56  		Args("x=foo ls").Rets([]region{
    57  			{0, 1, semanticRegion, variableRegion}, // x
    58  			{1, 2, lexicalRegion, "="},
    59  			{2, 5, lexicalRegion, barewordRegion}, // foo
    60  			{6, 8, semanticRegion, commandRegion}, // ls
    61  		}),
    62  
    63  		// The "var" special command
    64  		Args("var x = foo").Rets([]region{
    65  			{0, 3, semanticRegion, commandRegion},  // var
    66  			{4, 5, semanticRegion, variableRegion}, // x
    67  			{6, 7, semanticRegion, keywordRegion},  // =
    68  			{8, 11, lexicalRegion, barewordRegion}, // foo
    69  		}),
    70  
    71  		// The "set" special command
    72  		Args("set x = foo").Rets([]region{
    73  			{0, 3, semanticRegion, commandRegion},  // var
    74  			{4, 5, semanticRegion, variableRegion}, // x
    75  			{6, 7, semanticRegion, keywordRegion},  // =
    76  			{8, 11, lexicalRegion, barewordRegion}, // foo
    77  		}),
    78  
    79  		// The "tmp" special command
    80  		Args("tmp x = foo").Rets([]region{
    81  			{0, 3, semanticRegion, commandRegion},  // tmp
    82  			{4, 5, semanticRegion, variableRegion}, // x
    83  			{6, 7, semanticRegion, keywordRegion},  // =
    84  			{8, 11, lexicalRegion, barewordRegion}, // foo
    85  		}),
    86  
    87  		// The "del" special command
    88  		Args("del x y").Rets([]region{
    89  			{0, 3, semanticRegion, commandRegion},  // tmp
    90  			{4, 5, semanticRegion, variableRegion}, // x
    91  			{6, 7, semanticRegion, variableRegion}, // y
    92  		}),
    93  
    94  		// The "if" special command.
    95  
    96  		Args("if x { }").Rets([]region{
    97  			{0, 2, semanticRegion, commandRegion}, // if
    98  			{3, 4, lexicalRegion, barewordRegion}, // x
    99  			{5, 6, lexicalRegion, "{"},
   100  			{7, 8, lexicalRegion, "}"},
   101  		}),
   102  		Args("if x { } else { }").Rets([]region{
   103  			{0, 2, semanticRegion, commandRegion}, // if
   104  			{3, 4, lexicalRegion, barewordRegion}, // x
   105  			{5, 6, lexicalRegion, "{"},
   106  			{7, 8, lexicalRegion, "}"},
   107  			{9, 13, semanticRegion, keywordRegion}, // else
   108  			{14, 15, lexicalRegion, "{"},
   109  			{16, 17, lexicalRegion, "}"},
   110  		}),
   111  		Args("if x { } elif y { }").Rets([]region{
   112  			{0, 2, semanticRegion, commandRegion}, // if
   113  			{3, 4, lexicalRegion, barewordRegion}, // x
   114  			{5, 6, lexicalRegion, "{"},
   115  			{7, 8, lexicalRegion, "}"},
   116  			{9, 13, semanticRegion, keywordRegion},  // elif
   117  			{14, 15, lexicalRegion, barewordRegion}, // y
   118  			{16, 17, lexicalRegion, "{"},
   119  			{18, 19, lexicalRegion, "}"},
   120  		}),
   121  
   122  		// The "for" special command.
   123  
   124  		Args("for x [] { }").Rets([]region{
   125  			{0, 3, semanticRegion, commandRegion},  // for
   126  			{4, 5, semanticRegion, variableRegion}, // x
   127  			{6, 7, lexicalRegion, "["},
   128  			{7, 8, lexicalRegion, "]"},
   129  			{9, 10, lexicalRegion, "{"},
   130  			{11, 12, lexicalRegion, "}"},
   131  		}),
   132  		Args("for x [] { } else { }").Rets([]region{
   133  			{0, 3, semanticRegion, commandRegion},  // for
   134  			{4, 5, semanticRegion, variableRegion}, // x
   135  			{6, 7, lexicalRegion, "["},
   136  			{7, 8, lexicalRegion, "]"},
   137  			{9, 10, lexicalRegion, "{"},
   138  			{11, 12, lexicalRegion, "}"},
   139  			{13, 17, semanticRegion, keywordRegion}, // else
   140  			{18, 19, lexicalRegion, "{"},
   141  			{20, 21, lexicalRegion, "}"},
   142  		}),
   143  
   144  		// The "try" special command.
   145  
   146  		Args("try { } except e { }").Rets([]region{
   147  			{0, 3, semanticRegion, commandRegion}, // try
   148  			{4, 5, lexicalRegion, "{"},
   149  			{6, 7, lexicalRegion, "}"},
   150  			{8, 14, semanticRegion, keywordRegion},   // except
   151  			{15, 16, semanticRegion, variableRegion}, // e
   152  			{17, 18, lexicalRegion, "{"},
   153  			{19, 20, lexicalRegion, "}"},
   154  		}),
   155  
   156  		Args("try { } except e { } else { }").Rets([]region{
   157  			{0, 3, semanticRegion, commandRegion}, // try
   158  			{4, 5, lexicalRegion, "{"},
   159  			{6, 7, lexicalRegion, "}"},
   160  			{8, 14, semanticRegion, keywordRegion},   // except
   161  			{15, 16, semanticRegion, variableRegion}, // e
   162  			{17, 18, lexicalRegion, "{"},
   163  			{19, 20, lexicalRegion, "}"},
   164  			{21, 25, semanticRegion, keywordRegion}, // else
   165  			{26, 27, lexicalRegion, "{"},
   166  			{28, 29, lexicalRegion, "}"},
   167  		}),
   168  
   169  		// Regression test for b.elv.sh/1358.
   170  		Args("try { } except { }").Rets([]region{
   171  			{0, 3, semanticRegion, commandRegion}, // try
   172  			{4, 5, lexicalRegion, "{"},
   173  			{6, 7, lexicalRegion, "}"},
   174  			{8, 14, semanticRegion, keywordRegion}, // except
   175  			{15, 16, lexicalRegion, "{"},
   176  			{17, 18, lexicalRegion, "}"},
   177  		}),
   178  
   179  		Args("try { } catch e { }").Rets([]region{
   180  			{0, 3, semanticRegion, commandRegion}, // try
   181  			{4, 5, lexicalRegion, "{"},
   182  			{6, 7, lexicalRegion, "}"},
   183  			{8, 13, semanticRegion, keywordRegion},   // catch
   184  			{14, 15, semanticRegion, variableRegion}, // e
   185  			{16, 17, lexicalRegion, "{"},
   186  			{18, 19, lexicalRegion, "}"},
   187  		}),
   188  
   189  		Args("try { } catch e { } else { }").Rets([]region{
   190  			{0, 3, semanticRegion, commandRegion}, // try
   191  			{4, 5, lexicalRegion, "{"},
   192  			{6, 7, lexicalRegion, "}"},
   193  			{8, 13, semanticRegion, keywordRegion},   // catch
   194  			{14, 15, semanticRegion, variableRegion}, // e
   195  			{16, 17, lexicalRegion, "{"},
   196  			{18, 19, lexicalRegion, "}"},
   197  			{20, 24, semanticRegion, keywordRegion}, // else
   198  			{25, 26, lexicalRegion, "{"},
   199  			{27, 28, lexicalRegion, "}"},
   200  		}),
   201  
   202  		// Regression test for b.elv.sh/1358.
   203  		Args("try { } catch { }").Rets([]region{
   204  			{0, 3, semanticRegion, commandRegion}, // try
   205  			{4, 5, lexicalRegion, "{"},
   206  			{6, 7, lexicalRegion, "}"},
   207  			{8, 13, semanticRegion, keywordRegion}, // catch
   208  			{14, 15, lexicalRegion, "{"},
   209  			{16, 17, lexicalRegion, "}"},
   210  		}),
   211  
   212  		Args("try { } finally { }").Rets([]region{
   213  			{0, 3, semanticRegion, commandRegion}, // try
   214  			{4, 5, lexicalRegion, "{"},
   215  			{6, 7, lexicalRegion, "}"},
   216  			{8, 15, semanticRegion, keywordRegion}, // finally
   217  			{16, 17, lexicalRegion, "{"},
   218  			{18, 19, lexicalRegion, "}"},
   219  		}),
   220  	)
   221  }
   222  
   223  func getRegionsFromString(code string) []region {
   224  	// Ignore error.
   225  	tree, _ := parse.Parse(parse.SourceForTest(code), parse.Config{})
   226  	return getRegions(tree.Root)
   227  }