Skip to content

Commit 778496b

Browse files
authored
LSP fix syntax highlight on semantic tokens (#4065)
1 parent 760e4be commit 778496b

File tree

4 files changed

+19
-28
lines changed

4 files changed

+19
-28
lines changed

private/buf/buflsp/file.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import (
4444
"github.com/bufbuild/protocompile/experimental/incremental"
4545
"github.com/bufbuild/protocompile/experimental/incremental/queries"
4646
"github.com/bufbuild/protocompile/experimental/ir"
47-
"github.com/bufbuild/protocompile/experimental/report"
4847
"github.com/bufbuild/protocompile/experimental/seq"
4948
"github.com/bufbuild/protocompile/experimental/source"
5049
"go.lsp.dev/protocol"
@@ -656,7 +655,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
656655
}
657656
msg.def = msg
658657
resolved = append(resolved, msg)
659-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsType().Options(), report.Span{})...)
658+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsType().Options())...)
660659
case ir.SymbolKindEnum:
661660
enum := &symbol{
662661
ir: irSymbol,
@@ -668,7 +667,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
668667
}
669668
enum.def = enum
670669
resolved = append(resolved, enum)
671-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsType().Options(), report.Span{})...)
670+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsType().Options())...)
672671
case ir.SymbolKindEnumValue:
673672
name := &symbol{
674673
ir: irSymbol,
@@ -689,7 +688,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
689688
}
690689
tag.def = tag
691690
resolved = append(resolved, tag)
692-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMember().Options(), report.Span{})...)
691+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMember().Options())...)
693692
case ir.SymbolKindField:
694693
typ := &symbol{
695694
ir: irSymbol,
@@ -723,7 +722,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
723722
}
724723
tag.def = tag
725724
resolved = append(resolved, tag)
726-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMember().Options(), report.Span{})...)
725+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMember().Options())...)
727726
case ir.SymbolKindExtension:
728727
// TODO: we should figure out if we need to do any resolution here.
729728
ext := &symbol{
@@ -735,7 +734,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
735734
},
736735
}
737736
resolved = append(resolved, ext)
738-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMember().Options(), report.Span{})...)
737+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMember().Options())...)
739738
case ir.SymbolKindOneof:
740739
oneof := &symbol{
741740
ir: irSymbol,
@@ -747,7 +746,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
747746
}
748747
oneof.def = oneof
749748
resolved = append(resolved, oneof)
750-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsOneof().Options(), report.Span{})...)
749+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsOneof().Options())...)
751750
case ir.SymbolKindService:
752751
service := &symbol{
753752
ir: irSymbol,
@@ -759,7 +758,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
759758
}
760759
service.def = service
761760
resolved = append(resolved, service)
762-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsService().Options(), report.Span{})...)
761+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsService().Options())...)
763762
case ir.SymbolKindMethod:
764763
method := &symbol{
765764
ir: irSymbol,
@@ -793,7 +792,7 @@ func (f *file) irToSymbols(irSymbol ir.Symbol) ([]*symbol, []*symbol) {
793792
},
794793
}
795794
unresolved = append(unresolved, outputSym)
796-
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMethod().Options(), report.Span{})...)
795+
unresolved = append(unresolved, f.messageToSymbols(irSymbol.AsMethod().Options())...)
797796
}
798797
return resolved, unresolved
799798
}
@@ -822,19 +821,14 @@ func (f *file) importToSymbol(imp ir.Import) *symbol {
822821
}
823822
}
824823

