From 526e40a54ba325a6f75f35817799614d7b5997b7 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 19 Aug 2025 21:32:09 +0300 Subject: lsp: strip inline spans for completions\n\n- Add stripInlineCodeSpan helper to extract first inline backtick span\n- Apply only in completion path after fence stripping\n- Add comprehensive unit tests for inline span handling --- internal/lsp/handlers.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'internal/lsp/handlers.go') diff --git a/internal/lsp/handlers.go b/internal/lsp/handlers.go index d140ba1..cfd71ea 100644 --- a/internal/lsp/handlers.go +++ b/internal/lsp/handlers.go @@ -482,7 +482,17 @@ func (s *Server) tryLLMCompletion(p CompletionParams, above, current, below, fun // Update response counters (received) s.incRecvCounters(len(text)) s.logLLMStats() - cleaned := stripCodeFences(strings.TrimSpace(text)) + cleaned := stripCodeFences(strings.TrimSpace(text)) + // For code completion responses, also strip inline single-backtick code spans + // when the model returns prose like: "Use `expr` here". + if cleaned != "" { + if strings.ContainsRune(cleaned, '`') { + inline := stripInlineCodeSpan(cleaned) + if strings.TrimSpace(inline) != "" { + cleaned = inline + } + } + } if cleaned != "" { cleaned = stripDuplicateAssignmentPrefix(current[:p.Position.Character], cleaned) } @@ -780,6 +790,28 @@ func stripCodeFences(s string) string { return t } +// stripInlineCodeSpan returns only the contents of the first inline backtick +// code span if present, e.g., "some text `x := y()` more" -> "x := y()". +// If no matching pair of backticks exists, it returns the input unchanged. +// This is intended for code completion responses where the model may wrap a +// small snippet in single backticks among prose. +func stripInlineCodeSpan(s string) string { + t := strings.TrimSpace(s) + if t == "" { + return t + } + i := strings.IndexByte(t, '`') + if i < 0 { + return t + } + jrel := strings.IndexByte(t[i+1:], '`') + if jrel < 0 { + return t + } + j := i + 1 + jrel + return t[i+1 : j] +} + func labelForCompletion(cleaned, filter string) string { label := trimLen(firstLine(cleaned)) if filter != "" && !strings.HasPrefix(strings.ToLower(label), strings.ToLower(filter)) { -- cgit v1.2.3