@@ -60,12 +60,47 @@ func OpeningLine(styles []CommentStyle) string {
60
60
61
61
// visible for testing
62
62
func MatchingLine (line string , styles []CommentStyle ) string {
63
- middleLine := fmt .Sprintf (`[\t\v\f\r ]*%s?[\t\v\f\r ]*\Q%s\E[,.;:?!\t\v\f\r ]*\n?` , combineRegexes (styles , func (style CommentStyle ) string { return style .GetString () }), line )
63
+ openingStyleSymbolRegex := combineRegexes (styles , func (style CommentStyle ) string { return style .GetString () })
64
+ normalizedLine := normalizePunctuation (line )
65
+ middleLine := fmt .Sprintf (`[\t\v\f\r ]*%s?[\t\v\f\r ]*\Q%s\E[,.;:?!\t\v\f\r ]*\n?` , openingStyleSymbolRegex , normalizedLine )
64
66
builder := strings.Builder {}
65
67
builder .WriteString (middleLine )
66
68
return builder .String ()
67
69
}
68
70
71
+ func normalizePunctuation (line string ) string {
72
+ ignore := `\E.?\Q`
73
+ normalizedLine := ""
74
+ // we could use a(n only) slightly better heuristic with a regex matching all dots
75
+ // not prefixed by a digit or a couple of { and 0-n spaces
76
+ // but no support for negative lookbehind in Golang regex engine so here we go :(
77
+ for k , v := range line {
78
+ if v == ',' || v == ';' || v == ':' || v == '?' || v == '!' {
79
+ normalizedLine += ignore
80
+ } else if v == '.' && (k == len (line )- 1 || line [k :k + 2 ] == ". " ) {
81
+ // dots in template expressions must and in numerical expressions should be preserved
82
+ // hence the poor heuristic of the following space or dot as last line's character
83
+ normalizedLine += ignore
84
+ } else {
85
+ normalizedLine += string (v )
86
+ }
87
+ }
88
+ result := strings .NewReplacer (
89
+ `\t\t` , `\t` ,
90
+ `\v\v` , `\v` ,
91
+ `\f\f` , `\f` ,
92
+ `\r\r` , `\r` ,
93
+ " " , " " ,
94
+ ).Replace (normalizedLine )
95
+ return strings .NewReplacer (
96
+ "\t " , `\E\t+\Q` ,
97
+ "\v " , `\E\v+\Q` ,
98
+ "\f " , `\E\f+\Q` ,
99
+ "\r " , `\E\r+\Q` ,
100
+ " " , `\E +\Q` ,
101
+ ).Replace (result )
102
+ }
103
+
69
104
// visible for testing
70
105
func ClosingLine (styles []CommentStyle ) string {
71
106
closingLine := fmt .Sprintf (`(?:[\t\v\f\r ]*%s[\t\v\f\r ]*)?` , combineRegexes (styles , func (style CommentStyle ) string { return style .GetClosingString () }))
0 commit comments