Skip to content

Commit

Permalink
fix(lsp): fix trigger completion of zk LSP (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rahlir authored Mar 31, 2024
1 parent b10d51d commit 05c50a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
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

0 comments on commit 05c50a7

Please sign in to comment.