Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions malva/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub enum Error {
/// and the second component is error line number,
/// and the third component is error column number.
Parser(raffia::error::Error, usize, usize),

/// The specified range is outside of the source file bounds.
RangeOutOfBounds {
range: std::ops::Range<usize>,
source_len: usize,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source_len: usize,
total: usize,

},
}

impl Display for Error {
Expand All @@ -16,6 +22,13 @@ impl Display for Error {
Error::Parser(error, line, col) => {
write!(f, "syntax error at line {line}, col {col}: {}", error.kind)
}
Error::RangeOutOfBounds { range, source_len } => {
write!(
f,
"range {}..{} is out of bounds (source length: {})",
range.start, range.end, source_len
)
}
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions malva/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ mod doc_gen;
mod error;
mod helpers;
mod line_bounds;
pub mod range;
mod state;

pub use crate::range::FormatRangeResult;
use crate::{config::FormatOptions, ctx::Ctx, doc_gen::DocGen, state::State};
pub use crate::{error::Error, line_bounds::LineBounds};
pub use raffia::Syntax;
Expand Down Expand Up @@ -107,3 +109,18 @@ pub fn detect_syntax(path: impl AsRef<Path>) -> Option<Syntax> {
_ => None,
}
}

/// Format a specific range of CSS code.
///
/// This function formats only the specified range of code, which is useful for
/// editor integrations where users select a portion of code to format.
///
/// See [`crate::range::format_range`] for more details.
pub fn format_range(
source: &str,
range: std::ops::Range<usize>,
syntax: Syntax,
options: &FormatOptions,
) -> Result<FormatRangeResult, Error> {
crate::range::format_range(source, range, syntax, options)
}
Loading