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

fix(lsp): fix trigger completion of zk LSP #397

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/adapter/lsp/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ func (d *document) IsTagPosition(position protocol.Position, noteContentParser c
if targetWord == "" {
return false
}
if string(targetWord[0]) == "#" {
targetWord = targetWord[1:]
}

content := strings.Join(lines, "\n")
note, err := noteContentParser.ParseNoteContent(content)
Expand Down
27 changes: 22 additions & 5 deletions internal/adapter/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,16 @@ func (s *Server) refreshDiagnosticsOfDocument(doc *document, notify glsp.NotifyF
// buildInvokedCompletionList builds the completion item response for a
// completion started automatically when typing an identifier, or manually.
func (s *Server) buildInvokedCompletionList(notebook *core.Notebook, doc *document, position protocol.Position) ([]protocol.CompletionItem, error) {
if !doc.IsTagPosition(position, notebook.Parser) {
return nil, nil
currentWord := doc.WordAt(position)
if strings.HasPrefix(doc.LookBehind(position, len(currentWord)+2), "[[") {
return s.buildLinkCompletionList(notebook, doc, position)
}
return s.buildTagCompletionList(notebook, doc.WordAt(position))

if doc.IsTagPosition(position, notebook.Parser) {
return s.buildTagCompletionList(notebook, doc.WordAt(position))
}

return nil, nil
}

// buildTriggerCompletionList builds the completion item response for a
Expand Down Expand Up @@ -804,12 +810,18 @@ func (s *Server) newCompletionItem(notebook *core.Notebook, note core.MinimalNot
if s.useAdditionalTextEditsWithNotebook(notebook) {
addTextEdits := []protocol.TextEdit{}

startOffset := -2
if doc.LookBehind(pos, 2) != "[[" {
currentWord := doc.WordAt(pos)
startOffset = -2 - len(currentWord)
}

// Some LSP clients (e.g. VSCode) don't support deleting the trigger
// characters with the main TextEdit. So let's add an additional
// TextEdit for that.
addTextEdits = append(addTextEdits, protocol.TextEdit{
NewText: "",
Range: rangeFromPosition(pos, -2, 0),
Range: rangeFromPosition(pos, startOffset, 0),
})

item.AdditionalTextEdits = addTextEdits
Expand All @@ -836,7 +848,12 @@ func (s *Server) newTextEditForLink(notebook *core.Notebook, note core.MinimalNo
// Overwrite [[ trigger directly if the additional text edits are disabled.
startOffset := 0
if !s.useAdditionalTextEditsWithNotebook(notebook) {
startOffset = -2
if doc.LookBehind(pos, 2) == "[[" {
startOffset = -2
} else {
currentWord := doc.WordAt(pos)
startOffset = -2 - len(currentWord)
}
}

// Some LSP clients (e.g. VSCode) auto-pair brackets, so we need to
Expand Down