Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ftplugin/astro.vim and autoload/astro.vim #3

Merged
merged 1 commit into from
Oct 5, 2023

Conversation

romainl
Copy link

@romainl romainl commented Dec 5, 2022

This pull request adds two files: ftplugin/astro.vim and autoload/astro.vim.

Overview

ftplugin/astro.vim enables/fine-tunes the following features:

  • autoformatting and manual formatting, via formatoptions,
  • project-wide navigation, via path, define, include, includeexpr,suffixesadd, and b:astro_paths,
  • matching with %, via matchpairs, b:match_ignorecase, and b:match_words,
  • keyword definition, via iskeyword,
  • default HTML comment format, via comments and commentstring,
  • scope-specific comment format, via a CursorMoved autocommand.

autoload/astro.vim hosts the following support functions:

  • astro#IdentifyScope() and astro#Comments(), define the proper comments and commentstring for the current scope,
  • astro#CollectPathsFromConfig(), collects aliases defined in tsconfig.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 by astro#CollectPathsFromConfig() with a list of all the aliases defined in tsconfig.json or jsconfig.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 to astro#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" for gf, include/definition search, etc.

  • include is set to a value that should work for ESM imports like import Foo from "..., import type Foo from "..., or import "....

    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() set comments and commentstring 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 uses commentstring.

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.

@romainl romainl mentioned this pull request Dec 5, 2022
@wuelnerdotexe wuelnerdotexe added the enhancement New feature or request label Oct 5, 2023
@wuelnerdotexe wuelnerdotexe self-requested a review October 5, 2023 22:33
@wuelnerdotexe wuelnerdotexe linked an issue Oct 5, 2023 that may be closed by this pull request
Copy link
Owner

@wuelnerdotexe wuelnerdotexe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! @romainl - first, sorry for leaving so much time without approving your excellent contribution.

Thank you very much for contributing your knowledge to this little plugin, everything seems incredible to me. 🤩

@wuelnerdotexe wuelnerdotexe merged commit 9b0d948 into wuelnerdotexe:main Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include ftplugin
3 participants