1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
|
package pkglint
import (
"netbsd.org/pkglint/textproc"
"sort"
"strings"
)
// VaralignBlock checks that all variable assignments from a paragraph
// use the same indentation depth for their values.
// It also checks that the indentation uses tabs instead of spaces.
//
// In general, all values should be aligned using tabs.
// As an exception, a single very long line (called an outlier) may be
// aligned with a single space.
// A typical example is a SITES.very-long-file-name.tar.gz variable
// between HOMEPAGE and DISTFILES.
//
// Continuation lines are also aligned to the single-line assignments.
// There are two types of continuation lines. The first type has just
// the continuation backslash in the first line:
//
// MULTI_LINE= \
// The value starts in the second line.
//
// The backslash in the first line is usually aligned to the other variables
// in the same paragraph. If the variable name is longer than the indentation
// of the paragraph, it may be indented with a single space.
// In multi-line shell commands or AWK programs, the backslash is
// often indented to column 73, as are the backslashes from the follow-up
// lines, to act as a visual guideline.
//
// The indentation of the first value of the variable determines the minimum
// indentation for the remaining continuation lines. To allow long variable
// values to be indented as little as possible, the follow-up lines only need
// to be indented by a single tab, even if the other
// variables in the paragraph are aligned further to the right. If the
// indentation is not a single tab, it must match the indentation of the
// other lines in the paragraph.
//
// MULTI_LINE= The value starts in the first line \
// and continues in the second line.
//
// In lists or plain text, like in the example above, all values are
// aligned in the same column. Some variables also contain code, and in
// these variables, the line containing the first word defines how deep
// the follow-up lines must be indented at least.
//
// SHELL_CMD= \
// if ${PKG_ADMIN} pmatch ${PKGNAME} ${dependency}; then \
// ${ECHO} yes; \
// else \
// ${ECHO} no; \
// fi
//
// In the continuation lines, each follow-up line is indented with at least
// one tab, to avoid confusing them with regular single-lines. This is
// especially true for CONFIGURE_ENV, since the environment variables are
// typically uppercase as well.
//
// For the purpose of aligning the variable assignments, each raw line is
// split into several parts, as described by the varalignParts type.
//
// The alignment checks are performed on the raw lines instead of
// the logical lines, since this check is about the visual appearance
// as opposed to the meaning of the variable assignment.
//
// FIXME: Implement each requirement from the above documentation.
//
// Next try for the spec, from November 2019.
// Completely built from the existing examples, striving to be short and clear.
// Needs some more time to mature.
// After implementing it, it will be translated into English.
//
// Ebenen: Datei > Absatz > MkZeile > Zeile
//
// ### Datei
//
// #. Ein einzelner Absatz, der einen Tab weniger eingerückt ist als die übrigen,
// darf auf die Einrückung der anderen Absätze angeglichen werden,
// sofern der Absatz dadurch nicht zu breit wird.
//
// ### Einzelner Absatz
//
// #. Jede Zeile besteht aus #, VarOp, Leerraum, Wert, Leerraum und Fortsetzung.
//
// #. Die Werte aller Zeilen sind mit Tabs an einer gemeinsamen vertikalen Linie
// (Ausrichtung) ausgerichtet.
//
// #. Das Ausrichten mit mehr als 1 Tab ist erlaubt, wenn die Ausrichtung einheitlich ist.
//
// #. Wenn VarOp über die Ausrichtung hinausragt (Ausreißer),
// darf zwischen VarOp und Wert statt der Ausrichtung 1 Leerzeichen sein.
//
// #. Die minimale Ausrichtung ergibt sich aus der maximalen Breite von # und VarOp
// aller Zeilen, gerundet zum nächsten Tabstopp.
// Dabei zählen auch Zeilen mit, die rechts von VarOp komplett leer sind.
//
// #. Die maximale Ausrichtung ergibt sich aus der maximalen Breite von Wert
// und Kommentar, abgezogen vom maximalen rechten Rand (in Spalte 73).
//
// #. Beim Umformatieren darf die Zeilenbreite die 73 Zeichen nicht überschreiten,
// damit am rechten Rand eindeutig ist, wo jede Zeile aufhört.
// Zeilen, die bereits vorher breiter waren, dürfen ruhig noch breiter werden.
//
// #. Das Verhältnis zwischen Tab-Zeilen und hinausragenden Zeilen muss ausgewogen sein.
// Nicht zu viele hinausragende Zeilen. (Noch zu definieren.)
// Möglicher Ansatz: Anteil der Leerfläche?
//
// ### Mehrzeilig
//
// #. Jede MkZeile hat für alle ihre Zeilen einen gemeinsamen rechten Rand.
//
// #. Die Fortsetzungen jeder MkZeile sind entweder alle durch je 1 Leerzeichen abgetrennt,
// oder alle Fortsetzungen sind am rechten Rand.
//
// #. Um den gemeinsamen rechten Rand zu bestimmen, werden alle Zeilen ignoriert,
// in denen die Fortsetzung durch 1 Leerzeichen abgetrennt ist.
//
// #. Einzelne Fortsetzungen dürfen über den rechten Rand hinausragen.
// Die Fortsetzung wird dann durch 1 Leerzeichen abgetrennt.
//
// ### Mehrzeilig, Erstzeile
//
// #. Die Fortsetzung der Erstzeile ist durch 1 Leerzeichen abgetrennt,
// wenn sie rechts von der Ausrichtung steht,
// andernfalls durch Tabs an der Ausrichtung.
//
// #. Eine leere Erstzeile mit 1 fortgesetzer Zeile ist nur zulässig,
// wenn die kombinierte Zeile breiter als 73 Zeichen wäre.
// Sonst werden die beiden Zeilen kombiniert.
//
// ### Mehrzeilig, fortgesetzte Zeilen
//
// #. Nach einer leeren Erstzeile ist die erste fortgesetzte Zeile an der
// Ausrichtung aller Zeilen eingerückt, wenn die Erstzeile über die
// Ausrichtung ragt und der Platz aller Zeilen es zulässt, andernfalls
// mit 1 Tab.
//
// #. Bei mehrzeiligen einrückbaren Werten (AWK, Shell, Listen aus Tupeln)
// dürfen die weiteren Fortsetzungszeilen weiter eingerückt sein als die erste.
// Ihre Einrückung besteht aus Tabs, gefolgt von 0 bis 7 Leerzeichen.
type VaralignBlock struct {
mkinfos []*varalignMkLine
skip bool
}
func (va *VaralignBlock) Process(mkline *MkLine) {
switch {
case mkline.IsEmpty():
va.Finish()
case mkline.IsVarassignMaybeCommented():
va.processVarassign(mkline)
case mkline.IsComment(), mkline.IsDirective():
default:
trace.Stepf("Skipping varalign block because of line %s", mkline.String())
va.skip = true
}
}
func (va *VaralignBlock) processVarassign(mkline *MkLine) {
switch {
case mkline.Op() == opAssignEval && matches(mkline.Varname(), `^[a-z]`):
// Arguments to procedures do not take part in block alignment.
//
// Example:
// pkgpath := ${PKGPATH}
// .include "../../mk/pkg-build-options.mk"
return
case mkline.Value() == "" && !mkline.HasComment():
// Multiple-inclusion guards usually appear in a block of
// their own and therefore do not need alignment.
//
// Example:
// .if !defined(INCLUSION_GUARD_MK)
// INCLUSION_GUARD_MK:=
// # ...
// .endif
return
}
var infos []*varalignLine
for rawIndex := range mkline.raw {
parts := NewVaralignSplitter().split(mkline.RawText(rawIndex), rawIndex == 0)
info := varalignLine{mkline, rawIndex, false, parts}
infos = append(infos, &info)
}
va.mkinfos = append(va.mkinfos, &varalignMkLine{infos})
}
func (va *VaralignBlock) Finish() {
if len(va.mkinfos) == 0 || va.skip {
*va = VaralignBlock{}
return
}
if trace.Tracing {
defer trace.Call()()
}
newWidth := va.optimalWidth()
for _, mkinfo := range va.mkinfos {
mkinfo.realign(newWidth)
}
*va = VaralignBlock{}
}
func (l *varalignMkLine) realign(newWidth int) {
// When the indentation of the initial line of a multiline is
// changed, all its follow-up lines are shifted by the same
// amount and in the same direction. Typical examples are
// SUBST_SED, shell programs and AWK programs like in
// GENERATE_PLIST.
//
// The amount by which the follow-up lines are shifted.
// Positive values mean shifting to the right, negative values
// mean shifting to the left.
var indentDiff optInt
_, rightMargin := l.rightMargin()
isMultiEmpty := l.isMultiEmpty()
if info := l.infos[0]; !isMultiEmpty && info.isContinuation() {
indentDiff.set(newWidth - info.valueColumn())
}
for _, info := range l.infos {
if newWidth > 0 || info.rawIndex > 0 {
info.realignDetails(newWidth, &indentDiff, isMultiEmpty)
}
if !info.fixedSpaceBeforeContinuation {
info.alignContinuation(newWidth, rightMargin)
}
}
}
// optimalWidth computes the desired screen width for the variable assignment
// lines. If the paragraph is already indented consistently, it is kept as-is.
//
// There may be a single line sticking out from the others (called outlier).
// This is to prevent a single SITES.* variable from forcing the rest of the
// paragraph to be indented too far to the right.
func (va *VaralignBlock) optimalWidth() int {
minVarnameOpWidth, outlier := va.varnameOpWidths()
minTotalWidth, maxTotalWidth := va.spaceWidths(outlier)
va.traceWidths(minTotalWidth, maxTotalWidth, minVarnameOpWidth, outlier)
if minTotalWidth > minVarnameOpWidth && minTotalWidth == maxTotalWidth && minTotalWidth%8 == 0 {
// The whole paragraph is already indented to the same width.
return minTotalWidth
}
if minVarnameOpWidth == 0 {
// Only continuation lines in this paragraph.
return 0
}
return minVarnameOpWidth&-8 + 8
}
// varnameOpWidths calculates the required width of varnameOp,
// without the trailing whitespace, as well as the outlier.
func (va *VaralignBlock) varnameOpWidths() (int, int) {
var widths bag
for i, mkinfo := range va.mkinfos {
if !mkinfo.isMultiEmpty() {
info := mkinfo.infos[0]
widths.add(i, info.spaceBeforeValueColumn())
}
}
if widths.len() == 0 {
return 0, 0
}
widths.sortDesc()
longest := widths.opt(0)
secondLongest := widths.opt(1)
haveOutlier := secondLongest != 0 &&
secondLongest/8+1 < longest/8 &&
!va.mkinfos[widths.key(0).(int)].infos[0].isContinuation()
if haveOutlier {
return secondLongest, longest
}
return longest, 0
}
func (va *VaralignBlock) spaceWidths(outlier int) (min, max int) {
// Widths of the current indentation (including whitespace)
spaceWidths := newInterval()
for _, mkinfo := range va.mkinfos {
info := mkinfo.infos[0]
if mkinfo.isMultiEmpty() || outlier > 0 && info.spaceBeforeValueColumn() == outlier {
continue
}
spaceWidths.add(info.valueColumn())
}
return spaceWidths.min, spaceWidths.max
}
func (va *VaralignBlock) traceWidths(minTotalWidth int, maxTotalWidth int, minVarnameOpWidth int, outlier int) {
if trace.Tracing {
trace.Stepf("Indentation including whitespace is between %d and %d.",
minTotalWidth, maxTotalWidth)
trace.Stepf("Minimum required indentation is %d + 1.", minVarnameOpWidth)
if outlier != 0 {
trace.Stepf("The outlier is at indentation %d.", outlier)
}
}
}
func (info *varalignLine) realignDetails(newWidth int, indentDiff *optInt, isMultiEmpty bool) {
switch {
case info.rawIndex == 0 && info.isContinuation():
info.alignValueInitial(newWidth)
case isMultiEmpty:
oldWidth := tabWidth(info.spaceBeforeValue)
if !indentDiff.isSet {
diff := 0
if newWidth != 0 {
diff = newWidth - oldWidth
if diff > 0 && !info.isCommentedOut() {
diff = 0
}
}
indentDiff.set(diff)
}
width := imax(oldWidth+indentDiff.get(), 8)
info.alignValueMultiFollow(width)
case info.rawIndex > 0:
width := imax(newWidth, info.valueColumn()+indentDiff.get())
info.alignValueMultiFollow(width)
default:
info.alignValueSingle(newWidth)
}
}
// VaralignSplitter parses the text of a raw line into those parts that
// are relevant for aligning the variable values, and the backslash
// continuation markers.
//
// See MkLineParser.unescapeComment for very similar code.
type VaralignSplitter struct {
}
func NewVaralignSplitter() VaralignSplitter {
return VaralignSplitter{}
}
func (s VaralignSplitter) split(rawText string, initial bool) varalignParts {
assert(!hasSuffix(rawText, "\n"))
parser := NewMkParser(nil, rawText)
lexer := parser.lexer
leadingComment := s.parseLeadingComment(lexer, initial)
varnameOp, spaceBeforeValue := s.parseVarnameOp(parser, initial)
value, spaceAfterValue, continuation := s.parseValue(lexer)
return varalignParts{
leadingComment,
varnameOp,
spaceBeforeValue,
value,
spaceAfterValue,
continuation,
}
}
func (VaralignSplitter) parseLeadingComment(lexer *textproc.Lexer, initial bool) string {
if hasPrefix(lexer.Rest(), "# ") {
return ""
}
comment := lexer.NextString("#")
if comment != "" {
return comment
}
mark := lexer.Mark()
if initial && lexer.SkipByte(' ') {
lexer.SkipHspace()
}
return lexer.Since(mark)
}
func (VaralignSplitter) parseVarnameOp(parser *MkParser, initial bool) (string, string) {
lexer := parser.lexer
if !initial {
return "", lexer.NextHspace()
}
mark := lexer.Mark()
_ = parser.mklex.Varname()
lexer.SkipHspace()
ok, _ := parser.Op()
assert(ok)
return lexer.Since(mark), lexer.NextHspace()
}
func (VaralignSplitter) parseValue(lexer *textproc.Lexer) (string, string, string) {
rest := lexer.Rest()
for !lexer.EOF() && lexer.PeekByte() != '#' && lexer.Rest() != "\\" {
if lexer.NextBytesSet(unescapeMkCommentSafeChars) != "" ||
lexer.SkipString("[#") ||
lexer.SkipByte('[') {
continue
}
assert(lexer.SkipByte('\\'))
lexer.Skip(1)
}
end := len(rest)
backslash := end
for backslash > 0 && rest[backslash-1] == '\\' {
backslash--
}
if (end-backslash)%2 == 0 {
return rest[:end], "", ""
}
continuation := rest[backslash:]
valueAndSpace := rest[:backslash]
value := rtrimHspace(valueAndSpace)
space := valueAndSpace[len(value):]
return value, space, continuation
}
type varalignMkLine struct {
infos []*varalignLine
}
// Is true for multilines that don't have a value in their first
// physical line.
//
// The follow-up lines of these lines may be indented with as few
// as a single tab. Example:
// VAR= \
// value1 \
// value2
// In all other lines, the indentation must be at least the indentation
// of the first value found.
func (l *varalignMkLine) isMultiEmpty() bool {
return l.infos[0].isEmptyContinuation()
}
// rightMargin calculates the column in which the continuation backslashes
// should be placed.
// In addition, it returns whether the right margin is already in its
// canonical form.
func (l *varalignMkLine) rightMargin() (ok bool, margin int) {
restIndex := condInt(l.infos[0].value == "", 1, 0)
infos := l.infos[restIndex:]
var columns []int
for _, info := range infos {
if info.isContinuation() {
space := info.spaceBeforeContinuation()
if space != "" && space != " " {
columns = append(columns, info.continuationColumn())
}
}
}
if len(columns) <= 1 {
return false, 0
}
sort.Ints(columns)
for i := len(columns) - 2; i >= 0; i-- {
col := columns[i]
if col == columns[i+1] {
ok := columns[0] == columns[len(columns)-1] && col <= 72
return ok, imin(col, 72)
}
}
var min int
for _, info := range infos {
if info.isContinuation() {
mainWidth := info.uptoValueWidth()
if mainWidth > min {
min = mainWidth
}
}
}
return false, min&-8 + 8
}
type varalignLine struct {
fixer Autofixer
rawIndex int
fixedSpaceBeforeContinuation bool
varalignParts
}
func (info *varalignLine) alignValueSingle(newWidth int) {
leadingComment := info.leadingComment
varnameOp := info.varnameOp
oldSpace := info.spaceBeforeValue
newSpace := alignmentToWidths(tabWidthSlice(leadingComment, varnameOp), newWidth)
// Indent the outlier with a space instead of a tab to keep the line short.
if newSpace == "" && info.isCanonicalInitial(newWidth) {
return
}
if newSpace == "" {
newSpace = " "
}
if newSpace == oldSpace {
return
}
hasSpace := strings.IndexByte(oldSpace, ' ') != -1
oldColumn := tabWidthSlice(leadingComment, varnameOp, oldSpace)
column := tabWidthSlice(leadingComment, varnameOp, newSpace)
fix := info.fixer.Autofix()
if newSpace == " " {
fix.Notef(
"This outlier variable value should be aligned " +
"with a single space.")
info.explainWrongColumn(fix)
} else if hasSpace && column != oldColumn {
fix.Notef(
"This variable value should be aligned "+
"with tabs, not spaces, to column %d instead of %d.",
column+1, oldColumn+1)
info.explainWrongColumn(fix)
} else if column != oldColumn {
fix.Notef(
"This variable value should be aligned to column %d instead of %d.",
column+1, oldColumn+1)
info.explainWrongColumn(fix)
} else {
fix.Notef("Variable values should be aligned with tabs, not spaces.")
}
fix.ReplaceAt(info.rawIndex, info.spaceBeforeValueIndex(), oldSpace, newSpace)
fix.Apply()
info.spaceBeforeValue = newSpace
}
func (info *varalignLine) alignValueInitial(newWidth int) {
if info.isEmptyContinuation() && info.valueColumn() > newWidth {
return
}
newSpace := alignmentToWidths(info.spaceBeforeValueColumn(), newWidth)
if newSpace == info.spaceBeforeValue {
return
}
info.alignValue(newWidth)
}
func (info *varalignLine) alignValueMultiFollow(newWidth int) {
newSpace := indent(newWidth)
if newSpace == info.spaceBeforeValue {
return
}
info.alignFollow(newSpace)
}
func (info *varalignLine) alignValue(width int) {
oldSpace := info.spaceBeforeValue
oldWidth := info.valueColumn()
newSpace := alignmentToWidths(info.spaceBeforeValueColumn(), width)
fix := info.fixer.Autofix()
if width != oldWidth && contains(oldSpace, " ") {
fix.Notef(
"This variable value should be aligned "+
"with tabs, not spaces, to column %d instead of %d.",
width+1, oldWidth+1)
} else if width != oldWidth {
fix.Notef(
"This variable value should be aligned "+
"to column %d instead of %d.",
width+1, oldWidth+1)
} else {
fix.Notef("Variable values should be aligned with tabs, not spaces.")
}
fix.ReplaceAt(info.rawIndex, info.spaceBeforeValueIndex(), oldSpace, newSpace)
fix.Apply()
info.spaceBeforeValue = newSpace
}
func (info *varalignLine) alignFollow(newSpace string) {
// Below a continuation marker, there may be a completely empty line.
// This is confusing to the human readers, but technically allowed.
if info.varalignParts.isEmpty() {
return
}
continuationColumn := 0
if info.spaceBeforeContinuation() != " " {
continuationColumn = imin(72, info.continuationColumn())
}
fix := info.fixer.Autofix()
fix.Notef("This continuation line should be indented with %q.", newSpace)
info.replaceSpaceBeforeValue(fix, newSpace)
if info.isContinuation() {
info.replaceSpaceBeforeContinuationSilently(fix, continuationColumn)
}
fix.Apply()
}
func (info *varalignLine) alignContinuation(valueColumn, rightMarginColumn int) {
if !info.isContinuation() {
return
}
oldSpace := info.spaceBeforeContinuation()
if oldSpace == " " {
return
}
column := info.continuationColumn()
if column <= 72 && oldSpace == "\t" {
return
}
if column == 72 || column == rightMarginColumn || column <= valueColumn {
return
}
newSpace := " "
fix := info.fixer.Autofix()
if oldSpace == "" || rightMarginColumn == 0 {
fix.Notef("The continuation backslash should be preceded by a single space or tab.")
} else if info.isTooLongFor(valueColumn) {
fix.Notef("The continuation backslash should be preceded by a single space.")
} else {
newSpace = alignmentToWidths(info.uptoValueWidth(), rightMarginColumn)
fix.Notef(
"The continuation backslash should be in column %d, not %d.",
rightMarginColumn+1, column+1)
}
index := info.continuationIndex() - len(oldSpace)
fix.ReplaceAt(info.rawIndex, index, oldSpace, newSpace)
info.setSpaceBeforeContinuation(newSpace)
fix.Apply()
}
func (info *varalignLine) replaceSpaceBeforeValue(fix *Autofix, newSpace string) {
index := info.spaceBeforeValueIndex()
fix.ReplaceAt(info.rawIndex, index, info.spaceBeforeValue, newSpace)
info.spaceBeforeValue = newSpace
}
func (info *varalignLine) replaceSpaceBeforeContinuationSilently(fix *Autofix, column int) {
if info.value == "" {
return
}
oldSpace := info.spaceBeforeContinuation()
if oldSpace == " " {
return
}
newSpaceColumn := info.uptoValueWidth()
newSpace := alignmentToWidths(newSpaceColumn, column)
if newSpace == "" {
newSpace = " "
}
if oldSpace == newSpace {
return
}
index := info.spaceBeforeContinuationIndex()
fix.ReplaceAt(info.rawIndex, index, oldSpace+"\\", newSpace+"\\")
info.varalignParts.setSpaceBeforeContinuation(newSpace)
info.fixedSpaceBeforeContinuation = true
}
func (*varalignLine) explainWrongColumn(fix *Autofix) {
fix.Explain(
"Normally, all variable values in a block should start at the same column.",
"This provides orientation, especially for sequences",
"of variables that often appear in the same order.",
"For these it suffices to look at the variable values only.",
"",
"There are some exceptions to this rule:",
"",
"Definitions for long variable names may be indented with a single space instead of tabs,",
"but only if they appear in a block that is otherwise indented with tabs.",
"",
"Variable definitions that span multiple lines are not checked for alignment at all.",
"",
"When the block contains something else than variable definitions",
"and directives like .if or .for, it is not checked at all.")
}
type varalignParts struct {
leadingComment string // either the # or some rarely used U+0020 spaces
varnameOp string // empty iff it is a follow-up line
spaceBeforeValue string // for follow-up lines, this is the indentation
value string // including any trailing comment
spaceAfterValue string
continuation string // either a single backslash or empty
}
func (p *varalignParts) String() string {
return p.leadingComment +
p.varnameOp + p.spaceBeforeValue +
p.value + p.spaceAfterValue +
p.continuation
}
func (p *varalignParts) leadingCommentIndex() int {
return 0
}
func (p *varalignParts) varnameOpIndex() int {
return p.leadingCommentIndex() + len(p.leadingComment)
}
// spaceBeforeValueIndex returns the string index at which the space before the value starts.
// It's the same as the end of the assignment operator. Example:
// #VAR= value
// The index is 5.
func (p *varalignParts) spaceBeforeValueIndex() int {
return p.varnameOpIndex() + len(p.varnameOp)
}
func (p *varalignParts) valueIndex() int {
return p.spaceBeforeValueIndex() + len(p.spaceBeforeValue)
}
func (p *varalignParts) spaceAfterValueIndex() int {
return p.valueIndex() + len(p.value)
}
func (p *varalignParts) continuationIndex() int {
return p.spaceAfterValueIndex() + len(p.spaceAfterValue)
}
func (p *varalignParts) leadingCommentColumn() int {
return 0
}
func (p *varalignParts) varnameOpColumn() int {
return tabWidthAppend(p.leadingCommentColumn(), p.leadingComment)
}
func (p *varalignParts) spaceBeforeValueColumn() int {
return tabWidthAppend(p.varnameOpColumn(), p.varnameOp)
}
func (p *varalignParts) valueColumn() int {
return tabWidthAppend(p.spaceBeforeValueColumn(), p.spaceBeforeValue)
}
func (p *varalignParts) spaceAfterValueColumn() int {
return tabWidthAppend(p.valueColumn(), p.value)
}
func (p *varalignParts) continuationColumn() int {
return tabWidthAppend(p.spaceAfterValueColumn(), p.spaceAfterValue)
}
// continuation returns whether this line ends with a backslash.
func (p *varalignParts) isContinuation() bool {
return p.continuation != ""
}
func (p *varalignParts) isEmptyContinuation() bool {
return p.value == "" && p.isContinuation()
}
func (p *varalignParts) isEmpty() bool {
return p.value == "" && !p.isContinuation()
}
func (p *varalignParts) spaceBeforeContinuation() string {
if p.value == "" {
return p.spaceBeforeValue
}
return p.spaceAfterValue
}
func (p *varalignParts) setSpaceBeforeContinuation(space string) {
if p.value == "" {
p.spaceBeforeValue = space
} else {
p.spaceAfterValue = space
}
}
func (p *varalignParts) spaceBeforeContinuationIndex() int {
assert(p.isContinuation())
assert(p.value != "")
return p.spaceAfterValueIndex()
}
func (p *varalignParts) uptoValueWidth() int {
if p.value != "" {
return p.spaceAfterValueColumn()
} else {
return p.spaceBeforeValueColumn()
}
}
func (p *varalignParts) isCommentedOut() bool {
return hasPrefix(p.leadingComment, "#")
}
// isCanonicalInitial returns whether the space between the assignment
// operator and the value has its canonical form, which is either
// at least one tab, or a single space, but only for lines that stick out.
func (p *varalignParts) isCanonicalInitial(column int) bool {
space := p.spaceBeforeValue
if space == "" {
return false
}
if space == " " && p.valueColumn() > column {
return true
}
return strings.TrimLeft(space, "\t") == ""
}
// isCanonicalFollow returns whether the space before the value has its
// canonical form, which is at least one tab, followed by up to 7 spaces.
func (p *varalignParts) isCanonicalFollow() bool {
column := p.valueColumn()
return column >= 8 && p.spaceBeforeValue == indent(column)
}
func (p *varalignParts) isTooLongFor(valueColumn int) bool {
column := tabWidthAppend(imax(valueColumn, 8), p.value)
if p.isContinuation() {
column += 2
}
return column > 73
}
|