@@ -96,6 +96,21 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
9696 const [ fileSearchResults , setFileSearchResults ] = useState < SearchResult [ ] > ( [ ] )
9797 const [ searchLoading , setSearchLoading ] = useState ( false )
9898 const [ searchRequestId , setSearchRequestId ] = useState < string > ( "" )
99+ const [ isDraggingOver , setIsDraggingOver ] = useState ( false )
100+ const [ textAreaBaseHeight , setTextAreaBaseHeight ] = useState < number | undefined > ( undefined )
101+ const [ showContextMenu , setShowContextMenu ] = useState ( false )
102+ const [ cursorPosition , setCursorPosition ] = useState ( 0 )
103+ const [ searchQuery , setSearchQuery ] = useState ( "" )
104+ const textAreaRef = useRef < HTMLTextAreaElement | null > ( null )
105+ const [ isMouseDownOnMenu , setIsMouseDownOnMenu ] = useState ( false )
106+ const highlightLayerRef = useRef < HTMLDivElement > ( null )
107+ const [ selectedMenuIndex , setSelectedMenuIndex ] = useState ( - 1 )
108+ const [ selectedType , setSelectedType ] = useState < ContextMenuOptionType | null > ( null )
109+ const [ justDeletedSpaceAfterMention , setJustDeletedSpaceAfterMention ] = useState ( false )
110+ const [ intendedCursorPosition , setIntendedCursorPosition ] = useState < number | null > ( null )
111+ const contextMenuContainerRef = useRef < HTMLDivElement > ( null )
112+ const [ isEnhancingPrompt , setIsEnhancingPrompt ] = useState ( false )
113+ const [ isFocused , setIsFocused ] = useState ( false )
99114
100115 // Close dropdown when clicking outside.
101116 useEffect ( ( ) => {
@@ -135,28 +150,63 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
135150 if ( message . requestId === searchRequestId ) {
136151 setFileSearchResults ( message . results || [ ] )
137152 }
153+ } else if ( message . type === "editorContext" ) {
154+ // Handle editor context response
155+ if ( message . editorContext && textAreaRef . current ) {
156+ const editorContext = message . editorContext
157+ let insertValue = ""
158+
159+ if ( editorContext . filePath ) {
160+ // Format: filename:startLine-endLine (selected text preview)
161+ const fileName = editorContext . filePath . split ( "/" ) . pop ( ) || editorContext . filePath
162+ let contextText = fileName
163+
164+ if ( editorContext . startLine !== undefined ) {
165+ if (
166+ editorContext . endLine !== undefined &&
167+ editorContext . endLine !== editorContext . startLine
168+ ) {
169+ contextText += `:${ editorContext . startLine } -${ editorContext . endLine } `
170+ } else {
171+ contextText += `:${ editorContext . startLine } `
172+ }
173+ }
174+
175+ if ( editorContext . selectedText && editorContext . selectedText . trim ( ) ) {
176+ const preview = editorContext . selectedText . trim ( ) . substring ( 0 , 50 )
177+ contextText += ` (${ preview } ${ editorContext . selectedText . length > 50 ? "..." : "" } )`
178+ }
179+
180+ insertValue = contextText
181+ } else {
182+ insertValue = "current-editor"
183+ }
184+
185+ const { newValue, mentionIndex } = insertMention (
186+ textAreaRef . current . value ,
187+ cursorPosition ,
188+ insertValue ,
189+ )
190+
191+ setInputValue ( newValue )
192+ const newCursorPosition = newValue . indexOf ( " " , mentionIndex + insertValue . length ) + 1
193+ setCursorPosition ( newCursorPosition )
194+ setIntendedCursorPosition ( newCursorPosition )
195+
196+ // Scroll to cursor
197+ setTimeout ( ( ) => {
198+ if ( textAreaRef . current ) {
199+ textAreaRef . current . blur ( )
200+ textAreaRef . current . focus ( )
201+ }
202+ } , 0 )
203+ }
138204 }
139205 }
140206
141207 window . addEventListener ( "message" , messageHandler )
142208 return ( ) => window . removeEventListener ( "message" , messageHandler )
143- } , [ setInputValue , searchRequestId ] )
144-
145- const [ isDraggingOver , setIsDraggingOver ] = useState ( false )
146- const [ textAreaBaseHeight , setTextAreaBaseHeight ] = useState < number | undefined > ( undefined )
147- const [ showContextMenu , setShowContextMenu ] = useState ( false )
148- const [ cursorPosition , setCursorPosition ] = useState ( 0 )
149- const [ searchQuery , setSearchQuery ] = useState ( "" )
150- const textAreaRef = useRef < HTMLTextAreaElement | null > ( null )
151- const [ isMouseDownOnMenu , setIsMouseDownOnMenu ] = useState ( false )
152- const highlightLayerRef = useRef < HTMLDivElement > ( null )
153- const [ selectedMenuIndex , setSelectedMenuIndex ] = useState ( - 1 )
154- const [ selectedType , setSelectedType ] = useState < ContextMenuOptionType | null > ( null )
155- const [ justDeletedSpaceAfterMention , setJustDeletedSpaceAfterMention ] = useState ( false )
156- const [ intendedCursorPosition , setIntendedCursorPosition ] = useState < number | null > ( null )
157- const contextMenuContainerRef = useRef < HTMLDivElement > ( null )
158- const [ isEnhancingPrompt , setIsEnhancingPrompt ] = useState ( false )
159- const [ isFocused , setIsFocused ] = useState ( false )
209+ } , [ setInputValue , searchRequestId , cursorPosition ] )
160210
161211 // Use custom hook for prompt history navigation
162212 const { handleHistoryNavigation, resetHistoryNavigation, resetOnInputChange } = usePromptHistory ( {
@@ -277,6 +327,12 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
277327 insertValue = "problems"
278328 } else if ( type === ContextMenuOptionType . Terminal ) {
279329 insertValue = "terminal"
330+ } else if ( type === ContextMenuOptionType . EditorContext ) {
331+ // Request editor context from backend
332+ vscode . postMessage ( { type : "requestEditorContext" } )
333+ setShowContextMenu ( false )
334+ setSelectedType ( null )
335+ return
280336 } else if ( type === ContextMenuOptionType . Git ) {
281337 insertValue = value || ""
282338 }
0 commit comments