feat(lsp): implement quickfix for utf8 byte array is valid hex string warning

This commit is contained in:
rvcas 2023-11-28 21:02:15 -05:00
parent 858a9621fc
commit 1503b525b2
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
2 changed files with 34 additions and 1 deletions

View File

@ -1618,8 +1618,8 @@ impl ExtraData for Warning {
| Warning::UnusedType { .. }
| Warning::UnusedVariable { .. }
| Warning::DiscardedLetAssignment { .. }
| Warning::Utf8ByteArrayIsValidHexString { .. }
| Warning::ValidatorInLibraryModule { .. } => None,
Warning::Utf8ByteArrayIsValidHexString { value, .. } => Some(value.clone()),
Warning::UnusedImportedModule { location, .. } => {
Some(format!("{},{}", false, location.start))
}

View File

@ -10,6 +10,8 @@ const UNKNOWN_CONSTRUCTOR: &str = "aiken::check::unknown::type_constructor";
const UNKNOWN_MODULE: &str = "aiken::check::unknown::module";
const UNUSED_IMPORT_VALUE: &str = "aiken::check::unused:import::value";
const UNUSED_IMPORT_MODULE: &str = "aiken::check::unused::import::module";
const UTF8_BYTE_ARRAY_IS_VALID_HEX_STRING: &str =
"aiken::check::syntax::bytearray_literal_is_hex_string";
/// Errors for which we can provide quickfixes
#[allow(clippy::enum_variant_names)]
@ -18,6 +20,7 @@ pub enum Quickfix {
UnknownModule(lsp_types::Diagnostic),
UnknownConstructor(lsp_types::Diagnostic),
UnusedImports(Vec<lsp_types::Diagnostic>),
Utf8ByteArrayIsValidHexString(lsp_types::Diagnostic),
}
fn match_code(
@ -54,6 +57,14 @@ pub fn assert(diagnostic: lsp_types::Diagnostic) -> Option<Quickfix> {
return Some(Quickfix::UnusedImports(vec![diagnostic]));
}
if match_code(
&diagnostic,
Severity::WARNING,
UTF8_BYTE_ARRAY_IS_VALID_HEX_STRING,
) {
return Some(Quickfix::Utf8ByteArrayIsValidHexString(diagnostic));
}
None
}
@ -99,6 +110,12 @@ pub fn quickfix(
.collect(),
),
),
Quickfix::Utf8ByteArrayIsValidHexString(diagnostic) => each_as_distinct_action(
&mut actions,
text_document,
diagnostic,
utf8_byte_array_is_hex_string(diagnostic),
),
};
}
@ -252,3 +269,19 @@ fn unused_imports(
edits
}
fn utf8_byte_array_is_hex_string(diagnostic: &lsp_types::Diagnostic) -> Vec<AnnotatedEdit> {
let mut edits = Vec::new();
if let Some(serde_json::Value::String(ref value)) = diagnostic.data.as_ref() {
edits.push((
"Prefix with #".to_string(),
lsp_types::TextEdit {
range: diagnostic.range,
new_text: format!("#\"{value}\""),
},
))
}
edits
}