Skip to content

Conversation

@xiaoye6688
Copy link

@xiaoye6688 xiaoye6688 commented Jan 1, 2026

Summary

  • Display selected code info (filename, line range) above chat input in real-time
  • Listen to editor selection changes and sync to WebView
  • Fix alien-signals reactivity: replace Vue watch with alien effect

Changes

  • src/extension.ts: Add selection change listeners
  • src/webview/src/composables/useRuntime.ts: Fix signal sync issue
  • src/webview/src/core/Session.ts: Enable selection by default
  • src/webview/src/pages/ChatPage.vue: Add selection indicator UI

Test plan

  • Select code in editor, verify indicator appears
  • Change selection, verify indicator updates
  • Clear selection, verify indicator disappears
image

Summary by Sourcery

Add real-time propagation of editor code selections to the webview and surface them in the chat UI while defaulting messages to include the current selection.

New Features:

  • Display the currently selected file and line range above the chat input based on live editor selections.
  • Automatically include the active editor selection in chat messages by default when sending to the backend.

Bug Fixes:

  • Fix selection reactivity by syncing alien-signal based selection state correctly between the app context and session context.

Enhancements:

  • Propagate VS Code editor selection and active editor changes to the webview via the existing transport channel and keep session selection state up to date.

- Display selected code info (filename, line range) above chat input
- Listen to editor selection changes and sync to WebView
- Fix alien-signals reactivity: replace Vue watch with alien effect
- Use absolute positioning to avoid layout shifts
@sourcery-ai
Copy link

sourcery-ai bot commented Jan 1, 2026

Reviewer's Guide

Implements a real-time code selection indicator by wiring VS Code editor selection events through the extension transport into the webview’s alien-signal-based session state, enabling ChatPage to render a small inline badge above the chat input showing the selected file and line range, while fixing alien-signals reactivity and enabling selection inclusion by default when sending messages.

Sequence diagram for real-time code selection indicator update

sequenceDiagram
  actor User
  participant VSCodeEditor
  participant VSCodeExtension as Extension
  participant Transport
  participant Webview as WebviewHost
  participant Runtime as RuntimeInstance
  participant AppContext
  participant SessionStore
  participant Session
  participant ChatPage

  User->>VSCodeEditor: Change text selection
  VSCodeEditor->>VSCodeExtension: onDidChangeTextEditorSelection
  VSCodeExtension->>VSCodeExtension: sendSelectionUpdate(editor)
  VSCodeExtension->>Transport: send type selection_changed
  Transport->>Webview: postMessage selection_changed
  Webview->>Runtime: selectionEvents callback(selection)
  Runtime->>AppContext: currentSelection(selection or undefined)
  AppContext->>Runtime: currentSelection signal updated
  Runtime->>SessionStore: currentSelectionSignal updated via effect
  SessionStore->>Session: selection SelectionRange updated
  Session->>ChatPage: selection reactive value
  ChatPage->>ChatPage: compute selectionInfo from Session.selection
  ChatPage-->>User: Render selection indicator above input
Loading

Sequence diagram for including selection when sending a message

sequenceDiagram
  actor User
  participant ChatPage
  participant Session
  participant Connection

  User->>ChatPage: Submit chat input
  ChatPage->>Session: send(input, attachments, includeSelection=true)
  Session->>Session: detect isSlash from input
  Session->>Connection: getConnection()
  Session->>Session: launchClaude()
  Session->>Connection: getCurrentSelection()
  Connection-->>Session: selection response
  Session->>Session: selection(selection or undefined)
  Session->>Connection: send chat payload with selection if includeSelection and not isSlash
Loading

Updated class diagram for runtime selection handling and Session

