From 332334fb4fa2d26f8ede9a625dea0385dc96d71a Mon Sep 17 00:00:00 2001 From: ntbang0901 <121003585+ntbang0901@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:56:41 +0530 Subject: [PATCH] fix(outline): show type aliases a language server reports as variables (#4869) TypeScript type aliases showed as variables in the Structure view and breadcrumbs, and Rust type aliases / associated types had no usages lens, because LSP has no kind for type aliases and servers report Variable or TypeParameter. doc_outline now takes the syntax tree's type kind (merge_kinds) where the server reported one of those placeholder kinds for a declaration the syntax outline types at the same name and position. Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com> Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com> --- crates/ide-host/src/ops/doc/commands.rs | 9 ++- crates/ide-language/src/lib.rs | 2 +- crates/ide-language/src/syntax/mod.rs | 2 +- crates/ide-language/src/syntax/outline.rs | 30 +++++++++ crates/ide-language/src/syntax/tests.rs | 82 +++++++++++++++++++++++ src/bindings.ts | 2 +- 6 files changed, 121 insertions(+), 6 deletions(-) diff --git a/crates/ide-host/src/ops/doc/commands.rs b/crates/ide-host/src/ops/doc/commands.rs index b9c5c7eea..3e591558f 100644 --- a/crates/ide-host/src/ops/doc/commands.rs +++ b/crates/ide-host/src/ops/doc/commands.rs @@ -85,7 +85,7 @@ pub async fn doc_set_line_ending(host: &Host, window: &str, doc: DocId, line_end } /// Structure view: LSP document symbols when available (with tree-sitter -/// visibility), tree-sitter otherwise. +/// visibility and type-alias kinds), tree-sitter otherwise. /// Tree-sitter answers come from the per-document tree cache (incremental /// re-parse); large documents get no syntax structure. #[command(trust = always)] @@ -113,10 +113,13 @@ pub async fn doc_outline(host: &Host, window: &str, doc: DocId) -> IdeResult { if !snap.large { - ide_language::merge_visibility(&mut items, &syntax()); + let syntax = syntax(); + ide_language::merge_visibility(&mut items, &syntax); + ide_language::merge_kinds(&mut items, &syntax); } items } diff --git a/crates/ide-language/src/lib.rs b/crates/ide-language/src/lib.rs index 00911c5e4..4cf4af1f7 100644 --- a/crates/ide-language/src/lib.rs +++ b/crates/ide-language/src/lib.rs @@ -25,7 +25,7 @@ pub use syntax::{ CSS_CLASS, CSS_ID, CSS_VARIABLE, ClassBases, ConfigurationKey, ExternalFold, ExternalSymbol, ExternalSyntax, ExternalToken, FileReference, Literal, LiteralKind, MAX_CACHED_TREES, MAX_PARSE_BYTES, ParsedTree, SpellingKind, SpellingRegion, Syntax, SyntaxCache, SyntaxCacheStats, SyntaxHighlightKind, SyntaxHighlights, TreeError, - VueComponentImport, VueRefAccess, VueRefElement, WebReference, WebReferenceKind, merge_visibility, + VueComponentImport, VueRefAccess, VueRefElement, WebReference, WebReferenceKind, merge_kinds, merge_visibility, set_external_syntax, shift_pos, shift_range, }; pub use syntax::{MethodBody, PhpAlignment, PythonClassFacts, PythonFacts, PythonMethodFacts}; diff --git a/crates/ide-language/src/syntax/mod.rs b/crates/ide-language/src/syntax/mod.rs index 9ff2cf058..11d3d6a2d 100644 --- a/crates/ide-language/src/syntax/mod.rs +++ b/crates/ide-language/src/syntax/mod.rs @@ -28,7 +28,7 @@ use ide_core::{LanguageId, OutlineItem, Pos, Range}; pub use inheritance::ClassBases; pub use lines::{shift_pos, shift_range}; pub use literals::{Literal, LiteralKind, SpellingKind, SpellingRegion}; -pub use outline::merge_visibility; +pub use outline::{merge_kinds, merge_visibility}; pub use php_align::PhpAlignment; pub use python_facts::{MethodBody, PythonClassFacts, PythonFacts, PythonMethodFacts}; use std::cell::RefCell; diff --git a/crates/ide-language/src/syntax/outline.rs b/crates/ide-language/src/syntax/outline.rs index c2e3ec095..a8edc1ad3 100644 --- a/crates/ide-language/src/syntax/outline.rs +++ b/crates/ide-language/src/syntax/outline.rs @@ -495,6 +495,36 @@ pub fn merge_visibility(target: &mut [OutlineItem], source: &[OutlineItem]) { } } +/// Takes the syntax tree's kind where a server reported a declared type as a value or a type +/// parameter: LSP has no kind for type aliases, so servers fall back to one of those +/// (typescript-language-server: Variable; rust-analyzer, sourcekit-lsp: TypeParameter). +/// Matched by name and position like [`merge_visibility`]. +pub fn merge_kinds(target: &mut [OutlineItem], source: &[OutlineItem]) { + fn index(items: &[OutlineItem], out: &mut HashMap<(ide_core::Pos, String), SymbolKind>) { + for i in items { + if i.kind.is_type_like() { + out.insert((i.selection_range.start, i.name.clone()), i.kind); + } + index(&i.children, out); + } + } + fn apply(items: &mut [OutlineItem], map: &HashMap<(ide_core::Pos, String), SymbolKind>) { + for i in items { + if matches!(i.kind, SymbolKind::Variable | SymbolKind::TypeParameter) + && let Some(&kind) = map.get(&(i.selection_range.start, i.name.clone())) + { + i.kind = kind; + } + apply(&mut i.children, map); + } + } + let mut map = HashMap::new(); + index(source, &mut map); + if !map.is_empty() { + apply(target, &map); + } +} + /// End of `node` without trailing whitespace (C macros and others include the newline). fn trimmed_end(node: &Node, text: &str) -> usize { let (s, e) = (node.start_byte(), node.end_byte()); diff --git a/crates/ide-language/src/syntax/tests.rs b/crates/ide-language/src/syntax/tests.rs index d13b40947..0ac60d891 100644 --- a/crates/ide-language/src/syntax/tests.rs +++ b/crates/ide-language/src/syntax/tests.rs @@ -1476,6 +1476,88 @@ fn merge_visibility_matches_by_name_and_position() { assert_eq!(lsp[1].visibility, None); } +#[test] +fn merge_kinds_names_types_a_server_reported_as_values_or_type_parameters() { + fn kinds(items: &[OutlineItem], out: &mut Vec<(String, SymbolKind)>) { + for i in items { + out.push((i.name.clone(), i.kind)); + kinds(&i.children, out); + } + } + /// The syntax outline with each kind replaced as a server reports it. + fn as_server(ts: &[OutlineItem], server: &[(&str, SymbolKind)]) -> Vec { + fn set(items: &mut [OutlineItem], server: &[(&str, SymbolKind)], at: &mut usize) { + for i in items { + i.kind = server[*at].1; + assert_eq!(i.name, server[*at].0); + *at += 1; + set(&mut i.children, server, at); + } + } + let mut lsp = ts.to_vec(); + set(&mut lsp, server, &mut 0); + lsp + } + fn merged(language: &str, src: &str, server: &[(&str, SymbolKind)]) -> Vec<(String, SymbolKind)> { + let ts = syntax().outline(&lang(language), src); + let mut lsp = as_server(&ts, server); + crate::merge_kinds(&mut lsp, &ts); + let mut out = Vec::new(); + kinds(&lsp, &mut out); + out + } + use SymbolKind as K; + let owned = |v: &[(&str, SymbolKind)]| v.iter().map(|(n, k)| (n.to_string(), *k)).collect::>(); + + // typescript-language-server: a type alias is a Variable; `const` is one too. + let ts = "type Cents = number;\ninterface Shape {}\nconst rate = 1;\n"; + assert_eq!( + merged("typescript", ts, &[("Cents", K::Variable), ("Shape", K::Interface), ("rate", K::Variable)]), + owned(&[("Cents", K::TypeAlias), ("Shape", K::Interface), ("rate", K::Variable)]), + ); + + // rust-analyzer: type aliases and associated types are TypeParameters, a trait an Interface. + let rs = "pub type Meters = u32;\ntrait Measure { type Unit; }\nstruct Rope;\nimpl Measure for Rope { type Unit = Meters; }\n"; + assert_eq!( + merged( + "rust", + rs, + &[ + ("Meters", K::TypeParameter), + ("Measure", K::Interface), + ("Unit", K::TypeParameter), + ("Rope", K::Struct), + ("impl Measure for Rope", K::Variable), + ("Unit", K::TypeParameter), + ], + ), + owned(&[ + ("Meters", K::TypeAlias), + ("Measure", K::Interface), + ("Unit", K::TypeAlias), + ("Rope", K::Struct), + ("impl Measure for Rope", K::Variable), + ("Unit", K::TypeAlias), + ]), + ); + + // sourcekit-lsp: typealias and associatedtype are TypeParameters. + let swift = "typealias Meters = Int\nprotocol Measure { associatedtype Unit }\n"; + assert_eq!( + merged("swift", swift, &[("Meters", K::TypeParameter), ("Measure", K::Interface), ("Unit", K::TypeParameter)]), + owned(&[("Meters", K::TypeAlias), ("Measure", K::Interface), ("Unit", K::TypeAlias)]), + ); + + // A type kind the server chose stays: clangd's Class for a typedef, gopls's Struct. + let c = "typedef unsigned int meters;\n"; + assert_eq!(merged("c", c, &[("meters", K::Class)]), owned(&[("meters", K::Class)])); + let go = "package p\n\ntype Point struct{}\n"; + assert_eq!( + merged("go", go, &[("p", K::Package), ("Point", K::Struct)]), + owned(&[("p", K::Package), ("Point", K::Struct)]) + ); +} + #[test] fn spelling_regions_cover_declared_names_only() { let src = "// note\nfn parse_input(raw_text: &str) -> Value { let mut counter = helper(raw_text); \"hi\" }\n"; diff --git a/src/bindings.ts b/src/bindings.ts index edb6dfd2b..fb3f33034 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -538,7 +538,7 @@ export const commands = { docOpenVirtual: (uri: string) => typedError(__TAURI_INVOKE("doc_open_virtual", { uri })), /** * Structure view: LSP document symbols when available (with tree-sitter - * visibility), tree-sitter otherwise. + * visibility and type-alias kinds), tree-sitter otherwise. * Tree-sitter answers come from the per-document tree cache (incremental * re-parse); large documents get no syntax structure. */