@@ -12,6 +12,7 @@ import { useRoute } from "@tui/context/route"
1212import { useProject } from "@tui/context/project"
1313import { useSync } from "@tui/context/sync"
1414import { useEvent } from "@tui/context/event"
15+ import { useEditorContext } from "@tui/context/editor"
1516import { MessageID , PartID } from "@/session/schema"
1617import { createStore , produce , unwrap } from "solid-js/store"
1718import { useKeybind } from "@tui/context/keybind"
@@ -21,7 +22,7 @@ import { usePromptStash } from "./stash"
2122import { DialogStash } from "../dialog-stash"
2223import { type AutocompleteRef , Autocomplete } from "./autocomplete"
2324import { useCommandDialog } from "../dialog-command"
24- import { useRenderer , type JSX } from "@opentui/solid"
25+ import { useRenderer , useTerminalDimensions , type JSX } from "@opentui/solid"
2526import * as Editor from "@tui/util/editor"
2627import { useExit } from "../../context/exit"
2728import * as Clipboard from "../../util/clipboard"
@@ -94,6 +95,7 @@ export function Prompt(props: PromptProps) {
9495 const local = useLocal ( )
9596 const args = useArgs ( )
9697 const sdk = useSDK ( )
98+ const editor = useEditorContext ( )
9799 const route = useRoute ( )
98100 const project = useProject ( )
99101 const sync = useSync ( )
@@ -104,11 +106,34 @@ export function Prompt(props: PromptProps) {
104106 const stash = usePromptStash ( )
105107 const command = useCommandDialog ( )
106108 const renderer = useRenderer ( )
109+ const dimensions = useTerminalDimensions ( )
107110 const { theme, syntax } = useTheme ( )
108111 const kv = useKV ( )
109112 const animationsEnabled = createMemo ( ( ) => kv . get ( "animations_enabled" , true ) )
110113 const list = createMemo ( ( ) => props . placeholders ?. normal ?? [ ] )
111114 const shell = createMemo ( ( ) => props . placeholders ?. shell ?? [ ] )
115+ const editorPath = createMemo ( ( ) => editor . selection ( ) ?. filePath )
116+ const editorSelectionLabel = createMemo ( ( ) => {
117+ const selection = editor . selection ( ) ?. selection
118+ if ( ! selection ) return
119+ if ( selection . start . line === selection . end . line && selection . start . character === selection . end . character ) return
120+ if ( selection . start . line === selection . end . line ) return `#${ selection . start . line } `
121+ return `#${ selection . start . line } -${ selection . end . line } `
122+ } )
123+ const editorFileLabel = createMemo ( ( ) => {
124+ const value = editorPath ( )
125+ if ( ! value ) return
126+ const filename = path . basename ( value )
127+ const file = / ^ i n d e x \. [ ^ . / ] + $ / . test ( filename )
128+ ? [ path . basename ( path . dirname ( value ) ) , filename ] . filter ( Boolean ) . join ( "/" )
129+ : filename
130+ return `${ file . split ( path . sep ) . join ( "/" ) } ${ editorSelectionLabel ( ) ?? "" } `
131+ } )
132+ const editorFileLabelDisplay = createMemo ( ( ) => {
133+ const file = editorFileLabel ( )
134+ if ( ! file ) return
135+ return Locale . truncateMiddle ( file , Math . max ( 12 , Math . min ( 48 , Math . floor ( dimensions ( ) . width / 3 ) ) ) )
136+ } )
112137 const [ auto , setAuto ] = createSignal < AutocompleteRef > ( )
113138 const currentProviderLabel = createMemo ( ( ) => local . model . parsed ( ) . provider )
114139 const hasRightContent = createMemo ( ( ) => Boolean ( props . right ) )
@@ -721,6 +746,27 @@ export function Prompt(props: PromptProps) {
721746 // Capture mode before it gets reset
722747 const currentMode = store . mode
723748 const variant = local . model . variant . current ( )
749+ const editorSelection = editor . selection ( )
750+ const editorParts = editorSelection
751+ ? [
752+ {
753+ id : PartID . ascending ( ) ,
754+ type : "text" as const ,
755+ text : ( ( ) => {
756+ const start = editorSelection . selection . start
757+ const end = editorSelection . selection . end
758+ if ( start . line === end . line && start . character === end . character ) {
759+ return `Note: The user opened the file "${ editorSelection . filePath } ".`
760+ }
761+ if ( start . line === end . line ) {
762+ return `Note: The user selected line ${ start . line } from "${ editorSelection . filePath } ": ${ editorSelection . text } `
763+ }
764+ return `Note: The user selected lines ${ start . line } to ${ end . line } from "${ editorSelection . filePath } ": ${ editorSelection . text } `
765+ } ) ( ) ,
766+ synthetic : true ,
767+ } ,
768+ ]
769+ : [ ]
724770
725771 if ( store . mode === "shell" ) {
726772 void sdk . client . session . shell ( {
@@ -773,6 +819,7 @@ export function Prompt(props: PromptProps) {
773819 model : selectedModel ,
774820 variant,
775821 parts : [
822+ ...editorParts ,
776823 {
777824 id : PartID . ascending ( ) ,
778825 type : "text" ,
@@ -1332,6 +1379,7 @@ export function Prompt(props: PromptProps) {
13321379 </ Show >
13331380 < Show when = { status ( ) . type !== "retry" } >
13341381 < box gap = { 2 } flexDirection = "row" >
1382+ < Show when = { editorFileLabelDisplay ( ) } > { ( file ) => < text fg = { theme . secondary } > { file ( ) } </ text > } </ Show >
13351383 < Switch >
13361384 < Match when = { store . mode === "normal" } >
13371385 < Switch >
0 commit comments