vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

astro.vim (5832B)


      1 " Vim filetype plugin file
      2 " Language:     Astro
      3 " Maintainer:   Romain Lafourcade <romainlafourcade@gmail.com>
      4 " Last Change:  2024 Apr 21
      5 "               2024 May 24 by Riley Bruins <ribru17@gmail.com> ('commentstring')
      6 "               2025 Aug 29 by Vim project, add try/catch around json_decode(), #18141
      7 
      8 if exists("b:did_ftplugin")
      9     finish
     10 endif
     11 let b:did_ftplugin = 1
     12 
     13 let s:cpo_save = &cpo
     14 set cpo-=C
     15 
     16 function! s:IdentifyScope(start, end) abort
     17     let pos_start = searchpairpos(a:start, '', a:end, 'bnW')
     18     let pos_end = searchpairpos(a:start, '', a:end, 'nW')
     19 
     20     return pos_start != [0, 0]
     21                 \ && pos_end != [0, 0]
     22                 \ && pos_start[0] != getpos('.')[1]
     23 endfunction
     24 
     25 function! s:AstroComments() abort
     26     if s:IdentifyScope('^---\n\s*\S', '^---\n\n')
     27                 \ || s:IdentifyScope('^\s*<script', '^\s*<\/script>')
     28         " ECMAScript comments
     29         setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://
     30         setlocal commentstring=//\ %s
     31 
     32     elseif s:IdentifyScope('^\s*<style', '^\s*<\/style>')
     33         " CSS comments
     34         setlocal comments=s1:/*,mb:*,ex:*/
     35         setlocal commentstring=/*\ %s\ */
     36 
     37     else
     38         " HTML comments
     39         setlocal comments=s:<!--,m:\ \ \ \ ,e:-->
     40         setlocal commentstring=<!--\ %s\ -->
     41     endif
     42 endfunction
     43 
     44 " https://code.visualstudio.com/docs/languages/jsconfig
     45 function! s:CollectPathsFromConfig() abort
     46     let config_json = findfile('tsconfig.json', '.;')
     47 
     48     if empty(config_json)
     49         let config_json = findfile('jsconfig.json', '.;')
     50 
     51         if empty(config_json)
     52             return
     53         endif
     54     endif
     55 
     56     try
     57         let paths_from_config = config_json
     58                 \ ->readfile()
     59                 \ ->filter({ _, val -> val =~ '^\s*[\[\]{}"0-9]' })
     60                 \ ->join()
     61                 \ ->json_decode()
     62                 \ ->get('compilerOptions', {})
     63                 \ ->get('paths', {})
     64     catch /^Vim\%((\a\+)\)\=:E491:/ " invalid json
     65         let paths_from_config = {}
     66     endtry
     67 
     68     if !empty(paths_from_config)
     69         let b:astro_paths = paths_from_config
     70                     \ ->map({key, val -> [
     71                     \     key->glob2regpat(),
     72                     \     val[0]->substitute('\/\*$', '', '')
     73                     \   ]})
     74                     \ ->values()
     75     endif
     76 
     77     let b:undo_ftplugin ..= " | unlet! b:astro_paths"
     78 endfunction
     79 
     80 function! s:AstroInclude(filename) abort
     81     let decorated_filename = a:filename
     82                 \ ->substitute("^", "@", "")
     83 
     84     let found_path = b:
     85                 \ ->get("astro_paths", [])
     86                 \ ->indexof({ key, val -> decorated_filename =~ val[0]})
     87 
     88     if found_path != -1
     89         let alias = b:astro_paths[found_path][0]
     90         let path  = b:astro_paths[found_path][1]
     91                     \ ->substitute('\(\/\)*$', '/', '')
     92 
     93         return decorated_filename
     94                     \ ->substitute(alias, path, '')
     95     endif
     96 
     97     return a:filename
     98 endfunction
     99 
    100 let b:undo_ftplugin = "setlocal"
    101             \ .. " formatoptions<"
    102             \ .. " path<"
    103             \ .. " suffixesadd<"
    104             \ .. " matchpairs<"
    105             \ .. " comments<"
    106             \ .. " commentstring<"
    107             \ .. " iskeyword<"
    108             \ .. " define<"
    109             \ .. " include<"
    110             \ .. " includeexpr<"
    111 
    112 " Create self-resetting autocommand group
    113 augroup Astro
    114     autocmd! * <buffer>
    115 augroup END
    116 
    117 " Set 'formatoptions' to break comment lines but not other lines,
    118 " and insert the comment leader when hitting <CR> or using "o".
    119 setlocal formatoptions-=t
    120 setlocal formatoptions+=croql
    121 
    122 " Remove irrelevant part of 'path'.
    123 setlocal path-=/usr/include
    124 
    125 " Seed 'path' with default directories for :find, gf, etc.
    126 setlocal path+=src/**
    127 setlocal path+=public/**
    128 
    129 " Help Vim find extension-less filenames
    130 let &l:suffixesadd =
    131             \ ".astro"
    132             \ .. ",.js,.jsx,.es,.es6,.cjs,.mjs,.jsm"
    133             \ .. ",.json"
    134             \ .. ",.scss,.sass,.css"
    135             \ .. ",.svelte"
    136             \ .. ",.ts,.tsx,.d.ts"
    137             \ .. ",.vue"
    138 
    139 " From $VIMRUNTIME/ftplugin/html.vim
    140 setlocal matchpairs+=<:>
    141 
    142 " Matchit configuration
    143 if exists("loaded_matchit")
    144     let b:match_ignorecase = 0
    145 
    146     " From $VIMRUNTIME/ftplugin/javascript.vim
    147     let b:match_words =
    148                 \ '\<do\>:\<while\>,'
    149                 \ .. '<\@<=\([^ \t>/]\+\)\%(\s\+[^>]*\%([^/]>\|$\)\|>\|$\):<\@<=/\1>,'
    150                 \ .. '<\@<=\%([^ \t>/]\+\)\%(\s\+[^/>]*\|$\):/>'
    151 
    152     " From $VIMRUNTIME/ftplugin/html.vim
    153     let b:match_words ..=
    154                 \ '<!--:-->,'
    155                 \ .. '<:>,'
    156                 \ .. '<\@<=[ou]l\>[^>]*\%(>\|$\):<\@<=li\>:<\@<=/[ou]l>,'
    157                 \ .. '<\@<=dl\>[^>]*\%(>\|$\):<\@<=d[td]\>:<\@<=/dl>,'
    158                 \ .. '<\@<=\([^/!][^ \t>]*\)[^>]*\%(>\|$\):<\@<=/\1>'
    159 
    160     let b:undo_ftplugin ..= " | unlet! b:match_ignorecase b:match_words"
    161 endif
    162 
    163 " Change what constitutes a word, mainly useful for CSS/SASS
    164 setlocal iskeyword+=-
    165 setlocal iskeyword+=$
    166 setlocal iskeyword+=%
    167 
    168 " Define paths/aliases for module resolution
    169 call s:CollectPathsFromConfig()
    170 
    171 " Find ESM imports
    172 setlocal include=^\\s*\\(import\\\|import\\s\\+[^\/]\\+from\\)\\s\\+['\"]
    173 
    174 " Process aliases if file can't be found
    175 setlocal includeexpr=s:AstroInclude(v:fname)
    176 
    177 " Set 'define' to a comprehensive value
    178 " From $VIMRUNTIME/ftplugin/javascript.vim and
    179 " $VIMRUNTIME/ftplugin/sass.vim
    180 let &l:define =
    181             \ '\(^\s*(*async\s\+function\|(*function\)'
    182             \ .. '\|^\s*\(\*\|static\|async\|get\|set\|\i\+\.\)'
    183             \ .. '\|^\s*\(\ze\i\+\)\(([^)]*).*{$\|\s*[:=,]\)'
    184 
    185 
    186 " Set &comments and &commentstring according to current scope
    187 autocmd Astro CursorMoved <buffer> call s:AstroComments()
    188 
    189 let &cpo = s:cpo_save
    190 unlet s:cpo_save
    191 
    192 " vim: textwidth=78 tabstop=8 shiftwidth=4 softtabstop=4 expandtab