@@ -4617,15 +4617,21 @@ def parse(self, block: Block) -> None:
4617
4617
fail ("'preserve' only works for blocks that don't produce any output!" )
4618
4618
block .output = self .saved_output
4619
4619
4620
- @staticmethod
4621
- def valid_line (line : str ) -> bool :
4620
+ def in_docstring (self ) -> bool :
4621
+ """Return true if we are processing a docstring."""
4622
+ return self .state in {
4623
+ self .state_parameter_docstring ,
4624
+ self .state_function_docstring ,
4625
+ }
4626
+
4627
+ def valid_line (self , line : str ) -> bool :
4622
4628
# ignore comment-only lines
4623
4629
if line .lstrip ().startswith ('#' ):
4624
4630
return False
4625
4631
4626
4632
# Ignore empty lines too
4627
4633
# (but not in docstring sections!)
4628
- if not line .strip ():
4634
+ if not self . in_docstring () and not line .strip ():
4629
4635
return False
4630
4636
4631
4637
return True
@@ -5262,12 +5268,20 @@ def state_parameter_docstring_start(self, line: str) -> None:
5262
5268
assert self .indent .depth == 3
5263
5269
return self .next (self .state_parameter_docstring , line )
5264
5270
5271
+ def docstring_append (self , obj : Function | Parameter , line : str ) -> None :
5272
+ """Add a rstripped line to the current docstring."""
5273
+ docstring = obj .docstring
5274
+ if docstring :
5275
+ docstring += "\n "
5276
+ if stripped := line .rstrip ():
5277
+ docstring += self .indent .dedent (stripped )
5278
+ obj .docstring = docstring
5279
+
5265
5280
# every line of the docstring must start with at least F spaces,
5266
5281
# where F > P.
5267
5282
# these F spaces will be stripped.
5268
5283
def state_parameter_docstring (self , line : str ) -> None :
5269
- stripped = line .strip ()
5270
- if stripped .startswith ('#' ):
5284
+ if not self .valid_line (line ):
5271
5285
return
5272
5286
5273
5287
indent = self .indent .measure (line )
@@ -5281,16 +5295,8 @@ def state_parameter_docstring(self, line: str) -> None:
5281
5295
return self .next (self .state_function_docstring , line )
5282
5296
5283
5297
assert self .function and self .function .parameters
5284
- last_parameter = next (reversed (list (self .function .parameters .values ())))
5285
-
5286
- new_docstring = last_parameter .docstring
5287
-
5288
- if new_docstring :
5289
- new_docstring += '\n '
5290
- if stripped :
5291
- new_docstring += self .indent .dedent (line )
5292
-
5293
- last_parameter .docstring = new_docstring
5298
+ last_param = next (reversed (self .function .parameters .values ()))
5299
+ self .docstring_append (last_param , line )
5294
5300
5295
5301
# the final stanza of the DSL is the docstring.
5296
5302
def state_function_docstring (self , line : str ) -> None :
@@ -5299,19 +5305,10 @@ def state_function_docstring(self, line: str) -> None:
5299
5305
if self .group :
5300
5306
fail ("Function " + self .function .name + " has a ] without a matching [." )
5301
5307
5302
- stripped = line .strip ()
5303
- if stripped .startswith ('#' ):
5308
+ if not self .valid_line (line ):
5304
5309
return
5305
5310
5306
- new_docstring = self .function .docstring
5307
- if new_docstring :
5308
- new_docstring += "\n "
5309
- if stripped :
5310
- line = self .indent .dedent (line ).rstrip ()
5311
- else :
5312
- line = ''
5313
- new_docstring += line
5314
- self .function .docstring = new_docstring
5311
+ self .docstring_append (self .function , line )
5315
5312
5316
5313
def format_docstring (self ) -> str :
5317
5314
f = self .function
@@ -5580,12 +5577,6 @@ def do_post_block_processing_cleanup(self) -> None:
5580
5577
if no_parameter_after_star :
5581
5578
fail ("Function " + self .function .name + " specifies '*' without any parameters afterwards." )
5582
5579
5583
- # remove trailing whitespace from all parameter docstrings
5584
- for name , value in self .function .parameters .items ():
5585
- if not value :
5586
- continue
5587
- value .docstring = value .docstring .rstrip ()
5588
-
5589
5580
self .function .docstring = self .format_docstring ()
5590
5581
5591
5582
0 commit comments