Skip to content

Commit a3bd951

Browse files
committed
Improve indent detection
1 parent 7694097 commit a3bd951

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

gopls/internal/lsp/source/lines.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,29 @@ func splitLines(
5252
_, _, _, target := findSplitGroupTarget(file, fset, start, end)
5353

5454
// get the original line indent of target.
55+
firstLineIndent := getIndent(src, fset, target)
56+
eltIndent := firstLineIndent + "\t"
57+
58+
return fset, processLines(fset, target, src, file, ",\n", "\n", ",\n"+firstLineIndent, eltIndent), nil
59+
}
60+
61+
func getIndent(src []byte, fset *token.FileSet, target ast.Node) string {
62+
var pos token.Pos
63+
switch node := target.(type) {
64+
case *ast.FieldList:
65+
pos = node.Opening
66+
case *ast.CallExpr:
67+
pos = node.Lparen
68+
case *ast.CompositeLit:
69+
pos = node.Lbrace
70+
}
71+
5572
split := bytes.Split(src, []byte("\n"))
56-
targetLineNumber := safetoken.StartPosition(fset, target.Pos()).Line
73+
targetLineNumber := safetoken.StartPosition(fset, pos).Line
5774
firstLine := string(split[targetLineNumber-1])
5875
trimmed := strings.TrimFunc(string(firstLine), unicode.IsSpace)
5976

60-
firstLineIndent := firstLine[:strings.Index(firstLine, trimmed)]
61-
eltIndent := firstLineIndent + "\t"
62-
63-
return fset, processLines(fset, target, src, file, ",\n", "\n", ",\n"+firstLineIndent, eltIndent), nil
77+
return firstLine[:strings.Index(firstLine, trimmed)]
6478
}
6579

6680
func groupLines(
@@ -75,13 +89,7 @@ func groupLines(
7589
return fset, processLines(fset, target, src, file, ", ", "", "", ""), nil
7690
}
7791

78-
func processLines(
79-
fset *token.FileSet,
80-
target ast.Node,
81-
src []byte,
82-
file *ast.File,
83-
sep, prefix, suffix, indent string,
84-
) *analysis.SuggestedFix {
92+
func processLines(fset *token.FileSet, target ast.Node, src []byte, file *ast.File, sep, prefix, suffix, indent string) *analysis.SuggestedFix {
8593
var replPos, replEnd token.Pos
8694
var lines []string
8795

gopls/internal/test/marker/testdata/codeaction/splitlines.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@ func a() {
118118
)) //@codeaction("hello", "hello", "refactor.rewrite", indent, "Split parameters into separate lines")
119119
}
120120

121+
-- indent2/indent2.go --
122+
package indent2
123+
124+
import "fmt"
125+
126+
func a() {
127+
fmt.
128+
Println(1, 2, 3, fmt.Sprintf("hello %d", 4)) //@codeaction("1", "1", "refactor.rewrite", indent2, "Split parameters into separate lines")
129+
}
130+
131+
-- @indent2/indent2/indent2.go --
132+
package indent2
133+
134+
import "fmt"
135+
136+
func a() {
137+
fmt.
138+
Println(
139+
1,
140+
2,
141+
3,
142+
fmt.Sprintf("hello %d", 4),
143+
) //@codeaction("1", "1", "refactor.rewrite", indent2, "Split parameters into separate lines")
144+
}
145+
121146
-- structelts/structelts.go --
122147
package structelts
123148

0 commit comments

Comments
 (0)