Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed unwantted selecting behavior for send-to-chat #4515

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Jazzcort
Copy link

@Jazzcort Jazzcort commented Mar 6, 2025

Description

The original behavior selects the entire first line even though it's only partially selected in the editor. This fix is to get rid of the extra selection from the first line and only send the exact part that gets selected in the editor to the chat section. Granite-Code/granite-code#43

Testing instructions

Partially select the first line and then Cmd-L, check if code snippet appearing in the chat section display the exact part you select in the editor.

Copy link

netlify bot commented Mar 6, 2025

Deploy Preview for continuedev canceled.

Name Link
🔨 Latest commit 67957e2
🔍 Latest deploy log https://app.netlify.com/sites/continuedev/deploys/67cb3fb0eac2b000088a0098

@owtaylor
Copy link
Contributor

owtaylor commented Mar 6, 2025

Hmm, this entirely bypasses the code in getRangeInFileWithContents() that is causing the problem:

    // adjust starting position to include indentation
    const start = new vscode.Position(selection.start.line, 0);
    const selectionRange = new vscode.Range(start, selection.end);

But I think that code does have a point - if you select with shift-downarrow, it's easy to get a selection that includes the first line without the whitespace, and the rest of the lines with the whitespace:

image

In that case, we don't want to send to the model:

multiply(number) {
    this.result *= number;
    return this;
  }

Probably that code needs to be fancier - to check if the text at the beginning of the first line is all whitespace and only then adjust the first line boundary to be the start of the line.

@Jazzcort Jazzcort force-pushed the Jazzcort/exact-select-send-to-chat branch 3 times, most recently from a913098 to 9ca0f55 Compare March 7, 2025 15:52
Copy link
Contributor

@owtaylor owtaylor left a comment

Choose a reason for hiding this comment

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

This looks good to me now, but I think there are a few changes additional changes that could be done to tighten up the code and make it more readable.

@@ -150,6 +183,10 @@ function getRangeInFileWithContents(
async function addHighlightedCodeToContext(
webviewProtocol: VsCodeWebviewProtocol | undefined,
) {
const editor = vscode.window.activeTextEditor;
if (!editor) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look necessary - getRangeInFileWithContents() already handles that.

const beginningOfSelectionStartLine = new vscode.Position(
selection.start.line,
0,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a little compression we can do here:

const beginningOfSelectionStartLine = selection.start.with(undefined, 0);

beginningOfSelectionStartLine,
selection.start,
);
const contextBeforeSelectionStart = document.getText(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is just "text" not "context". I also think it would be clearer not to have the rangeBeforeSelectionStart variable and just create the range inline.

selectionRange = new vscode.Range(
new vscode.Position(selection.start.line, 0),
selection.end,
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Writing it this way makes it not clear that we're just adjusting the start position of selectionRange -range also has a with() -

selectionRange = selectionRange.with({ start: beginningOfSelectionStartLine });

would be clearer.

@Jazzcort Jazzcort force-pushed the Jazzcort/exact-select-send-to-chat branch from 9ca0f55 to 24ebc7a Compare March 7, 2025 16:45
@owtaylor
Copy link
Contributor

owtaylor commented Mar 7, 2025

Looks good to me now!

The original behavior selects the entire first line even though it's
only partially selected in the editor. This fix is to get rid of the
extra selection from the first line and only send the exact part that
gets selected in the editor to the chat section. However, there is an
exception that if the context before the start of the selection
contains only whitespace, we want to include the wihitespace into the
selection because it's the indentation.
Granite-Code/granite-code#43
@Jazzcort Jazzcort force-pushed the Jazzcort/exact-select-send-to-chat branch from 24ebc7a to 67957e2 Compare March 7, 2025 18:49
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