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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use crate::completion::imports::{ImportEntity, ImportResolver};
use crate::completion::tokenizer;
use crate::completion::types::{Symbol, SymbolKind};
use crate::graph::MagellanIntegration;
use anyhow::Result;
use rusqlite::Connection;
use serde_json::Value as JsonValue;
use std::path::PathBuf;
use std::sync::Arc;
/// Context around cursor position
#[derive(Debug, Clone)]
/// Context around cursor position.
pub struct CompletionContext {
/// Absolute path to the file being edited.
pub file_path: PathBuf,
/// 1-based line number of the cursor.
pub cursor_line: usize,
/// 1-based column number of the cursor.
pub cursor_col: usize,
/// Function enclosing the cursor, if any.
pub enclosing_function: Option<Symbol>,
/// Module enclosing the cursor, if any.
pub enclosing_module: Option<Symbol>,
/// Symbols visible from the cursor position.
pub visible_symbols: Vec<Symbol>,
/// Token currently being typed at the cursor, if any.
pub current_token: Option<String>,
}
impl CompletionContext {
/// Normalize file path to absolute path for database queries
/// Converts relative paths (e.g., "src/file.rs") to absolute paths
fn normalize_path(file_path: &PathBuf) -> PathBuf {
if file_path.is_absolute() {
file_path.clone()
} else {
// Convert relative path to absolute by prepending current directory
std::env::current_dir()
.unwrap_or_else(|_| PathBuf::from("."))
.join(file_path)
.canonicalize()
.unwrap_or_else(|_| file_path.clone())
}
}
/// Analyze code context at cursor position
pub fn analyze(
file_path: &PathBuf,
line: usize,
column: usize,
magellan: &Arc<MagellanIntegration>,
) -> Result<Self> {
// Normalize relative paths to absolute for database queries
let normalized_path = Self::normalize_path(file_path);
// Find enclosing function
let enclosing_function = Self::find_enclosing_function(&normalized_path, line, magellan)?;
// Find enclosing module
let enclosing_module = Self::find_enclosing_module(&normalized_path, magellan)?;
// Get local symbols (in current file)
let local_symbols =
Self::get_visible_symbols(&normalized_path, &enclosing_module, magellan)?;
// Get imported symbols (from other files)
let imported_symbols =
Self::get_imported_symbols(&normalized_path, magellan).unwrap_or_default(); // Graceful degradation if import resolution fails
// Merge local and imported symbols
let mut visible_symbols = local_symbols;
visible_symbols.extend(imported_symbols);
// Extract current token being typed
let current_token = tokenizer::extract_current_token(&normalized_path, line, column);
Ok(Self {
file_path: normalized_path,
cursor_line: line,
cursor_col: column,
enclosing_function,
enclosing_module,
visible_symbols,
current_token,
})
}
fn find_enclosing_function(
file_path: &PathBuf,
line: usize,
magellan: &Arc<MagellanIntegration>,
) -> Result<Option<Symbol>> {
// Query for symbols in the same file
let db_query = r#"
SELECT id, name, kind, file_path, data
FROM graph_entities
WHERE file_path = ?1
AND kind = 'Symbol'
LIMIT 1000
"#;
let conn = Connection::open(magellan.db_path())
.map_err(|e| anyhow::anyhow!("Failed to open database: {}", e))?;
let mut stmt = conn.prepare(db_query)?;
let rows = stmt.query_map([&file_path.to_string_lossy()], |row| {
Ok((
row.get::<_, i64>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
row.get::<_, String>(3)?,
row.get::<_, String>(4)?,
))
})?;
// Find the function that encloses the cursor position
let mut best_match: Option<Symbol> = None;
for row_result in rows {
let (id, name, _kind, path, data_str) = row_result?;
let id = id.to_string(); // Convert i64 to String
// Parse the data JSON
if let Ok(data) = serde_json::from_str::<JsonValue>(&data_str) {
if let Some(start_line) = data["start_line"].as_u64() {
if let Some(end_line) = data["end_line"].as_u64() {
// Check if cursor is within this symbol's span
if (line as u64) >= start_line && (line as u64) <= end_line {
// Check if it's a function
if let Some(symbol_kind) = data["kind"].as_str() {
if symbol_kind == "Function" || symbol_kind == "Method" {
let symbol = Symbol {
id,
name,
kind: Self::parse_symbol_kind(symbol_kind),
path,
line: start_line as usize,
column: data["start_col"].as_u64().unwrap_or(0) as usize,
};
// Keep the most specific (smallest) enclosing function
if best_match.is_none()
|| symbol.line > best_match.as_ref().unwrap().line
{
best_match = Some(symbol);
}
}
}
}
}
}
}
}
Ok(best_match)
}
fn find_enclosing_module(
_file_path: &PathBuf,
_magellan: &Arc<MagellanIntegration>,
) -> Result<Option<Symbol>> {
// Extract module path from file_path
// e.g., src/patch/engine.rs -> patch::engine
// For now, return None - will be implemented with more sophisticated logic
Ok(None)
}
fn get_visible_symbols(
file_path: &PathBuf,
_module: &Option<Symbol>,
magellan: &Arc<MagellanIntegration>,
) -> Result<Vec<Symbol>> {
// Query for symbols in the same file (simplified for MVP)
let db_query = r#"
SELECT id, name, kind, file_path, data
FROM graph_entities
WHERE file_path = ?1
AND kind = 'Symbol'
ORDER BY name
LIMIT 100
"#;
let conn = Connection::open(magellan.db_path())
.map_err(|e| anyhow::anyhow!("Failed to open database: {}", e))?;
let mut stmt = conn.prepare(db_query)?;
let rows = stmt.query_map([&file_path.to_string_lossy()], |row| {
Ok((
row.get::<_, i64>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
row.get::<_, String>(3)?,
row.get::<_, String>(4)?,
))
})?;
let mut symbols = Vec::new();
for row_result in rows {
let (id, name, _kind, path, data_str) = row_result?;
let id = id.to_string(); // Convert i64 to String
// Parse the data JSON to get symbol kind and location
if let Ok(data) = serde_json::from_str::<JsonValue>(&data_str) {
let kind_str = data["kind"].as_str().unwrap_or("Unknown");
let start_line = data["start_line"].as_u64().unwrap_or(0) as usize;
let start_col = data["start_col"].as_u64().unwrap_or(0) as usize;
let symbol = Symbol {
id,
name,
kind: Self::parse_symbol_kind(kind_str),
path,
line: start_line,
column: start_col,
};
symbols.push(symbol);
}
}
Ok(symbols)
}
fn get_imported_symbols(
file_path: &PathBuf,
magellan: &Arc<MagellanIntegration>,
) -> Result<Vec<Symbol>> {
let resolver = ImportResolver::new(&magellan.db_path().to_path_buf());
// Get all imports for this file
let imports = resolver.get_file_imports(file_path)?;
let mut imported_symbols = Vec::new();
// For each import, get symbols from target file
for import in imports {
// Resolve import path to target file
// For MVP: Use direct file path matching
// TODO: Integrate ModulePathIndex for proper resolution
// Build potential target file paths
let target_files = Self::resolve_import_targets(&import, file_path);
for target_file in target_files {
// Get public symbols from target file
if let Ok(symbols) = Self::get_public_symbols(&target_file, magellan) {
let filtered = if import.is_glob {
symbols // All public symbols
} else {
// Filter by imported_names
symbols
.into_iter()
.filter(|s| import.imported_names.contains(&s.name))
.collect()
};
imported_symbols.extend(filtered);
}
}
}
Ok(imported_symbols)
}
fn resolve_import_targets(import: &ImportEntity, current_file: &PathBuf) -> Vec<PathBuf> {
// For MVP: Try sibling files and common patterns
// TODO: Use ModulePathIndex for proper resolution
let mut targets = Vec::new();
// Try same directory
if let Some(parent) = current_file.parent() {
for segment in &import.import_path {
let target = parent.join(segment).with_extension("rs");
if target.exists() {
targets.push(target);
}
}
// Try mod.rs
let mod_path = parent.join("mod.rs");
if mod_path.exists() {
targets.push(mod_path);
}
}
targets
}
fn get_public_symbols(
file_path: &PathBuf,
magellan: &Arc<MagellanIntegration>,
) -> Result<Vec<Symbol>> {
let db_query = r#"
SELECT id, name, kind, file_path, data
FROM graph_entities
WHERE file_path = ?1
AND kind = 'Symbol'
LIMIT 100
"#;
let conn = Connection::open(magellan.db_path())
.map_err(|e| anyhow::anyhow!("Failed to open database: {}", e))?;
let mut stmt = conn.prepare(db_query)?;
let rows = stmt.query_map([file_path.to_string_lossy()], |row| {
Ok((
row.get::<_, i64>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
row.get::<_, String>(3)?,
row.get::<_, String>(4)?,
))
})?;
let mut symbols = Vec::new();
for row_result in rows {
let (id, name, _kind, path, data_str) = row_result?;
let id = id.to_string();
// Parse data to check if symbol is public
if let Ok(data) = serde_json::from_str::<JsonValue>(&data_str) {
// Symbol is public if it has canonical_fqn or visibility=public
let is_public = data.get("canonical_fqn").is_some()
|| data.get("visibility").and_then(|v| v.as_str()) == Some("public");
if is_public {
let kind_str = data
.get("kind")
.and_then(|v| v.as_str())
.unwrap_or("Unknown");
let start_line =
data.get("start_line").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
let start_col =
data.get("start_col").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
let symbol = Symbol {
id: id.clone(),
name,
kind: Self::parse_symbol_kind(kind_str),
path,
line: start_line,
column: start_col,
};
symbols.push(symbol);
}
}
}
Ok(symbols)
}
fn parse_symbol_kind(kind_str: &str) -> SymbolKind {
match kind_str {
"Function" | "Method" => SymbolKind::Function,
"Struct" => SymbolKind::Struct,
"Enum" => SymbolKind::Enum,
"Trait" => SymbolKind::Trait,
"Module" => SymbolKind::Module,
"Constant" => SymbolKind::Constant,
"TypeAlias" => SymbolKind::TypeAlias,
"Constructor" => SymbolKind::Constructor,
"Impl" => SymbolKind::Impl,
_ => SymbolKind::Variable,
}
}
}