@@ -54,7 +54,7 @@ struct Formatter {
5454 tree : Tree ,
5555 original_source : Option < String > ,
5656 indent_string : String ,
57- // Original text of each # gd-formatter:disable ... # gd-formatter:enable region,
57+ // Original text of each `# fmt: off` ... `# fmt: on` region,
5858 // indexed by the order they appear in the file. Used to restore regions after formatting.
5959 disabled_regions : Vec < String > ,
6060}
@@ -157,23 +157,41 @@ impl Formatter {
157157 /// pre-applying rules that could be performance-intensive through topiary.
158158 #[ inline( always) ]
159159 fn preprocess ( & mut self ) -> & mut Self {
160- // Replace # gd-formatter:disable ... # gd-formatter:enable regions with placeholder
161- // comments so they pass through Topiary and post-processing untouched.
162160 self . extract_disabled_regions ( ) ;
163161 self
164162 }
165163
166- /// Scans the content for # gd-formatter:disable / # gd-formatter:enable regions.
167- /// Each complete region (including the marker lines) is stored verbatim in
164+ /// Scans the content for `# fmt: off` / `# fmt: on` regions (ignoring whitespace) .
165+ /// Each complete region (including the marker lines) is stored literally in
168166 /// self.disabled_regions and replaced with a single placeholder comment of the
169- /// form `# gd-formatter :preserved-region:N`. This prevents Topiary and all
167+ /// form `# fmt :preserved-region:N`. This prevents Topiary and all
170168 /// post-processing steps from touching the content inside those regions.
171169 fn extract_disabled_regions ( & mut self ) {
172- const DISABLE_MARKER : & str = "# gd-formatter:disable" ;
173- const ENABLE_MARKER : & str = "# gd-formatter:enable" ;
170+ enum LineKind {
171+ FmtOn ,
172+ FmtOff ,
173+ Other ,
174+ }
174175
175- if !self . content . contains ( DISABLE_MARKER ) {
176- return ;
176+ /// Checks whether `line` is a `# fmt: off` or `# fmt: on` marker.
177+ fn classify_line ( line : & str ) -> LineKind {
178+ if !line. contains ( '#' ) {
179+ return LineKind :: Other ;
180+ }
181+
182+ let line = line. trim ( ) ;
183+ let Some ( after_hash) = line. strip_prefix ( '#' ) else {
184+ return LineKind :: Other ;
185+ } ;
186+ let Some ( after_fmt) = after_hash. trim_start ( ) . strip_prefix ( "fmt:" ) else {
187+ return LineKind :: Other ;
188+ } ;
189+
190+ match after_fmt. trim_start ( ) {
191+ "off" => LineKind :: FmtOff ,
192+ "on" => LineKind :: FmtOn ,
193+ _ => LineKind :: Other ,
194+ }
177195 }
178196
179197 let mut result = String :: new ( ) ;
@@ -183,46 +201,43 @@ impl Formatter {
183201 // split_inclusive keeps the '\n' attached to each line so we never lose
184202 // trailing newlines when we reassemble the string.
185203 for line in self . content . split_inclusive ( '\n' ) {
186- let trimmed = line. trim ( ) ;
187-
188- if !in_disabled_region && trimmed == DISABLE_MARKER {
189- // Begin accumulating the disabled region (include the marker line itself).
190- in_disabled_region = true ;
191- current_region. push_str ( line) ;
192- } else if in_disabled_region && trimmed == ENABLE_MARKER {
193- // Close the region (include the closing marker line).
194- current_region. push_str ( line) ;
195- in_disabled_region = false ;
196- let region_index = self . disabled_regions . len ( ) ;
197- self . disabled_regions . push ( current_region. clone ( ) ) ;
198- current_region. clear ( ) ;
199- // Emit a single placeholder comment so the rest of the pipeline sees
200- // one clean comment node in place of the entire disabled block.
201- result. push_str ( & format ! (
202- "# gd-formatter:preserved-region:{}\n " ,
203- region_index
204- ) ) ;
205- } else if in_disabled_region {
206- current_region. push_str ( line) ;
207- } else {
208- result. push_str ( line) ;
204+ match classify_line ( line) {
205+ LineKind :: FmtOff if !in_disabled_region => {
206+ in_disabled_region = true ;
207+ current_region. push_str ( line) ;
208+ }
209+ LineKind :: FmtOn if in_disabled_region => {
210+ current_region. push_str ( line) ;
211+ in_disabled_region = false ;
212+ let region_index = self . disabled_regions . len ( ) ;
213+ self . disabled_regions . push ( current_region. clone ( ) ) ;
214+ current_region. clear ( ) ;
215+ result. push_str ( & format ! ( "# fmt:preserved-region:{}\n " , region_index) ) ;
216+ }
217+ _ => {
218+ if in_disabled_region {
219+ current_region. push_str ( line) ;
220+ } else {
221+ result. push_str ( line) ;
222+ }
223+ }
209224 }
210225 }
211226
212227 // An unclosed disable region (no matching enable marker) is also preserved.
213228 if in_disabled_region {
214229 let region_index = self . disabled_regions . len ( ) ;
215230 self . disabled_regions . push ( current_region) ;
216- result. push_str ( & format ! (
217- "# gd-formatter:preserved-region:{}\n " ,
218- region_index
219- ) ) ;
231+ result. push_str ( & format ! ( "# fmt:preserved-region:{}\n " , region_index) ) ;
220232 }
221233
222234 self . content = result;
223- // Re-parse so self.tree matches the new placeholder-containing content before
224- // it is handed to Topiary.
225- self . tree = self . parser . parse ( & self . content , None ) . unwrap ( ) ;
235+
236+ // Reparse the tree in case we modified the source code and replaced
237+ // some regions with disabled formatting.
238+ if !self . disabled_regions . is_empty ( ) {
239+ self . tree = self . parser . parse ( & self . content , None ) . unwrap ( ) ;
240+ }
226241 }
227242
228243 /// Replaces every placeholder comment emitted by extract_disabled_regions() with
@@ -240,7 +255,7 @@ impl Formatter {
240255 // Strip leading whitespace before checking for the placeholder; Topiary
241256 // may have adjusted indentation on comment lines.
242257 let trimmed = line. trim ( ) ;
243- if let Some ( rest) = trimmed. strip_prefix ( "# gd-formatter :preserved-region:" ) {
258+ if let Some ( rest) = trimmed. strip_prefix ( "# fmt :preserved-region:" ) {
244259 if let Ok ( index) = rest. parse :: < usize > ( ) {
245260 if let Some ( original) = self . disabled_regions . get ( index) {
246261 result. push_str ( original) ;
0 commit comments