golang.org/x/tools/gopls@v0.15.3/internal/protocol/semantic.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package protocol
     6  
     7  // The file defines helpers for semantics tokens.
     8  
     9  import "fmt"
    10  
    11  // SemanticTypes to use in case there is no client, as in the command line, or tests
    12  func SemanticTypes() []string {
    13  	return semanticTypes[:]
    14  }
    15  
    16  // SemanticModifiers to use in case there is no client.
    17  func SemanticModifiers() []string {
    18  	return semanticModifiers[:]
    19  }
    20  
    21  // SemType returns a string equivalent of the type, for gopls semtok
    22  func SemType(n int) string {
    23  	tokTypes := SemanticTypes()
    24  	tokMods := SemanticModifiers()
    25  	if n >= 0 && n < len(tokTypes) {
    26  		return tokTypes[n]
    27  	}
    28  	// not found for some reason
    29  	return fmt.Sprintf("?%d[%d,%d]?", n, len(tokTypes), len(tokMods))
    30  }
    31  
    32  // SemMods returns the []string equivalent of the mods, for gopls semtok.
    33  func SemMods(n int) []string {
    34  	tokMods := SemanticModifiers()
    35  	mods := []string{}
    36  	for i := 0; i < len(tokMods); i++ {
    37  		if (n & (1 << uint(i))) != 0 {
    38  			mods = append(mods, tokMods[i])
    39  		}
    40  	}
    41  	return mods
    42  }
    43  
    44  // From https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_semanticTokens
    45  var (
    46  	semanticTypes = [...]string{
    47  		"namespace", "type", "class", "enum", "interface",
    48  		"struct", "typeParameter", "parameter", "variable", "property", "enumMember",
    49  		"event", "function", "method", "macro", "keyword", "modifier", "comment",
    50  		"string", "number", "regexp", "operator",
    51  	}
    52  	semanticModifiers = [...]string{
    53  		"declaration", "definition", "readonly", "static",
    54  		"deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary",
    55  	}
    56  )