Skip to content

fix: send the inline completion trigger and the selected popup item - #1715

Merged
angelozerr merged 3 commits into
redhat-developer:mainfrom
lagergren:fix/inline-completion-context
Oct 19, 2026
Merged

angelozerr merged 3 commits into
redhat-developer:mainfrom
lagergren:fix/inline-completion-context

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

Summary

Why?

This makes LSP4IJ's inline completion requests say what the specification says they should; it is not a change of style.

  • Every request claims to be automatic. LSPInlineCompletionSupport replaces the context of each request with InlineCompletionTriggerKind.Automatic. The specification distinguishes Invoked, "Completion was triggered explicitly by a user gesture. Return multiple completion items to enable cycling through them", from Automatic, "triggered automatically while editing. It is sufficient to return a single completion item in this case". So when the user explicitly asks for an inline completion, the server is told it may return a single item.
  • The completion popup's selected item is never sent. selectedCompletionInfo "provides information about the currently selected item in the autocomplete widget if it is visible", and "inline completion providers are requested again whenever the selected item changes". LSP4IJ never sets it and does not request again when the selection changes.
  • The same caret position reuses the earlier response. The support only cancels its cached request when the caret offset changes, so an explicit invocation, or a different selected item at the same caret, gets the earlier automatic response without asking the server.

How?

  • LSPInlineCompletionProvider builds the context: Invoked for a direct call, Automatic otherwise, and selectedCompletionInfo from the visible popup's item, with the item's edit text and range. A snippet's source notation is not reported as literal text, since it can resolve variables and placeholders on insertion. The selection is UI state, so it is read on the EDT together with the document position, in a cancellable readAction.
  • LSPInlineCompletionSupport keeps that context, maps the document URI for each server without changing the caller's parameters, and treats a request with a different context as a different request.
  • Inline completion is requested again on a direct call and when the popup selection changes (InlineCompletionEvent.LookupChange), and, as the specification requires, a suggestion that does not extend the selected item over the same range is not shown.

Implications

Servers see Invoked for explicit invocations, and receive the popup's selected item while the popup is visible. Typing at a new position behaves as before. Inline completion is now requested again when the selected popup item changes, as the specification asks.

Changes

  • LSPInlineCompletionProvider and LSPInlineCompletionSupport.
  • 6 tests. LSPInlineCompletionContextTest sends an automatic request, an explicit one and one with a popup selection at the same caret through the mock server, and checks that each reaches the server with its own context, that an equivalent request reuses the response, and that a changed selection is sent again. LSPInlineCompletionProviderContextTest checks the context the provider builds: Invoked for a direct call, the selected item's insertion text and exact edit range, the lookup prefix range when the item has no edit, the selection retained on explicit invocation, and no literal text for a snippet.

This relates to the inline completion support tracked in #643. I found no issue or pull request about the trigger kind or the selected item.

Manual verification

To reproduce, use a server that returns different inline completions for Invoked and Automatic, or logs the request context, then invoke inline completion explicitly at a position where an automatic suggestion was already shown. Without this change the server receives no new request, and every request it does receive says Automatic; with it, the explicit request says Invoked. I did not run this by hand; the tests above cover it.

Automated checks completed:

  • Against unchanged upstream 755ba4d7, LSPInlineCompletionContextTest fails: only 1 of the 3 requests reaches the server (expected:<3> but was:<1>). LSPInlineCompletionProviderContextTest exercises the new createParams, which does not exist upstream. All 6 tests pass with this change.
  • Rebased onto 76df99e4. ./gradlew check --rerun-tasks --no-build-cache: 558 tests passed, 3 existing skips (the Docker Bake and Perl inline completion tests), no failures or errors. Run with java.io.tmpdir under the home directory, since the default macOS temporary directory gives unrelated VfsRootAccess failures with and without this change.

Inline completion always told the server it was triggered automatically
and never described the completion popup's selected item:
LSPInlineCompletionSupport replaced every context with
InlineCompletionTriggerKind.Automatic, and the cache was keyed by caret
offset only, so an explicit invocation, or another selected item at the
same caret, reused the earlier automatic response.

The provider now builds the context: Invoked for a direct call (an
explicit user gesture), Automatic otherwise, and selectedCompletionInfo
from the visible popup's item, with its edit text and range (a snippet's
source notation is not reported as literal text). The support keeps that
context, maps the URI per server without changing the caller's
parameters, and treats a different context as a different request. As
the specification asks, inline completion is requested again when the
selected item changes, and a suggestion that does not extend the
selected item over the same range is not shown. The selection is read on
the EDT, together with the document position, in a cancellable read.
@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8c83ece5-fe99-42cf-929e-ca38bf6685c1
📥 Commits

Reviewing files that changed from the base of the PR and between 76df99e and d5b0edb.

📒 Files selected for processing: 4
  • src/main/java/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionSupport.java
  • src/main/kotlin/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionProvider.kt
  • src/test/java/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionContextTest.java
  • src/test/kotlin/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionProviderContextTest.kt

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Improvements
    • Inline completions now reflect how they were requested, including whether the request was automatic or explicitly invoked.
    • When a completion popup selection is active, suggestions better match its text and replacement range. Blank or incompatible suggestions are discarded.
    • Changing the selected popup item refreshes inline suggestions, while equivalent requests can reuse results.
    • Requests are refreshed when relevant completion context changes, even if the cursor remains at the same position.

The inline completion provider adds invocation and selected-completion context to requests, handles lookup changes, and filters returned items against the current selection. The support class preserves request context and cancels prior requests when the full parameters change.

Changes: Inline completion context

