1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Import resolution for cross-file completion.
//!
//! This module provides functionality to query the Magellan database for Import entities
//! and resolve them to target files, enabling code completion to suggest symbols from
//! imported modules across file boundaries.
//!
//! # Architecture
//!
//! The import resolution system works by:
//! 1. Querying the Magellan database for Import entities in a given file
//! 2. Parsing import metadata (kind, path, names, glob/reexport flags)
//! 3. Resolving import paths to target file paths
//! 4. Extracting public symbols from target files
//! 5. Merging imported symbols with local symbols for completion
//!
//! # Database Schema
//!
//! Uses Magellan's graph schema:
//! - `graph_entities` table: Contains Import entities with metadata
//! - `graph_edges` table: Contains IMPORTS relationships (File → Import)
//! - JSON `data` field: Contains import details (path, names, flags)
//!
//! # Example
//!
//! ```no_run
//! use splice::completion::imports::ImportResolver;
//! use std::path::PathBuf;
//!
//! let db_path = PathBuf::from(".magellan/splice.db");
//! let resolver = ImportResolver::new(&db_path);
//!
//! let file_path = PathBuf::from("src/main.rs");
//! let imports = resolver.get_file_imports(&file_path).unwrap();
//!
//! for import in imports {
//! println!("Import: {:?} from {}",
//! import.imported_names, import.import_path.join("::"));
//! }
//! ```
//!
//! # Import Kinds
//!
//! - **plain_use**: Regular `use` statement (e.g., `use crate::foo::Bar`)
//! - **glob_use**: Wildcard import (e.g., `use crate::foo::*`)
//! - **reexport**: Public re-export (e.g., `pub use crate::foo::Bar`)
//!
//! # Performance
//!
//! - Query time: ~1-2ms per file
//! - Caches database connections internally
//! - Designed for incremental resolution (per-file basis)
use Result;
use Connection;
use Value as JsonValue;
use PathBuf;
/// Import entity from Magellan database.
///
/// Represents a single import statement found in the source code,
/// with full metadata for resolution and filtering.
///
/// # Fields
///
/// - `id`: Magellan entity ID (for database grounding)
/// - `file_path`: Absolute path to file containing this import
/// - `import_kind`: Type of import ("plain_use", "glob_use", "reexport")
/// - `import_path`: Module path segments (e.g., ["crate", "api", "handler"])
/// - `imported_names`: Specific names imported (empty for globs)
/// - `is_glob`: Whether this is a wildcard import (`use foo::*`)
/// - `is_reexport`: Whether this is a public re-export (`pub use`)
///
/// # Example
///
/// ```rust
/// use splice::completion::imports::ImportEntity;
///
/// // For: use crate::api::{RequestHandler, process_request};
/// let _entity = ImportEntity {
/// id: "12345".to_string(),
/// file_path: "/path/to/src/main.rs".to_string(),
/// import_kind: "plain_use".to_string(),
/// import_path: vec!["crate".to_string(), "api".to_string()],
/// imported_names: vec!["RequestHandler".to_string(), "process_request".to_string()],
/// is_glob: false,
/// is_reexport: false,
/// };
/// ```
/// Import resolver using Magellan database.
///
/// Queries the Magellan graph database to extract Import entities for a given file,
/// parsing JSON metadata to provide structured import information.
///
/// # Usage
///
/// ```no_run
/// use splice::completion::imports::ImportResolver;
/// use std::path::PathBuf;
///
/// let resolver = ImportResolver::new(&PathBuf::from(".magellan/splice.db"));
/// let imports = resolver.get_file_imports(&PathBuf::from("src/main.rs")).unwrap();
/// ```
///
/// # Database Query
///
/// The resolver executes a SQL query joining `graph_entities` and `graph_edges`:
///
/// ```sql
/// SELECT ge.id, ge.file_path, ge.data
/// FROM graph_entities ge
/// JOIN graph_edges e ON ge.id = e.to_id
/// WHERE e.from_id = (
/// SELECT id FROM graph_entities
/// WHERE file_path = ?1 AND kind = 'File'
/// LIMIT 1
/// )
/// AND e.edge_type = 'IMPORTS'
/// AND ge.kind = 'Import'
/// ```
///
/// This finds all Import entities connected to the file via IMPORTS edges.