History

docs: kdoc: extract output formatting from dump_struct()

The last thing done in dump_struct() is to format the structure for printing. That, too, is a separate activity; split it out into its own function. dump_struct() now fits in a single, full-hight editor screen.

Jonathan Corbet committed 1 year ago

Commit 23c47b01 parent e6dd4e21 file · +37 −35Browse files

Unified · Split

scripts/lib/kdoc/kdoc_parser.py+37−35
@@ -739,6 +739,42 @@ class KernelDoc: · 738 unmodified lines
739739 tuples = struct_members.findall(members)
740740 return members
741741
742+ #
743+ # Format the struct declaration into a standard form for inclusion in the
744+ # resulting docs.
745+ #
746+ def format_struct_decl(self, declaration):
747+ #
748+ # Insert newlines, get rid of extra spaces.
749+ #
750+ declaration = KernRe(r'([\{;])').sub(r'\1\n', declaration)
751+ declaration = KernRe(r'\}\s+;').sub('};', declaration)
752+ #
753+ # Format inline enums with each member on its own line.
754+ #
755+ r = KernRe(r'(enum\s+\{[^\}]+),([^\n])')
756+ while r.search(declaration):
757+ declaration = r.sub(r'\1,\n\2', declaration)
758+ #
759+ # Now go through and supply the right number of tabs
760+ # for each line.
761+ #
762+ def_args = declaration.split('\n')
763+ level = 1
764+ declaration = ""
765+ for clause in def_args:
766+ clause = KernRe(r'\s+').sub(' ', clause.strip(), count=1)
767+ if clause:
768+ if '}' in clause and level > 1:
769+ level -= 1
770+ if not clause.startswith('#'):
771+ declaration += "\t" * level
772+ declaration += "\t" + clause + "\n"
773+ if "{" in clause and "}" not in clause:
774+ level += 1
775+ return declaration
776+
777+
742778 def dump_struct(self, ln, proto):
743779 """
744780 Store an entry for an struct or union
@@ -777,42 +813,8 @@ class KernelDoc: · 32 unmodified lines
777813 self.create_parameter_list(ln, decl_type, members, ';',
778814 declaration_name)
779815 self.check_sections(ln, declaration_name, decl_type)
780−
781− # Adjust declaration for better display
782− declaration = KernRe(r'([\{;])').sub(r'\1\n', declaration)
783− declaration = KernRe(r'\}\s+;').sub('};', declaration)
784−
785− # Better handle inlined enums
786− while True:
787− r = KernRe(r'(enum\s+\{[^\}]+),([^\n])')
788− if not r.search(declaration):
789− break
790−
791− declaration = r.sub(r'\1,\n\2', declaration)
792−
793− def_args = declaration.split('\n')
794− level = 1
795− declaration = ""
796− for clause in def_args:
797−
798− clause = clause.strip()
799− clause = KernRe(r'\s+').sub(' ', clause, count=1)
800−
801− if not clause:
802− continue
803−
804− if '}' in clause and level > 1:
805− level -= 1
806−
807− if not KernRe(r'^\s*#').match(clause):
808− declaration += "\t" * level
809−
810− declaration += "\t" + clause + "\n"
811− if "{" in clause and "}" not in clause:
812− level += 1
813−
814816 self.output_declaration(decl_type, declaration_name,
815− definition=declaration,
817+ definition=self.format_struct_decl(declaration),
816818 purpose=self.entry.declaration_purpose)
817819
818820 def dump_enum(self, ln, proto):

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t