Optional source map generation (#6894)

This commit is contained in:
Micha Reiser 2023-08-26 18:00:43 +02:00 committed by GitHub
parent 15b73bdb8a
commit eae59cf088
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 117 additions and 73 deletions

View file

@ -396,6 +396,10 @@ impl<'a> Printer<'a> {
}
fn push_marker(&mut self) {
if self.options.source_map_generation.is_disabled() {
return;
}
let marker = SourceMarker {
source: self.state.source_position,
dest: self.state.buffer.text_len(),

View file

@ -14,39 +14,10 @@ pub struct PrinterOptions {
/// Whether the printer should use tabs or spaces to indent code and if spaces, by how many.
pub indent_style: IndentStyle,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PrintWidth(u32);
impl PrintWidth {
pub fn new(width: u32) -> Self {
Self(width)
}
}
impl Default for PrintWidth {
fn default() -> Self {
LineWidth::default().into()
}
}
impl From<LineWidth> for PrintWidth {
fn from(width: LineWidth) -> Self {
Self(u32::from(u16::from(width)))
}
}
impl From<PrintWidth> for usize {
fn from(width: PrintWidth) -> Self {
width.0 as usize
}
}
impl From<PrintWidth> for u32 {
fn from(width: PrintWidth) -> Self {
width.0
}
/// Whether the printer should build a source map that allows mapping positions in the source document
/// to positions in the formatted document.
pub source_map_generation: SourceMapGeneration,
}
impl<'a, O> From<&'a O> for PrinterOptions
@ -94,6 +65,64 @@ impl PrinterOptions {
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PrintWidth(u32);
impl PrintWidth {
pub fn new(width: u32) -> Self {
Self(width)
}
}
impl Default for PrintWidth {
fn default() -> Self {
LineWidth::default().into()
}
}
impl From<LineWidth> for PrintWidth {
fn from(width: LineWidth) -> Self {
Self(u32::from(u16::from(width)))
}
}
impl From<PrintWidth> for usize {
fn from(width: PrintWidth) -> Self {
width.0 as usize
}
}
impl From<PrintWidth> for u32 {
fn from(width: PrintWidth) -> Self {
width.0
}
}
/// Configures whether the formatter and printer generate a source map that allows mapping
/// positions in the source document to positions in the formatted code.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SourceMapGeneration {
/// The formatter generates no source map.
#[default]
Disabled,
/// The formatter generates a source map that allows mapping positions in the source document
/// to positions in the formatted document. The ability to map positions is useful for range formatting
/// or when trying to identify where to move the cursor so that it matches its position in the source document.
Enabled,
}
impl SourceMapGeneration {
pub const fn is_enabled(self) -> bool {
matches!(self, SourceMapGeneration::Enabled)
}
pub const fn is_disabled(self) -> bool {
matches!(self, SourceMapGeneration::Disabled)
}
}
#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub enum LineEnding {