@@ -1181,6 +1181,35 @@ func main() {
1181
1181
},
1182
1182
},
1183
1183
},
1184
+ "renaming" : {
1185
+ rootURI : "file:///src/test/pkg" ,
1186
+ fs : map [string ]string {
1187
+ "a.go" : `package p
1188
+ import "fmt"
1189
+
1190
+ func main() {
1191
+ str := A()
1192
+ fmt.Println(str)
1193
+ }
1194
+
1195
+ func A() string {
1196
+ return "test"
1197
+ }
1198
+ ` ,
1199
+ },
1200
+ cases : lspTestCases {
1201
+ wantRenames : map [string ]map [string ]string {
1202
+ "a.go:5:5" : map [string ]string {
1203
+ "4:4-4:7" : "/src/test/pkg/a.go" ,
1204
+ "5:16-5:19" : "/src/test/pkg/a.go" ,
1205
+ },
1206
+ "a.go:9:6" : map [string ]string {
1207
+ "4:11-4:12" : "/src/test/pkg/a.go" ,
1208
+ "8:5-8:6" : "/src/test/pkg/a.go" ,
1209
+ },
1210
+ },
1211
+ },
1212
+ },
1184
1213
}
1185
1214
1186
1215
func TestServer (t * testing.T ) {
@@ -1314,6 +1343,7 @@ type lspTestCases struct {
1314
1343
wantSignatures map [string ]string
1315
1344
wantWorkspaceReferences map [* lspext.WorkspaceReferencesParams ][]string
1316
1345
wantFormatting map [string ]map [string ]string
1346
+ wantRenames map [string ]map [string ]string
1317
1347
}
1318
1348
1319
1349
func copyFileToOS (ctx context.Context , fs * AtomicFS , targetFile , srcFile string ) error {
@@ -1495,6 +1525,12 @@ func lspTests(t testing.TB, ctx context.Context, h *LangHandler, c *jsonrpc2.Con
1495
1525
formattingTest (t , ctx , c , rootURI , file , want )
1496
1526
})
1497
1527
}
1528
+
1529
+ for pos , want := range cases .wantRenames {
1530
+ tbRun (t , fmt .Sprintf ("renaming-%s" , strings .Replace (pos , "/" , "-" , - 1 )), func (t testing.TB ) {
1531
+ renamingTest (t , ctx , c , rootURI , pos , want )
1532
+ })
1533
+ }
1498
1534
}
1499
1535
1500
1536
// tbRun calls (testing.T).Run or (testing.B).Run.
@@ -1716,6 +1752,29 @@ func formattingTest(t testing.TB, ctx context.Context, c *jsonrpc2.Conn, rootURI
1716
1752
}
1717
1753
}
1718
1754
1755
+ func renamingTest (t testing.TB , ctx context.Context , c * jsonrpc2.Conn , rootURI lsp.DocumentURI , pos string , want map [string ]string ) {
1756
+ file , line , char , err := parsePos (pos )
1757
+ if err != nil {
1758
+ t .Fatal (err )
1759
+ }
1760
+
1761
+ workspaceEdit , err := callRenaming (ctx , c , uriJoin (rootURI , file ), line , char , "" )
1762
+ if err != nil {
1763
+ t .Fatal (err )
1764
+ }
1765
+
1766
+ got := map [string ]string {}
1767
+ for file , edits := range workspaceEdit .Changes {
1768
+ for _ , edit := range edits {
1769
+ got [edit .Range .String ()] = util .UriToPath (lsp .DocumentURI (file ))
1770
+ }
1771
+ }
1772
+
1773
+ if ! reflect .DeepEqual (got , want ) {
1774
+ t .Errorf ("got %v, want: %v" , got , want )
1775
+ }
1776
+ }
1777
+
1719
1778
func parsePos (s string ) (file string , line , char int , err error ) {
1720
1779
parts := strings .Split (s , ":" )
1721
1780
if len (parts ) != 3 {
@@ -1970,6 +2029,16 @@ func callFormatting(ctx context.Context, c *jsonrpc2.Conn, uri lsp.DocumentURI)
1970
2029
return edits , err
1971
2030
}
1972
2031
2032
+ func callRenaming (ctx context.Context , c * jsonrpc2.Conn , uri lsp.DocumentURI , line , char int , newName string ) (lsp.WorkspaceEdit , error ) {
2033
+ var edit lsp.WorkspaceEdit
2034
+ err := c .Call (ctx , "textDocument/rename" , lsp.RenameParams {
2035
+ TextDocument : lsp.TextDocumentIdentifier {URI : uri },
2036
+ Position : lsp.Position {Line : line , Character : char },
2037
+ NewName : newName ,
2038
+ }, & edit )
2039
+ return edit , err
2040
+ }
2041
+
1973
2042
type markedStrings []lsp.MarkedString
1974
2043
1975
2044
func (v * markedStrings ) UnmarshalJSON (data []byte ) error {
0 commit comments