Skip to content

Commit f81f766

Browse files
shugoclaude
andcommitted
Address PR #193 review: triggerKind comments, contextSupport, and tests
- Add inline comments (Invoked / TriggerCharacter) to triggerKind values - Extract context-building into lsp_completion_context helper method - Add contextSupport: true to textDocument.completion client capabilities - Add test_client_capabilities assertion for completion contextSupport - Add test/textbringer/commands/test_lsp.rb with 6 tests covering both branches of the trigger context selection logic Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 80aad79 commit f81f766

4 files changed

Lines changed: 52 additions & 6 deletions

File tree

lib/textbringer/commands/lsp.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ module Commands
4949
buffer.goto_char(start_point)
5050
buffer.point > 0 ? (buffer.backward_char; buffer.char_after) : nil
5151
}
52-
context = if prefix.empty? && trigger_chars.include?(char_before_start)
53-
{ triggerKind: 2, triggerCharacter: char_before_start }
54-
else
55-
{ triggerKind: 1 }
56-
end
52+
context = lsp_completion_context(prefix, trigger_chars, char_before_start)
5753

5854
# Request completion
5955
client.completion(uri: uri, line: line, character: character, context: context) do |items, error|
@@ -186,6 +182,15 @@ module Commands
186182

187183
# Helper methods
188184

185+
def lsp_completion_context(prefix, trigger_chars, char_before_start)
186+
if prefix.empty? && trigger_chars.include?(char_before_start)
187+
{ triggerKind: 2, # TriggerCharacter
188+
triggerCharacter: char_before_start }
189+
else
190+
{ triggerKind: 1 } # Invoked
191+
end
192+
end
193+
189194
# Convert a string's character length to UTF-16 code unit count.
190195
# LSP positions use UTF-16 offsets by default.
191196
def lsp_utf16_length(str)

lib/textbringer/lsp/client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ def client_capabilities
252252
},
253253
completionItemKind: {
254254
valueSet: (1..25).to_a
255-
}
255+
},
256+
contextSupport: true
256257
},
257258
signatureHelp: {
258259
signatureInformation: {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require_relative "../../test_helper"
2+
3+
class TestLSPCommands < Textbringer::TestCase
4+
def test_lsp_completion_context_invoked_with_prefix
5+
# When there is a prefix typed, always use Invoked (triggerKind 1)
6+
context = lsp_completion_context("fo", ["."], ".")
7+
assert_equal({ triggerKind: 1 }, context)
8+
end
9+
10+
def test_lsp_completion_context_invoked_no_trigger_char
11+
# Empty prefix but char before start is not a trigger character
12+
context = lsp_completion_context("", ["."], "x")
13+
assert_equal({ triggerKind: 1 }, context)
14+
end
15+
16+
def test_lsp_completion_context_invoked_no_trigger_chars_configured
17+
# No trigger characters configured on the server
18+
context = lsp_completion_context("", [], ".")
19+
assert_equal({ triggerKind: 1 }, context)
20+
end
21+
22+
def test_lsp_completion_context_trigger_character
23+
# Empty prefix and char before start is a trigger character → TriggerCharacter (triggerKind 2)
24+
context = lsp_completion_context("", ["."], ".")
25+
assert_equal({ triggerKind: 2, triggerCharacter: "." }, context)
26+
end
27+
28+
def test_lsp_completion_context_trigger_character_colon
29+
# Empty prefix and :: trigger
30+
context = lsp_completion_context("", [":", "::", "."], ":")
31+
assert_equal({ triggerKind: 2, triggerCharacter: ":" }, context)
32+
end
33+
34+
def test_lsp_completion_context_prefix_overrides_trigger_char
35+
# Even when char before start is a trigger char, prefix present → Invoked
36+
context = lsp_completion_context("bo", ["."], ".")
37+
assert_equal({ triggerKind: 1 }, context)
38+
end
39+
end

test/textbringer/lsp/test_client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def test_client_capabilities
100100
assert(capabilities[:textDocument][:completion])
101101
assert(capabilities[:textDocument][:synchronization])
102102
refute(capabilities[:textDocument][:completion][:completionItem][:snippetSupport])
103+
assert(capabilities[:textDocument][:completion][:contextSupport])
103104

104105
# Signature help capabilities
105106
sig_help = capabilities[:textDocument][:signatureHelp]

0 commit comments

Comments
 (0)