Skip to content

Commit cd5f6fc

Browse files
author
sutong.527608
committed
Fix hasBlankLineBeforeStatement to scan backwards
The loop condition was incorrectly scanning forward from pos to stmt.End(). Fixed to scan backwards from pos-1 to find preceding newlines before the statement, which correctly identifies blank lines in the leading trivia.
1 parent 965bd16 commit cd5f6fc

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

internal/ls/organizeimports_service.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,18 @@ func (l *LanguageService) hasBlankLineBeforeStatement(file *ast.SourceFile, stmt
154154
pos := stmt.Pos()
155155
newlineCount := 0
156156

157-
for i := pos; i < stmt.End() && i < len(text); i++ {
157+
i := pos - 1
158+
for i >= 0 {
158159
ch := text[i]
159160
if ch == '\n' {
160161
newlineCount++
161-
} else if ch == '\r' {
162-
if i+1 < len(text) && text[i+1] == '\n' {
163-
i++
162+
i--
163+
if i >= 0 && text[i] == '\r' {
164+
i--
164165
}
165-
newlineCount++
166-
} else if ch != ' ' && ch != '\t' {
166+
} else if ch == ' ' || ch == '\t' || ch == '\r' {
167+
i--
168+
} else {
167169
break
168170
}
169171
}

0 commit comments

Comments
 (0)