|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if exists("b:did_ftplugin") |
|
|
finish |
|
|
endif |
|
|
let b:did_ftplugin = 1 |
|
|
|
|
|
let s:cpo_save = &cpo |
|
|
set cpo-=C |
|
|
|
|
|
function! s:IdentifyScope(start, end) abort |
|
|
let pos_start = searchpairpos(a:start, '', a:end, 'bnW') |
|
|
let pos_end = searchpairpos(a:start, '', a:end, 'nW') |
|
|
|
|
|
return pos_start != [0, 0] |
|
|
\ && pos_end != [0, 0] |
|
|
\ && pos_start[0] != getpos('.')[1] |
|
|
endfunction |
|
|
|
|
|
function! s:AstroComments() abort |
|
|
if s:IdentifyScope('^---\n\s*\S', '^---\n\n') |
|
|
\ || s:IdentifyScope('^\s*<script', '^\s*<\/script>') |
|
|
|
|
|
setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,:// |
|
|
setlocal commentstring=//\ %s |
|
|
|
|
|
elseif s:IdentifyScope('^\s*<style', '^\s*<\/style>') |
|
|
|
|
|
setlocal comments=s1:/*,mb:*,ex:*/ |
|
|
setlocal commentstring=/*\ %s\ */ |
|
|
|
|
|
else |
|
|
|
|
|
setlocal comments=s:<!--,m:\ \ \ \ ,e:--> |
|
|
setlocal commentstring=<!--\ %s\ --> |
|
|
endif |
|
|
endfunction |
|
|
|
|
|
|
|
|
function! s:CollectPathsFromConfig() abort |
|
|
let config_json = findfile('tsconfig.json', '.;') |
|
|
|
|
|
if empty(config_json) |
|
|
let config_json = findfile('jsconfig.json', '.;') |
|
|
|
|
|
if empty(config_json) |
|
|
return |
|
|
endif |
|
|
endif |
|
|
|
|
|
try |
|
|
let paths_from_config = config_json |
|
|
\ ->readfile() |
|
|
\ ->filter({ _, val -> val =~ '^\s*[\[\]{}"0-9]' }) |
|
|
\ ->join() |
|
|
\ ->json_decode() |
|
|
\ ->get('compilerOptions', {}) |
|
|
\ ->get('paths', {}) |
|
|
catch /^Vim\%((\a\+)\)\=:E491:/ |
|
|
let paths_from_config = {} |
|
|
catch /^Vim\%((\a\+)\)\=:E474:/ |
|
|
let paths_from_config = {} |
|
|
endtry |
|
|
|
|
|
if !empty(paths_from_config) |
|
|
let b:astro_paths = paths_from_config |
|
|
\ ->map({key, val -> [ |
|
|
\ key->glob2regpat(), |
|
|
\ val[0]->substitute('\/\*$', '', '') |
|
|
\ ]}) |
|
|
\ ->values() |
|
|
endif |
|
|
|
|
|
let b:undo_ftplugin ..= " | unlet! b:astro_paths" |
|
|
endfunction |
|
|
|
|
|
function! s:AstroInclude(filename) abort |
|
|
let decorated_filename = a:filename |
|
|
\ ->substitute("^", "@", "") |
|
|
|
|
|
let found_path = b: |
|
|
\ ->get("astro_paths", []) |
|
|
\ ->indexof({ key, val -> decorated_filename =~ val[0]}) |
|
|
|
|
|
if found_path != -1 |
|
|
let alias = b:astro_paths[found_path][0] |
|
|
let path = b:astro_paths[found_path][1] |
|
|
\ ->substitute('\(\/\)*$', '/', '') |
|
|
|
|
|
return decorated_filename |
|
|
\ ->substitute(alias, path, '') |
|
|
endif |
|
|
|
|
|
return a:filename |
|
|
endfunction |
|
|
|
|
|
let b:undo_ftplugin = "setlocal" |
|
|
\ .. " formatoptions<" |
|
|
\ .. " path<" |
|
|
\ .. " suffixesadd<" |
|
|
\ .. " matchpairs<" |
|
|
\ .. " comments<" |
|
|
\ .. " commentstring<" |
|
|
\ .. " iskeyword<" |
|
|
\ .. " define<" |
|
|
\ .. " include<" |
|
|
\ .. " includeexpr<" |
|
|
|
|
|
|
|
|
augroup Astro |
|
|
autocmd! * <buffer> |
|
|
augroup END |
|
|
|
|
|
|
|
|
" and insert the comment leader when hitting <CR> or using "o |
|
|
setlocal formatoptions-=t |
|
|
setlocal formatoptions+=croql |
|
|
|
|
|
|
|
|
setlocal path-=/usr/include |
|
|
|
|
|
|
|
|
setlocal path+=src/** |
|
|
setlocal path+=public/** |
|
|
|
|
|
|
|
|
let &l:suffixesadd = |
|
|
\ ".astro" |
|
|
\ .. ",.js,.jsx,.es,.es6,.cjs,.mjs,.jsm" |
|
|
\ .. ",.json" |
|
|
\ .. ",.scss,.sass,.css" |
|
|
\ .. ",.svelte" |
|
|
\ .. ",.ts,.tsx,.d.ts" |
|
|
\ .. ",.vue" |
|
|
|
|
|
|
|
|
setlocal matchpairs+=<:> |
|
|
|
|
|
|
|
|
if exists("loaded_matchit") |
|
|
let b:match_ignorecase = 0 |
|
|
|
|
|
|
|
|
let b:match_words = |
|
|
\ '\<do\>:\<while\>,' |
|
|
\ .. '<\@<=\([^ \t>/]\+\)\%(\s\+[^>]*\%([^/]>\|$\)\|>\|$\):<\@<=/\1>,' |
|
|
\ .. '<\@<=\%([^ \t>/]\+\)\%(\s\+[^/>]*\|$\):/>' |
|
|
|
|
|
|
|
|
let b:match_words ..= |
|
|
\ '<!--:-->,' |
|
|
\ .. '<:>,' |
|
|
\ .. '<\@<=[ou]l\>[^>]*\%(>\|$\):<\@<=li\>:<\@<=/[ou]l>,' |
|
|
\ .. '<\@<=dl\>[^>]*\%(>\|$\):<\@<=d[td]\>:<\@<=/dl>,' |
|
|
\ .. '<\@<=\([^/!][^ \t>]*\)[^>]*\%(>\|$\):<\@<=/\1>' |
|
|
|
|
|
let b:undo_ftplugin ..= " | unlet! b:match_ignorecase b:match_words" |
|
|
endif |
|
|
|
|
|
|
|
|
setlocal iskeyword+=- |
|
|
setlocal iskeyword+=$ |
|
|
setlocal iskeyword+=% |
|
|
|
|
|
|
|
|
call s:CollectPathsFromConfig() |
|
|
|
|
|
|
|
|
setlocal include=^\\s*\\(import\\\|import\\s\\+[^\/]\\+from\\)\\s\\+['\"] |
|
|
|
|
|
" Process aliases if file can't be found |
|
|
setlocal includeexpr=s:AstroInclude(v:fname) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let &l:define = |
|
|
\ '\(^\s*(*async\s\+function\|(*function\)' |
|
|
\ .. '\|^\s*\(\*\|static\|async\|get\|set\|\i\+\.\)' |
|
|
\ .. '\|^\s*\(\ze\i\+\)\(([^)]*).*{$\|\s*[:=,]\)' |
|
|
|
|
|
|
|
|
|
|
|
autocmd Astro CursorMoved <buffer> call s:AstroComments() |
|
|
|
|
|
let &cpo = s:cpo_save |
|
|
unlet s:cpo_save |
|
|
|
|
|
|
|
|
|