solidity.vim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. " Vim indent file
  2. " Language: Solidity
  3. " Maintainer: Cothi (jiungdev@gmail.com)
  4. " Original Author: tomlion (https://github.com/tomlion/vim-solidity)
  5. " Last Change: 2022 Sep 27
  6. " 2023 Aug 22 Vim Project (undo_indent)
  7. "
  8. " Acknowledgement: Based off of vim-javascript
  9. "
  10. " 0. Initialization {{{1
  11. " =================
  12. " Only load this indent file when no other was loaded.
  13. if exists("b:did_indent")
  14. finish
  15. endif
  16. let b:did_indent = 1
  17. setlocal nosmartindent
  18. " Now, set up our indentation expression and keys that trigger it.
  19. setlocal indentexpr=GetSolidityIndent()
  20. setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e
  21. let b:undo_indent = "setlocal indentexpr< indentkeys< smartindent<"
  22. " Only define the function once.
  23. if exists("*GetSolidityIndent")
  24. finish
  25. endif
  26. let s:cpo_save = &cpo
  27. set cpo&vim
  28. " 1. Variables {{{1
  29. " ============
  30. let s:js_keywords = '^\s*\(break\|case\|catch\|continue\|debugger\|default\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)'
  31. " Regex of syntax group names that are or delimit string or are comments.
  32. let s:syng_strcom = 'string\|regex\|comment\c'
  33. " Regex of syntax group names that are strings.
  34. let s:syng_string = 'regex\c'
  35. " Regex of syntax group names that are strings or documentation.
  36. let s:syng_multiline = 'comment\c'
  37. " Regex of syntax group names that are line comment.
  38. let s:syng_linecom = 'linecomment\c'
  39. " Expression used to check whether we should skip a match with searchpair().
  40. let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
  41. let s:line_term = '\s*\%(\%(\/\/\).*\)\=$'
  42. " Regex that defines continuation lines, not including (, {, or [.
  43. let s:continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s:line_term
  44. " Regex that defines continuation lines.
  45. " TODO: this needs to deal with if ...: and so on
  46. let s:msl_regex = '\%([\\*+/.:([]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s:line_term
  47. let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
  48. " Regex that defines blocks.
  49. let s:block_regex = '\%([{[]\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
  50. let s:var_stmt = '^\s*var'
  51. let s:comma_first = '^\s*,'
  52. let s:comma_last = ',\s*$'
  53. let s:ternary = '^\s\+[?|:]'
  54. let s:ternary_q = '^\s\+?'
  55. " 2. Auxiliary Functions {{{1
  56. " ======================
  57. " Check if the character at lnum:col is inside a string, comment, or is ascii.
  58. function s:IsInStringOrComment(lnum, col)
  59. return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
  60. endfunction
  61. " Check if the character at lnum:col is inside a string.
  62. function s:IsInString(lnum, col)
  63. return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
  64. endfunction
  65. " Check if the character at lnum:col is inside a multi-line comment.
  66. function s:IsInMultilineComment(lnum, col)
  67. return !s:IsLineComment(a:lnum, a:col) && synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_multiline
  68. endfunction
  69. " Check if the character at lnum:col is a line comment.
  70. function s:IsLineComment(lnum, col)
  71. return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_linecom
  72. endfunction
  73. " Find line above 'lnum' that isn't empty, in a comment, or in a string.
  74. function s:PrevNonBlankNonString(lnum)
  75. let in_block = 0
  76. let lnum = prevnonblank(a:lnum)
  77. while lnum > 0
  78. " Go in and out of blocks comments as necessary.
  79. " If the line isn't empty (with opt. comment) or in a string, end search.
  80. let line = getline(lnum)
  81. if line =~ '/\*'
  82. if in_block
  83. let in_block = 0
  84. else
  85. break
  86. endif
  87. elseif !in_block && line =~ '\*/'
  88. let in_block = 1
  89. elseif !in_block && line !~ '^\s*\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
  90. break
  91. endif
  92. let lnum = prevnonblank(lnum - 1)
  93. endwhile
  94. return lnum
  95. endfunction
  96. " Find line above 'lnum' that started the continuation 'lnum' may be part of.
  97. function s:GetMSL(lnum, in_one_line_scope)
  98. " Start on the line we're at and use its indent.
  99. let msl = a:lnum
  100. let lnum = s:PrevNonBlankNonString(a:lnum - 1)
  101. while lnum > 0
  102. " If we have a continuation line, or we're in a string, use line as MSL.
  103. " Otherwise, terminate search as we have found our MSL already.
  104. let line = getline(lnum)
  105. let col = match(line, s:msl_regex) + 1
  106. if (col > 0 && !s:IsInStringOrComment(lnum, col)) || s:IsInString(lnum, strlen(line))
  107. let msl = lnum
  108. else
  109. " Don't use lines that are part of a one line scope as msl unless the
  110. " flag in_one_line_scope is set to 1
  111. "
  112. if a:in_one_line_scope
  113. break
  114. end
  115. let msl_one_line = s:Match(lnum, s:one_line_scope_regex)
  116. if msl_one_line == 0
  117. break
  118. endif
  119. endif
  120. let lnum = s:PrevNonBlankNonString(lnum - 1)
  121. endwhile
  122. return msl
  123. endfunction
  124. function s:RemoveTrailingComments(content)
  125. let single = '\/\/\(.*\)\s*$'
  126. let multi = '\/\*\(.*\)\*\/\s*$'
  127. return substitute(substitute(a:content, single, '', ''), multi, '', '')
  128. endfunction
  129. " Find if the string is inside var statement (but not the first string)
  130. function s:InMultiVarStatement(lnum)
  131. let lnum = s:PrevNonBlankNonString(a:lnum - 1)
  132. " let type = synIDattr(synID(lnum, indent(lnum) + 1, 0), 'name')
  133. " loop through previous expressions to find a var statement
  134. while lnum > 0
  135. let line = getline(lnum)
  136. " if the line is a js keyword
  137. if (line =~ s:js_keywords)
  138. " check if the line is a var stmt
  139. " if the line has a comma first or comma last then we can assume that we
  140. " are in a multiple var statement
  141. if (line =~ s:var_stmt)
  142. return lnum
  143. endif
  144. " other js keywords, not a var
  145. return 0
  146. endif
  147. let lnum = s:PrevNonBlankNonString(lnum - 1)
  148. endwhile
  149. " beginning of program, not a var
  150. return 0
  151. endfunction
  152. " Find line above with beginning of the var statement or returns 0 if it's not
  153. " this statement
  154. function s:GetVarIndent(lnum)
  155. let lvar = s:InMultiVarStatement(a:lnum)
  156. let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
  157. if lvar
  158. let line = s:RemoveTrailingComments(getline(prev_lnum))
  159. " if the previous line doesn't end in a comma, return to regular indent
  160. if (line !~ s:comma_last)
  161. return indent(prev_lnum) - &sw
  162. else
  163. return indent(lvar) + &sw
  164. endif
  165. endif
  166. return -1
  167. endfunction
  168. " Check if line 'lnum' has more opening brackets than closing ones.
  169. function s:LineHasOpeningBrackets(lnum)
  170. let open_0 = 0
  171. let open_2 = 0
  172. let open_4 = 0
  173. let line = getline(a:lnum)
  174. let pos = match(line, '[][(){}]', 0)
  175. while pos != -1
  176. if !s:IsInStringOrComment(a:lnum, pos + 1)
  177. let idx = stridx('(){}[]', line[pos])
  178. if idx % 2 == 0
  179. let open_{idx} = open_{idx} + 1
  180. else
  181. let open_{idx - 1} = open_{idx - 1} - 1
  182. endif
  183. endif
  184. let pos = match(line, '[][(){}]', pos + 1)
  185. endwhile
  186. return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
  187. endfunction
  188. function s:Match(lnum, regex)
  189. let col = match(getline(a:lnum), a:regex) + 1
  190. return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
  191. endfunction
  192. function s:IndentWithContinuation(lnum, ind, width)
  193. " Set up variables to use and search for MSL to the previous line.
  194. let p_lnum = a:lnum
  195. let lnum = s:GetMSL(a:lnum, 1)
  196. let line = getline(lnum)
  197. " If the previous line wasn't a MSL and is continuation return its indent.
  198. " TODO: the || s:IsInString() thing worries me a bit.
  199. if p_lnum != lnum
  200. if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line))
  201. return a:ind
  202. endif
  203. endif
  204. " Set up more variables now that we know we aren't continuation bound.
  205. let msl_ind = indent(lnum)
  206. " If the previous line ended with [*+/.-=], start a continuation that
  207. " indents an extra level.
  208. if s:Match(lnum, s:continuation_regex)
  209. if lnum == p_lnum
  210. return msl_ind + a:width
  211. else
  212. return msl_ind
  213. endif
  214. endif
  215. return a:ind
  216. endfunction
  217. function s:InOneLineScope(lnum)
  218. let msl = s:GetMSL(a:lnum, 1)
  219. if msl > 0 && s:Match(msl, s:one_line_scope_regex)
  220. return msl
  221. endif
  222. return 0
  223. endfunction
  224. function s:ExitingOneLineScope(lnum)
  225. let msl = s:GetMSL(a:lnum, 1)
  226. if msl > 0
  227. " if the current line is in a one line scope ..
  228. if s:Match(msl, s:one_line_scope_regex)
  229. return 0
  230. else
  231. let prev_msl = s:GetMSL(msl - 1, 1)
  232. if s:Match(prev_msl, s:one_line_scope_regex)
  233. return prev_msl
  234. endif
  235. endif
  236. endif
  237. return 0
  238. endfunction
  239. " 3. GetSolidityIndent Function {{{1
  240. " =========================
  241. function GetSolidityIndent()
  242. " 3.1. Setup {{{2
  243. " ----------
  244. " Set up variables for restoring position in file. Could use v:lnum here.
  245. let vcol = col('.')
  246. " 3.2. Work on the current line {{{2
  247. " -----------------------------
  248. let ind = -1
  249. " Get the current line.
  250. let line = getline(v:lnum)
  251. " previous nonblank line number
  252. let prevline = prevnonblank(v:lnum - 1)
  253. " If we got a closing bracket on an empty line, find its match and indent
  254. " according to it. For parentheses we indent to its column - 1, for the
  255. " others we indent to the containing line's MSL's level. Return -1 if fail.
  256. let col = matchend(line, '^\s*[],})]')
  257. if col > 0 && !s:IsInStringOrComment(v:lnum, col)
  258. call cursor(v:lnum, col)
  259. let lvar = s:InMultiVarStatement(v:lnum)
  260. if lvar
  261. let prevline_contents = s:RemoveTrailingComments(getline(prevline))
  262. " check for comma first
  263. if (line[col - 1] =~ ',')
  264. " if the previous line ends in comma or semicolon don't indent
  265. if (prevline_contents =~ '[;,]\s*$')
  266. return indent(s:GetMSL(line('.'), 0))
  267. " get previous line indent, if it's comma first return prevline indent
  268. elseif (prevline_contents =~ s:comma_first)
  269. return indent(prevline)
  270. " otherwise we indent 1 level
  271. else
  272. return indent(lvar) + &sw
  273. endif
  274. endif
  275. endif
  276. let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
  277. if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
  278. if line[col-1]==')' && col('.') != col('$') - 1
  279. let ind = virtcol('.')-1
  280. else
  281. let ind = indent(s:GetMSL(line('.'), 0))
  282. endif
  283. endif
  284. return ind
  285. endif
  286. " If the line is comma first, dedent 1 level
  287. if (getline(prevline) =~ s:comma_first)
  288. return indent(prevline) - &sw
  289. endif
  290. if (line =~ s:ternary)
  291. if (getline(prevline) =~ s:ternary_q)
  292. return indent(prevline)
  293. else
  294. return indent(prevline) + &sw
  295. endif
  296. endif
  297. " If we are in a multi-line comment, cindent does the right thing.
  298. if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1)
  299. return cindent(v:lnum)
  300. endif
  301. " Check for multiple var assignments
  302. " let var_indent = s:GetVarIndent(v:lnum)
  303. " if var_indent >= 0
  304. " return var_indent
  305. " endif
  306. " 3.3. Work on the previous line. {{{2
  307. " -------------------------------
  308. " If the line is empty and the previous nonblank line was a multi-line
  309. " comment, use that comment's indent. Deduct one char to account for the
  310. " space in ' */'.
  311. if line =~ '^\s*$' && s:IsInMultilineComment(prevline, 1)
  312. return indent(prevline) - 1
  313. endif
  314. " Find a non-blank, non-multi-line string line above the current line.
  315. let lnum = s:PrevNonBlankNonString(v:lnum - 1)
  316. " If the line is empty and inside a string, use the previous line.
  317. if line =~ '^\s*$' && lnum != prevline
  318. return indent(prevnonblank(v:lnum))
  319. endif
  320. " At the start of the file use zero indent.
  321. if lnum == 0
  322. return 0
  323. endif
  324. " Set up variables for current line.
  325. let line = getline(lnum)
  326. let ind = indent(lnum)
  327. " If the previous line ended with a block opening, add a level of indent.
  328. if s:Match(lnum, s:block_regex)
  329. return indent(s:GetMSL(lnum, 0)) + &sw
  330. endif
  331. " If the previous line contained an opening bracket, and we are still in it,
  332. " add indent depending on the bracket type.
  333. if line =~ '[[({]'
  334. let counts = s:LineHasOpeningBrackets(lnum)
  335. if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
  336. if col('.') + 1 == col('$')
  337. return ind + &sw
  338. else
  339. return virtcol('.')
  340. endif
  341. elseif counts[1] == '1' || counts[2] == '1'
  342. return ind + &sw
  343. else
  344. call cursor(v:lnum, vcol)
  345. end
  346. endif
  347. " 3.4. Work on the MSL line. {{{2
  348. " --------------------------
  349. let ind_con = ind
  350. let ind = s:IndentWithContinuation(lnum, ind_con, &sw)
  351. " }}}2
  352. "
  353. "
  354. let ols = s:InOneLineScope(lnum)
  355. if ols > 0
  356. let ind = ind + &sw
  357. else
  358. let ols = s:ExitingOneLineScope(lnum)
  359. while ols > 0 && ind > 0
  360. let ind = ind - &sw
  361. let ols = s:InOneLineScope(ols - 1)
  362. endwhile
  363. endif
  364. return ind
  365. endfunction
  366. " }}}1
  367. let &cpo = s:cpo_save
  368. unlet s:cpo_save