Fix gopls replace_symbol_body corrupting type/var/const declarations#1530
Fix gopls replace_symbol_body corrupting type/var/const declarations#1530tphakala wants to merge 1 commit into
Conversation
|
The failing CI check is unrelated to this change. The only failing test is This PR only touches the Go language server ( This looks like a CI environment issue rather than a regression: the same test fails the same way on other unrelated open PRs (for example #1528), and it was still passing on |
gopls reports the document-symbol range of single type/var/const declarations starting at the identifier (after the keyword), unlike func declarations whose range includes the func keyword. Serena derives both the displayed body and the replacement range from that range, so the keyword was dropped from both for these declarations and a keyword-inclusive edit corrupted the file (type Foo became type type Foo). Override request_document_symbols in gopls to extend the range start back to the keyword when only the keyword and indentation precede the identifier, recomputing the body so it stays consistent with the replacement range. Funcs, grouped declarations, and selectionRange are left untouched. Adds a unit test for the symbol body and range plus an end-to-end replace_symbol_body round-trip regression test.
fef00bb to
ec8cfe1
Compare
Fixes #1529
Problem
gopls reports the document-symbol range of single
type/var/constdeclarations starting at the identifier (after the keyword), unlikefuncdeclarations whose range includes thefunckeyword. Serena derives both the displayed symbol body and thereplace_symbol_bodyreplacement range from that range, so the keyword is dropped from both for these declarations. A keyword-inclusive edit then corrupts the file (type Foobecomestype type Foo), the tool returnsOK, and the resulting syntax error breaks subsequent symbolic edits on the file.Fix
Override
request_document_symbolsingopls.pyto extend the range start of a singletype/var/constdeclaration back to the keyword when only that keyword (plus optional indentation) precedes the identifier on the start line, mirroring the existingnixd/fortranpost-processing pattern. The body is recomputed so the displayed body stays consistent with the replacement range.funcdeclarations (range already at the keyword) and grouped declarations (var ( ... ), keyword on a separate line) are left untouched, andselectionRangeis preserved so references and rename continue to point at the identifier.The override operates on copies so the cached symbols are not mutated.
Tests
test/solidlsp/go/test_go_basic.py::TestGoLanguageServer::test_type_var_const_body_includes_leading_keyword: asserts the body and range oftype/var/constsymbols include the keyword, thatselectionRangestill points at the identifier, and that grouped vars are unaffected.test/serena/test_symbol_editing.py::test_go_symbol_replacement_no_double_keyword: an end-to-endreplace_symbol_bodyround-trip that producestype typeon the old code and passes with the fix.Both tests fail against the pre-fix code and pass with the fix.
poe format,poe type-check, and the Go test suite are green (gopls v0.22.0, go 1.26.1).