Debian Codegraph @

gopls/internal/file/kind.go

raw symbols SCIP
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 file
6
7 import (
8 "fmt"
9
10 "golang.org/x/tools/gopls/internal/protocol"
11 )
12
13 // Kind describes the kind of the file in question.
14 // It can be one of Go,mod, Sum, or Tmpl.
15 type Kind int
16
17 const (
18 // UnknownKind is a file type we don't know about.
19 UnknownKind = Kind(iota)
20
21 // Go is a Go source file.
22 Go
23 // Mod is a go.mod file.
24 Mod
25 // Sum is a go.sum file.
26 Sum
27 // Tmpl is a template file.
28 Tmpl
29 // Work is a go.work file.
30 Work
31 // Asm is a Go assembly (.s) file.
32 Asm
33 )
34
35 func (k Kind) String() string {
36 switch k {
37 case Go:
38 return "go"
39 case Mod:
40 return "go.mod"
41 case Sum:
42 return "go.sum"
43 case Tmpl:
44 return "tmpl"
45 case Work:
46 return "go.work"
47 case Asm:
48 return "Go assembly"
49 default:
50 return fmt.Sprintf("internal error: unknown file kind %d", k)
51 }
52 }
53
54 // KindForLang returns the gopls file [Kind] associated with the given LSP
55 // LanguageKind string from the LanguageID field of [protocol.TextDocumentItem],
56 // or UnknownKind if the language is not one recognized by gopls.
57 func KindForLang(langID protocol.LanguageKind) Kind {
58 switch langID {
59 case "go":
60 return Go
61 case "go.mod":
62 return Mod
63 case "go.sum":
64 return Sum
65 case "tmpl", "gotmpl":
66 return Tmpl
67 case "go.work":
68 return Work
69 case "go.s":
70 return Asm
71 default:
72 return UnknownKind
73 }
74 }