@@ -61,7 +61,7 @@ import { invoke } from '@tauri-apps/api/core'
6161import { useToast } from '../plugins/toast'
6262import { StreamLanguage } from '@codemirror/language'
6363import { EditorConfig } from '../types/app.ts'
64- import { EditorView } from '@codemirror/view'
64+ import { EditorView , hoverTooltip } from '@codemirror/view'
6565
6666interface Props
6767{
@@ -82,7 +82,8 @@ export function useCodeMirrorEditor(props: Props)
8282 indent_with_tab : true ,
8383 tab_size : 2 ,
8484 font_size : 14 ,
85- show_line_numbers : false
85+ show_line_numbers : false ,
86+ show_function_help : false
8687 }
8788
8889 // 主题映射
@@ -191,8 +192,34 @@ export function useCodeMirrorEditor(props: Props)
191192 }
192193 } )
193194
195+ // 显示函数提示扩展
196+ const showFunctionHelpHover = hoverTooltip ( ( view , pos , side ) => {
197+ let { from, to, text } = view . state . doc . lineAt ( pos )
198+ let start = pos , end = pos
199+ while ( start > from && / \w / . test ( text [ start - from - 1 ] ) ) {
200+ start --
201+ }
202+ while ( end < to && / \w / . test ( text [ end - from ] ) ) {
203+ end ++
204+ }
205+ if ( start == pos && side < 0 || end == pos && side > 0 ) {
206+ return null
207+ }
208+ return {
209+ pos : start ,
210+ end,
211+ above : true ,
212+ create ( _view )
213+ {
214+ let dom = document . createElement ( 'div' )
215+ dom . textContent = text . slice ( start - from , end - from )
216+ return { dom }
217+ }
218+ }
219+ } , { hoverTime : 500 } )
220+
194221 // 更新扩展的函数
195- const updateExtensions = async ( showLineNumbers ?: boolean ) => {
222+ const updateExtensions = async ( showLineNumbers ?: boolean , showFunctionHelp ?: boolean ) => {
196223 const result = [ ]
197224
198225 // 添加主题扩展
@@ -209,12 +236,16 @@ export function useCodeMirrorEditor(props: Props)
209236
210237 // 处理行号显示逻辑
211238 const shouldShowLineNumbers = showLineNumbers ?? editorConfig . value ?. show_line_numbers ?? false
212-
213239 // 如果配置为不显示行号,则添加隐藏行号的扩展
214240 if ( ! shouldShowLineNumbers ) {
215241 result . push ( hideLineNumbersTheme )
216242 }
217243
244+ const shouldShowFunctionHelp = showFunctionHelp ?? editorConfig . value ?. show_function_help ?? false
245+ if ( shouldShowFunctionHelp ) {
246+ result . push ( showFunctionHelpHover )
247+ }
248+
218249 extensions . value = result
219250
220251 // 如果组件还没准备好,等待下一个 tick 后设置为准备好
@@ -309,6 +340,12 @@ export function useCodeMirrorEditor(props: Props)
309340 await reRenderEditor ( )
310341 } , { immediate : false } )
311342
343+ // 监听函数帮助配置变化
344+ watch ( ( ) => editorConfig . value ?. show_function_help , async ( ) => {
345+ console . log ( '函数帮助配置变化:' , editorConfig . value ?. show_function_help )
346+ await reRenderEditor ( )
347+ } )
348+
312349 return {
313350 // 状态
314351 isReady,
0 commit comments