From a1896293418513ea0a698f51b39d5137143c8983 Mon Sep 17 00:00:00 2001 From: Mathew Pareles Date: Mon, 14 Apr 2025 22:46:08 -0700 Subject: [PATCH] fix current file --- .../contrib/void/browser/chatThreadService.ts | 42 +++++++++++++--- .../contrib/void/browser/sidebarActions.ts | 49 ++++--------------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/vs/workbench/contrib/void/browser/chatThreadService.ts b/src/vs/workbench/contrib/void/browser/chatThreadService.ts index 34b499ca..cb2816ee 100644 --- a/src/vs/workbench/contrib/void/browser/chatThreadService.ts +++ b/src/vs/workbench/contrib/void/browser/chatThreadService.ts @@ -35,6 +35,32 @@ import { truncate } from '../../../../base/common/strings.js'; import { THREAD_STORAGE_KEY } from '../common/storageKeys.js'; import { IConvertToLLMMessageService } from './convertToLLMMessageService.js'; +export const findStagingSelectionIndex = (currentSelections: StagingSelectionItem[] | undefined, newSelection: StagingSelectionItem): number | null => { + if (!currentSelections) return null + + for (let i = 0; i < currentSelections.length; i += 1) { + const s = currentSelections[i] + + if (s.uri.fsPath !== newSelection.uri.fsPath) continue + + if (s.type === 'File' && newSelection.type === 'File') { + return i + } + if (s.type === 'CodeSelection' && newSelection.type === 'CodeSelection') { + if (s.uri.fsPath !== newSelection.uri.fsPath) continue + // if there's any collision return true + const [oldStart, oldEnd] = s.range + const [newStart, newEnd] = newSelection.range + if (oldStart !== newStart || oldEnd !== newEnd) continue + return i + } + if (s.type === 'Folder' && newSelection.type === 'Folder') { + return i + } + } + return null +} + /* @@ -281,14 +307,16 @@ class ChatThreadService extends Disposable implements IChatThreadService { } const oldStagingSelections = this.getCurrentThreadState().stagingSelections || []; - const fileIsAlreadyHere = oldStagingSelections.some(s => s.type === 'File' && s.uri.fsPath === newStagingSelection.uri.fsPath) - if (fileIsAlreadyHere) return - // remove all old selectons that are marked as `wasAddedAsCurrentFile`, and add new selection - const newStagingSelections: StagingSelectionItem[] = [ - ...oldStagingSelections.filter(s => !s.state?.wasAddedAsCurrentFile), - newStagingSelection - ] + // remove all old selectons that are marked as `wasAddedAsCurrentFile` + const newStagingSelections: StagingSelectionItem[] = oldStagingSelections.filter(s => s.state && !s.state.wasAddedAsCurrentFile) + + const fileIsAlreadyHere = oldStagingSelections.some(s => s.type === 'File' && s.uri.fsPath === newStagingSelection.uri.fsPath) + + if (!fileIsAlreadyHere) { + newStagingSelections.push(newStagingSelection) + } + this.setCurrentThreadState({ stagingSelections: newStagingSelections }); } diff --git a/src/vs/workbench/contrib/void/browser/sidebarActions.ts b/src/vs/workbench/contrib/void/browser/sidebarActions.ts index 7bcb0ff0..0c2db06b 100644 --- a/src/vs/workbench/contrib/void/browser/sidebarActions.ts +++ b/src/vs/workbench/contrib/void/browser/sidebarActions.ts @@ -24,7 +24,7 @@ import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js'; import { Disposable } from '../../../../base/common/lifecycle.js'; import { localize2 } from '../../../../nls.js'; import { StagingSelectionItem } from '../common/chatThreadServiceTypes.js'; -import { IChatThreadService } from './chatThreadService.js'; +import { findStagingSelectionIndex, IChatThreadService } from './chatThreadService.js'; // ---------- Register commands and keybindings ---------- @@ -63,31 +63,6 @@ export const roundRangeToLines = (range: IRange | null | undefined, options: { e // } -const findStagingItemToReplace = (currentSelections: StagingSelectionItem[] | undefined, newSelection: StagingSelectionItem): [number, StagingSelectionItem] | null => { - if (!currentSelections) return null - - for (let i = 0; i < currentSelections.length; i += 1) { - const s = currentSelections[i] - - if (s.uri.fsPath !== newSelection.uri.fsPath) continue - - if (s.type === 'File' && newSelection.type === 'File') { - return [i, s] as const - } - if (s.type === 'CodeSelection' && newSelection.type === 'CodeSelection') { - if (s.uri.fsPath !== newSelection.uri.fsPath) continue - // if there's any collision return true - const [oldStart, oldEnd] = s.range - const [newStart, newEnd] = newSelection.range - if (oldStart !== newStart || oldEnd !== newEnd) continue - return [i, s] as const - } - if (s.type === 'Folder' && newSelection.type === 'Folder') { - return [i, s] as const - } - } - return null -} const VOID_OPEN_SIDEBAR_ACTION_ID = 'void.sidebar.open' registerAction2(class extends Action2 { @@ -132,7 +107,7 @@ registerAction2(class extends Action2 { } - const selection: StagingSelectionItem = !selectionRange || (selectionRange.startLineNumber > selectionRange.endLineNumber) ? { + const newSelection: StagingSelectionItem = !selectionRange || (selectionRange.startLineNumber > selectionRange.endLineNumber) ? { type: 'File', uri: model.uri, language: model.getLanguageId(), @@ -163,21 +138,17 @@ registerAction2(class extends Action2 { } // if matches with existing selection, overwrite (since text may change) - const replaceRes = findStagingItemToReplace(selections, selection) - if (replaceRes) { - const [idx, newSel] = replaceRes - - if (idx !== undefined && idx !== -1) { - setSelections([ - ...selections!.slice(0, idx), - newSel, - ...selections!.slice(idx + 1, Infinity) - ]) - } + const idx = findStagingSelectionIndex(selections, newSelection) + if (idx !== null && idx !== -1) { + setSelections([ + ...selections!.slice(0, idx), + newSelection, + ...selections!.slice(idx + 1, Infinity) + ]) } // if no match, add it else { - setSelections([...(selections ?? []), selection]) + setSelections([...(selections ?? []), newSelection]) } }