diff options
author | Andrew Burgess <aburgess@redhat.com> | 2022-05-16 19:26:54 +0100 |
---|---|---|
committer | Andrew Burgess <aburgess@redhat.com> | 2022-05-28 10:36:50 +0100 |
commit | 51e8dbe1fbe7d8955589703140ca5eba7b4f1bd7 (patch) | |
tree | 41910ff02a589623a9c674ee80d4c8d5d2cbe3c7 /gdb/python/python-internal.h | |
parent | 0e77ff2c86a163fe11135633837c8f19aee31ca0 (diff) | |
download | gdb-51e8dbe1fbe7d8955589703140ca5eba7b4f1bd7.zip gdb-51e8dbe1fbe7d8955589703140ca5eba7b4f1bd7.tar.gz gdb-51e8dbe1fbe7d8955589703140ca5eba7b4f1bd7.tar.bz2 |
gdb/python: improve formatting of help text for user defined commands
Consider this command defined in Python (in the file test-cmd.py):
class test_cmd (gdb.Command):
"""
This is the first line.
Indented second line.
This is the third line.
"""
def __init__ (self):
super ().__init__ ("test-cmd", gdb.COMMAND_OBSCURE)
def invoke (self, arg, from_tty):
print ("In test-cmd")
test_cmd()
Now, within a GDB session:
(gdb) source test-cmd.py
(gdb) help test-cmd
This is the first line.
Indented second line.
This is the third line.
(gdb)
I think there's three things wrong here:
1. The leading blank line,
2. The trailing blank line, and
3. Every line is indented from the left edge slightly.
The problem of course, is that GDB is using the Python doc string
verbatim as its help text. While the user has formatted the help text
so that it appears clear within the .py file, this means that the text
appear less well formatted when displayed in the "help" output.
The same problem can be observed for gdb.Parameter objects in their
set/show output.
In this commit I aim to improve the "help" output for commands and
parameters.
To do this I have added gdbpy_fix_doc_string_indentation, a new
function that rewrites the doc string text following the following
rules:
1. Leading blank lines are removed,
2. Trailing blank lines are removed, and
3. Leading whitespace is removed in a "smart" way such that the
relative indentation of lines is retained.
With this commit in place the above example now looks like this:
(gdb) source ~/tmp/test-cmd.py
(gdb) help test-cmd
This is the first line.
Indented second line.
This is the third line.
(gdb)
Which I think is much neater. Notice that the indentation of the
second line is retained. Any blank lines within the help text (not
leading or trailing) will be retained.
I've added a NEWS entry to note that there has been a change in
behaviour, but I didn't update the manual. The existing manual is
suitably vague about how the doc string is used, so I think the new
behaviour is covered just as well by the existing text.
Diffstat (limited to 'gdb/python/python-internal.h')
-rw-r--r-- | gdb/python/python-internal.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index d947b96..217bc15 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -822,4 +822,25 @@ extern bool gdbpy_is_architecture (PyObject *obj); extern bool gdbpy_is_progspace (PyObject *obj); +/* Take DOC, the documentation string for a GDB command defined in Python, + and return an (possibly) modified version of that same string. + + When a command is defined in Python, the documentation string will + usually be indented based on the indentation of the surrounding Python + code. However, the documentation string is a literal string, all the + white-space added for indentation is included within the documentation + string. + + This indentation is then included in the help text that GDB displays, + which looks odd out of the context of the original Python source code. + + This function analyses DOC and tries to figure out what white-space + within DOC was added as part of the indentation, and then removes that + white-space from the copy that is returned. + + If the analysis of DOC fails then DOC will be returned unmodified. */ + +extern gdb::unique_xmalloc_ptr<char> gdbpy_fix_doc_string_indentation + (gdb::unique_xmalloc_ptr<char> doc); + #endif /* PYTHON_PYTHON_INTERNAL_H */ |