1// Copyright 2018 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
5package lsp
6
7import (
8	"context"
9
10	"golang.org/x/tools/internal/lsp/protocol"
11	"golang.org/x/tools/internal/lsp/source"
12	"golang.org/x/tools/internal/span"
13	"golang.org/x/tools/internal/telemetry/log"
14	"golang.org/x/tools/internal/telemetry/tag"
15)
16
17func (s *Server) signatureHelp(ctx context.Context, params *protocol.SignatureHelpParams) (*protocol.SignatureHelp, error) {
18	uri := span.NewURI(params.TextDocument.URI)
19	view, err := s.session.ViewOf(uri)
20	if err != nil {
21		return nil, err
22	}
23	snapshot := view.Snapshot()
24	f, err := view.GetFile(ctx, uri)
25	if err != nil {
26		return nil, err
27	}
28	if f.Kind() != source.Go {
29		return nil, nil
30	}
31	info, err := source.SignatureHelp(ctx, snapshot, f, params.Position)
32	if err != nil {
33		log.Print(ctx, "no signature help", tag.Of("At", params.Position), tag.Of("Failure", err))
34		return nil, nil
35	}
36	return toProtocolSignatureHelp(info), nil
37}
38
39func toProtocolSignatureHelp(info *source.SignatureInformation) *protocol.SignatureHelp {
40	return &protocol.SignatureHelp{
41		ActiveParameter: float64(info.ActiveParameter),
42		ActiveSignature: 0, // there is only ever one possible signature
43		Signatures: []protocol.SignatureInformation{
44			{
45				Label:         info.Label,
46				Documentation: info.Documentation,
47				Parameters:    toProtocolParameterInformation(info.Parameters),
48			},
49		},
50	}
51}
52
53func toProtocolParameterInformation(info []source.ParameterInformation) []protocol.ParameterInformation {
54	var result []protocol.ParameterInformation
55	for _, p := range info {
56		result = append(result, protocol.ParameterInformation{
57			Label: p.Label,
58		})
59	}
60	return result
61}
62