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

Function sort_includes

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

Sort includes according to Core Lightning rules. For .c files: all includes in alphabetical order For .h files: config.h first (if present), then others alphabetically Includes ending in "_gen.h" or with any leading whitespace are preserved in their original positions. Comment

(items, file_type)

Source from the content-addressed store, hash-verified

200
201
202def sort_includes(items, file_type):
203 """
204 Sort includes according to Core Lightning rules.
205
206 For .c files: all includes in alphabetical order
207 For .h files: config.h first (if present), then others alphabetically
208
209 Includes ending in "_gen.h" or with any leading whitespace are preserved
210 in their original positions. Comments and blank lines are also preserved.
211 """
212 if not items:
213 return items
214
215 # Track includes that should be preserved at their positions
216 preserved_positions = {} # position -> (type, line)
217 regular_includes = [] # list of (position, include_line) tuples to sort
218
219 for pos, (item_type, line) in enumerate(items):
220 if item_type != 'include':
221 # Preserve comments and blank lines at their positions
222 preserved_positions[pos] = (item_type, line)
223 else:
224 # Check if this include should be preserved
225 # (has any leading whitespace, or ends in "_gen.h")
226 stripped = line.lstrip()
227 has_leading_whitespace = line != stripped
228 is_gen_h = '_gen.h"' in line or "_gen.h>" in line
229
230 if has_leading_whitespace or is_gen_h:
231 # Preserve at original position
232 preserved_positions[pos] = (item_type, line)
233 else:
234 # Regular include to be sorted
235 regular_includes.append((pos, line))
236
237 # Separate config.h from other regular includes for header files
238 config_h_pos = None
239 config_h_include = None
240 other_regular = []
241
242 for pos, inc in regular_includes:
243 if file_type == "h" and '"config.h"' in inc:
244 config_h_pos = pos
245 config_h_include = inc
246 else:
247 other_regular.append((pos, inc))
248
249 # Sort other regular includes using C locale (by the include content, not position)
250 other_regular_sorted = sorted(other_regular, key=lambda x: locale.strxfrm(x[1]))
251
252 # Build sorted list of regular includes
253 sorted_regular = []
254 if config_h_include:
255 sorted_regular.append((config_h_pos, config_h_include))
256 sorted_regular.extend(other_regular_sorted)
257
258 # Build result: preserved items at original positions, sorted regular includes elsewhere
259 result = []

Callers 1

fix_file_includesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected