dte test coverage


Directory: ./
File: src/indent.h
Date: 2025-05-08 15:05:54
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 3 3 100.0%
Branches: 6 6 100.0%

Line Branch Exec Source
1 #ifndef INDENT_H
2 #define INDENT_H
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include "block-iter.h"
7 #include "options.h"
8 #include "util/bit.h"
9 #include "util/debug.h"
10 #include "util/macros.h"
11 #include "util/string-view.h"
12
13 typedef struct {
14 size_t bytes; // Size in bytes
15 size_t width; // Width in columns
16 size_t level; // Number of whole `indent-width` levels
17 bool wsonly; // Empty or whitespace-only line
18
19 // Only spaces or tabs, depending on `use_spaces_for_indent()`.
20 // Note that a "sane" line can contain spaces after tabs for alignment.
21 bool sane;
22 } IndentInfo;
23
24 // Divide `x` by `d`, to obtain the number of whole indent levels.
25 // If `d` is a power of 2, shift right by `u32_ctz(d)` instead, to
26 // avoid the expensive divide operation. This optimization applies
27 // to widths of 1, 2, 4 and 8, which covers all of the sensible ones.
28 229 static inline size_t indent_level(size_t x, size_t d)
29 {
30 229 BUG_ON(d - 1 >= INDENT_WIDTH_MAX);
31
2/2
✓ Branch 0 (4→5) taken 157 times.
✓ Branch 1 (4→7) taken 72 times.
229 return likely(IS_POWER_OF_2(d)) ? x >> u32_ctz(d) : x / d;
32 }
33
34 751 static inline size_t indent_remainder(size_t x, size_t m)
35 {
36 751 BUG_ON(m - 1 >= INDENT_WIDTH_MAX);
37
2/2
✓ Branch 0 (4→5) taken 679 times.
✓ Branch 1 (4→6) taken 72 times.
751 return likely(IS_POWER_OF_2(m)) ? x & (m - 1) : x % m;
38 }
39
40 223 static inline size_t next_indent_width(size_t x, size_t m)
41 {
42 223 BUG_ON(m - 1 >= INDENT_WIDTH_MAX);
43
2/2
✓ Branch 0 (4→5) taken 138 times.
✓ Branch 1 (4→6) taken 85 times.
223 return likely(IS_POWER_OF_2(m)) ? next_multiple(x + 1, m) : ((x + m) / m) * m;
44 }
45
46 char *make_indent(const LocalOptions *options, size_t width);
47 char *get_indent_for_next_line(const LocalOptions *options, const StringView *line);
48 IndentInfo get_indent_info(const LocalOptions *options, const StringView *line);
49 size_t get_indent_width(const StringView *line, unsigned int tab_width);
50 size_t get_indent_level_bytes_left(const LocalOptions *options, const BlockIter *cursor);
51 size_t get_indent_level_bytes_right(const LocalOptions *options, const BlockIter *cursor);
52
53 #endif
54