From 67957e222524ea1e2c2d9a02a6769e13242081de Mon Sep 17 00:00:00 2001 From: Chih-Tao Lee Date: Wed, 5 Mar 2025 16:10:17 -0500 Subject: [PATCH] Fixed unwantted selecting behavior for send-to-chat 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. https://github.com/Granite-Code/granite-code/issues/43 --- extensions/vscode/src/commands.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index fa6b44c2a..8601ac49c 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -123,9 +123,20 @@ function getRangeInFileWithContents( return null; } - // adjust starting position to include indentation - const start = new vscode.Position(selection.start.line, 0); - const selectionRange = new vscode.Range(start, selection.end); + let selectionRange = new vscode.Range(selection.start, selection.end); + const document = editor.document; + // Select the context from the beginning of the selection start line to the selection start position + const beginningOfSelectionStartLine = selection.start.with(undefined, 0); + const textBeforeSelectionStart = document.getText( + new vscode.Range(beginningOfSelectionStartLine, selection.start), + ); + // If there are only whitespace before the start of the selection, include the indentation + if (textBeforeSelectionStart.trim().length === 0) { + selectionRange = selectionRange.with({ + start: beginningOfSelectionStartLine, + }); + } + const contents = editor.document.getText(selectionRange); return {