From: Markus Armbruster <armbru@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: [PATCH v5 08/20] scripts/qapi/parser.py: improve doc comment indent handling
Date: Tue, 22 Sep 2020 09:27:10 +0200 [thread overview]
Message-ID: <87o8lytgoh.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <CAFEAcA_LEKRON2EUUCfXoAXmTGQSrqvFG_waBf1S-tsn8fJ6bA@mail.gmail.com> (Peter Maydell's message of "Mon, 21 Sep 2020 16:06:42 +0100")
Peter Maydell <peter.maydell@linaro.org> writes:
> On Fri, 4 Sep 2020 at 10:03, Markus Armbruster <armbru@redhat.com> wrote:
>>
>> Peter Maydell <peter.maydell@linaro.org> writes:
>>
>> > Make the handling of indentation in doc comments more sophisticated,
>
>> > def append(self, line):
>> > + # Strip leading spaces corresponding to the expected indent level
>> > + # Blank lines are always OK.
>> > + if line:
>> > + spacecount = len(line) - len(line.lstrip(" "))
>>
>> Works, but I'd prefer
>>
>> indent = re.match(r'\s*', line).end()
>
> OK.
>
>> > + if spacecount > self._indent:
>> > + spacecount = self._indent
>> > + if spacecount < self._indent:
>> > + raise QAPIParseError(self._parser, "unexpected de-indent")
>>
>> New error needs test coverage. I append a possible test.
>>
>> Reporting the expected indentation might be helpful.
>
> Fixed; new message produces reports like:
> doc-bad-indent.json:6:1: unexpected de-indent (expected at least 4 spaces)
>
> (I have not special-cased "1 spaces" -> "1 space"...)
>
>> > + line = line[spacecount:]
>>
>> If you use self._indent instead of spacecount here (which I find
>> clearer), you don't need to cap spacecount at self._indent above.
>
> Fixed.
>
>> > +
>
>> > @@ -460,7 +485,17 @@ class QAPIDoc:
>> >
>> > if name.startswith('@') and name.endswith(':'):
>> > line = line[len(name)+1:]
>> > - self._start_features_section(name[1:-1])
>> > + if not line or line.isspace():
>> > + # Line is just the "@name:" header, no ident for following lines
>>
>> pycodestyle complains:
>> scripts/qapi/parser.py:489:80: E501 line too long (80 > 79 characters)
>
> Fixed.
>
>> > + indent = 0
>> > + line = ''
>> > + else:
>> > + # Line is "@arg: first line of description"; following
>> > + # lines should be indented by len(name) + 3, and we
>> > + # pad out this first line so it is handled the same way
>> > + indent = len(name) + 1
>>
>> Comment claims + 3, code uses + 1.
>
> Yeah. This is because at this point 'name' is not actually just the
> name "arg" but includes the leading '@' and trailing ':' so I got
> confused between "we want the length of the name ("arg") plus 3"
> and the expression you need to actually use. I got this right in the
> comment in _append_args_line() but not in _append_features_line().
> Will clarify (in both functions) to:
>
> # Line is "@arg: first line of description"; since 'name'
> # at this point is "@arg:" any following lines should be
> # indented by len(name) + 1. We pad out this first line
> # so it is handled the same way.
>
>> Does this do the right thing when @arg: is followed by multiple
>> whitespace characters?
>
> The assumption is that if you added extra whitespace characters that's
> because you wanted to specify a line of rST which starts with leading
> spaces. So the handling here is that if you say
>
> @foo: bar
> baz
>
> it's because you want the rST to be
>
> bar
> baz
>
> If this turns out to be invalid rST then the rST parser will
> find that out later on.
In general, I'm wary of making the amount of whitespace within a line
significant, but in this case, the visual misalignment of bar and baz
should make accidents unlikely.
How does
@foo: bar
baz
@frob: gnu
gnat
behave?
This is something people may actually write.
> As it happens I'm not sure whether there is any useful rST
> syntax which has leading spaces and where you'd want to be able
> to start an argument docstring with it, but it means we're
> consistent with our handling of free-form doc comments, where
> writing
>
> Foo
> bar
>
> and writing
>
> Foo
> bar
>
> are different things. Also with the change you suggest later
> to avoid special-casing the "Examples" section then literal
> text becomes an example of where it makes a difference.
Valid points.
>> > + line = ' ' * indent + line
>> > + self._start_features_section(name[1:-1], indent)
>> > elif self._is_section_tag(name):
>> > self._append_line = self._append_various_line
>> > self._append_various_line(line)
>> > @@ -493,11 +528,23 @@ class QAPIDoc:
>> > % (name, self.sections[0].name))
>> > if self._is_section_tag(name):
>> > line = line[len(name)+1:]
>> > - self._start_section(name[:-1])
>> > + if not line or line.isspace():
>> > + # Line is just "SectionName:", no indent for following lines
>> > + indent = 0
>> > + line = ''
>> > + elif name.startswith("Example"):
>> > + # The "Examples" section is literal-text, so preserve
>> > + # all the indentation as-is
>> > + indent = 0
>>
>> Section "Example" is an exception. Needs to be documented. Do we
>> really need the exception? As far as I can see, it's only ever used in
>> documentation of block-latency-histogram-set.
>
> Hmm, so you'd rather we changed the documentation of that
> command so that instead of
>
> # Example: remove all latency histograms:
> #
> # -> { "execute": "block-latency-histogram-set",
> # "arguments": { "id": "drive0" } }
> # <- { "return": {} }
>
> it would be
>
> # Example:
> # remove all latency histograms:
> #
> # -> { "execute": "block-latency-histogram-set",
> # "arguments": { "id": "drive0" } }
> # <- { "return": {} }
>
> and remove the special-case for "Example" so that if you did
> write
>
> Example: something on the same line
> more stuff here
>
> it would be treated as literal text
>
> something on the same line
> more stuff here
>
> ?
>
> Seems reasonable. (I think I put this special case in only
> because I was trying to avoid changes to the existing doc
> comments if it was easy to accommodate them in the parser.)
> That command does seem to be the only outlier, so I've added
> a patch to v6 which will fix up its documentation comment
> and dropped the special casing.
Sounds like a good trade.
>> > + else:
>> > + # Line is "SectionName: some text", indent required
>>
>> Same situation as above, much terser comment.
>
> Fixed to use the expanded comment from earlier.
>
>> > + indent = len(name) + 1
>> > + line = ' ' * indent + line
>> > + self._start_section(name[:-1], indent)
>> >
>> > self._append_freeform(line)
>
>> > @@ -543,7 +590,7 @@ class QAPIDoc:
>> > def connect_member(self, member):
>> > if member.name not in self.args:
>> > # Undocumented TODO outlaw
>> > - self.args[member.name] = QAPIDoc.ArgSection(member.name)
>> > + self.args[member.name] = QAPIDoc.ArgSection(self._parser, member.name)
>>
>> pycodestyle complains:
>> scripts/qapi/parser.py:593:80: E501 line too long (82 > 79 characters)
>
> Fixed.
>
>> > self.args[member.name].connect(member)
>> >
>> > def connect_feature(self, feature):
>> > @@ -551,6 +598,8 @@ class QAPIDoc:
>> > raise QAPISemError(feature.info,
>> > "feature '%s' lacks documentation"
>> > % feature.name)
>> > + self.features[feature.name] = QAPIDoc.ArgSection(self._parser,
>> > + feature.name)
>>
>> pylint points out:
>> scripts/qapi/parser.py:601:12: W0101: Unreachable code (unreachable)
>>
>
> Yeah; this part of the patch used to be a "just update all the
> callsites of QAPIDoc.ArgSection() to pass the extra argument"
> hunk. It looks like your commit 8ec0e1a4e68781 removed this
> callsite entirely as dead code, but I missed that in the rebase
> and accidentally reintroduced the dead code. Fixed.
>
>> Suggested new test doc-bad-deintent.json, cribbed from your PATCH 06 of
>> doc-good.json:
>>
>> ##
>> # @Alternate:
>> # @i: an integer
>> # @b is undocumented
>> ##
>> { 'alternate': 'Alternate',
>> 'data': { 'i': 'int', 'b': 'bool' } }
>
> The '@' at the front of the second line here is not relevant to
> the mis-indentation and it's kind of confusing (as the correct
> fix is "add a colon", not "reindent the line"), so I think I'd
> rather have a test that's clearly looking at the indent:
>
> # Multiline doc comments should have consistent indentation
>
> ##
> # @foo:
> # @a: line one
> # line two is wrongly indented
> ##
> { 'command': 'foo', 'data': { 'a': 'int' } }
>
> which expects the error:
>
> doc-bad-indent.json:6:1: unexpected de-indent (expected at least 4 spaces)
Yes, that's better.
next prev parent reply other threads:[~2020-09-22 7:28 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-10 19:49 [PATCH v5 00/20] Convert QAPI doc comments to generate rST instead of texinfo Peter Maydell
2020-08-10 19:50 ` [PATCH v5 01/20] qapi/migration.json: Fix indentation Peter Maydell
2020-08-10 19:50 ` [PATCH v5 02/20] qapi: Fix indentation, again Peter Maydell
2020-08-14 18:39 ` Richard Henderson
2020-08-10 19:50 ` [PATCH v5 03/20] qapi/block-core.json: Fix nbd-server-start docs Peter Maydell
2020-08-14 18:39 ` Richard Henderson
2020-08-10 19:50 ` [PATCH v5 04/20] qapi/qapi-schema.json: Put headers in their own doc-comment blocks Peter Maydell
2020-08-10 19:50 ` [PATCH v5 05/20] qapi/machine.json: Escape a literal '*' in doc comment Peter Maydell
2020-08-10 19:50 ` [PATCH v5 06/20] tests/qapi/doc-good.json: Prepare for qapi-doc Sphinx extension Peter Maydell
2020-09-04 8:10 ` Markus Armbruster
2020-09-04 12:17 ` Peter Maydell
2020-08-10 19:50 ` [PATCH v5 07/20] scripts/qapi: Move doc-comment whitespace stripping to doc.py Peter Maydell
2020-08-10 19:50 ` [PATCH v5 08/20] scripts/qapi/parser.py: improve doc comment indent handling Peter Maydell
2020-09-04 9:03 ` Markus Armbruster
2020-09-21 15:06 ` Peter Maydell
2020-09-22 7:27 ` Markus Armbruster [this message]
2020-09-22 11:48 ` Peter Maydell
2020-09-22 14:08 ` Markus Armbruster
2020-09-22 15:28 ` Peter Maydell
2020-08-10 19:50 ` [PATCH v5 09/20] docs/sphinx: Add new qapi-doc Sphinx extension Peter Maydell
2020-08-14 18:40 ` Richard Henderson
2020-09-04 12:29 ` Markus Armbruster
2020-09-21 18:06 ` Peter Maydell
2020-09-22 11:42 ` Markus Armbruster
2020-09-24 13:25 ` Peter Maydell
2020-09-24 16:30 ` Peter Maydell
2020-09-25 5:51 ` Markus Armbruster
2020-09-04 14:44 ` Markus Armbruster
2020-09-04 14:52 ` Peter Maydell
2020-09-21 16:50 ` Peter Maydell
2020-09-22 11:47 ` Markus Armbruster
2020-08-10 19:50 ` [PATCH v5 10/20] docs/interop: Convert qemu-ga-ref to rST Peter Maydell
2020-09-04 13:16 ` Markus Armbruster
2020-09-04 13:18 ` Peter Maydell
2020-09-21 15:30 ` Peter Maydell
2020-09-22 12:00 ` Markus Armbruster
2020-09-22 12:58 ` Peter Maydell
2020-09-22 14:13 ` Markus Armbruster
2020-09-22 14:21 ` Peter Maydell
2020-09-22 14:42 ` Markus Armbruster
2020-08-10 19:50 ` [PATCH v5 11/20] docs/interop: Convert qemu-qmp-ref " Peter Maydell
2020-08-10 19:50 ` [PATCH v5 12/20] qapi: Use rST markup for literal blocks Peter Maydell
2020-09-04 13:02 ` Markus Armbruster
2020-08-10 19:50 ` [PATCH v5 13/20] qga/qapi-schema.json: Add some headings Peter Maydell
2020-08-10 19:50 ` [PATCH v5 14/20] scripts/qapi: Remove texinfo generation support Peter Maydell
2020-09-04 13:37 ` Markus Armbruster
2020-09-24 18:14 ` Peter Maydell
2020-09-25 6:48 ` Markus Armbruster
2020-08-10 19:50 ` [PATCH v5 15/20] docs/devel/qapi-code-gen.txt: Update to new rST backend conventions Peter Maydell
2020-09-17 9:24 ` Markus Armbruster
2020-08-10 19:50 ` [PATCH v5 16/20] Makefile: Remove redundant Texinfo related rules Peter Maydell
2020-08-10 19:50 ` [PATCH v5 17/20] scripts/texi2pod: Delete unused script Peter Maydell
2020-08-10 19:50 ` [PATCH v5 18/20] Remove Texinfo related files from .gitignore and git.orderfile Peter Maydell
2020-08-10 19:50 ` [PATCH v5 19/20] configure: Drop texinfo requirement Peter Maydell
2020-08-10 19:50 ` [PATCH v5 20/20] Remove texinfo dependency from docker and CI configs Peter Maydell
2020-08-27 11:25 ` [PATCH v5 00/20] Convert QAPI doc comments to generate rST instead of texinfo Peter Maydell
2020-09-04 14:34 ` Markus Armbruster
2020-09-04 14:48 ` Peter Maydell
2020-09-04 15:54 ` Markus Armbruster
2020-09-04 16:05 ` Peter Maydell
2020-09-24 14:13 ` Peter Maydell
2020-09-24 14:49 ` Markus Armbruster
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87o8lytgoh.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.