Skip to content

Commit ed983b2

Browse files
committed
x/tools/gopls: add "controlFlow" semantic tokens modifier
While it's not defined in the LSP specification (as of version 3.17) it's supported by other tools such as Rust Analyzer Fixes golang/go#67663
1 parent 019da39 commit ed983b2

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

gopls/internal/golang/semtok.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
356356
tv.token(n.OpPos, len(n.Op.String()), semtok.TokOperator, nil)
357357
case *ast.BlockStmt:
358358
case *ast.BranchStmt:
359-
tv.token(n.TokPos, len(n.Tok.String()), semtok.TokKeyword, nil)
359+
tv.token(n.TokPos, len(n.Tok.String()), semtok.TokKeyword, []string{"controlFlow"})
360360
if n.Label != nil {
361-
tv.token(n.Label.Pos(), len(n.Label.Name), semtok.TokLabel, nil)
361+
tv.token(n.Label.Pos(), len(n.Label.Name), semtok.TokLabel, []string{"controlFlow"})
362362
}
363363
case *ast.CallExpr:
364364
if n.Ellipsis.IsValid() {
@@ -369,7 +369,7 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
369369
if n.List == nil {
370370
iam = "default"
371371
}
372-
tv.token(n.Case, len(iam), semtok.TokKeyword, nil)
372+
tv.token(n.Case, len(iam), semtok.TokKeyword, []string{"controlFlow"})
373373
case *ast.ChanType:
374374
// chan | chan <- | <- chan
375375
switch {
@@ -392,15 +392,15 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
392392
case *ast.CompositeLit:
393393
case *ast.DeclStmt:
394394
case *ast.DeferStmt:
395-
tv.token(n.Defer, len("defer"), semtok.TokKeyword, nil)
395+
tv.token(n.Defer, len("defer"), semtok.TokKeyword, []string{"controlFlow"})
396396
case *ast.Ellipsis:
397397
tv.token(n.Ellipsis, len("..."), semtok.TokOperator, nil)
398398
case *ast.EmptyStmt:
399399
case *ast.ExprStmt:
400400
case *ast.Field:
401401
case *ast.FieldList:
402402
case *ast.ForStmt:
403-
tv.token(n.For, len("for"), semtok.TokKeyword, nil)
403+
tv.token(n.For, len("for"), semtok.TokKeyword, []string{"controlFlow"})
404404
case *ast.FuncDecl:
405405
case *ast.FuncLit:
406406
case *ast.FuncType:
@@ -410,15 +410,15 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
410410
case *ast.GenDecl:
411411
tv.token(n.TokPos, len(n.Tok.String()), semtok.TokKeyword, nil)
412412
case *ast.GoStmt:
413-
tv.token(n.Go, len("go"), semtok.TokKeyword, nil)
413+
tv.token(n.Go, len("go"), semtok.TokKeyword, []string{"controlFlow"})
414414
case *ast.Ident:
415415
tv.ident(n)
416416
case *ast.IfStmt:
417-
tv.token(n.If, len("if"), semtok.TokKeyword, nil)
417+
tv.token(n.If, len("if"), semtok.TokKeyword, []string{"controlFlow"})
418418
if n.Else != nil {
419419
// x.Body.End() or x.Body.End()+1, not that it matters
420420
pos := tv.findKeyword("else", n.Body.End(), n.Else.Pos())
421-
tv.token(pos, len("else"), semtok.TokKeyword, nil)
421+
tv.token(pos, len("else"), semtok.TokKeyword, []string{"controlFlow"})
422422
}
423423
case *ast.ImportSpec:
424424
tv.importSpec(n)
@@ -436,36 +436,36 @@ func (tv *tokenVisitor) inspect(n ast.Node) (descend bool) {
436436
tv.token(n.Map, len("map"), semtok.TokKeyword, nil)
437437
case *ast.ParenExpr:
438438
case *ast.RangeStmt:
439-
tv.token(n.For, len("for"), semtok.TokKeyword, nil)
439+
tv.token(n.For, len("for"), semtok.TokKeyword, []string{"controlFlow"})
440440
// x.TokPos == token.NoPos is legal (for range foo {})
441441
offset := n.TokPos
442442
if offset == token.NoPos {
443443
offset = n.For
444444
}
445445
pos := tv.findKeyword("range", offset, n.X.Pos())
446-
tv.token(pos, len("range"), semtok.TokKeyword, nil)
446+
tv.token(pos, len("range"), semtok.TokKeyword, []string{"controlFlow"})
447447
case *ast.ReturnStmt:
448-
tv.token(n.Return, len("return"), semtok.TokKeyword, nil)
448+
tv.token(n.Return, len("return"), semtok.TokKeyword, []string{"controlFlow"})
449449
case *ast.SelectStmt:
450-
tv.token(n.Select, len("select"), semtok.TokKeyword, nil)
450+
tv.token(n.Select, len("select"), semtok.TokKeyword, []string{"controlFlow"})
451451
case *ast.SelectorExpr:
452452
case *ast.SendStmt:
453-
tv.token(n.Arrow, len("<-"), semtok.TokOperator, nil)
453+
tv.token(n.Arrow, len("<-"), semtok.TokOperator, []string{"controlFlow"})
454454
case *ast.SliceExpr:
455455
case *ast.StarExpr:
456456
tv.token(n.Star, len("*"), semtok.TokOperator, nil)
457457
case *ast.StructType:
458458
tv.token(n.Struct, len("struct"), semtok.TokKeyword, nil)
459459
case *ast.SwitchStmt:
460-
tv.token(n.Switch, len("switch"), semtok.TokKeyword, nil)
460+
tv.token(n.Switch, len("switch"), semtok.TokKeyword, []string{"controlFlow"})
461461
case *ast.TypeAssertExpr:
462462
if n.Type == nil {
463463
pos := tv.findKeyword("type", n.Lparen, n.Rparen)
464464
tv.token(pos, len("type"), semtok.TokKeyword, nil)
465465
}
466466
case *ast.TypeSpec:
467467
case *ast.TypeSwitchStmt:
468-
tv.token(n.Switch, len("switch"), semtok.TokKeyword, nil)
468+
tv.token(n.Switch, len("switch"), semtok.TokKeyword, []string{"controlFlow"})
469469
case *ast.UnaryExpr:
470470
tv.token(n.OpPos, len(n.Op.String()), semtok.TokOperator, nil)
471471
case *ast.ValueSpec:

gopls/internal/protocol/semantic.go

+1
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ var (
5252
semanticModifiers = [...]string{
5353
"declaration", "definition", "readonly", "static",
5454
"deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary",
55+
"controlFlow",
5556
}
5657
)

0 commit comments

Comments
 (0)