From d6ca43cba4b86cd0097c76dd2b505edc4515e695 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Fri, 27 Nov 2026 16:33:09 +0900 Subject: [PATCH] feat(mdx): add MDX language support through @mdx-js/language-server (#6355) Adds an mdx language backed by @mdx-js/language-server and moves .mdx out of markdown.toml; the prose keeps Markdown highlighting, outline and folding. The server needs typescript.tsdk, which the launcher fills with the TypeScript vtsls uses (selected version, else vtsls' bundled one, else the one installed with the server, else the project's); an explicit tsdk setting wins, and the managed install adds typescript@6. Places keyed on markdown (live rendering, large-file outline, spell checking as prose, auto-indent, Surround With, colour preview) now include mdx. Formatting follows the Markdown entry of the Prettier / oxfmt language lists so saved lists keep formatting .mdx, and Prettier gets --parser mdx from mdx.toml instead of an extension special case. Closes #6340 Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- crates/ide-host/src/formatter.rs | 27 +++- crates/ide-host/src/ops/doc/commands.rs | 2 +- crates/ide-host/src/ops/spell.rs | 2 +- crates/ide-language/src/registry.rs | 2 + crates/ide-lsp/tests/real_servers.rs | 73 +++++++++++ crates/ide-sdk/src/apply.rs | 3 + crates/ide-sdk/src/lib.rs | 1 + crates/ide-sdk/src/mdx.rs | 120 ++++++++++++++++++ crates/ide-tools/src/formatter.rs | 7 +- crates/ide-tools/src/registry.rs | 11 ++ crates/ide-tools/src/tests.rs | 4 +- docs/en/languages/index.mdx | 3 +- docs/zh/languages/index.mdx | 3 +- languages/markdown.toml | 2 +- languages/mdx.toml | 19 +++ src/editor/core/cm/editing.ts | 1 + src/editor/core/cm/languages.ts | 1 + src/editor/core/model/slot.ts | 2 +- src/editor/lsp/actions.ts | 4 +- src/editor/lsp/format.test.ts | 4 + src/features/color-preview/colors.ts | 2 +- .../settings/languages/FormatterPage.tsx | 2 +- src/features/templates/live/builtins.ts | 2 +- src/features/viewers/markdown/live.ts | 3 +- 24 files changed, 281 insertions(+), 19 deletions(-) create mode 100644 crates/ide-sdk/src/mdx.rs create mode 100644 languages/mdx.toml diff --git a/crates/ide-host/src/formatter.rs b/crates/ide-host/src/formatter.rs index 6f983d5bf7..dc6139ebc3 100644 --- a/crates/ide-host/src/formatter.rs +++ b/crates/ide-host/src/formatter.rs @@ -261,6 +261,12 @@ fn supports_external_format(language: &LanguageConfig, tool: ExternalFormatter) } } +/// The entry of the formatters' language lists that covers `language`. MDX was part of Markdown +/// and follows its entry, so lists saved before it had its own language keep formatting it. +fn formatter_language(language: &str) -> &str { + if language == "mdx" { "markdown" } else { language } +} + /// The external formatter of a file, for a language with an external formatter. /// `forced` names the tool (Reformat with Prettier, oxfmt, Biome, Ruff, or Black). Ruff is /// the base formatter for Python, replaced by Black when configured or selected; Biome, @@ -295,7 +301,8 @@ fn external_tool( ))) }; } - let enabled = |tool| settings.formatter(tool).languages.iter().any(|l| l == language.id.as_str()); + let id = formatter_language(language.id.as_str()); + let enabled = |tool| settings.formatter(tool).languages.iter().any(|l| l == id); if language.formatter.as_ref().is_some_and(|f| f.tool == "ruff") { return Ok(if enabled(Black) && (settings.black.prefer || black_configured(file)) { Some(Black) @@ -897,6 +904,24 @@ mod tests { external_tool(forced, language.as_ref(), file, settings) } + #[test] + fn mdx_follows_the_markdown_entry_of_saved_language_lists() { + // Saved before MDX had its own language: the whole list, with no `mdx` in it. + let old: ide_core::settings::Settings = serde_json::from_str( + r#"{"languages":{"prettier":{"languages":["markdown","css"]},"oxfmt":{"languages":["css"]}}}"#, + ) + .unwrap(); + let file = Path::new("/project/docs/intro.mdx"); + assert_eq!(tool(None, "mdx", file, &old.languages).unwrap(), Some(Prettier)); + let mut no_markdown = old.languages.clone(); + no_markdown.prettier.languages.retain(|l| l != "markdown"); + assert_eq!(tool(None, "mdx", file, &no_markdown).unwrap(), None); + let mut oxfmt = old.languages.clone(); + oxfmt.oxfmt.languages.push("markdown".into()); + oxfmt.oxfmt.prefer = true; + assert_eq!(tool(None, "mdx", file, &oxfmt).unwrap(), Some(Oxfmt)); + } + #[test] fn default_settings_use_ruff_for_python_and_prettier_for_the_web() { let settings = LanguagesSettings::default(); diff --git a/crates/ide-host/src/ops/doc/commands.rs b/crates/ide-host/src/ops/doc/commands.rs index 532ea88768..008a8bfc3b 100644 --- a/crates/ide-host/src/ops/doc/commands.rs +++ b/crates/ide-host/src/ops/doc/commands.rs @@ -163,7 +163,7 @@ pub async fn doc_outline(host: &Host, window: &str, doc: DocId) -> IdeResult Vec::new(), + None if snap.large && !matches!(snap.language.as_str(), "markdown" | "mdx") => Vec::new(), None => syntax(), }) }) diff --git a/crates/ide-host/src/ops/spell.rs b/crates/ide-host/src/ops/spell.rs index c218ae9199..a317679fbf 100644 --- a/crates/ide-host/src/ops/spell.rs +++ b/crates/ide-host/src/ops/spell.rs @@ -11,7 +11,7 @@ use ide_spell::{Region, RegionKind, SpellScope, Speller, Typo}; use std::path::{Path, PathBuf}; /// Languages checked as prose (all text but code spans). -const PROSE: &[&str] = &["plaintext", "markdown", "restructuredtext", "asciidoc", "latex"]; +const PROSE: &[&str] = &["plaintext", "markdown", "mdx", "restructuredtext", "asciidoc", "latex"]; /// Custom dictionary paths: `~/…` from home, relative ones from the project root. fn dictionary_paths(settings: &SpellingSettings, root: &Path) -> Vec { diff --git a/crates/ide-language/src/registry.rs b/crates/ide-language/src/registry.rs index aef7ec885e..7e545dd23f 100644 --- a/crates/ide-language/src/registry.rs +++ b/crates/ide-language/src/registry.rs @@ -63,6 +63,7 @@ const BUILTIN: &[(&str, &str)] = &[ ("lua", include_str!("../../../languages/lua.toml")), ("makefile", include_str!("../../../languages/makefile.toml")), ("markdown", include_str!("../../../languages/markdown.toml")), + ("mdx", include_str!("../../../languages/mdx.toml")), ("nix", include_str!("../../../languages/nix.toml")), ("objective-c", include_str!("../../../languages/objective-c.toml")), ("objective-cpp", include_str!("../../../languages/objective-cpp.toml")), @@ -1281,6 +1282,7 @@ mod tests { ("Views/MainWindow.axaml", "axaml"), ("MainWindow.xaml", "xml"), ("README.md", "markdown"), + ("Intro.mdx", "mdx"), ("Dockerfile", "dockerfile"), ("Dockerfile.dev", "dockerfile"), ("Makefile", "makefile"), diff --git a/crates/ide-lsp/tests/real_servers.rs b/crates/ide-lsp/tests/real_servers.rs index c29496e905..70ba79a767 100644 --- a/crates/ide-lsp/tests/real_servers.rs +++ b/crates/ide-lsp/tests/real_servers.rs @@ -969,6 +969,79 @@ async fn bundler_path_aliases_resolve_below_tsconfig_paths() { tokio::time::timeout(Duration::from_secs(10), mgr.shutdown_all()).await.unwrap(); } +const MDX_BUTTON: &str = + "export function Button(props: { label: string }) {\n return ;\n}\n"; +const MDX_DOC: &str = "import { Button } from \"./Button\"\n\nexport const count = 2\n\n# Hello\n\n