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