Skip to content

Commit 4e303b9

Browse files
Preserve initial whitespace
1 parent 51dd815 commit 4e303b9

File tree

1 file changed

+49
-65
lines changed

1 file changed

+49
-65
lines changed

scripts/reflow.awk

Lines changed: 49 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,92 @@
1-
# ZERO-REGEX VERSION — preserves indentation for merged comment lines
1+
# Merge Rust comment lines while preserving:
2+
# - original spacing after //, ///, //!
3+
# - empty lines
4+
# - === headings
5+
# - bullets / numbered lists
6+
# - fenced code blocks
7+
# - Markdown headings
28

39
function ltrim(s) {
4-
while (substr(s,1,1) == " " || substr(s,1,1) == "\t")
5-
s = substr(s,2)
10+
while (substr(s,1,1)==" " || substr(s,1,1)=="\t") s=substr(s,2)
611
return s
712
}
813

9-
function leading_spaces(s, i, count, c) {
10-
count=0
11-
for (i=1; i<=length(s); i++) {
12-
c=substr(s,i,1)
13-
if (c==" " || c=="\t") count++
14-
else break
15-
}
16-
return count
17-
}
18-
19-
function is_numbered_list(s, i, c) {
14+
function is_numbered_list(s, i,c) {
2015
found_digit=0
21-
for (i=1; i<=length(s); i++) {
16+
for(i=1;i<=length(s);i++){
2217
c=substr(s,i,1)
23-
if (c >= "0" && c <= "9") { found_digit=1; continue }
24-
if (found_digit && (c=="." || c==")")) return 1
18+
if(c>="0" && c<="9"){found_digit=1; continue}
19+
if(found_digit && (c=="."||c==")")) return 1
2520
return 0
2621
}
2722
return 0
2823
}
2924

3025
{
31-
line = $0
26+
line=$0
3227

33-
# ---------- Detect comment type ----------
34-
if (substr(line,1,3) == "///")
35-
type = "doc"
36-
else if (substr(line,1,3) == "//!")
37-
type = "innerdoc"
38-
else if (substr(line,1,2) == "//")
39-
type = "line"
40-
else
41-
type = "none"
28+
# Detect comment type
29+
if(substr(line,1,3)=="///") type="doc"
30+
else if(substr(line,1,3)=="//!") type="innerdoc"
31+
else if(substr(line,1,2)=="//") type="line"
32+
else type="none"
4233

43-
# ---------- Handle comment lines ----------
44-
if (type != "none") {
34+
if(type!="none"){
35+
if(type=="doc") prefix="///"
36+
else if(type=="innerdoc") prefix="//!"
37+
else prefix="//"
4538

46-
prefix = (type=="doc" ? "///" : type=="innerdoc" ? "//!" : "//")
47-
raw = substr(line, length(prefix)+1)
48-
trimmed = ltrim(raw)
39+
raw=substr(line,length(prefix)+1)
40+
# Capture the original spaces after prefix
41+
match_space=""
42+
i=1
43+
while(i<=length(raw) && (substr(raw,i,1)==" " || substr(raw,i,1)=="\t")){ match_space = match_space substr(raw,i,1); i++ }
44+
text=substr(raw,i)
4945

50-
# Track indentation
51-
indent = leading_spaces(raw)
46+
trimmed=ltrim(raw)
5247

5348
# ---------- Fenced code block ----------
54-
if (substr(trimmed,1,3) == "```") {
55-
if (in_comment) { print out_prefix merged; in_comment=0 }
56-
if (fenced==0) fenced=1; else fenced=0
49+
if(substr(trimmed,1,3)=="```"){
50+
if(in_comment){ print out_prefix merged; in_comment=0; out_space="" }
51+
if(fenced==0) fenced=1; else fenced=0
5752
print line
5853
next
5954
}
60-
if (fenced==1) { print line; next }
55+
if(fenced==1){ print line; next }
6156

62-
# ---------- Empty comment line ----------
57+
# ---------- Empty line ----------
6358
empty=1
64-
for (i=1; i<=length(trimmed); i++) {
65-
c=substr(trimmed,i,1)
66-
if (c!=" " && c!="\t") { empty=0; break }
67-
}
68-
if (empty) { if (in_comment) { print out_prefix merged; in_comment=0 } print line; next }
59+
for(j=1;j<=length(trimmed);j++){c=substr(trimmed,j,1); if(c!=" " && c!="\t"){empty=0; break}}
60+
if(empty){ if(in_comment){print out_prefix merged; in_comment=0; out_space=""} print line; next }
6961

70-
# ---------- === section line ----------
71-
if (substr(trimmed,1,3) == "===") { if (in_comment) { print out_prefix merged; in_comment=0 } print line; next }
62+
# ---------- === heading ----------
63+
if(substr(trimmed,1,3)=="==="){ if(in_comment){print out_prefix merged; in_comment=0; out_space=""} print line; next }
7264

7365
# ---------- Markdown heading ----------
74-
if (substr(trimmed,1,1) == "#") { if (in_comment) { print out_prefix merged; in_comment=0 } print line; next }
66+
if(substr(trimmed,1,1)=="#"){ if(in_comment){print out_prefix merged; in_comment=0; out_space=""} print line; next }
7567

7668
# ---------- Bullet or numbered list ----------
77-
first = substr(trimmed,1,1)
78-
if (first=="*" || first=="-" || is_numbered_list(trimmed)) { if (in_comment) { print out_prefix merged; in_comment=0 } print line; next }
69+
first=substr(trimmed,1,1)
70+
if(first=="*" || first=="-" || is_numbered_list(trimmed)){ if(in_comment){print out_prefix merged; in_comment=0; out_space=""} print line; next }
7971

8072
# ---------- Mergeable line ----------
81-
if (in_comment && type==last_type) {
82-
merged = merged " " ltrim(raw) # preserve relative spacing inside line
83-
if (indent < out_indent) out_indent = indent
73+
if(in_comment && type==last_type){
74+
merged=merged " " text
8475
} else {
85-
if (in_comment) print out_prefix merged
76+
if(in_comment) print out_prefix merged
8677
in_comment=1
8778
last_type=type
88-
merged = ltrim(raw)
89-
out_prefix = prefix
90-
out_indent = indent
79+
merged=text
80+
out_prefix = prefix match_space
9181
}
9282

9383
next
9484
}
9585

96-
# ---------- Non-comment line ----------
97-
if (in_comment) {
98-
# Apply preserved indentation
99-
printf "%s%*s%s\n", out_prefix, out_indent, "", merged
100-
in_comment=0
101-
}
86+
# Non-comment line
87+
if(in_comment){ print out_prefix merged; in_comment=0; out_space="" }
10288
print line
10389
}
10490

105-
END {
106-
if (in_comment) printf "%s%*s%s\n", out_prefix, out_indent, "", merged
107-
}
91+
END { if(in_comment) print out_prefix merged }
10892

0 commit comments

Comments
 (0)