@@ -15,6 +15,10 @@ pub struct PrinterOptions {
1515
1616 /// Whether the printer should use tabs or spaces to indent code and if spaces, by how many.
1717 pub indent_style : IndentStyle ,
18+
19+ /// Should the printer generate a source map that allows mapping positions in the source document
20+ /// to positions in the formatted document.
21+ pub source_map_generation : SourceMapGeneration ,
1822}
1923
2024#[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
@@ -81,10 +85,20 @@ impl PrinterOptions {
8185 self
8286 }
8387
88+ pub fn with_source_map_generation ( mut self , source_map : SourceMapGeneration ) -> Self {
89+ self . source_map_generation = source_map;
90+
91+ self
92+ }
93+
8494 pub ( crate ) fn indent_style ( & self ) -> IndentStyle {
8595 self . indent_style
8696 }
8797
98+ pub const fn source_map_generation ( & self ) -> SourceMapGeneration {
99+ self . source_map_generation
100+ }
101+
88102 /// Width of an indent in characters.
89103 pub ( super ) const fn indent_width ( & self ) -> IndentWidth {
90104 self . indent_width
@@ -103,6 +117,32 @@ impl Default for PrinterOptions {
103117 print_width : PrintWidth :: default ( ) ,
104118 indent_style : Default :: default ( ) ,
105119 line_ending : LineEnding :: Lf ,
120+ source_map_generation : SourceMapGeneration :: default ( ) ,
106121 }
107122 }
108123}
124+
125+ /// Configures whether the printer generates a source map that allows mapping
126+ /// positions in the source document to positions in the formatted code.
127+ #[ derive( Copy , Clone , Debug , Eq , PartialEq , Default ) ]
128+ #[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
129+ pub enum SourceMapGeneration {
130+ /// The printer generates no source map.
131+ #[ default]
132+ Disabled ,
133+
134+ /// The printer generates a source map that allows mapping positions in the source document
135+ /// to positions in the formatted document. The ability to map positions is useful for range formatting
136+ /// or when trying to identify where to move the cursor so that it matches its position in the source document.
137+ Enabled ,
138+ }
139+
140+ impl SourceMapGeneration {
141+ pub const fn is_enabled ( self ) -> bool {
142+ matches ! ( self , Self :: Enabled )
143+ }
144+
145+ pub const fn is_disabled ( self ) -> bool {
146+ matches ! ( self , Self :: Disabled )
147+ }
148+ }
0 commit comments