mdx-js / mdx-analyzer

MDX extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=unifiedjs.vscode-mdx
MIT License
338 stars 21 forks source link

Hover/completions do not work when using mdx_analyzer from Neovim #474

Closed b0o closed 1 month ago

b0o commented 1 month ago

Initial checklist

Affected packages and versions

@mdx-js/language-server@0.4.10

Link to runnable example

No response

Steps to reproduce

I'm trying to use the MDX language server from Neovim.

The server starts and attaches to MDX files, and it reports diagnostics for structural issues like unclosed tags, but hover and completion do not work, and TypeScript errors are not reported.

I'm using the default config from lspconfig:

require'lspconfig'.mdx_analyzer.setup{}

I've tested in my own project (which is private), as well as this repo. MDX works as expected in both of them in VSCode, but not Neovim.

Other Neovim users seem to be having trouble as well: https://www.reddit.com/r/neovim/comments/1ewwtok/has_anyone_figured_out_intellisense_in_mdx_files/

Expected behavior

Actual behavior

Runtime

Node v20

Package manager

pnpm

OS

Linux

Build and bundle tools

Next.js

remcohaszing commented 1 month ago

Hey @b0o, thanks for letting us know, and also for linking to the Reddit post.

MDX language server can run either with or without TypeScript enabled. This can be configured using the typescript.enabled initialize option. This is false by default.

If you enable TypeScript support via the MDX language server, it needs to perform things that the TypeScript language server does as well. It needs to create the same project, process the same files, etc. This option exists for editors that don’t support TypeScript plugins. I don’t know if this is the case for Neovim.

The recommended option is to use @mdx-js/typescript-plugin instead. This adds TypeScript support for MDX based on the TypeScript language server you probably already use. This means less memory usage, and compatibility with other plugins that work the same way, such as the Vue TypeScript plugin.


Since this is documented, but you and other Neovim users didn’t find it, do you have suggestions how to improve this? Are there other places that should document these options?

github-actions[bot] commented 1 month ago

Hi team! Could you describe why this has been marked as external?

Thanks, — bb

github-actions[bot] commented 1 month ago

Hi! Thanks for reaching out! Because we treat issues as our backlog, we close issues that are questions since they don’t represent a task to be completed.

See our support docs for how and where to ask questions.

Thanks, — bb

b0o commented 1 month ago

Hi, thanks for getting back to me!

Since this is documented, but you and other Neovim users didn’t find it, do you have suggestions how to improve this?

I did see the documentation about this, but I couldn't get it to work, and it's difficult to debug. Personally, I don't care about using more CPU/memory, I would prefer to just add mdx_analyzer to my LSP config and have it just work.

wooorm commented 1 month ago

AFAIK none of us are Neovim users so it’s an unknown for us, what to do

haxibami commented 4 weeks ago

Hi, I noticed something weird:

I'm using vtsls (a TS language server on Neovim). I installed and setup @mdx-js/typescript-plugin like this:

setup.lua ```lua require('lspconfig').vtsls.setup({ settings = { vtsls = { autoUseWorkspaceTsdk = true, tsserver = { globalPlugins = { { name = '@mdx-js/typescript-plugin', enableForWorkspaceTypeScriptVersions = true, languages = { 'mdx', } }, } } }, }, filetypes = { 'javascript', 'javascriptreact', 'javascript.jsx', 'typescript', 'typescriptreact', 'typescript.tsx', 'mdx', -- <- Start TS server when you open a .mdx file }, ... }) ```

In my minimum sample project, I created a tsconfig.json file to enable TS support for .mdx files.

tsconfig.json ```json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, "plugins": [ { "name": "@mdx-js/typescript-plugin" } ] }, "mdx": { "checkMdx": true, "plugins": [["remark-frontmatter", ["toml", "yaml"]], "remark-gfm"] } } ```

And I have a test.mdx file with the following content. When I open this file, I get a lot of errors and hover doesn't work.

test.mdx ```mdx --- date: 2024-09-05 --- {/** * @typedef Props * @property {string} name * Who to greet. */} # Hello {props.name} ```
errors ![image](https://github.com/user-attachments/assets/5b964c3b-2090-43f8-8d36-6fec03fba80d)

So I tried another way: instead of the original .cjs script from @mdx-js/typescript-plugin (which resolves to node_modules/@mdx-js/typescript-plugin/index.cjs), I used the bundled .js script from the VSCode extension (<vscode_extensions_dir>/unifiedjs.vscode-mdx-<version>/node_modules/@mdx-js/typescript-plugin.js). I just removed the .cjs with package.json, and copied the .js to the same dir.

rm node_modules/@mdx-js/typescript-plugin/index.cjs node_modules/@mdx-js/typescript-plugin/package.json
cp <vscode_extensions_dir>/unifiedjs.vscode-mdx-1.8.10/node_modules/@mdx-js/typescript-plugin.js node_modules/@mdx-js/typescript-plugin/index.js

Now everything works like a charm. I can see the hover and the errors are gone.

hover, with no errors ![image](https://github.com/user-attachments/assets/99287f57-bc70-44fa-9db4-640c9e37ffd7)

I tested this in another project (which uses ESM) and the result was the same.

haxibami commented 4 weeks ago

No copying was necessary, just changing this line to "main": "./index.cjs" (or adding) worked.

https://github.com/mdx-js/mdx-analyzer/blob/284608be96f445696265504e2e5375ec0bef69d1/packages/typescript-plugin/package.json#L16