Layer / File(s) Summary
Build and validate provider context
src/main/kotlin/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionProvider.kt, src/test/kotlin/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionProviderContextTest.kt
The provider captures request data on the EDT, adds invocation and selected-completion metadata, and handles lookup changes. It rejects returned items that do not match the selected completion. Tests cover invocation metadata, selection ranges, snippet handling, and lookup restarts.
Forward and track requests
src/main/java/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionSupport.java, src/test/java/com/redhat/devtools/lsp4ij/features/inlineCompletion/LSPInlineCompletionContextTest.java
The support class compares full request parameters when managing prior requests. It sends each server a request with a separate document identifier and preserves the supplied context. Tests cover context forwarding and cache reuse.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant LSPInlineCompletionProvider
  participant LSPInlineCompletionSupport
  participant LanguageServer
  LSPInlineCompletionProvider->>LSPInlineCompletionSupport: Send parameters with invocation and selection context
  LSPInlineCompletionSupport->>LanguageServer: Send server-specific request with preserved context
  LanguageServer-->>LSPInlineCompletionSupport: Return completion items
  LSPInlineCompletionSupport-->>LSPInlineCompletionProvider: Return completion items
  LSPInlineCompletionProvider->>LSPInlineCompletionProvider: Filter items against selected completion
Loading

Suggested reviewers: angelozerr

Merge Risk: ⚪ Minimal · up to d5b0e

No actionable merge-blocking issue is established for the inline-completion context change; it is ready for normal merge checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 5.26% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the main change: sending the inline-completion trigger and selected popup item.
Description check Passed The description directly explains the inline-completion context, selected-item handling, request invalidation, tests, and verification results.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI

Comment @coderabbitai help to get the list of available commands.

@lagergren

Copy link
Copy Markdown
Collaborator Author

The red Test jobs here aren't caused by this PR. They fail intermittently on main as well, in tests this PR doesn't touch:

#1717 fixes both, together with a third flake. CI needs #1717 merged first; then this branch can be updated from main to get a reliably green run. In the meantime I've pushed an empty commit (d5b0edb) to re-run the checks.

@angelozerr

Copy link
Copy Markdown
Contributor

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@angelozerr angelozerr added the enhancement New feature or request label Oct 19, 2026
@angelozerr
angelozerr merged commit 04bce87 into redhat-developer:main Oct 19, 2026
7 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Java Tooling Oct 19, 2026
@angelozerr

Copy link
Copy Markdown
Contributor

Thanks @lagergren !

@lagergren
lagergren deleted the fix/inline-completion-context branch October 26, 2026 21:43
angelozerr pushed a commit that referenced this pull request Nov 4, 2026
* docs: describe the 0.30.0 LSP and DAP changes that the docs miss

Several changes merged for 0.30.0 changed behavior that the docs
describe, or added behavior that they don't. LSPSupport.md still said
that pull diagnostics don't support related documents, which #1723
changed.

LSP:
- LSPSupport.md, Pull Diagnostics: previousResultId, and the
  relatedDocuments of either kind of report (#1723, #1735), in place
  of "It doesn't support the related documents."
- LSPSupport.md, Inline Completion: the trigger kind and the selected
  completion item (#1715).
- LSPSupport.md, Signature Help: the retrigger, the active parameter
  of each signature, and signatures without parameters (#1712, #1734).
- LSPSupport.md, Formatting: a file without an open editor (#1707).
- LSPSupport.md, new Did Change Watched Files section: the directories
  outside the project that watchers name, and the watchers of a stopped
  server (#1742). The support list links to it.
- UserGuide.md, Formatting: the Do not format patterns (#1817).
- UserDefinedLanguageServer.md, Configuration tab: null properties
  (#1710, #1847) and invalid JSON (#1828).
- DeveloperGuide.md, Workspace Configuration: null members of
  createSettings() (#1710, #1847).

DAP, dap/UserGuide.md:
- Exception breakpoint: the hint built from exceptionInfo (#1761). The
  screenshot of the stop at the exception moves back after the example
  it shows; the Smart Step Into section had come between them.
- Large variables: a page with fewer children than asked for is the
  last one (#1851).

Validation: each statement was checked against the code at d1a281d:
DocumentContentSynchronizer.refreshPullDiagnostic,
LSPInlineCompletionProvider.createParams, LSPParameterInfoHandler,
ClientCapabilitiesFactory, AbstractLSPFormattingService.canFormat,
LSPFormattingSupport, FileSystemWatchRoots, LanguageServerPanel
.validateJsonFields, JsonSettingsTypeAdapterFactory,
DAPClient.getExceptionHint and DAPVariableChildren. The other changes
merged for 0.30.0 are already documented, by their own PRs, #1784 and
#1810, or change nothing that the docs describe.

Limits: docs only, no new screenshots, no build run.

* docs: say that the JSON fields of a language server are read leniently

CodeRabbit noted that "must be valid JSON" is stricter than what
LSP4IJ accepts. The dialog's check (LanguageServerPanel.jsonError,
JsonParser.parseString) and the readers at run time
(SettingsHelper.parseJson, JsonParser.parseReader for the
initialization options, experimental and installer, Gson.fromJson for
the client configuration and workspace folders) all read leniently.

The paragraph now says which JSON is accepted and which isn't.

Validation: probed Gson 2.15.0's JsonParser.parseString and
Gson.fromJson. Both accept // and /* */ and # comments, unquoted names
and single-quoted strings, and both reject a trailing comma, a missing
brace and an unescaped backslash, such as in C:\Users.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request inlineCompletion

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants