Skip to main content

shape_ast/error/parse_error/
suggestions.rs

1//! Suggestion and related information types for error fixes
2
3use crate::error::SourceLocation;
4
5/// A suggestion for fixing the error
6#[derive(Debug, Clone)]
7pub struct Suggestion {
8    /// Human-readable description
9    pub message: String,
10    /// Machine-applicable text edit (if applicable)
11    pub edit: Option<TextEdit>,
12    /// Confidence level
13    pub confidence: SuggestionConfidence,
14}
15
16impl Suggestion {
17    pub fn new(message: impl Into<String>) -> Self {
18        Self {
19            message: message.into(),
20            edit: None,
21            confidence: SuggestionConfidence::Maybe,
22        }
23    }
24
25    pub fn certain(message: impl Into<String>) -> Self {
26        Self {
27            message: message.into(),
28            edit: None,
29            confidence: SuggestionConfidence::Certain,
30        }
31    }
32
33    pub fn likely(message: impl Into<String>) -> Self {
34        Self {
35            message: message.into(),
36            edit: None,
37            confidence: SuggestionConfidence::Likely,
38        }
39    }
40
41    pub fn with_edit(mut self, edit: TextEdit) -> Self {
42        self.edit = Some(edit);
43        self
44    }
45}
46
47/// Text edit for auto-fix
48#[derive(Debug, Clone)]
49pub struct TextEdit {
50    /// Start position (line, column, both 1-based)
51    pub start: (usize, usize),
52    /// End position
53    pub end: (usize, usize),
54    /// Replacement text
55    pub new_text: String,
56}
57
58impl TextEdit {
59    pub fn insert(line: usize, col: usize, text: impl Into<String>) -> Self {
60        Self {
61            start: (line, col),
62            end: (line, col),
63            new_text: text.into(),
64        }
65    }
66
67    pub fn replace(start: (usize, usize), end: (usize, usize), text: impl Into<String>) -> Self {
68        Self {
69            start,
70            end,
71            new_text: text.into(),
72        }
73    }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq)]
77pub enum SuggestionConfidence {
78    Certain, // Definitely correct fix
79    Likely,  // Probably correct
80    Maybe,   // One of several possibilities
81}
82
83/// Related information (e.g., "unclosed brace opened here")
84#[derive(Debug, Clone)]
85pub struct RelatedInfo {
86    /// Location of related code
87    pub location: SourceLocation,
88    /// Description
89    pub message: String,
90}
91
92impl RelatedInfo {
93    pub fn new(message: impl Into<String>, location: SourceLocation) -> Self {
94        Self {
95            location,
96            message: message.into(),
97        }
98    }
99}