Skip to content

Commit 2b471e6

Browse files
committed
Rename works...Again
1 parent 36b5881 commit 2b471e6

File tree

6 files changed

+100
-12
lines changed

6 files changed

+100
-12
lines changed

crates/code-agent-sdk/src/cli/cli.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use clap::{Parser, Subcommand};
22
use code_agent_sdk::{
33
CodeIntelligence, FindReferencesByLocationRequest, FindReferencesByNameRequest,
44
FindSymbolsRequest, GetDocumentSymbolsRequest, GotoDefinitionRequest,
5-
RenameSymbolRequest, FormatCodeRequest,
5+
RenameSymbolRequest, FormatCodeRequest, OpenFileRequest,
66
};
77
use code_agent_sdk::model::types::ApiSymbolKind;
8+
use code_agent_sdk::utils::logging;
89
use std::path::PathBuf;
910
use std::str::FromStr;
1011

@@ -92,6 +93,13 @@ enum Commands {
9293

9394
#[tokio::main]
9495
async fn main() -> anyhow::Result<()> {
96+
// Initialize file logging for debugging
97+
if let Err(e) = logging::init_file_logging() {
98+
eprintln!("Warning: Failed to initialize logging: {}", e);
99+
} else {
100+
println!("📝 Logging enabled to code_intelligence.log");
101+
}
102+
95103
let cli = Cli::parse();
96104

97105
// Auto-detect workspace and initialize
@@ -245,6 +253,17 @@ async fn main() -> anyhow::Result<()> {
245253
new_name,
246254
dry_run,
247255
} => {
256+
// Open the file first to ensure LSP server processes it
257+
let content = std::fs::read_to_string(&file)?;
258+
code_intel.open_file(OpenFileRequest {
259+
file_path: file.clone(),
260+
content,
261+
}).await?;
262+
263+
// Wait for LSP server to process the file
264+
println!("⏳ Waiting for LSP server to process file...");
265+
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
266+
248267
let request = RenameSymbolRequest {
249268
file_path: file.clone(),
250269
row,

crates/code-agent-sdk/src/lsp/client.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ impl LspClient {
155155
/// # Returns
156156
/// * `Result<Option<WorkspaceEdit>>` - Workspace changes or None
157157
pub async fn rename(&self, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
158-
self.send_lsp_request("textDocument/rename", params).await
158+
tracing::trace!("LSP rename request: method=textDocument/rename, params={:?}", params);
159+
let result = self.send_lsp_request("textDocument/rename", params).await;
160+
tracing::trace!("LSP rename response: {:?}", result);
161+
result
159162
}
160163

161164
/// Format a document
@@ -197,18 +200,27 @@ impl LspClient {
197200
T: serde::Serialize,
198201
R: serde::de::DeserializeOwned,
199202
{
203+
tracing::trace!("Sending LSP request: method={}, params={:?}", method, serde_json::to_value(&params)?);
204+
200205
let (tx, rx) = oneshot::channel();
201206

202207
self.send_request(method, json!(params), move |result| {
208+
tracing::trace!("LSP request callback received result: {:?}", result);
203209
let _ = tx.send(result);
204210
})
205211
.await?;
206212

213+
tracing::trace!("Waiting for LSP response...");
207214
let result = rx.await??;
215+
tracing::trace!("Raw LSP response: {:?}", result);
216+
208217
if result.is_null() {
218+
tracing::trace!("LSP response is null, returning None");
209219
Ok(None)
210220
} else {
211-
Ok(Some(serde_json::from_value(result)?))
221+
let parsed: R = serde_json::from_value(result)?;
222+
tracing::trace!("Successfully parsed LSP response");
223+
Ok(Some(parsed))
212224
}
213225
}
214226

crates/code-agent-sdk/src/model/entities.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,29 @@ pub struct RenameResult {
2727
impl RenameResult {
2828
/// Create from LSP WorkspaceEdit
2929
pub fn from_lsp_workspace_edit(edit: &lsp_types::WorkspaceEdit) -> Self {
30-
let file_count = edit.changes.as_ref().map(|c| c.len()).unwrap_or(0);
31-
let edit_count = edit.changes.as_ref()
32-
.map(|changes| changes.values().map(|edits| edits.len()).sum())
33-
.unwrap_or(0);
30+
let mut file_count = 0;
31+
let mut edit_count = 0;
32+
33+
// Handle changes field
34+
if let Some(changes) = &edit.changes {
35+
file_count += changes.len();
36+
edit_count += changes.values().map(|edits| edits.len()).sum::<usize>();
37+
}
38+
39+
// Handle document_changes field
40+
if let Some(document_changes) = &edit.document_changes {
41+
match document_changes {
42+
lsp_types::DocumentChanges::Edits(edits) => {
43+
file_count += edits.len();
44+
edit_count += edits.iter().map(|edit| edit.edits.len()).sum::<usize>();
45+
}
46+
lsp_types::DocumentChanges::Operations(_) => {
47+
// Operations like create/rename/delete files
48+
file_count += 1;
49+
edit_count += 1;
50+
}
51+
}
52+
}
3453

3554
Self { file_count, edit_count }
3655
}

crates/code-agent-sdk/src/sdk/services/coding_service.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,47 +57,60 @@ impl CodingService for LspCodingService {
5757
workspace_manager: &mut WorkspaceManager,
5858
request: RenameSymbolRequest,
5959
) -> Result<Option<WorkspaceEdit>> {
60+
tracing::trace!("Starting rename_symbol: file={:?}, row={}, col={}, new_name={}",
61+
request.file_path, request.row, request.column, request.new_name);
62+
6063
// Ensure initialized
6164
if !workspace_manager.is_initialized() {
65+
tracing::trace!("Workspace not initialized, initializing...");
6266
workspace_manager.initialize().await?;
6367
}
6468

6569
let canonical_path = canonicalize_path(&request.file_path)?;
70+
tracing::trace!("Canonical path: {:?}", canonical_path);
71+
6672
let content = std::fs::read_to_string(&canonical_path)?;
73+
tracing::trace!("File content length: {} bytes", content.len());
74+
6775
self.workspace_service
6876
.open_file(workspace_manager, &canonical_path, content)
6977
.await?;
78+
tracing::trace!("File opened in workspace");
7079

7180
let client = workspace_manager
7281
.get_client_for_file(&canonical_path)
7382
.await?
7483
.ok_or_else(|| anyhow::anyhow!("No language server for file"))?;
84+
tracing::trace!("Got LSP client for file");
7585

7686
let uri = Url::from_file_path(&canonical_path)
7787
.map_err(|_| anyhow::anyhow!("Invalid file path"))?;
88+
tracing::trace!("File URI: {}", uri);
7889

7990
let params = RenameParams {
8091
text_document_position: TextDocumentPositionParams {
81-
text_document: TextDocumentIdentifier { uri },
82-
position: Position {
83-
line: request.row,
84-
character: request.column,
85-
},
92+
text_document: TextDocumentIdentifier { uri: uri.clone() },
93+
position: crate::utils::position::to_lsp_position(request.row, request.column),
8694
},
8795
new_name: request.new_name.clone(),
8896
work_done_progress_params: Default::default(),
8997
};
98+
tracing::trace!("Sending rename request to LSP: {:?}", params);
9099

91100
let result = client.rename(params).await;
101+
tracing::trace!("LSP rename result: {:?}", result);
92102

93103
// Apply edits if not dry_run
94104
if !request.dry_run {
95105
if let Ok(Some(ref workspace_edit)) = result {
106+
tracing::trace!("Applying workspace edit (not dry-run)");
96107
use crate::utils::apply_workspace_edit;
97108
if let Err(e) = apply_workspace_edit(workspace_edit) {
98109
tracing::trace!("Failed to apply workspace edit: {}", e);
99110
}
100111
}
112+
} else {
113+
tracing::trace!("Dry-run mode, not applying edits");
101114
}
102115

103116
result
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::fs::OpenOptions;
2+
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
3+
4+
/// Initialize logging to file with trace level for LSP operations
5+
pub fn init_file_logging() -> anyhow::Result<()> {
6+
let log_file = OpenOptions::new()
7+
.create(true)
8+
.append(true)
9+
.open("code_intelligence.log")?;
10+
11+
tracing_subscriber::registry()
12+
.with(
13+
fmt::layer()
14+
.with_writer(log_file)
15+
.with_ansi(false)
16+
.with_target(true)
17+
.with_thread_ids(true)
18+
)
19+
.with(EnvFilter::from_default_env().add_directive("code_agent_sdk=trace".parse()?))
20+
.try_init()
21+
.map_err(|e| anyhow::anyhow!("Failed to initialize logging: {}", e))?;
22+
23+
Ok(())
24+
}

crates/code-agent-sdk/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
pub mod file;
77
pub mod fuzzy_search;
88
pub mod position;
9+
pub mod logging;
910

1011
// Re-export commonly used functions for convenience
1112
pub use file::{apply_text_edits, apply_workspace_edit, canonicalize_path, ensure_absolute_path};

0 commit comments

Comments
 (0)