Skip to content

Commit

Permalink
Fix autofix mode for non-latin comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak committed May 16, 2020
1 parent d40d72a commit 4e037ac
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
7 changes: 5 additions & 2 deletions godot.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func check(fset *token.FileSet, group *ast.CommentGroup) (iss Issue, ok bool) {
func checkComment(comment string) (pos position, ok bool) {
// Check last line of "//"-comment
if strings.HasPrefix(comment, "//") {
pos.column = len(comment)
pos.column = len([]rune(comment)) // runes for non-latin chars
comment = strings.TrimPrefix(comment, "//")
if checkLastChar(comment) {
return position{}, true
Expand All @@ -189,7 +189,9 @@ func checkComment(comment string) (pos position, ok bool) {
comment = lines[i]
comment = strings.TrimSuffix(comment, "*/")
comment = strings.TrimRight(comment, " ")
pos.column = len(comment) // last non-space char in comment line
// Get position of the last non-space char in comment line, use runes
// in case of non-latin chars
pos.column = len([]rune(comment))
comment = strings.TrimPrefix(comment, "/*")

if checkLastChar(comment) {
Expand Down Expand Up @@ -238,6 +240,7 @@ func makeReplacement(s string, pos position) string {
line := []rune(lines[pos.line])
if len(line) < pos.column {
// This should never happen
println(s, len(line), pos.column)
return ""
}
// Insert a period
Expand Down
38 changes: 37 additions & 1 deletion godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ func TestCheckComment(t *testing.T) {
ok: true,
pos: position{},
},
{
name: "singleline comment: cyrillic, with period",
comment: "// Кириллица.",
ok: true,
pos: position{},
},
{
name: "singleline comment: cyrillic, without period",
comment: "// Кириллица",
ok: false,
pos: position{line: 0, column: 12},
},
// Multiline comments
{
name: "multiline comment: ok",
Expand Down Expand Up @@ -212,10 +224,22 @@ func TestCheckComment(t *testing.T) {
},
{
name: "multiline comment: url at the end of line",
comment: "/*\n" + "Read more: http://example.com/" + "*/",
comment: "/*\n" + "Read more: http://example.com/\n" + "*/",
ok: true,
pos: position{},
},
{
name: "multiline comment: cyrillic, with period",
comment: "/*\n" + "Кириллица.\n" + "*/",
ok: true,
pos: position{},
},
{
name: "multiline comment: cyrillic, without period",
comment: "/*\n" + "Кириллица\n" + "*/",
ok: false,
pos: position{line: 1, column: 9},
},
}

for _, tt := range testCases {
Expand Down Expand Up @@ -254,6 +278,12 @@ func TestMakeReplacement(t *testing.T) {
pos: position{line: 0, column: 3},
replacement: "//x.",
},
{
name: "cyrillic singleline comment",
comment: "// Привет, мир",
pos: position{line: 0, column: 14},
replacement: "// Привет, мир.",
},
{
name: "multiline comment",
comment: "/*\n" + "Hello, world\n" + "*/",
Expand All @@ -272,6 +302,12 @@ func TestMakeReplacement(t *testing.T) {
pos: position{line: 0, column: 15},
replacement: "/* Hello, world. */",
},
{
name: "cyrillic multiline comment",
comment: "/* Привет, мир */",
pos: position{line: 0, column: 14},
replacement: "/* Привет, мир. */",
},
}

for _, tt := range testCases {
Expand Down
5 changes: 5 additions & 0 deletions testdata/checkall/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func CgoExportedFunction(a, b int) int {
return a + b
}

// Кириллица FAIL
func NonLatin() string {
return "привет, мир"
}

func noComment() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
Expand Down
5 changes: 5 additions & 0 deletions testdata/checkall/result/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func CgoExportedFunction(a, b int) int {
return a + b
}

// Кириллица FAIL.
func NonLatin() string {
return "привет, мир"
}

func noComment() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
Expand Down
5 changes: 5 additions & 0 deletions testdata/default/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func CgoExportedFunction(a, b int) int {
return a + b
}

// Кириллица FAIL
func NonLatin() string {
return "привет, мир"
}

func noComment() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
Expand Down
5 changes: 5 additions & 0 deletions testdata/default/result/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func CgoExportedFunction(a, b int) int {
return a + b
}

// Кириллица FAIL.
func NonLatin() string {
return "привет, мир"
}

func noComment() {
cs := C.CString("Hello from stdio\n")
C.myprint(cs)
Expand Down

0 comments on commit 4e037ac

Please sign in to comment.