Add ftplugin/astro.vim and autoload/astro.vim #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds two files:
ftplugin/astro.vim
andautoload/astro.vim
.Overview
ftplugin/astro.vim
enables/fine-tunes the following features:formatoptions
,path
,define
,include
,includeexpr
,suffixesadd
, andb:astro_paths
,%
, viamatchpairs
,b:match_ignorecase
, andb:match_words
,iskeyword
,comments
andcommentstring
,CursorMoved
autocommand.autoload/astro.vim
hosts the following support functions:astro#IdentifyScope()
andastro#Comments()
, define the propercomments
andcommentstring
for the current scope,astro#CollectPathsFromConfig()
, collects aliases defined intsconfig.json
for use in the function below,astro#AstroInclude()
, replaces aliases in filenames for:find
,gf
, etc.Details
Project-wide navigation
path
is set according to https://docs.astro.build/en/core-concepts/project-structure/#directories-and-files.It allows doing
gf
on"src/components/Foo.astro"
,"./Foo.astro"
,"../bar/Baz.astro"
in the front matter. It also allows doing:find *Foo<Tab>
for the simple cases.It also helps resolving includes for include/definition search.
suffixesadd
is a mix of all the values found in$VIMRUNTIME/ftplugin/{javascript,typescript,css,sass,html}.vim
, plus some Astro-specific ones.It tells Vim what extensions to try for extension-less filenames. This allows doing
gf
on<Foo />
in a template for the simple cases.b:astro_paths
is populated byastro#CollectPathsFromConfig()
with a list of all the aliases defined intsconfig.json
orjsconfig.json
. This is not directly useful for the user but useful for the next option.includeexpr
is executed if the target is not found. It is bound toastro#AstroInclude()
, which replaces the alias in the filename with the corresponding path.So, in addition to the simple cases, it also handles cases like
"@components/Foo.astro"
forgf
, include/definition search, etc.include
is set to a value that should work for ESM imports likeimport Foo from "...
,import type Foo from "...
, orimport "...
.It allows things like
[<C-i>
on a type, etc.define
is set to a value that mixes the default values for JavaScript and SASS.It should help locating definitions in local and included files with commands like
[D
or:dsearch
.Scoped comment formats
astro#Comments()
setcomments
andcommentstring
to the appropriate value for the current scope. The implementation honestly looks like it should be slow but it only takes a couple of milliseconds and I didn't notice any performance issue.I did this for
tpope/commentary
, which usescommentstring
.Note: it only covers SASS/SCSS/CSS, JS/TS, and HTML at the moment.
Tested in Vim 9.0.270 and Vim 9.0472 on a Mac.