Skip to content

Commit

Permalink
chore(ui): set active text tab as default context in Code Browser chat (
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung authored Jan 20, 2025
1 parent 72478e5 commit 221bed8
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
34 changes: 20 additions & 14 deletions ee/tabby-ui/app/files/components/chat-side-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
48 changes: 29 additions & 19 deletions ee/tabby-ui/app/files/components/code-editor-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,37 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ 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)
}
Expand Down Expand Up @@ -270,6 +279,7 @@ const CodeEditorView: React.FC<CodeEditorViewProps> = ({ value, language }) => {

return () => {
window.removeEventListener('keydown', handleKeyDown)
emitter.emit('selection_change', null)
}
}, [editorView])

Expand Down
33 changes: 19 additions & 14 deletions ee/tabby-ui/app/files/lib/selection-extension/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
}
)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-ui/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 221bed8

Please sign in to comment.