-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathsemcode-lsp.rs
More file actions
471 lines (409 loc) · 17.6 KB
/
semcode-lsp.rs
File metadata and controls
471 lines (409 loc) · 17.6 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// SPDX-License-Identifier: MIT OR Apache-2.0
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::sync::Arc;
use tokio::sync::Mutex;
use tower_lsp_server::jsonrpc::Result as LspResult;
use tower_lsp_server::ls_types::*;
use tower_lsp_server::{Client, LanguageServer, LspService, Server};
use semcode::{database_utils, DatabaseManager};
#[derive(Debug, Default, Deserialize, Serialize)]
struct SemcodeLspConfig {
database_path: Option<String>,
}
pub struct SemcodeLspBackend {
database: Arc<Mutex<Option<DatabaseManager>>>,
config: Arc<Mutex<SemcodeLspConfig>>,
git_sha: Arc<Mutex<Option<String>>>, // Cache the current git commit SHA
git_repo_path: Arc<Mutex<Option<String>>>, // Cache the git repo path for resolving relative paths
}
impl SemcodeLspBackend {
pub fn new(_client: Client) -> Self {
Self {
database: Arc::new(Mutex::new(None)),
config: Arc::new(Mutex::new(SemcodeLspConfig::default())),
git_sha: Arc::new(Mutex::new(None)),
git_repo_path: Arc::new(Mutex::new(None)),
}
}
async fn ensure_database_connection(&self, workspace_uri: Option<&Uri>) -> Result<()> {
let mut db = self.database.lock().await;
if db.is_some() {
return Ok(());
}
// Try to determine database path and git repo path from config or workspace
let config = self.config.lock().await;
let env_db = std::env::var("SEMCODE_DB")
.ok()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty());
let (db_path, git_repo_path) = if let Some(path) = &config.database_path {
// Custom database path provided
let git_repo = std::env::current_dir()
.ok()
.and_then(|p| p.to_str().map(|s| s.to_string()))
.unwrap_or_else(|| ".".to_string());
(path.clone(), git_repo)
} else if let Some(env_path) = env_db {
// SEMCODE_DB environment variable
let git_repo = std::env::current_dir()
.ok()
.and_then(|p| p.to_str().map(|s| s.to_string()))
.unwrap_or_else(|| ".".to_string());
(env_path, git_repo)
} else if let Some(uri) = workspace_uri {
// Use workspace directory (process_database_path will add .semcode.db)
let workspace_path = uri
.to_file_path()
.ok_or_else(|| anyhow::anyhow!("Failed to convert workspace URI to file path"))?;
let workspace_str = workspace_path
.to_str()
.ok_or_else(|| anyhow::anyhow!("Invalid workspace path"))?
.to_string();
// Pass workspace directory directly, let process_database_path add .semcode.db
(workspace_str.clone(), workspace_str)
} else {
// Default to current directory (process_database_path will add .semcode.db)
let current_dir = std::env::current_dir()
.ok()
.and_then(|p| p.to_str().map(|s| s.to_string()))
.unwrap_or_else(|| ".".to_string());
// Pass current directory, let process_database_path add .semcode.db
(current_dir.clone(), current_dir)
};
// Process the database path using semcode's utility function
let processed_path = database_utils::process_database_path(Some(&db_path), None);
if !Path::new(&processed_path).exists() {
return Err(anyhow::anyhow!("Database not found"));
}
match DatabaseManager::new(&processed_path, git_repo_path.clone()).await {
Ok(database_manager) => {
*db = Some(database_manager);
// Get the current git SHA for git-aware lookups
let git_sha = semcode::git::get_git_sha(&git_repo_path).ok().flatten();
*self.git_sha.lock().await = git_sha;
*self.git_repo_path.lock().await = Some(git_repo_path);
Ok(())
}
Err(e) => Err(e),
}
}
/// Rebuild the working directory index to reflect current file state.
async fn refresh_workdir_index(&self) {
let db_guard = self.database.lock().await;
let db = match db_guard.as_ref() {
Some(db) => db,
None => return,
};
let repo_path_guard = self.git_repo_path.lock().await;
let repo_path = match repo_path_guard.as_ref() {
Some(p) => p.clone(),
None => return,
};
drop(repo_path_guard);
let path = std::path::Path::new(&repo_path);
let previous = db.take_workdir_index();
if let Ok(workdir) = semcode::WorkdirIndex::build_incremental(path, previous.as_ref()) {
if !workdir.is_empty() {
db.set_workdir_index(workdir);
}
}
}
async fn find_function_definition(&self, identifier_name: &str) -> Option<Location> {
self.refresh_workdir_index().await;
let db_guard = self.database.lock().await;
let db = db_guard.as_ref()?;
// Try git-aware lookup first if we have a git SHA
let git_sha_guard = self.git_sha.lock().await;
let (func_result, macro_result, type_result, typedef_result) =
if let Some(git_sha) = git_sha_guard.as_ref() {
// Use git-aware lookup to find function, macro, type, and typedef at current commit
let func = db.find_function_git_aware(identifier_name, git_sha).await;
let mac = db.find_function_git_aware(identifier_name, git_sha).await;
let typ = db.find_type_git_aware(identifier_name, git_sha).await;
let typedef = db.find_typedef_git_aware(identifier_name, git_sha).await;
(func, mac, typ, typedef)
} else {
// Fall back to non-git-aware lookup
let func = db.find_function(identifier_name).await;
let mac = db.find_function(identifier_name).await;
let typ = db.find_type(identifier_name).await;
let typedef = db.find_typedef(identifier_name).await;
(func, mac, typ, typedef)
};
drop(git_sha_guard);
// Prioritize: function > macro > type > typedef
let (file_path, line_start) = match (func_result, macro_result, type_result, typedef_result)
{
(Ok(Some(func)), _, _, _) => (func.file_path, func.line_start),
(_, Ok(Some(mac)), _, _) => (mac.file_path, mac.line_start),
(_, _, Ok(Some(typ)), _) => (typ.file_path, typ.line_start),
(_, _, _, Ok(Some(typedef))) => (typedef.file_path, typedef.line_start),
_ => return None,
};
// Convert relative file path to absolute path using git repo path
let repo_path_guard = self.git_repo_path.lock().await;
let absolute_path = if let Some(repo_path) = repo_path_guard.as_ref() {
// Join repo path with relative file path from database
std::path::Path::new(repo_path).join(&file_path)
} else {
// Fallback to relative path (shouldn't happen if database connected)
std::path::PathBuf::from(&file_path)
};
drop(repo_path_guard);
// Convert absolute file path to URI
let file_uri = Uri::from_file_path(&absolute_path)?;
// Create position (LSP uses 0-based line numbers)
let position = Position {
line: line_start.saturating_sub(1),
character: 0,
};
Some(Location {
uri: file_uri,
range: Range {
start: position,
end: position,
},
})
}
async fn find_function_references(&self, function_name: &str) -> Vec<Location> {
self.refresh_workdir_index().await;
let db_guard = self.database.lock().await;
let db = match db_guard.as_ref() {
Some(db) => db,
None => return Vec::new(),
};
// Try git-aware lookup first if we have a git SHA
let git_sha_guard = self.git_sha.lock().await;
let result = if let Some(git_sha) = git_sha_guard.as_ref() {
// Use git-aware lookup to find callers at current commit
db.get_function_callers_git_aware(function_name, git_sha)
.await
} else {
// Fall back to non-git-aware lookup
db.get_function_callers(function_name).await
};
drop(git_sha_guard);
let caller_names = match result {
Ok(callers) => callers,
Err(_) => return Vec::new(),
};
// Get detailed information for each caller
let mut locations = Vec::new();
for caller_name in caller_names {
// Use git-aware lookup to get the caller's location
// Try function first, then macro, then type, then typedef
let git_sha_guard = self.git_sha.lock().await;
let (file_path, line_start) = if let Some(git_sha) = git_sha_guard.as_ref() {
// Try in priority order
if let Ok(Some(func)) = db.find_function_git_aware(&caller_name, git_sha).await {
(func.file_path, func.line_start)
} else if let Ok(Some(mac)) =
db.find_function_git_aware(&caller_name, git_sha).await
{
(mac.file_path, mac.line_start)
} else if let Ok(Some(typ)) = db.find_type_git_aware(&caller_name, git_sha).await {
(typ.file_path, typ.line_start)
} else if let Ok(Some(typedef)) =
db.find_typedef_git_aware(&caller_name, git_sha).await
{
(typedef.file_path, typedef.line_start)
} else {
drop(git_sha_guard);
continue;
}
} else {
// Try in priority order
if let Ok(Some(func)) = db.find_function(&caller_name).await {
(func.file_path, func.line_start)
} else if let Ok(Some(mac)) = db.find_function(&caller_name).await {
(mac.file_path, mac.line_start)
} else if let Ok(Some(typ)) = db.find_type(&caller_name).await {
(typ.file_path, typ.line_start)
} else if let Ok(Some(typedef)) = db.find_typedef(&caller_name).await {
(typedef.file_path, typedef.line_start)
} else {
drop(git_sha_guard);
continue;
}
};
drop(git_sha_guard);
// Convert relative file path to absolute path
let repo_path_guard = self.git_repo_path.lock().await;
let absolute_path = if let Some(repo_path) = repo_path_guard.as_ref() {
std::path::Path::new(repo_path).join(&file_path)
} else {
std::path::PathBuf::from(&file_path)
};
drop(repo_path_guard);
// Convert to URI
if let Some(file_uri) = Uri::from_file_path(&absolute_path) {
let position = Position {
line: line_start.saturating_sub(1),
character: 0,
};
locations.push(Location {
uri: file_uri,
range: Range {
start: position,
end: position,
},
});
}
}
locations
}
// Extract function name from the current position in the document
fn extract_function_name_at_position(text: &str, position: &Position) -> Option<String> {
let lines: Vec<&str> = text.lines().collect();
if position.line as usize >= lines.len() {
return None;
}
let line = lines[position.line as usize];
let character = position.character as usize;
if character > line.len() {
return None;
}
// Find word boundaries around the cursor position
let chars: Vec<char> = line.chars().collect();
if character > chars.len() {
return None;
}
// Find start of identifier
let mut start = character;
while start > 0 && (chars[start - 1].is_alphanumeric() || chars[start - 1] == '_') {
start -= 1;
}
// Find end of identifier
let mut end = character;
while end < chars.len() && (chars[end].is_alphanumeric() || chars[end] == '_') {
end += 1;
}
if start < end {
let identifier: String = chars[start..end].iter().collect();
if !identifier.is_empty()
&& (identifier.chars().next().unwrap().is_alphabetic()
|| identifier.starts_with('_'))
{
return Some(identifier);
}
}
None
}
}
impl LanguageServer for SemcodeLspBackend {
async fn initialize(&self, params: InitializeParams) -> LspResult<InitializeResult> {
// Try to establish database connection using workspace folders (preferred) or root_uri (legacy)
let workspace_uri = params
.workspace_folders
.as_ref()
.and_then(|folders| folders.first())
.map(|f| &f.uri);
#[allow(deprecated)]
let workspace_uri = workspace_uri.or(params.root_uri.as_ref());
let _ = self.ensure_database_connection(workspace_uri).await;
Ok(InitializeResult {
server_info: Some(ServerInfo {
name: "semcode-lsp".to_string(),
version: Some("0.1.0".to_string()),
}),
offset_encoding: None,
capabilities: ServerCapabilities {
definition_provider: Some(OneOf::Left(true)),
references_provider: Some(OneOf::Left(true)),
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::NONE,
)),
..Default::default()
},
})
}
async fn initialized(&self, _: InitializedParams) {}
async fn shutdown(&self) -> LspResult<()> {
Ok(())
}
async fn goto_definition(
&self,
params: GotoDefinitionParams,
) -> LspResult<Option<GotoDefinitionResponse>> {
let uri = ¶ms.text_document_position_params.text_document.uri;
let position = ¶ms.text_document_position_params.position;
// Get the document text to extract the function name
let file_path = match uri.to_file_path() {
Some(p) => p.into_owned(),
None => return Ok(None),
};
let document_text = match std::fs::read_to_string(&file_path) {
Ok(text) => text,
Err(_) => return Ok(None),
};
// Extract function name at the cursor position
let function_name = match Self::extract_function_name_at_position(&document_text, position)
{
Some(name) => name,
None => return Ok(None),
};
// Find the function definition in the database
if let Some(location) = self.find_function_definition(&function_name).await {
Ok(Some(GotoDefinitionResponse::Scalar(location)))
} else {
Ok(None)
}
}
async fn references(&self, params: ReferenceParams) -> LspResult<Option<Vec<Location>>> {
let uri = ¶ms.text_document_position.text_document.uri;
let position = ¶ms.text_document_position.position;
// Get the document text to extract the function name
let file_path = match uri.to_file_path() {
Some(p) => p.into_owned(),
None => return Ok(None),
};
let document_text = match std::fs::read_to_string(&file_path) {
Ok(text) => text,
Err(_) => return Ok(None),
};
// Extract function name at the cursor position
let function_name = match Self::extract_function_name_at_position(&document_text, position)
{
Some(name) => name,
None => return Ok(None),
};
// Find all references (callers) of this function
let locations = self.find_function_references(&function_name).await;
if locations.is_empty() {
Ok(None)
} else {
Ok(Some(locations))
}
}
async fn did_change_configuration(&self, params: DidChangeConfigurationParams) {
// Try to extract semcode-lsp specific config
if let Some(config_obj) = params.settings.get("semcode") {
if let Ok(config) = serde_json::from_value::<SemcodeLspConfig>(config_obj.clone()) {
let mut current_config = self.config.lock().await;
*current_config = config;
// Reconnect database if path changed
let mut db = self.database.lock().await;
*db = None;
drop(db);
// Clear git SHA and repo path caches
let mut git_sha = self.git_sha.lock().await;
*git_sha = None;
drop(git_sha);
let mut repo_path = self.git_repo_path.lock().await;
*repo_path = None;
drop(repo_path);
let _ = self.ensure_database_connection(None).await;
}
}
}
}
#[tokio::main]
async fn main() -> Result<()> {
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let (service, socket) = LspService::new(SemcodeLspBackend::new);
Server::new(stdin, stdout, socket).serve(service).await;
Ok(())
}