withastro / language-tools

Language tools for Astro
MIT License
240 stars 45 forks source link

Auto import of Astro's navigate() in VS Code defaults to astro/dist/transitions/router #851

Closed glib-0 closed 2 months ago

glib-0 commented 2 months ago

Astro Info

Astro                    v4.5.16
Node                     v20.11.1
System                   Windows (x64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

N/A

Describe the Bug

I know this is pretty minor, but when using VS Code's auto importing to import navigate() in a React component, the default option is astro/dist/transitions/router instead of astro:transitions/client, which throws the following error:

image

The error is not super clear, and it's a very easy mistake to make.

image

The default option is correct inside .astro files, but obviously there is not much use for it there:

image

What's the expected result?

The navigate from astro:transitions/client to take higher priority in the list.

In the Stackblitz example, it defaults to 'astro/virtual-modules/transitions-router.js', which doesn't throw an error but also just doesn't work.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-baf1mz?file=src%2Fcomponents%2FReactComponent.tsx

Participation

glib-0 commented 2 months ago

Actually the Stackblitz default does work - I swear I tried it a few times 🙄. Bug still applies.

Princesseuh commented 2 months ago

Unfortunately, you can't really control the ordering of that list inside the project itself, it has to be done in the tooling. That's why it shows higher in Astro files, because we control all the tooling there.

The way to fix this in .ts(x) files would be to modify our TypeScript plugin, decorate the language service's method that does the completions and change the sortText property on the completions that start with astro:.

Here: https://github.com/withastro/language-tools/blob/0503392b80765c8a1292ddc9c063a1187425c187/packages/ts-plugin/src/index.ts#L4, instead of using Volar's all-in-one method for making the plugin, we should do all the decoration ourselves, like Vue does here: https://github.com/vuejs/language-tools/blob/e3fc9b97a74e6e50bf2c9bde81cb43e8ca3fa7bc/packages/typescript-plugin/index.ts#L17.

We should implement a decorateLanguageServiceForAstro function like they do, and in it augment the getCompletionsAtPosition function like they do here: https://github.com/vuejs/language-tools/blob/e3fc9b97a74e6e50bf2c9bde81cb43e8ca3fa7bc/packages/typescript-plugin/lib/common.ts#L20.

For the exact changes to do to the sortText, we can refer to this code: https://github.com/withastro/language-tools/blob/0503392b80765c8a1292ddc9c063a1187425c187/packages/language-server/src/plugins/typescript/completions.ts#L14-L17

If anyone is interested in sending a PR, let me know and I'll provide more details / I'm happy to answer more questions.