Skip to content

Commit dafdcf2

Browse files
committed
refactor(code_action): handle serialisation errors
and avoid to_string_lossy by using PathBuf. On serialisation invalid utf-8 paths will produce a serialisation error.
1 parent 4234c79 commit dafdcf2

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

crates/typos-lsp/src/lsp.rs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct DiagnosticData<'c> {
3131
struct IgnoreInProjectCommandArguments {
3232
typo: String,
3333
/// The configuration file that should be modified to ignore the typo
34-
config_file_path: String,
34+
config_file_path: PathBuf,
3535
}
3636

3737
impl LanguageServer for Backend<'static, 'static> {
@@ -225,35 +225,38 @@ impl LanguageServer for Backend<'static, 'static> {
225225
.router
226226
.at(&uri_path)
227227
{
228-
suggestions.push(CodeActionOrCommand::Command(Command {
229-
title: format!("Ignore `{}` in the project", typo),
230-
command: IGNORE_IN_PROJECT.to_string(),
231-
arguments: Some(
232-
[serde_json::to_value(IgnoreInProjectCommandArguments {
233-
typo: typo.to_string(),
234-
config_file_path: instance
235-
.config_file
236-
.to_string_lossy()
237-
.to_string(),
238-
})
239-
.unwrap()]
240-
.into(),
241-
),
242-
}));
228+
match serde_json::to_value(IgnoreInProjectCommandArguments {
229+
typo: typo.to_string(),
230+
config_file_path: instance.config_file.clone(),
231+
}) {
232+
Ok(args) => {
233+
suggestions.push(CodeActionOrCommand::Command(Command {
234+
title: format!("Ignore `{}` in the project", typo),
235+
command: IGNORE_IN_PROJECT.to_string(),
236+
arguments: Some(vec![args]),
237+
}));
238+
}
239+
Err(e) => {
240+
tracing::error!("Failed to serialize arguments: {}", e);
241+
}
242+
}
243243

244244
if let Some(explicit_config) = &instance.custom_config {
245-
suggestions.push(CodeActionOrCommand::Command(Command {
246-
title: format!("Ignore `{}` in the configuration file", typo),
247-
command: IGNORE_IN_PROJECT.to_string(),
248-
arguments: Some(
249-
[serde_json::to_value(IgnoreInProjectCommandArguments {
250-
typo: typo.to_string(),
251-
config_file_path: explicit_config.to_string_lossy().to_string(),
252-
})
253-
.unwrap()]
254-
.into(),
255-
),
256-
}));
245+
match serde_json::to_value(IgnoreInProjectCommandArguments {
246+
typo: typo.to_string(),
247+
config_file_path: explicit_config.clone(),
248+
}) {
249+
Ok(args) => {
250+
suggestions.push(CodeActionOrCommand::Command(Command {
251+
title: format!("Ignore `{}` in the configuration file", typo),
252+
command: IGNORE_IN_PROJECT.to_string(),
253+
arguments: Some(vec![args]),
254+
}));
255+
}
256+
Err(e) => {
257+
tracing::error!("Failed to serialize arguments: {}", e);
258+
}
259+
}
257260
}
258261
} else {
259262
tracing::warn!(
@@ -305,8 +308,7 @@ impl LanguageServer for Backend<'static, 'static> {
305308
..
306309
}) = serde_json::from_value::<IgnoreInProjectCommandArguments>(argument)
307310
{
308-
crate::config::add_ignore(PathBuf::from(config_file_path).as_path(), &typo)
309-
.unwrap();
311+
crate::config::add_ignore(&config_file_path, &typo).unwrap();
310312
// reload the instance so new ignore takes effect
311313
self.state.lock().unwrap().update_router().unwrap();
312314
};

crates/typos-lsp/tests/integration_test.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,6 @@ async fn test_execute_command_ignore_in_project() {
204204
let config_path = temp_dir.path().join("typos.toml");
205205

206206
let workspace_folder_uri = Uri::from_file_path(temp_dir.path()).unwrap();
207-
let config_uri_str = config_path.to_string_lossy();
208-
209207
let mut server = TestServer::new();
210208
let _ = server
211209
.request(&initialize_with(Some(&workspace_folder_uri), None, None))
@@ -218,7 +216,7 @@ async fn test_execute_command_ignore_in_project() {
218216
"command": "ignore-in-project",
219217
"arguments": [{
220218
"typo": "foobar",
221-
"config_file_path": config_uri_str
219+
"config_file_path": config_path
222220
}]
223221
},
224222
"id": 2

0 commit comments

Comments
 (0)