codekingpro/portable-devtools
115k
1" Vim filetype plugin file2" Language: Astro3" Maintainer: Romain Lafourcade <romainlafourcade@gmail.com>4" Last Change: 2024 Apr 215" 2024 May 24 by Riley Bruins <ribru17@gmail.com> ('commentstring')6" 2025 Aug 29 by Vim project, add try/catch around json_decode(), #181417 8if exists("b:did_ftplugin")9 finish10endif11let b:did_ftplugin = 112 13let s:cpo_save = &cpo14set cpo-=C15 16function! s:IdentifyScope(start, end) abort17 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]23endfunction24 25function! s:AstroComments() abort26 if s:IdentifyScope('^---\n\s*\S', '^---\n\n')27 \ || s:IdentifyScope('^\s*<script', '^\s*<\/script>')28 " ECMAScript comments29 setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://30 setlocal commentstring=//\ %s31 32 elseif s:IdentifyScope('^\s*<style', '^\s*<\/style>')33 " CSS comments34 setlocal comments=s1:/*,mb:*,ex:*/35 setlocal commentstring=/*\ %s\ */36 37 else38 " HTML comments39 setlocal comments=s:<!--,m:\ \ \ \ ,e:-->40 setlocal commentstring=<!--\ %s\ -->41 endif42endfunction43 44" https://code.visualstudio.com/docs/languages/jsconfig45function! s:CollectPathsFromConfig() abort46 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 return53 endif54 endif55 56 try57 let paths_from_config = config_json58 \ ->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 json65 let paths_from_config = {}66 endtry67 68 if !empty(paths_from_config)69 let b:astro_paths = paths_from_config70 \ ->map({key, val -> [71 \ key->glob2regpat(),72 \ val[0]->substitute('\/\*$', '', '')73 \ ]})74 \ ->values()75 endif76 77 let b:undo_ftplugin ..= " | unlet! b:astro_paths"78endfunction79 80function! s:AstroInclude(filename) abort81 let decorated_filename = a:filename82 \ ->substitute("^", "@", "")83 84 let found_path = b:85 \ ->get("astro_paths", [])86 \ ->indexof({ key, val -> decorated_filename =~ val[0]})87 88 if found_path != -189 let alias = b:astro_paths[found_path][0]90 let path = b:astro_paths[found_path][1]91 \ ->substitute('\(\/\)*$', '/', '')92 93 return decorated_filename94 \ ->substitute(alias, path, '')95 endif96 97 return a:filename98endfunction99 100let 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 group113augroup Astro114 autocmd! * <buffer>115augroup END116 117" Set 'formatoptions' to break comment lines but not other lines,118" and insert the comment leader when hitting <CR> or using "o".119setlocal formatoptions-=t120setlocal formatoptions+=croql121 122" Remove irrelevant part of 'path'.123setlocal path-=/usr/include124 125" Seed 'path' with default directories for :find, gf, etc.126setlocal path+=src/**127setlocal path+=public/**128 129" Help Vim find extension-less filenames130let &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.vim140setlocal matchpairs+=<:>141 142" Matchit configuration143if exists("loaded_matchit")144 let b:match_ignorecase = 0145 146 " From $VIMRUNTIME/ftplugin/javascript.vim147 let b:match_words =148 \ '\<do\>:\<while\>,'149 \ .. '<\@<=\([^ \t>/]\+\)\%(\s\+[^>]*\%([^/]>\|$\)\|>\|$\):<\@<=/\1>,'150 \ .. '<\@<=\%([^ \t>/]\+\)\%(\s\+[^/>]*\|$\):/>'151 152 " From $VIMRUNTIME/ftplugin/html.vim153 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"161endif162 163" Change what constitutes a word, mainly useful for CSS/SASS164setlocal iskeyword+=-165setlocal iskeyword+=$166setlocal iskeyword+=%167 168" Define paths/aliases for module resolution169call s:CollectPathsFromConfig()170 171" Find ESM imports172setlocal include=^\\s*\\(import\\\|import\\s\\+[^\/]\\+from\\)\\s\\+['\"]173 174" Process aliases if file can't be found175setlocal includeexpr=s:AstroInclude(v:fname)176 177" Set 'define' to a comprehensive value178" From $VIMRUNTIME/ftplugin/javascript.vim and179" $VIMRUNTIME/ftplugin/sass.vim180let &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 scope187autocmd Astro CursorMoved <buffer> call s:AstroComments()188 189let &cpo = s:cpo_save190unlet s:cpo_save191 192" vim: textwidth=78 tabstop=8 shiftwidth=4 softtabstop=4 expandtab193 