Skip to content

Commit 36b5881

Browse files
committed
fix code Args
1 parent cd17180 commit 36b5881

File tree

7 files changed

+89
-54
lines changed

7 files changed

+89
-54
lines changed

crates/chat-cli/src/cli/chat/tools/code.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub struct SearchSymbolsParams {
3737

3838
#[derive(Debug, Clone, Deserialize)]
3939
pub struct FindReferencesParams {
40-
pub symbol_name: String,
4140
pub file_path: String,
4241
pub row: i32,
4342
pub column: i32,
@@ -284,11 +283,11 @@ impl Code {
284283
if references.is_empty() {
285284
queue!(
286285
_stdout,
287-
style::Print("\n🔗 No references found for \""),
286+
style::Print("\n🔗 No references found at "),
288287
StyledText::warning_fg(),
289-
style::Print(&params.symbol_name),
288+
style::Print(&format!("{}:{}:{}", params.file_path, params.row, params.column)),
290289
StyledText::reset(),
291-
style::Print("\"\n"),
290+
style::Print("\n"),
292291
)?;
293292
} else {
294293
queue!(
@@ -297,11 +296,11 @@ impl Code {
297296
StyledText::success_fg(),
298297
style::Print(&references.len().to_string()),
299298
StyledText::reset(),
300-
style::Print(" reference(s) to \""),
299+
style::Print(" reference(s) at "),
301300
StyledText::success_fg(),
302-
style::Print(&params.symbol_name),
301+
style::Print(&format!("{}:{}:{}", params.file_path, params.row, params.column)),
303302
StyledText::reset(),
304-
style::Print("\":\n"),
303+
style::Print(":\n"),
305304
)?;
306305
for (i, reference) in references.iter().enumerate() {
307306
queue!(
@@ -500,11 +499,7 @@ impl Code {
500499
Code::FindReferences(params) => {
501500
queue!(
502501
output,
503-
style::Print("🔗 Finding references for "),
504-
StyledText::success_fg(),
505-
style::Print(&format!("\"{}\"", params.symbol_name)),
506-
StyledText::reset(),
507-
style::Print(" at "),
502+
style::Print("🔗 Finding references at "),
508503
StyledText::info_fg(),
509504
style::Print(&format!("{}:{}:{}", params.file_path, params.row, params.column)),
510505
StyledText::reset(),

crates/chat-cli/src/cli/chat/tools/tool_index.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@
458458
"find_references",
459459
"goto_definition",
460460
"rename_symbol",
461-
"format_code",
462-
"workspace_outline",
461+
"format",
463462
"get_document_symbols",
464463
"lookup_symbols",
465464
"initialize_workspace"
@@ -468,7 +467,7 @@
468467
},
469468
"symbol_name": {
470469
"type": "string",
471-
"description": "Name of symbol to search for (required for search_symbols and find_references operations)"
470+
"description": "Name of symbol to search for (required for search_symbols operation)"
472471
},
473472
"file_path": {
474473
"type": "string",

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use code_agent_sdk::{
66
};
77
use code_agent_sdk::model::types::ApiSymbolKind;
88
use std::path::PathBuf;
9+
use std::str::FromStr;
910

1011
#[derive(Parser)]
1112
#[command(name = "code-agent-cli")]
@@ -111,7 +112,7 @@ async fn main() -> anyhow::Result<()> {
111112
let request = FindSymbolsRequest {
112113
symbol_name: name,
113114
file_path: file,
114-
symbol_type: symbol_type.and_then(|s| ApiSymbolKind::from_str(&s)),
115+
symbol_type: symbol_type.and_then(|s| ApiSymbolKind::from_str(&s).ok()),
115116
limit: None, // Use default 20
116117
exact_match: false, // Enable fuzzy matching
117118
};
@@ -288,12 +289,14 @@ async fn main() -> anyhow::Result<()> {
288289
insert_spaces,
289290
};
290291

291-
let has_changes = code_intel.format_code(request).await?;
292+
let edit_count = code_intel.format_code(request).await?;
292293

293-
if !has_changes {
294+
if edit_count == 0 {
294295
println!("No formatting changes needed");
295296
} else {
296-
println!("✅ File formatted successfully");
297+
// Count unique lines affected by calculating from edit count
298+
println!("Applied formatting to {} lines", edit_count);
299+
println!("✅ Formatting applied successfully");
297300
}
298301
}
299302

crates/code-agent-sdk/src/mcp/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ impl CodeIntelligenceServer {
865865
)
866866
})?;
867867

868-
let message = if edits {
869-
"Code formatted successfully".to_string()
868+
let message = if edits > 0 {
869+
format!("Code formatted successfully ({} edits applied)", edits)
870870
} else {
871871
"No formatting changes needed".to_string()
872872
};

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,10 @@ impl CodeIntelligence {
492492
pub async fn format_code(
493493
&mut self,
494494
request: FormatCodeRequest,
495-
) -> Result<bool> {
496-
let edits = self.coding_service
495+
) -> Result<usize> {
496+
self.coding_service
497497
.format_code(&mut self.workspace_manager, request)
498-
.await?;
499-
500-
Ok(!edits.is_empty())
498+
.await
501499
}
502500

503501
// File operations - delegate to WorkspaceService

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait CodingService: Send + Sync {
2222
&self,
2323
workspace_manager: &mut WorkspaceManager,
2424
request: FormatCodeRequest,
25-
) -> Result<Vec<TextEdit>>;
25+
) -> Result<usize>;
2626
}
2727

2828
/// LSP-based implementation of CodingService
@@ -79,29 +79,35 @@ impl CodingService for LspCodingService {
7979
let params = RenameParams {
8080
text_document_position: TextDocumentPositionParams {
8181
text_document: TextDocumentIdentifier { uri },
82-
position: crate::utils::to_lsp_position(request.row, request.column),
82+
position: Position {
83+
line: request.row,
84+
character: request.column,
85+
},
8386
},
8487
new_name: request.new_name.clone(),
8588
work_done_progress_params: Default::default(),
8689
};
8790

88-
let workspace_edit = client.rename(params).await?;
91+
let result = client.rename(params).await;
8992

90-
// Apply workspace edit using LSP batch operations if not dry-run
91-
if let Some(ref edit) = workspace_edit {
92-
if !request.dry_run {
93-
client.apply_workspace_edit(edit).await?;
93+
// Apply edits if not dry_run
94+
if !request.dry_run {
95+
if let Ok(Some(ref workspace_edit)) = result {
96+
use crate::utils::apply_workspace_edit;
97+
if let Err(e) = apply_workspace_edit(workspace_edit) {
98+
tracing::trace!("Failed to apply workspace edit: {}", e);
99+
}
94100
}
95101
}
96102

97-
Ok(workspace_edit)
103+
result
98104
}
99105

100106
async fn format_code(
101107
&self,
102108
workspace_manager: &mut WorkspaceManager,
103109
request: FormatCodeRequest,
104-
) -> Result<Vec<TextEdit>> {
110+
) -> Result<usize> {
105111
// Ensure initialized
106112
if !workspace_manager.is_initialized() {
107113
workspace_manager.initialize().await?;
@@ -125,12 +131,12 @@ impl CodingService for LspCodingService {
125131
)
126132
})?;
127133

128-
let uri = Url::from_file_path(&canonical_path).map_err(|_| {
129-
anyhow::anyhow!("Invalid file path: {}", canonical_path.display())
130-
})?;
131-
132134
let params = DocumentFormattingParams {
133-
text_document: TextDocumentIdentifier { uri: uri.clone() },
135+
text_document: TextDocumentIdentifier {
136+
uri: Url::from_file_path(&canonical_path).map_err(|_| {
137+
anyhow::anyhow!("Invalid file path: {}", canonical_path.display())
138+
})?,
139+
},
134140
options: FormattingOptions {
135141
tab_size: request.tab_size,
136142
insert_spaces: request.insert_spaces,
@@ -147,25 +153,18 @@ impl CodingService for LspCodingService {
147153
.await?
148154
.unwrap_or_default();
149155

150-
// Apply formatting edits using LSP batch operations
156+
let edit_count = edits.len();
157+
158+
// Apply formatting edits to the actual file
151159
if !edits.is_empty() {
152-
let mut changes = std::collections::HashMap::new();
153-
changes.insert(uri, edits.clone());
154-
155-
let workspace_edit = WorkspaceEdit {
156-
changes: Some(changes),
157-
document_changes: None,
158-
change_annotations: None,
159-
};
160-
161-
client.apply_workspace_edit(&workspace_edit).await?;
160+
use crate::utils::apply_text_edits;
161+
apply_text_edits(&canonical_path, &edits)?;
162162
}
163163

164-
Ok(edits)
164+
Ok(edit_count)
165165
} else {
166166
// Format workspace - not commonly supported by LSPs
167-
// Return empty edits for now
168-
Ok(Vec::new())
167+
Ok(0)
169168
}
170169
}
171170
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn apply_workspace_edit(workspace_edit: &WorkspaceEdit) -> Result<()> {
7878
let mut applied_files = Vec::new();
7979
let mut failed_files = Vec::new();
8080

81+
// Handle changes field
8182
if let Some(changes) = &workspace_edit.changes {
8283
for (uri, edits) in changes {
8384
let file_path = Path::new(uri.path());
@@ -104,6 +105,46 @@ pub fn apply_workspace_edit(workspace_edit: &WorkspaceEdit) -> Result<()> {
104105
}
105106
}
106107
}
108+
109+
// Handle document_changes field
110+
if let Some(document_changes) = &workspace_edit.document_changes {
111+
match document_changes {
112+
lsp_types::DocumentChanges::Edits(edits) => {
113+
for edit in edits {
114+
let file_path = Path::new(edit.text_document.uri.path());
115+
116+
// Validate file exists and is writable
117+
if !file_path.exists() {
118+
failed_files.push((file_path.to_path_buf(), "File does not exist".to_string()));
119+
continue;
120+
}
121+
122+
if file_path.metadata()?.permissions().readonly() {
123+
failed_files.push((file_path.to_path_buf(), "File is read-only".to_string()));
124+
continue;
125+
}
126+
127+
let text_edits: Vec<TextEdit> = edit.edits.iter().map(|e| match e {
128+
lsp_types::OneOf::Left(text_edit) => text_edit.clone(),
129+
lsp_types::OneOf::Right(annotated_edit) => annotated_edit.text_edit.clone(),
130+
}).collect();
131+
132+
match apply_text_edits(file_path, &text_edits) {
133+
Ok(()) => {
134+
applied_files.push(file_path.to_path_buf());
135+
}
136+
Err(e) => {
137+
failed_files.push((file_path.to_path_buf(), e.to_string()));
138+
}
139+
}
140+
}
141+
}
142+
lsp_types::DocumentChanges::Operations(_) => {
143+
// Resource operations not yet supported - log as trace
144+
tracing::trace!("Resource operations not yet supported in workspace edit");
145+
}
146+
}
147+
}
107148

108149
// Report results
109150
if !failed_files.is_empty() {

0 commit comments

Comments
 (0)