diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-26 08:19:26 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-26 08:19:26 +0300 |
| commit | 9bcccbd80d36ae678d58cd8f83c4d0c790c16b48 (patch) | |
| tree | ccbfdec5119daf443332db020824bc5845bbcf78 /internal/lsp/handlers_document.go | |
| parent | 439ebb14fa6fb43bfda2e0ee6811c37f96b15ecc (diff) | |
Auto apply inline prompt completions
Diffstat (limited to 'internal/lsp/handlers_document.go')
| -rw-r--r-- | internal/lsp/handlers_document.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/internal/lsp/handlers_document.go b/internal/lsp/handlers_document.go index 282ef26..0340866 100644 --- a/internal/lsp/handlers_document.go +++ b/internal/lsp/handlers_document.go @@ -94,6 +94,10 @@ func (s *Server) detectAndHandleChat(uri string) { _, _, openChar, closeChar := s.inlineMarkers() for i, raw := range d.lines { if lineHasInlinePrompt(raw, openChar, closeChar) { + if s.currentLLMClient() != nil { + pos := Position{Line: i, Character: len(raw)} + go s.runInlinePrompt(uri, pos) + } continue } // Find last non-space character index @@ -210,6 +214,47 @@ func (s *Server) applyChatEdits(uri string, lineIdx int, lastNonSpace int, remov s.clientApplyEdit("Hexai: insert chat response", we) } +func (s *Server) runInlinePrompt(uri string, pos Position) { + if s.currentLLMClient() == nil { + return + } + d := s.getDocument(uri) + if d == nil || pos.Line < 0 || pos.Line >= len(d.lines) { + return + } + line := d.lines[pos.Line] + _, _, openChar, closeChar := s.inlineMarkers() + if !lineHasInlinePrompt(line, openChar, closeChar) { + return + } + p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: uri}, Position: Position{Line: pos.Line, Character: len(line)}} + p.Context = map[string]int{"triggerKind": 1} + above, current, below, funcCtx := s.lineContext(uri, p.Position) + docStr := s.buildDocString(p, above, current, below, funcCtx) + newFunc := s.isDefiningNewFunction(uri, p.Position) + extra, hasExtra := s.buildAdditionalContext(newFunc, uri, p.Position) + items, ok := s.tryLLMCompletion(p, above, current, below, funcCtx, docStr, hasExtra, extra) + if !ok || len(items) == 0 { + return + } + s.applyInlineCompletion(uri, items[0]) +} + +func (s *Server) applyInlineCompletion(uri string, item CompletionItem) { + var edits []TextEdit + if len(item.AdditionalTextEdits) > 0 { + edits = append(edits, item.AdditionalTextEdits...) + } + if item.TextEdit != nil { + edits = append(edits, *item.TextEdit) + } + if len(edits) == 0 { + return + } + we := WorkspaceEdit{Changes: map[string][]TextEdit{uri: edits}} + s.clientApplyEdit("Hexai: inline prompt", we) +} + // buildChatHistory walks upwards from the current line to collect the most recent // Q/A pairs in the in-editor transcript. Returns messages ending with current prompt. func (s *Server) buildChatHistory(uri string, lineIdx int, currentPrompt string) []llm.Message { |