825-
func (f *file) messageToSymbols(msg ir.MessageValue, prefix report.Span) []*symbol {
824+
func (f *file) messageToSymbols(msg ir.MessageValue) []*symbol {
826825
var symbols []*symbol
827826
for field := range msg.Fields() {
828827
if field.ValueAST().IsZero() {
829828
continue
830829
}
831830
for element := range seq.Values(field.Elements()) {
832831
span := element.Value().KeyASTs().At(element.ValueNodeIndex()).Span()
833-
// We only want the subset of the option path that applies specifically to this element,
834-
// and we trim the prefix.
835-
if !prefix.IsZero() {
836-
span.Start = prefix.End
837-
}
838832
elem := &symbol{
839833
// NOTE: no [ir.Symbol] for option elements
840834
file: f,
@@ -846,7 +840,7 @@ func (f *file) messageToSymbols(msg ir.MessageValue, prefix report.Span) []*symb
846840
}
847841
symbols = append(symbols, elem)
848842
if !element.AsMessage().IsZero() {
849-
symbols = append(symbols, f.messageToSymbols(element.AsMessage(), span)...)
843+
symbols = append(symbols, f.messageToSymbols(element.AsMessage())...)
850844
}
851845
}
852846
}
@@ -869,8 +863,8 @@ func (f *file) SymbolAt(ctx context.Context, cursor protocol.Position) *symbol {
869863
}
870864
symbol := f.symbols[idx]
871865
f.lsp.logger.DebugContext(ctx, "found symbol", slog.Any("symbol", symbol))
872-
// Check that cursor is before the end of the symbol.
873-
if comparePositions(symbol.Range().End, cursor) <= 0 {
866+
// Check that cursor is before the end of the symbol. Range is half-open [Start, End).
867+
if comparePositions(symbol.Range().End, cursor) < 0 {
874868
return nil
875869
}
876870

private/buf/buflsp/jsonrpc_wrappers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (c *connWrapper) Call(
7272
xslog.ErrorAttr(err),
7373
)
7474
} else {
75-
c.logger.Warn(
75+
c.logger.Debug(
7676
"call returned",
7777
slog.String("method", method),
7878
slog.Any("result", result),

private/buf/buflsp/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,19 +485,19 @@ func (s *server) SemanticTokensFull(
485485
newLine := uint32(i - 1)
486486
var newCol uint32
487487
if i == startLocation.Line {
488-
newCol = uint32(startLocation.Column)
488+
newCol = uint32(startLocation.Column - 1)
489489
if prevLine == newLine {
490490
newCol -= prevCol
491491
}
492492
}
493-
symbolLen := uint32(endLocation.Column)
493+
symbolLen := uint32(endLocation.Column - 1)
494494
if i == startLocation.Line {
495-
symbolLen -= uint32(startLocation.Column)
495+
symbolLen -= uint32(startLocation.Column - 1)
496496
}
497497
encoded = append(encoded, newLine-prevLine, newCol, symbolLen, semanticType, 0)
498498
prevLine = newLine
499499
if i == startLocation.Line {
500-
prevCol = uint32(startLocation.Column)
500+
prevCol = uint32(startLocation.Column - 1)
501501
} else {
502502
prevCol = 0
503503
}

private/buf/buflsp/symbol.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ func (s *symbol) LogValue() slog.Value {
170170
//
171171
// Returns the empty string if no docs are available.
172172
func (s *symbol) FormatDocs(ctx context.Context) string {
173-
missingDocs := "<missing docs>"
174-
var tooltip strings.Builder
175173
var def ast.DeclDef
176174
switch s.kind.(type) {
177175
case *imported:
@@ -240,7 +238,7 @@ func (s *symbol) FormatDocs(ctx context.Context) string {
240238
if ok {
241239
return strings.Join(comments, "\n")
242240
}
243-
return missingDocs
241+
return ""
244242
case *referenceable:
245243
referenceable, _ := s.kind.(*referenceable)
246244
def = referenceable.ast
@@ -251,11 +249,10 @@ func (s *symbol) FormatDocs(ctx context.Context) string {
251249
reference, _ := s.kind.(*reference)
252250
def = reference.def
253251
}
252+
var tooltip strings.Builder
254253
docs := getCommentsFromDef(def)
255254
if docs != "" {
256255
fmt.Fprintln(&tooltip, docs)
257-
} else {
258-
fmt.Fprintln(&tooltip, missingDocs)
259256
}
260257
return tooltip.String()
261258
}

0 commit comments

Comments
 (0)