fix selection range bug

This commit is contained in:
mp 2024-12-15 22:24:12 -08:00
parent 8403eed8ca
commit f973f8d4e7
2 changed files with 25 additions and 15 deletions

View file

@ -297,8 +297,6 @@ export const SidebarChat = () => {
const isDisabled = !instructions.trim() const isDisabled = !instructions.trim()
const [formHeight, setFormHeight] = useState(0) const [formHeight, setFormHeight] = useState(0)
const [sidebarHeight, setSidebarHeight] = useState(0) const [sidebarHeight, setSidebarHeight] = useState(0)
const formRef = useCallback((node: HTMLFormElement | null) => { if (node) { setFormHeight(node.clientHeight); } }, [setFormHeight]);
const sidebarRef = useCallback((node: HTMLDivElement | null) => { if (node) { setSidebarHeight(node.clientHeight); } }, [setSidebarHeight]);
const onChangeText = useCallback((newStr: string) => { setInstructions(newStr) }, [setInstructions]) const onChangeText = useCallback((newStr: string) => { setInstructions(newStr) }, [setInstructions])
@ -406,8 +404,14 @@ export const SidebarChat = () => {
const previousMessages = currentThread?.messages ?? [] const previousMessages = currentThread?.messages ?? []
const [_test, _setTest] = useState<string[]>([])
return <div return <div
ref={sidebarRef} ref={(ref) => { if (ref) { setSidebarHeight(ref.clientHeight); } }}
className={`w-full h-full`} className={`w-full h-full`}
> >
<ScrollToBottomContainer <ScrollToBottomContainer
@ -420,7 +424,11 @@ export const SidebarChat = () => {
{/* message stream */} {/* message stream */}
<ChatBubble chatMessage={{ role: 'assistant', content: messageStream, displayContent: messageStream || null }} /> <ChatBubble chatMessage={{ role: 'assistant', content: messageStream, displayContent: messageStream || null }} />
<div>{`text`}</div> <button type='button' onClick={() => { _setTest(d => [...d, 'asdasdsadasd']) }}>more divs</button>
{_test.map((_, i) => <div key={i}>div {i}</div>)}
<div>{`totalHeight: ${sidebarHeight - formHeight - 30}`}</div>
<div>{`sidebarHeight: ${sidebarHeight}`}</div>
<div>{`formHeight: ${formHeight}`}</div>
</ScrollToBottomContainer> </ScrollToBottomContainer>
@ -430,7 +438,7 @@ export const SidebarChat = () => {
className={`right-0 left-0 m-2 z-[999] ${previousMessages.length > 0 ? 'absolute bottom-0' : ''}`} className={`right-0 left-0 m-2 z-[999] ${previousMessages.length > 0 ? 'absolute bottom-0' : ''}`}
> >
<form <form
ref={formRef} ref={(ref) => { if (ref) { setFormHeight(ref.clientHeight); } }}
className={` className={`
flex flex-col gap-2 p-2 relative input text-left shrink-0 flex flex-col gap-2 p-2 relative input text-left shrink-0
transition-all duration-200 transition-all duration-200

View file

@ -73,20 +73,12 @@ registerAction2(class extends Action2 {
accessor.get(IEditorService).activeTextEditorControl?.getSelection() accessor.get(IEditorService).activeTextEditorControl?.getSelection()
) )
// add selection
const threadHistoryService = accessor.get(IThreadHistoryService)
const currentStaging = threadHistoryService.state._currentStagingSelections
const currentStagingEltIdx = currentStaging?.findIndex(s =>
s.fileURI.fsPath === model.uri.fsPath
&& s.range?.startLineNumber === selectionRange?.startLineNumber
&& s.range?.endLineNumber === selectionRange?.endLineNumber
)
if (selectionRange) { if (selectionRange) {
const selectionStr = getContentInRange(model, selectionRange) const selectionStr = getContentInRange(model, selectionRange)
const selection: CodeStagingSelection = selectionStr === null ? { const selection: CodeStagingSelection = selectionStr === null || selectionRange.startLineNumber > selectionRange.endLineNumber ? {
type: 'File', type: 'File',
fileURI: model.uri, fileURI: model.uri,
selectionStr: null, selectionStr: null,
@ -98,7 +90,16 @@ registerAction2(class extends Action2 {
range: selectionRange, range: selectionRange,
} }
// overwrite selections that match with this one (compares by `fileURI` and line numbers in `range`) // add selection to staging
const threadHistoryService = accessor.get(IThreadHistoryService)
const currentStaging = threadHistoryService.state._currentStagingSelections
const currentStagingEltIdx = currentStaging?.findIndex(s =>
s.fileURI.fsPath === model.uri.fsPath
&& s.range?.startLineNumber === selection.range?.startLineNumber
&& s.range?.endLineNumber === selection.range?.endLineNumber
)
// if matches with existing selection, overwrite
if (currentStagingEltIdx !== undefined && currentStagingEltIdx !== -1) { if (currentStagingEltIdx !== undefined && currentStagingEltIdx !== -1) {
threadHistoryService.setStaging([ threadHistoryService.setStaging([
...currentStaging!.slice(0, currentStagingEltIdx), ...currentStaging!.slice(0, currentStagingEltIdx),
@ -106,6 +107,7 @@ registerAction2(class extends Action2 {
...currentStaging!.slice(currentStagingEltIdx + 1, Infinity) ...currentStaging!.slice(currentStagingEltIdx + 1, Infinity)
]) ])
} }
// if no match, add
else { else {
threadHistoryService.setStaging([...(currentStaging ?? []), selection]) threadHistoryService.setStaging([...(currentStaging ?? []), selection])
} }