MCPcopy Index your code
hub / github.com/bytebase/bytebase / handleTextDocumentCompletion

Method handleTextDocumentCompletion

backend/api/lsp/completion.go:32–100  ·  view source on GitHub ↗
(ctx context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request, params lsp.CompletionParams)

Source from the content-addressed store, hash-verified

30}
31
32func (h *Handler) handleTextDocumentCompletion(ctx context.Context, _ *jsonrpc2.Conn, _ *jsonrpc2.Request, params lsp.CompletionParams) (*lsp.CompletionList, error) {
33 if !IsURI(params.TextDocument.URI) {
34 return nil, &jsonrpc2.Error{
35 Code: jsonrpc2.CodeInvalidParams,
36 Message: fmt.Sprintf("textDocument/completion not yet supported for out-of-workspace URI (%q)", params.TextDocument.URI),
37 }
38 }
39 content, err := h.readFile(ctx, params.TextDocument.URI)
40 if err != nil {
41 return nil, err
42 }
43 if len(content) > contentLengthLimit {
44 // We don't want to parse a huge file.
45 return newEmptyCompletionList(), nil
46 }
47 _, err = offsetForPosition(content, params.Position)
48 if err != nil {
49 return nil, errors.Wrapf(err, "invalid position %d:%d", params.Position.Line, params.Position.Character)
50 }
51
52 defaultDatabase := h.getDefaultDatabase()
53 engine := h.getEngineType(ctx)
54 if !common.EngineSupportAutoComplete(engine) {
55 slog.Debug("Engine is not supported", slog.String("engine", engine.String()))
56 return newEmptyCompletionList(), nil
57 }
58 candidates, err := parserbase.Completion(ctx, engine, parserbase.CompletionContext{
59 Scene: h.getScene(),
60 InstanceID: h.getInstanceID(),
61 DefaultDatabase: defaultDatabase,
62 DefaultSchema: h.getDefaultSchema(),
63 Metadata: h.GetDatabaseMetadataFunc,
64 ListDatabaseNames: h.ListDatabaseNamesFunc,
65 }, string(content), int(params.Position.Line)+1, int(params.Position.Character))
66 if err != nil {
67 // return errors will close the websocket connection, so we just log the error and return empty completion list.
68 slog.Error("Failed to get completion candidates", "err", err)
69 return newEmptyCompletionList(), nil
70 }
71
72 items := []lsp.CompletionItem{}
73 for _, candidate := range candidates {
74 label := candidate.Text
75 // Remove quotes or brackets from label.
76 if len(label) > 1 && (label[0] == '"' && label[len(label)-1] == '"' || label[0] == '`' && label[len(label)-1] == '`' || label[0] == '[' && label[len(label)-1] == ']') {
77 label = label[1 : len(label)-1]
78 label = strings.ReplaceAll(label, `""`, `"`)
79 }
80 completionItem := lsp.CompletionItem{
81 Label: label,
82 LabelDetails: &lsp.CompletionItemLabelDetails{
83 Detail: fmt.Sprintf("(%s)", string(candidate.Type)),
84 Description: candidate.Definition,
85 },
86 Kind: convertLSPCompletionItemKind(candidate.Type),
87 Documentation: &lsp.Or_CompletionItem_documentation{
88 Value: candidate.Comment,
89 },

Callers 1

handleMethod · 0.95

Calls 15

readFileMethod · 0.95
getDefaultDatabaseMethod · 0.95
getEngineTypeMethod · 0.95
getSceneMethod · 0.95
getInstanceIDMethod · 0.95
getDefaultSchemaMethod · 0.95
IsURIFunction · 0.85
newEmptyCompletionListFunction · 0.85
offsetForPositionFunction · 0.85
generateSortTextFunction · 0.85

Tested by

no test coverage detected