MCPcopy Create free account
hub / github.com/ElementsProject/lightning / extract_includes

Function extract_includes

devtools/include-order-fixer.py:83–199  ·  view source on GitHub ↗

Extract include directives from file content, preserving comments and whitespace. Returns: tuple: (main_items, trailing_items, include_start_line, blank_line_index, include_end_line) main_items: List of (type, line) tuples in main block where type is 'include', 'comment', o

(content)

Source from the content-addressed store, hash-verified

81
82
83def extract_includes(content):
84 """
85 Extract include directives from file content, preserving comments and whitespace.
86
87 Returns:
88 tuple: (main_items, trailing_items, include_start_line, blank_line_index, include_end_line)
89 main_items: List of (type, line) tuples in main block where type is 'include', 'comment', or 'blank'
90 trailing_items: List of (type, line) tuples after blank line (to be preserved)
91 include_start_line: Line number where includes start (0-indexed)
92 blank_line_index: Line number of blank line separator (None if no blank line)
93 include_end_line: Line number after last include (0-indexed)
94 """
95 lines = content.splitlines(keepends=True)
96 main_items = []
97 trailing_items = []
98 include_start = None
99 include_end = None
100 blank_line_index = None
101 in_trailing_block = False
102
103 # Pattern to match include directives (with optional leading whitespace)
104 include_pattern = re.compile(r'^\s*#include\s+[<"].*[>"]\s*$')
105 # Pattern to match comments (single-line or start of multi-line)
106 comment_pattern = re.compile(r'^\s*/\*|^\s*//')
107 # Pattern to match continuation lines of multi-line comments
108 comment_continuation_pattern = re.compile(r'^\s*\*|.*\*/')
109
110 in_multiline_comment = False
111
112 for i, line in enumerate(lines):
113 # Check if this line is an include
114 if include_pattern.match(line):
115 in_multiline_comment = False
116 if include_start is None:
117 include_start = i
118 # Preserve the line as-is (including leading whitespace)
119 if in_trailing_block:
120 trailing_items.append(('include', line))
121 else:
122 main_items.append(('include', line))
123 include_end = i + 1
124 elif include_start is not None:
125 # We've seen includes, but this line is not an include
126 if line.strip():
127 # Check if it's a comment start or continuation
128 if comment_pattern.match(line):
129 # Start of a comment
130 in_multiline_comment = True
131 # Check if it's a single-line comment (ends with */)
132 if '*/' in line:
133 in_multiline_comment = False
134 # Preserve comments
135 if in_trailing_block:
136 trailing_items.append(('comment', line))
137 else:
138 main_items.append(('comment', line))
139 include_end = i + 1
140 elif in_multiline_comment and comment_continuation_pattern.match(line):

Callers 1

fix_file_includesFunction · 0.85

Calls 2

compileMethod · 0.80
anyClass · 0.50

Tested by

no test coverage detected