@@ -60,14 +60,8 @@ import {
6060import { invoke } from '@tauri-apps/api/core'
6161import { useToast } from '../plugins/toast'
6262import { StreamLanguage } from '@codemirror/language'
63-
64- interface EditorConfig
65- {
66- theme ?: string
67- indent_with_tab ?: boolean
68- tab_size ?: number
69- font_size ?: number
70- }
63+ import { EditorConfig } from '../types/app.ts'
64+ import { EditorView , hoverTooltip } from '@codemirror/view'
7165
7266interface Props
7367{
@@ -83,6 +77,14 @@ export function useCodeMirrorEditor(props: Props)
8377 const isReady = ref ( false )
8478 const extensions = ref < any [ ] > ( [ ] )
8579 const editorConfig = ref < EditorConfig > ( { } )
80+ const defaultConfig = {
81+ theme : 'githubLight' ,
82+ indent_with_tab : true ,
83+ tab_size : 2 ,
84+ font_size : 14 ,
85+ show_line_numbers : false ,
86+ show_function_help : false
87+ }
8688
8789 // 主题映射
8890 const themeMap : Record < string , any > = {
@@ -180,8 +182,44 @@ export function useCodeMirrorEditor(props: Props)
180182 }
181183 }
182184
185+ // 隐藏行号的主题扩展
186+ const hideLineNumbersTheme = EditorView . theme ( {
187+ '.cm-lineNumbers' : {
188+ display : 'none !important'
189+ } ,
190+ '.cm-gutters' : {
191+ display : 'none !important'
192+ }
193+ } )
194+
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+
183221 // 更新扩展的函数
184- const updateExtensions = async ( ) => {
222+ const updateExtensions = async ( showLineNumbers ?: boolean , showFunctionHelp ?: boolean ) => {
185223 const result = [ ]
186224
187225 // 添加主题扩展
@@ -196,6 +234,18 @@ export function useCodeMirrorEditor(props: Props)
196234 }
197235 }
198236
237+ // 处理行号显示逻辑
238+ const shouldShowLineNumbers = showLineNumbers ?? editorConfig . value ?. show_line_numbers ?? false
239+ // 如果配置为不显示行号,则添加隐藏行号的扩展
240+ if ( ! shouldShowLineNumbers ) {
241+ result . push ( hideLineNumbersTheme )
242+ }
243+
244+ const shouldShowFunctionHelp = showFunctionHelp ?? editorConfig . value ?. show_function_help ?? false
245+ if ( shouldShowFunctionHelp ) {
246+ result . push ( showFunctionHelpHover )
247+ }
248+
199249 extensions . value = result
200250
201251 // 如果组件还没准备好,等待下一个 tick 后设置为准备好
@@ -219,26 +269,14 @@ export function useCodeMirrorEditor(props: Props)
219269 }
220270 else {
221271 // 如果没有配置,使用默认配置
222- editorConfig . value = {
223- theme : 'githubLight' ,
224- indent_with_tab : true ,
225- tab_size : 2 ,
226- font_size : 14
227- }
272+ editorConfig . value = defaultConfig
228273 await updateExtensions ( )
229274 }
230275 }
231276 catch ( error ) {
232277 console . error ( '获取配置失败:' , error )
233278 toast . error ( '获取配置失败 - 错误信息: ' + error )
234-
235- // 失败时使用默认配置
236- editorConfig . value = {
237- theme : 'githubLight' ,
238- indent_with_tab : true ,
239- tab_size : 2 ,
240- font_size : 14
241- }
279+ editorConfig . value = defaultConfig
242280 await updateExtensions ( )
243281 }
244282 }
@@ -296,6 +334,18 @@ export function useCodeMirrorEditor(props: Props)
296334 await reRenderEditor ( )
297335 } , { immediate : false } )
298336
337+ // 监听行号显示配置变化
338+ watch ( ( ) => editorConfig . value ?. show_line_numbers , async ( ) => {
339+ console . log ( '行号显示配置变化:' , editorConfig . value ?. show_line_numbers )
340+ await reRenderEditor ( )
341+ } , { immediate : false } )
342+
343+ // 监听函数帮助配置变化
344+ watch ( ( ) => editorConfig . value ?. show_function_help , async ( ) => {
345+ console . log ( '函数帮助配置变化:' , editorConfig . value ?. show_function_help )
346+ await reRenderEditor ( )
347+ } )
348+
299349 return {
300350 // 状态
301351 isReady,
@@ -313,4 +363,4 @@ export function useCodeMirrorEditor(props: Props)
313363 getThemeExtension,
314364 getLanguageExtension
315365 }
316- }
366+ }
0 commit comments