Skip to content

Commit d32ec34

Browse files
h9jianggopherbot
authored andcommitted
gopls/internal/protocol/generate: move injections to tables.go
For golang/go#76331 Change-Id: Id14b41c56789ad0fefc6fbd43bca9065a9008b60 Reviewed-on: https://go-review.googlesource.com/c/tools/+/722740 Auto-Submit: Hongxiang Jiang <hxjiang@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 98d172d commit d32ec34

3 files changed

Lines changed: 59 additions & 61 deletions

File tree

gopls/internal/protocol/generate/main.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,6 @@ func writeserver() {
147147
`)
148148
out.WriteString("type Server interface {\n")
149149
for _, k := range sdecls.keys() {
150-
if k == "codeAction/resolve" {
151-
sdecls[k] = `// To support microsoft/language-server-protocol#1164, the language server
152-
// need to read the client-supplied form answers and either returns a
153-
// CodeAction with errors in the form fields surfacing the error to the
154-
// client, or a CodeAction with properties the language client is waiting
155-
// for (e.g. edits, commands).
156-
//
157-
// The language client may call "codeAction/resolve" if the language server
158-
// returns a CodeAction with errors or try asking the user for completing the
159-
// form again.
160-
//
161-
// The language client may call "codeAction/resolve" multiple times with user
162-
// filled (re-filled) answers in the form until it obtains a CodeAction with
163-
// properties (e.g. edits, commands) it's waiting for.
164-
//
165-
` + sdecls[k]
166-
}
167150
out.WriteString(sdecls[k])
168151
}
169152
out.WriteString(`
@@ -212,50 +195,6 @@ func writeprotocol() {
212195
hack("WorkspaceFoldersServerCapabilities", "WorkspaceFolders5Gn")
213196
hack("_InitializeParams", "XInitializeParams")
214197

215-
// Insert a block of content into a type.
216-
insert := func(key, content string) {
217-
if _, ok := types[key]; !ok {
218-
log.Fatalf("types[%q] not found", key)
219-
}
220-
idx := strings.LastIndex(types[key], "}")
221-
if idx == -1 {
222-
log.Fatalf("could not find '}' in type %q", key)
223-
}
224-
types[key] = types[key][:idx] + content + types[key][idx:]
225-
}
226-
227-
// TODO(hxjiang): extend form resolve to codelens resolve.
228-
insert("CodeAction", `
229-
// FormFields and FormAnswers allow the server and client to exchange
230-
// interactive questions and answers during a resolveCodeAction request.
231-
//
232-
// The server populates FormFields to define the schema. The server may
233-
// optionally populate FormAnswers to preserve previous user input; if
234-
// provided, the client may present these as default values.
235-
//
236-
// When the client responds, it must provide FormAnswers. The client is not
237-
// required to send FormFields back to the server.
238-
239-
// FormFields defines the questions and validation errors.
240-
//
241-
// This is a server-to-client field. The language server defines these, and
242-
// the client uses them to render the form.
243-
//
244-
// Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#1164.
245-
FormFields []FormField `+"`json:\"formFields,omitempty\"`")
246-
insert("CodeAction", `
247-
// FormAnswers contains the values for the form questions.
248-
//
249-
// When sent by the language server, this field is optional but recommended
250-
// to support editing previous values.
251-
//
252-
// When sent by the language client, this field is required. The slice must
253-
// have the same length as FormFields (one answer per question), where the
254-
// answer at index i corresponds to the field at index i.
255-
//
256-
// Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#1164.
257-
FormAnswers []any `+"`json:\"formAnswers,omitempty\"`")
258-
259198
for _, k := range types.keys() {
260199
if k == "WatchKind" {
261200
types[k] = "type WatchKind = uint32" // strict gopls compatibility needs the '='

gopls/internal/protocol/generate/output.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func genDecl(model *Model, method string, param, result *Type, dir string) {
7474
}
7575
fragment := strings.ReplaceAll(strings.TrimPrefix(method, "$/"), "/", "_")
7676
msg := fmt.Sprintf("\t%s\t%s(context.Context%s) %s\n", lspLink(model, fragment), fname, p, ret)
77+
if doc, ok := prependMethodDocComments[fname]; ok {
78+
msg = doc + "\n\t//\n" + msg
79+
}
7780
switch dir {
7881
case "clientToServer":
7982
sdecls[method] = msg
@@ -294,6 +297,10 @@ func genProps(out *bytes.Buffer, props []NameType, name string) {
294297
fmt.Fprintf(out, "\t%s %s %s\n", goName(p.Name), tp, json)
295298
}
296299
}
300+
301+
if block, ok := appendTypeProp[name]; ok {
302+
out.WriteString(block)
303+
}
297304
}
298305

299306
func genAliases(model *Model) {

gopls/internal/protocol/generate/tables.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,55 @@ func methodName(method string) string {
284284
}
285285
return ans
286286
}
287+
288+
// prependMethodDocComments specifies doc comments that will be prepend to
289+
// an LSP method name defined in both Server and Client interface.
290+
var prependMethodDocComments = map[string]string{
291+
"ResolveCodeAction": `// To support microsoft/language-server-protocol#1164, the language server
292+
// need to read the form with client-supplied answers and either returns a
293+
// CodeAction with errors in the form surfacing the error to the client, or a
294+
// CodeAction with properties the language client is waiting for (e.g. edits,
295+
// commands).
296+
//
297+
// The language client may call "codeAction/resolve" if the language server
298+
// returns a CodeAction with errors or try asking the user for completing the
299+
// form again.
300+
// The language client may call "codeAction/resolve" multiple times with user
301+
// filled (re-filled) answers in the form until it obtains a CodeAction with
302+
// properties (e.g. edits, commands) it's waiting for.`,
303+
}
304+
305+
// appendTypeProp specifies block of code (typically properties with doc comment)
306+
// that will be append to a struct.
307+
var appendTypeProp = map[string]string{
308+
"CodeAction": `
309+
// FormFields and FormAnswers allow the server and client to exchange
310+
// interactive questions and answers during a resolveCodeAction request.
311+
//
312+
// The server populates FormFields to define the schema. The server may
313+
// optionally populate FormAnswers to preserve previous user input; if
314+
// provided, the client may present these as default values.
315+
//
316+
// When the client responds, it must provide FormAnswers. The client is not
317+
// required to send FormFields back to the server.
318+
319+
// FormFields defines the questions and validation errors.
320+
//
321+
// This is a server-to-client field. The language server defines these, and
322+
// the client uses them to render the form.
323+
//
324+
// Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#1164.
325+
FormFields []FormField ` + "`json:\"formFields,omitempty\"`" + `
326+
327+
// FormAnswers contains the values for the form questions.
328+
//
329+
// When sent by the language server, this field is optional but recommended
330+
// to support editing previous values.
331+
//
332+
// When sent by the language client, this field is required. The slice must
333+
// have the same length as FormFields (one answer per question), where the
334+
// answer at index i corresponds to the field at index i.
335+
//
336+
// Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#1164.
337+
FormAnswers []any ` + "`json:\"formAnswers,omitempty\"`",
338+
}

0 commit comments

Comments
 (0)