Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): code action for unused variable #5214

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions crates/cairo-lang-diagnostics/src/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ impl ErrorCode {
/// Format this error code in a way that is suitable for display in error message.
///
/// ```
/// # use cairo_lang_diagnostics::error_code;
/// assert_eq!(error_code!(E0001).display_bracketed(), "[E0001]");
/// # use cairo_lang_diagnostics::{codes, error_code};
/// assert_eq!(error_code!(codes::UNUSED_VARIABLE).display_bracketed(), "[E0001]");
/// ```
pub fn display_bracketed(self) -> String {
format!("[{}]", self)
Expand All @@ -39,16 +39,16 @@ impl fmt::Display for ErrorCode {
/// Constructs an [`ErrorCode`].
///
/// ```
/// # use cairo_lang_diagnostics::{error_code, ErrorCode};
/// let code: ErrorCode = error_code!(E0001);
/// # use cairo_lang_diagnostics::{codes, error_code, ErrorCode};
/// let code: ErrorCode = error_code!(codes::UNUSED_VARIABLE);
/// # assert_eq!(format!("{code}"), "E0001");
/// ```
#[macro_export]
macro_rules! error_code {
($code:expr) => {{
use $crate::ErrorCode;
// NOTE: This is a magic trick to trigger the validation in compile time.
const ENSURE_CONST: ErrorCode = ErrorCode::new(stringify!($code));
const ENSURE_CONST: ErrorCode = ErrorCode::new($code);
ENSURE_CONST
}};
}
Expand All @@ -62,11 +62,16 @@ impl OptionErrorCodeExt for Option<ErrorCode> {
/// Format this error code in a way that is suitable for display in error message.
///
/// ```
/// # use cairo_lang_diagnostics::{error_code, OptionErrorCodeExt};
/// assert_eq!(Some(error_code!(E0001)).display_bracketed(), "[E0001]");
/// # use cairo_lang_diagnostics::{codes, error_code, OptionErrorCodeExt};
/// assert_eq!(Some(error_code!(codes::UNUSED_VARIABLE)).display_bracketed(), "[E0001]");
/// assert_eq!(None.display_bracketed(), "");
/// ```
fn display_bracketed(self) -> String {
self.map(ErrorCode::display_bracketed).unwrap_or_default()
}
}

pub mod codes {
/// Unused variable
pub const UNUSED_VARIABLE: &str = "E0001";
}
2 changes: 1 addition & 1 deletion crates/cairo-lang-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use diagnostics::{
DiagnosticNote, Diagnostics, DiagnosticsBuilder, FormattedDiagnosticEntry, Maybe, Severity,
ToMaybe, ToOption,
};
pub use error_code::{ErrorCode, OptionErrorCodeExt};
pub use error_code::{codes, ErrorCode, OptionErrorCodeExt};
pub use location_marks::get_location_marks;

mod diagnostics;
Expand Down
58 changes: 57 additions & 1 deletion crates/cairo-lang-language-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use cairo_lang_defs::ids::{
TraitItemId, TraitLongId, UseLongId,
};
use cairo_lang_diagnostics::{
DiagnosticEntry, DiagnosticLocation, Diagnostics, Severity, ToOption,
codes, DiagnosticEntry, DiagnosticLocation, Diagnostics, Severity, ToOption,
};
use cairo_lang_filesystem::cfg::{Cfg, CfgSet};
use cairo_lang_filesystem::db::{
Expand Down Expand Up @@ -619,6 +619,7 @@ impl LanguageServer for Backend {
document_formatting_provider: Some(OneOf::Left(true)),
hover_provider: Some(HoverProviderCapability::Simple(true)),
definition_provider: Some(OneOf::Left(true)),
code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
..ServerCapabilities::default()
},
})
Expand Down Expand Up @@ -932,6 +933,60 @@ impl LanguageServer for Backend {
})
.await
}

#[tracing::instrument(level = "debug", skip_all, fields(uri = %params.text_document.uri))]
async fn code_action(&self, params: CodeActionParams) -> LSPResult<Option<CodeActionResponse>> {
eprintln!("Code action");
self.with_db(|db| {
let mut actions = Vec::with_capacity(params.context.diagnostics.len());
let file_id = file(db, params.text_document.uri.clone());
let (node, _lookup_items) = get_node_and_lookup_items(db, file_id, params.range.start)?;
for diagnostic in params.context.diagnostics.iter() {
let action = if let Some(NumberOrString::String(code)) = &diagnostic.code {
match code.as_str() {
codes::UNUSED_VARIABLE => unused_variable(
db,
&node,
diagnostic.clone(),
params.text_document.uri.clone(),
),
_ => CodeAction::default(),
}
} else {
CodeAction::default()
};
actions.push(CodeActionOrCommand::from(action));
}
Some(actions)
})
.await
}
}

/// Create a code action that prefixes an unused variable with an `_`.
#[tracing::instrument(level = "trace", skip_all)]
fn unused_variable(
db: &dyn SemanticGroup,
node: &SyntaxNode,
diagnostic: Diagnostic,
uri: Url,
) -> CodeAction {
CodeAction {
title: format!("Rename to `_{}`", node.get_text(db.upcast())),
edit: Some(WorkspaceEdit {
changes: Some(HashMap::from_iter([(
uri,
// The diagnostic range is just the first char of the variable name so we can just
// pass an underscore as the new text it won't replace the current variable name
// and it will prefix it with `_`
vec![TextEdit { range: diagnostic.range, new_text: "_".to_owned() }],
)])),
document_changes: None,
change_annotations: None,
}),
diagnostics: Some(vec![diagnostic]),
..Default::default()
}
}

#[tracing::instrument(level = "trace", skip_all)]
Expand Down Expand Up @@ -1601,6 +1656,7 @@ fn get_diagnostics<T: DiagnosticEntry>(
Severity::Error => DiagnosticSeverity::ERROR,
Severity::Warning => DiagnosticSeverity::WARNING,
}),
code: diagnostic.error_code().map(|code| NumberOrString::String(code.to_string())),
..Diagnostic::default()
});
}
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cairo_lang_defs::ids::{
};
use cairo_lang_defs::plugin::PluginDiagnostic;
use cairo_lang_diagnostics::{
error_code, DiagnosticAdded, DiagnosticEntry, DiagnosticLocation, Diagnostics,
codes, error_code, DiagnosticAdded, DiagnosticEntry, DiagnosticLocation, Diagnostics,
DiagnosticsBuilder, ErrorCode, Severity,
};
use cairo_lang_filesystem::ids::FileId;
Expand Down Expand Up @@ -1099,7 +1099,7 @@ pub enum SemanticDiagnosticKind {
impl SemanticDiagnosticKind {
pub fn error_code(&self) -> Option<ErrorCode> {
Some(match &self {
Self::UnusedVariable => error_code!(E0001),
Self::UnusedVariable => error_code!(codes::UNUSED_VARIABLE),
_ => return None,
})
}
Expand Down
Loading