classDiagram
  class SelectionRange {
    string filePath
    number startLine
    number endLine
    number startColumn
    number endColumn
  }

  class AppContext {
    +currentSelection() SelectionRange~optional~
    +currentSelection(selection SelectionRange~optional~) void
  }

  class SessionStore {
    -connectionManager ConnectionManager
    -selectionSignal SelectionRange~optional~
    +SessionStore(connectionManager ConnectionManager, options SessionStoreOptions)
  }

  class Session {
    -selection SelectionRange~optional~
    +send(input string, attachments AttachmentPayload[] = [], includeSelection boolean = true) Promise~void~
    +getConnection() Promise~Connection~
    +launchClaude() Promise~void~
    +selection() SelectionRange~optional~
    +selection(value SelectionRange~optional~) void
  }

  class Connection {
    +getCurrentSelection() Promise~SelectionResponse~
  }

  class SelectionResponse {
    +selection SelectionRange~optional~
  }

  class RuntimeInstance {
    +connectionManager ConnectionManager
    +appContext AppContext
    +sessionStore SessionStore
    +dispose() void
  }

  class ChatPageViewModel {
    +session Session
    +selectionInfo string
  }

  AppContext --> SessionStore : currentSelectionSignal
  SessionStore --> Session : manages
  Session --> Connection : uses
  Connection --> SelectionResponse : returns
  RuntimeInstance --> AppContext : owns
  RuntimeInstance --> SessionStore : owns
  ChatPageViewModel --> Session : reads selection
  ChatPageViewModel --> SelectionRange : formats to selectionInfo
Loading

File-Level Changes

Change Details Files
Surface VS Code text selection changes to the webview session state via the existing transport and selection events pipeline.
  • Add a helper that reads the current active editor selection, normalizes it to file path and 1-based line/column numbers, and sends it as a selection_changed request or null when no valid selection is present.
  • Register onDidChangeTextEditorSelection and onDidChangeActiveTextEditor listeners to call the helper whenever the selection or active editor changes.
  • Ensure the new disposables for selection listeners are added to the extension context subscriptions for proper cleanup.
src/extension.ts
Fix selection reactivity between AppContext and SessionContext by using alien-signal effects instead of Vue watch, and normalize clear-selection events.
  • Create an alien signal for currentSelectionSignal and synchronize it from AppContext.currentSelection using an alien effect so that changes to the alien signal are properly tracked.
  • Adjust selectionEvents handling to write undefined into AppContext.currentSelection when the selection is cleared rather than passing through null.
  • Store the effect cleanup function and invoke it in the runtime disposal path alongside command cleanup and connection closing.
src/webview/src/composables/useRuntime.ts
Enable automatic inclusion and storage of the latest editor selection in session state whenever the user sends a normal chat message.
  • Change the Session.send method default includeSelection parameter to true so that selection is considered by default.
  • Before computing the selection payload, call connection.getCurrentSelection() when includeSelection is true and the input is not a slash command, and update the session’s selection signal with the returned selection (or undefined on failure).
  • Retain the shouldIncludeSelection logic for downstream payload construction while ensuring the session’s internal selection reflects the latest editor selection.
src/webview/src/core/Session.ts
Render a visual selection indicator above the chat input that reflects the current session selection and is styled consistently with the VS Code theme.
  • Introduce a selectionInfo computed property that derives a display string from session.value?.selection.value, formatting it as filename #start-end (N 行) when a line range is available, or just the filename otherwise.
  • Conditionally render a selection-indicator container with icon and text above the ChatInputBox when selectionInfo is non-empty.
  • Make the input container position: relative and add CSS rules for the indicator’s positioning, icon size, and monospace text styling to match the editor aesthetic and stay anchored above the input area.
src/webview/src/pages/ChatPage.vue

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In selectionInfo, the if (start && end) check will break if startLine or endLine can ever be 0; consider checking for start != null && end != null instead of relying on truthiness.
  • The selection indicator label text includes a hardcoded Chinese unit ; if this UI is meant to be language-agnostic, consider using existing localization mechanisms or a neutral symbol-only representation (e.g., just (#start-#end, N lines) in the current UI language).
  • When setting up the selection listeners in extension.ts, you might want to call sendSelectionUpdate(vscode.window.activeTextEditor) once on activation so that the webview immediately reflects the current selection without waiting for the first change event.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `selectionInfo`, the `if (start && end)` check will break if `startLine` or `endLine` can ever be 0; consider checking for `start != null && end != null` instead of relying on truthiness.
- The selection indicator label text includes a hardcoded Chinese unit ``; if this UI is meant to be language-agnostic, consider using existing localization mechanisms or a neutral symbol-only representation (e.g., just `(#start-#end, N lines)` in the current UI language).
- When setting up the selection listeners in `extension.ts`, you might want to call `sendSelectionUpdate(vscode.window.activeTextEditor)` once on activation so that the webview immediately reflects the current selection without waiting for the first change event.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Haleclipse
Copy link
Owner

Haleclipse commented Jan 1, 2026

哇,一时语塞

不过这个功能不打算这么做 所以才空在那里的

微信dd我

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants