Skip to main content

perl_lsp_tooling/perl_critic/
quick_fix.rs

1use crate::perl_critic::{Violation, insertion_range};
2use perl_parser_core::position::Range;
3
4/// A quick fix for a violation
5#[derive(Debug, Clone)]
6pub struct QuickFix {
7    /// Human-readable title describing the fix action
8    pub title: String,
9    /// The text edit to apply as a fix
10    pub edit: TextEdit,
11}
12
13/// A text edit
14#[derive(Debug, Clone)]
15pub struct TextEdit {
16    /// The range of text to replace
17    pub range: Range,
18    /// The replacement text (empty string for deletion)
19    pub new_text: String,
20}
21
22#[cfg(feature = "lsp-compat")]
23pub(crate) fn perlcritic_quick_fix(violation: &Violation) -> Option<QuickFix> {
24    match violation.policy.as_str() {
25        "Variables::ProhibitUnusedVariables" => Some(QuickFix {
26            title: "Remove unused variable".to_string(),
27            edit: TextEdit { range: violation.range, new_text: String::new() },
28        }),
29        "Subroutines::ProhibitUnusedPrivateSubroutines" => Some(QuickFix {
30            title: "Remove unused subroutine".to_string(),
31            edit: TextEdit { range: violation.range, new_text: String::new() },
32        }),
33        "TestingAndDebugging::RequireUseStrict" => Some(use_statement_fix("strict")),
34        "TestingAndDebugging::RequireUseWarnings" => Some(use_statement_fix("warnings")),
35        _ => None,
36    }
37}
38
39pub(crate) fn built_in_quick_fix(violation: &Violation) -> Option<QuickFix> {
40    match violation.policy.as_str() {
41        "TestingAndDebugging::RequireUseStrict" => Some(use_statement_fix("strict")),
42        "TestingAndDebugging::RequireUseWarnings" => Some(use_statement_fix("warnings")),
43        _ => None,
44    }
45}
46
47fn use_statement_fix(feature: &str) -> QuickFix {
48    QuickFix {
49        title: format!("Add 'use {feature}'"),
50        edit: TextEdit { range: insertion_range(), new_text: format!("use {feature};\n") },
51    }
52}