blob: 39f68c6cc72ff954ba5b3f62381232ec52392da7 [file] [log] [blame]
Bram Moolenaar9b03d3e2022-08-30 20:26:34 +01001vim9script
2
3# Vim indent file
4# Language: gdscript (Godot game engine)
5# Maintainer: Maxim Kim <habamax@gmail.com>
6# Based on python indent file.
7
8if exists("b:did_indent")
9 finish
10endif
11b:did_indent = 1
12
13var undo_opts = "setl indentexpr< indentkeys< lisp< autoindent<"
14
15if exists('b:undo_indent')
16 b:undo_indent ..= "|" .. undo_opts
17else
18 b:undo_indent = undo_opts
19endif
20
21setlocal nolisp
22setlocal autoindent
23setlocal indentexpr=GDScriptIndent()
24setlocal indentkeys+=<:>,=elif,=except
25
26
27def GDScriptIndent(): number
28 # If this line is explicitly joined: If the previous line was also joined,
29 # line it up with that one, otherwise add two 'shiftwidth'
30 if getline(v:lnum - 1) =~ '\\$'
31 if v:lnum > 1 && getline(v:lnum - 2) =~ '\\$'
32 return indent(v:lnum - 1)
33 endif
34 return indent(v:lnum - 1) + (shiftwidth() * 2)
35 endif
36
37 # If the start of the line is in a string don't change the indent.
38 if has('syntax_items') && synIDattr(synID(v:lnum, 1, 1), "name") =~ "String$"
39 return -1
40 endif
41
42 # Search backwards for the previous non-empty line.
43 var plnum = prevnonblank(v:lnum - 1)
44
45 if plnum == 0
46 # This is the first non-empty line, use zero indent.
47 return 0
48 endif
49
50 var plindent = indent(plnum)
51 var plnumstart = plnum
52
53 # Get the line and remove a trailing comment.
54 # Use syntax highlighting attributes when possible.
55 var pline = getline(plnum)
56 var pline_len = strlen(pline)
57 if has('syntax_items')
58 # If the last character in the line is a comment, do a binary search for
59 # the start of the comment. synID() is slow, a linear search would take
60 # too long on a long line.
61 if synIDattr(synID(plnum, pline_len, 1), "name") =~ "\\(Comment\\|Todo\\)$"
62 var min = 1
63 var max = pline_len
64 while min < max
65 var col = (min + max) / 2
66 if synIDattr(synID(plnum, col, 1), "name") =~ "\\(Comment\\|Todo\\)$"
67 max = col
68 else
69 min = col + 1
70 endif
71 endwhile
72 pline = strpart(pline, 0, min - 1)
73 endif
74 else
75 var col = 0
76 while col < pline_len
77 if pline[col] == '#'
78 pline = strpart(pline, 0, col)
79 break
80 endif
81 col = col + 1
82 endwhile
83 endif
84
85
86 # When "inside" parenthesis: If at the first line below the parenthesis add
87 # one 'shiftwidth' ("inside" is simplified and not really checked)
88 # my_var = (
89 # a
90 # + b
91 # + c
92 # )
93 if pline =~ '[({\[]\s*$'
94 return indent(plnum) + shiftwidth()
95 endif
96
97
98 # If the previous line ended with a colon, indent this line
99 if pline =~ ':\s*$'
100 return plindent + shiftwidth()
101 endif
102
103 # If the previous line was a stop-execution statement...
104 if getline(plnum) =~ '^\s*\(break\|continue\|raise\|return\|pass\)\>'
105 # See if the user has already dedented
106 if indent(v:lnum) > indent(plnum) - shiftwidth()
107 # If not, recommend one dedent
108 return indent(plnum) - shiftwidth()
109 endif
110 # Otherwise, trust the user
111 return -1
112 endif
113
114 # If the current line begins with a keyword that lines up with "try"
115 if getline(v:lnum) =~ '^\s*\(except\|finally\)\>'
116 var lnum = v:lnum - 1
117 while lnum >= 1
118 if getline(lnum) =~ '^\s*\(try\|except\)\>'
119 var ind = indent(lnum)
120 if ind >= indent(v:lnum)
121 return -1 # indent is already less than this
122 endif
123 return ind # line up with previous try or except
124 endif
125 lnum = lnum - 1
126 endwhile
127 return -1 # no matching "try"!
128 endif
129
130
131 # If the current line begins with a header keyword, dedent
132 if getline(v:lnum) =~ '^\s*\(elif\|else\)\>'
133
134 # Unless the previous line was a one-liner
135 if getline(plnumstart) =~ '^\s*\(for\|if\|try\)\>'
136 return plindent
137 endif
138
139 # Or the user has already dedented
140 if indent(v:lnum) <= plindent - shiftwidth()
141 return -1
142 endif
143
144 return plindent - shiftwidth()
145 endif
146
147 return -1
148enddef