Debian Codegraph @

gopls/internal/protocol/generate/generate.go

raw symbols SCIP
1 // Copyright 2022 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 main
6
7 import (
8 "bytes"
9 "fmt"
10 "log"
11 "strings"
12 )
13
14 // a newType is a type that needs a name and a definition
15 // These are the various types that the json specification doesn't name
16 type newType struct {
17 name string
18 properties Properties // for struct/literal types
19 items []*Type // for other types ("and", "tuple")
20 line int
21 kind string // Or, And, Tuple, Lit, Map
22 typ *Type
23 }
24
25 func generateDoc(out *bytes.Buffer, doc string) {
26 if doc == "" {
27 return
28 }
29
30 if !strings.Contains(doc, "\n") {
31 fmt.Fprintf(out, "// %s\n", doc)
32 return
33 }
34 var list bool
35 for line := range strings.SplitSeq(doc, "\n") {
36 // Lists in metaModel.json start with a dash.
37 // To make a go doc list they have to be preceded
38 // by a blank line, and indented.
39 // (see type TextDccumentFilter in protocol.go)
40 if len(line) > 0 && line[0] == '-' {
41 if !list {
42 list = true
43 fmt.Fprintf(out, "//\n")
44 }
45 fmt.Fprintf(out, "// %s\n", line)
46 } else {
47 if len(line) == 0 {
48 list = false
49 }
50 fmt.Fprintf(out, "// %s\n", line)
51 }
52 }
53 }
54
55 // decide if a property is optional, and if it needs a *
56 // return ",omitempty" if it is optional, and "*" if it needs a pointer
57 func propStar(name string, t NameType, gotype string) (omitempty, indirect bool) {
58 if t.Optional {
59 switch gotype {
60 case "uint32", "int32":
61 // in FoldingRange.endLine, 0 and empty have different semantics
62 // There seem to be no other cases.
63 default:
64 indirect = true
65 omitempty = true
66 }
67 }
68 if strings.HasPrefix(gotype, "[]") || strings.HasPrefix(gotype, "map[") {
69 indirect = false // passed by reference, so no need for *
70 } else {
71 switch gotype {
72 case "bool", "string", "interface{}", "any":
73 indirect = false // gopls compatibility if t.Optional
74 }
75 }
76 oind, oomit := indirect, omitempty
77 if newStar, ok := goplsStar[prop{name, t.Name}]; ok {
78 switch newStar {
79 case nothing:
80 indirect, omitempty = false, false
81 case wantOpt:
82 indirect, omitempty = false, true
83 case wantOptStar:
84 indirect, omitempty = true, true
85 }
86 if indirect == oind && omitempty == oomit { // no change
87 log.Printf("goplsStar[ {%q, %q} ](%d) useless %v/%v %v/%v", name, t.Name, t.Line, oind, indirect, oomit, omitempty)
88 }
89 usedGoplsStar[prop{name, t.Name}] = true
90 }
91
92 return
93 }
94
95 func goName(s string) string {
96 // Go naming conventions
97 if strings.HasSuffix(s, "Id") {
98 s = s[:len(s)-len("Id")] + "ID"
99 } else if strings.HasSuffix(s, "Uri") {
100 s = s[:len(s)-3] + "URI"
101 } else if s == "uri" {
102 s = "URI"
103 } else if s == "id" {
104 s = "ID"
105 }
106
107 // renames for temporary GOPLS compatibility
108 if news := goplsType[s]; news != "" {
109 usedGoplsType[s] = true
110 s = news
111 }
112 // Names beginning _ are not exported
113 if strings.HasPrefix(s, "_") {
114 s = strings.Replace(s, "_", "X", 1)
115 }
116 if s != "string" { // base types are unchanged (textDocuemnt/diagnostic)
117 // Title is deprecated, but a) s is only one word, b) replacement is too heavy-weight
118 s = strings.Title(s)
119 }
120 return s
121 }