Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp: don't null-pointer-deref panic when TextDocumentCompletion.Context missing #39

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions internal/adapter/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ func NewServer(opts ServerOpts) *Server {
}

handler.TextDocumentCompletion = func(context *glsp.Context, params *protocol.CompletionParams) (interface{}, error) {
triggerChar := params.Context.TriggerCharacter
if params.Context.TriggerKind != protocol.CompletionTriggerKindTriggerCharacter || triggerChar == nil {
return nil, nil
triggerChar := "["
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auto-completion is also used for tags, so I'm afraid that defaulting to [ would make it unreachable. Maybe we could just take a look at the previously entered characters (like here) and ignore the trigger character in this case?

I didn't encounter an editor which doesn't support the context and trigger characters yet. How is the auto-completion triggered in this case, manually with C-Space? Which editor / LSP plugin are you using?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neovim's built in LSP doesn't do it yet. Moreover the spec says it's optional, so you do have to check the nil. I think it is perhaps a low priority feature, because generally LSPs are accepting file content as well as completion requests, and have access to a stored copy of the file. So nobody actually needs to know the trigger character. Not even you! I'll fix with a look behind instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also optional because completion gets triggered in many other ways than typing a character.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah in the end this context is not super useful then. Thanks for addressing this!

// Clients don't have to send Context if they don't implement it, so it may be missing.
if params.Context != nil {
if params.Context.TriggerKind != protocol.CompletionTriggerKindTriggerCharacter || params.Context.TriggerCharacter == nil {
return nil, nil
}
triggerChar = *params.Context.TriggerCharacter
}

doc, ok := server.documents[params.TextDocument.URI]
Expand All @@ -189,7 +193,7 @@ func NewServer(opts ServerOpts) *Server {
return nil, err
}

switch *triggerChar {
switch triggerChar {
case "#":
if notebook.Config.Format.Markdown.Hashtags {
return server.buildTagCompletionList(notebook, "#")
Expand Down