diff --git a/ee/tabby-ui/app/files/components/chat-side-bar.tsx b/ee/tabby-ui/app/files/components/chat-side-bar.tsx index 4badc028c50a..8e96acc9ef31 100644 --- a/ee/tabby-ui/app/files/components/chat-side-bar.tsx +++ b/ee/tabby-ui/app/files/components/chat-side-bar.tsx @@ -173,22 +173,28 @@ function ChatSideBarRenderer({ if (!textEditorViewRef.current || !activeEntryInfo) return null const context = getActiveSelection(textEditorViewRef.current) - const editorFileContext: EditorFileContext | null = - context && activeEntryInfo.basename && activeRepo - ? { - kind: 'file', - filepath: { - kind: 'git', - filepath: activeEntryInfo.basename, - gitUrl: activeRepo?.gitUrl - }, - range: { + + if (!context || !activeEntryInfo.basename || !activeRepo) { + return null + } + + const editorFileContext: EditorFileContext = { + kind: 'file', + filepath: { + kind: 'git', + filepath: activeEntryInfo.basename, + gitUrl: activeRepo?.gitUrl + }, + range: + 'startLine' in context + ? { start: context.startLine, end: context.endLine - }, - content: context.content - } - : null + } + : undefined, + content: context.content + } + return editorFileContext }) diff --git a/ee/tabby-ui/app/files/components/code-editor-view.tsx b/ee/tabby-ui/app/files/components/code-editor-view.tsx index ae3b66736515..fbfe7c4aaef0 100644 --- a/ee/tabby-ui/app/files/components/code-editor-view.tsx +++ b/ee/tabby-ui/app/files/components/code-editor-view.tsx @@ -108,28 +108,37 @@ const CodeEditorView: React.FC = ({ value, language }) => { result.push( SelectionChangeExtension( ( - context: { - content: string - startLine: number - endLine: number - } | null + context: + | { + content: string + } + | { + content: string + startLine: number + endLine: number + } + | null ) => { - const editorFileContext: EditorFileContext | null = - context && activeEntryInfo.basename && activeRepo - ? { - kind: 'file', - filepath: { - kind: 'git', - filepath: activeEntryInfo.basename, - gitUrl: activeRepo?.gitUrl - }, - range: { + if (!context || !activeEntryInfo?.basename || !activeRepo) { + return null + } + + const editorFileContext: EditorFileContext = { + kind: 'file', + filepath: { + kind: 'git', + filepath: activeEntryInfo.basename, + gitUrl: activeRepo?.gitUrl + }, + range: + 'startLine' in context + ? { start: context.startLine, end: context.endLine - }, - content: context.content - } - : null + } + : undefined, + content: context.content + } emitter.emit('selection_change', editorFileContext) } @@ -270,6 +279,7 @@ const CodeEditorView: React.FC = ({ value, language }) => { return () => { window.removeEventListener('keydown', handleKeyDown) + emitter.emit('selection_change', null) } }, [editorView]) diff --git a/ee/tabby-ui/app/files/lib/selection-extension/index.ts b/ee/tabby-ui/app/files/lib/selection-extension/index.ts index 0fc0f48e52b6..cf9319143b67 100644 --- a/ee/tabby-ui/app/files/lib/selection-extension/index.ts +++ b/ee/tabby-ui/app/files/lib/selection-extension/index.ts @@ -1,11 +1,13 @@ import { Extension } from '@codemirror/state' import { EditorView, ViewPlugin, ViewUpdate } from '@codemirror/view' -interface SelectionContext { - content: string - startLine: number - endLine: number -} +type SelectionContext = + | { + content: string + startLine: number + endLine: number + } + | { content: string } export function SelectionChangeExtension( onSelectionChange: (fileContext: SelectionContext | null) => void @@ -24,18 +26,17 @@ export function SelectionChangeExtension( // ignore changes if the view has lost focus return } else { - onSelectionChange(null) - } - if (update.selectionSet) { this.handleSelectionChange(update.view) - } else if (!update.focusChanged || update.view.hasFocus) { - onSelectionChange(null) } } - handleSelectionChange(view: EditorView) { - const data = getActiveSelection(view) - onSelectionChange(data) + handleSelectionChange(view: EditorView | null) { + if (!view) { + onSelectionChange(null) + } else { + const data = getActiveSelection(view) + onSelectionChange(data) + } } } ) @@ -44,7 +45,11 @@ export function SelectionChangeExtension( export function getActiveSelection(view: EditorView): SelectionContext | null { const selection = view.state.selection.main - if (selection.empty) return null + if (selection.empty) { + return { + content: view.state.doc.toString() + } + } const content = view.state.sliceDoc(selection.from, selection.to) const startLine = view.state.doc.lineAt(selection.from).number diff --git a/ee/tabby-ui/lib/utils/index.ts b/ee/tabby-ui/lib/utils/index.ts index bf256c26bcfc..15d59bf449b9 100644 --- a/ee/tabby-ui/lib/utils/index.ts +++ b/ee/tabby-ui/lib/utils/index.ts @@ -214,7 +214,7 @@ export function convertEditorContext( ): FileContext { const convertRange = (range: LineRange | PositionRange | undefined) => { // If the range is not provided, the whole file is considered. - if (!range) { + if (!range || typeof range.start === 'undefined') { return undefined }