1#![doc = include_str!("README.md")]
2
3pub mod lsp;
4pub mod treesitter;
5
6use lsp::{
7 HierarchyClient as LspHierarchyClient, LspProvider,
8 WorkspaceSymbolClient as LspWorkspaceSymbolClient,
9};
10use treesitter::{
11 HierarchyClient as TreeSitterHierarchyClient, TreeSitterProvider,
12 WorkspaceSymbolClient as TreeSitterWorkspaceSymbolClient,
13};
14
15use crate::state::{HierarchyDirection, SymbolIdentity};
16use tower_lsp::lsp_types::{Range, SymbolKind, Url};
17
18#[derive(Clone, Debug, Eq, PartialEq)]
19pub struct WorkspaceSymbolMatch {
20 pub name: String,
23 pub kind: SymbolKind,
24 pub container_name: Option<String>,
25 pub uri: Url,
26 pub range: Option<Range>,
27}
28
29impl WorkspaceSymbolMatch {
30 pub fn display_name(&self) -> String {
31 self.name.clone()
32 }
33}
34
35#[derive(Clone, Copy, Debug, Eq, PartialEq)]
36pub enum FetchSource {
37 Lsp,
38 TreeSitter,
39}
40
41#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
42pub enum CachePolicy {
43 #[default]
44 UseCache,
45 Refresh,
46}
47
48#[derive(Clone, Debug, Eq, PartialEq)]
50pub struct HierarchyQuery {
51 pub symbol: SymbolIdentity,
52 pub direction: HierarchyDirection,
53}
54
55#[derive(Clone, Debug, Eq, PartialEq)]
57pub struct HierarchyResponse {
58 pub query: HierarchyQuery,
59 pub children: Vec<SymbolIdentity>,
60 pub source: FetchSource,
61}
62
63#[derive(Clone, Debug)]
64pub enum WorkspaceSymbolClient {
65 Lsp(LspWorkspaceSymbolClient),
66 TreeSitter(TreeSitterWorkspaceSymbolClient),
67}
68
69impl WorkspaceSymbolClient {
70 pub async fn query(&self, query: &str) -> anyhow::Result<Vec<WorkspaceSymbolMatch>> {
71 match self {
72 Self::Lsp(client) => client.query(query).await,
73 Self::TreeSitter(client) => client.query(query).await,
74 }
75 }
76}
77
78impl From<LspWorkspaceSymbolClient> for WorkspaceSymbolClient {
79 fn from(client: LspWorkspaceSymbolClient) -> Self {
80 Self::Lsp(client)
81 }
82}
83
84impl From<TreeSitterWorkspaceSymbolClient> for WorkspaceSymbolClient {
85 fn from(client: TreeSitterWorkspaceSymbolClient) -> Self {
86 Self::TreeSitter(client)
87 }
88}
89
90#[derive(Clone, Debug)]
91pub enum HierarchyClient {
92 Lsp(LspHierarchyClient),
93 TreeSitter(TreeSitterHierarchyClient),
94 Hybrid {
95 lsp: LspHierarchyClient,
96 tree_sitter: TreeSitterHierarchyClient,
97 },
98}
99
100impl HierarchyClient {
101 pub fn with_fallback(lsp: LspHierarchyClient, tree_sitter: TreeSitterHierarchyClient) -> Self {
102 Self::Hybrid { lsp, tree_sitter }
103 }
104
105 pub async fn query(&self, query: HierarchyQuery) -> anyhow::Result<HierarchyResponse> {
106 match self {
107 Self::Lsp(client) => client.query(query).await,
108 Self::TreeSitter(client) => client.query(query).await,
109 Self::Hybrid { lsp, tree_sitter } => {
110 if lsp.supports(query.symbol.kind) {
111 lsp.query(query).await
112 } else {
113 tree_sitter.query(query).await
114 }
115 }
116 }
117 }
118}
119
120impl From<LspHierarchyClient> for HierarchyClient {
121 fn from(client: LspHierarchyClient) -> Self {
122 Self::Lsp(client)
123 }
124}
125
126impl From<TreeSitterHierarchyClient> for HierarchyClient {
127 fn from(client: TreeSitterHierarchyClient) -> Self {
128 Self::TreeSitter(client)
129 }
130}
131
132#[derive(Debug, Default)]
133pub struct FetchCoordinator {
134 lsp: Option<LspProvider>,
135 tree_sitter: Option<TreeSitterProvider>,
136}
137
138impl FetchCoordinator {
139 pub fn with_lsp(lsp: LspProvider) -> Self {
140 Self {
141 lsp: Some(lsp),
142 tree_sitter: None,
143 }
144 }
145
146 pub fn with_tree_sitter(tree_sitter: TreeSitterProvider) -> Self {
147 Self {
148 lsp: None,
149 tree_sitter: Some(tree_sitter),
150 }
151 }
152
153 pub fn lsp(&self) -> Option<&LspProvider> {
154 self.lsp.as_ref()
155 }
156
157 pub fn workspace_symbol_client(&self) -> Option<WorkspaceSymbolClient> {
158 self.lsp
159 .as_ref()
160 .map(LspProvider::workspace_symbol_client)
161 .map(WorkspaceSymbolClient::from)
162 .or_else(|| {
163 self.tree_sitter
164 .as_ref()
165 .map(TreeSitterProvider::workspace_symbol_client)
166 .map(WorkspaceSymbolClient::from)
167 })
168 }
169
170 pub fn hierarchy_client(&self) -> Option<HierarchyClient> {
171 self.lsp
172 .as_ref()
173 .map(LspProvider::hierarchy_client)
174 .map(HierarchyClient::from)
175 .or_else(|| {
176 self.tree_sitter
177 .as_ref()
178 .map(TreeSitterProvider::hierarchy_client)
179 .map(HierarchyClient::from)
180 })
181 }
182
183 pub async fn workspace_symbols(
184 &self,
185 query: &str,
186 ) -> anyhow::Result<Vec<WorkspaceSymbolMatch>> {
187 self.workspace_symbol_client()
188 .ok_or_else(|| anyhow::anyhow!("no workspace-symbol provider is configured"))?
189 .query(query)
190 .await
191 }
192
193 pub async fn hierarchy(&self, query: HierarchyQuery) -> anyhow::Result<HierarchyResponse> {
194 self.hierarchy_client()
195 .ok_or_else(|| anyhow::anyhow!("no hierarchy provider is configured"))?
196 .query(query)
197 .await
198 }
199}