Skip to content

Commit c880f52

Browse files
authored
Merge pull request #1 from piechologist/main
Add indent/dedent patterns
2 parents 38ecdfe + cc60f21 commit c880f52

2 files changed

Lines changed: 86 additions & 26 deletions

File tree

languages/fish/config.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ brackets = [
1616
"string",
1717
] },
1818
]
19+
20+
# Indent if the line starts with a keyword that must be followed by one or more parameters *or*
21+
# if the line starts and ends with `begin` or `else` *or*
22+
# if the line ends with an opening parentheses or a backslash.
23+
increase_indent_pattern = "^\\s*(function|if|else\\s+if|while|for|switch|case)\\b|^\\s*(begin|else)\\s*$|[(\\\\]\\s*$"
24+
25+
# Dedent if the line starts with a keyword that ends a block or with a closing parentheses.
26+
decrease_indent_pattern = "^\\s*(else|case|end)\\b|^\\s*\\)"

languages/fish/highlights.scm

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
[(integer) (float)] @number
77

88
[
9+
"not"
10+
"!"
11+
"and"
12+
"or"
913
"&&"
1014
"||"
1115
"|"
1216
"&"
13-
".."
1417
(direction)
1518
(stream_redirect)
1619
] @operator
@@ -25,38 +28,87 @@
2528
name: (word) @punctuation.bracket (#match? @punctuation.bracket "^\\[$")
2629
argument: (word) @operator (#match? @operator "^(!?=|-[a-zA-Z]+)$"))
2730

28-
(variable_expansion) @constant
31+
[(variable_expansion) (list_element_access)] @constant
2932

30-
[
31-
"["
32-
"]"
33-
"{"
34-
"}"
35-
"("
36-
")"
37-
] @punctuation.bracket
33+
(command_substitution ["$" "(" ")"]) @punctuation.bracket
34+
35+
; Brace expansion and globbing.
36+
; Note: (glob) matches "*" but not "?". It's in the grammar and we can't
37+
; query it differently.
38+
["{" "}" "," (home_dir_expansion) (glob)] @operator
3839

39-
"," @punctuation.delimiter
40+
; Conditionals
41+
(if_statement ["if" "end"] @keyword)
42+
(switch_statement ["switch" "end"] @keyword)
43+
(case_clause ["case"] @keyword)
44+
(else_clause ["else"] @keyword)
45+
(else_if_clause ["else" "if"] @keyword)
4046

41-
(function_definition name: [(word) (concatenation)] @function)
42-
(command name: (word) @function)
47+
; Loops/Blocks
48+
(while_statement ["while" "end"] @keyword)
49+
(for_statement ["for" "end"] @keyword)
50+
(begin_statement ["begin" "end"] @keyword)
4351

52+
; Functions
53+
(function_definition ["function" "end"] @keyword)
54+
55+
; Keywords
4456
[
45-
"switch"
46-
"case"
4757
"in"
48-
"begin"
49-
"function"
50-
"if"
51-
"else"
52-
"end"
53-
"while"
54-
"for"
55-
"not"
56-
"!"
57-
"and"
58-
"or"
58+
";"
5959
"return"
6060
(break)
6161
(continue)
6262
] @keyword
63+
64+
; Treat "&" as a background operator only if it's preceded by a command.
65+
; Note that an expression like `echo _&_` contains a background operator
66+
; prior to fish 3.5 and that's how it's handled here.
67+
((command) "&" @keyword)
68+
69+
70+
;; Work around two issues in the compiled grammar.
71+
72+
; (1)
73+
; Issue: the leading file descriptor in a redirection is missing.
74+
; Expample: redirect stderr with 2> and the "2" won't get matched.
75+
; Workaround: within a command, locate the last integer node preceding the
76+
; redirect and mark that as @operator.
77+
(command
78+
name: (concatenation _+ (integer) @operator .)
79+
redirect: _)
80+
81+
; (2)
82+
; Issue: commands are not split into the fields (name) (arguments) (comment).
83+
;
84+
; The whole expression is wrapped in the field (name) which in turn consists
85+
; either of one (word) or a (concatenation).
86+
;
87+
; The grammar doesn't mark trailing comments and we have to look for "#" and
88+
; assign all following nodes ourselves. Parentheses within trailing commands
89+
; are problematic as they start a command substitution and can't be matched
90+
; by the wildcard node. Sigh.
91+
;
92+
; Workaround: use four rules to do the splitting:
93+
; 1. capture commands with no arguments (word)
94+
; 2. capture commands with arguments (concatenation)
95+
; 3. capture command options (arguments starting with "-")
96+
; 4. capture trailing comments (can't catch everything)
97+
98+
(command name: [
99+
((word) @function)
100+
(concatenation . (word) @function)
101+
(concatenation (word) @constant (#match? @constant "^-"))
102+
(concatenation ("#" @comment _* @comment))
103+
])
104+
105+
(function_definition name: [
106+
((word) @function)
107+
(concatenation . (word) @function)
108+
(concatenation (word) @constant (#match? @constant "^-"))
109+
(concatenation ("#" @comment _* @comment))
110+
])
111+
112+
113+
;; Error
114+
(ERROR) @hint

0 commit comments

Comments
 (0)