Skip to content

Commit 940c2e3

Browse files
committed
feat (ui): 支持显示字段帮助配置
1 parent 0b72dfe commit 940c2e3

4 files changed

Lines changed: 55 additions & 9 deletions

File tree

src-tauri/src/config.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ static CONFIG_MANAGER: Mutex<Option<ConfigManager>> = Mutex::new(None);
1212

1313
#[derive(Debug, Clone, Serialize, Deserialize)]
1414
pub struct EditorConfig {
15-
pub indent_with_tab: Option<bool>, // 是否使用 tab 缩进
16-
pub tab_size: Option<u32>, // tab 缩进, 空格数,默认为 2
17-
pub theme: Option<String>, // 编辑器主题
18-
pub font_size: Option<u32>, // 编辑器字体大小
19-
pub show_line_numbers: Option<bool>, // 是否显示行号
15+
pub indent_with_tab: Option<bool>, // 是否使用 tab 缩进
16+
pub tab_size: Option<u32>, // tab 缩进, 空格数,默认为 2
17+
pub theme: Option<String>, // 编辑器主题
18+
pub font_size: Option<u32>, // 编辑器字体大小
19+
pub show_line_numbers: Option<bool>, // 是否显示行号
20+
pub show_function_help: Option<bool>, // 是否显示函数帮助
2021
}
2122

2223
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -43,6 +44,7 @@ impl Default for AppConfig {
4344
theme: Some("githubLight".to_string()),
4445
font_size: Some(14),
4546
show_line_numbers: Some(true),
47+
show_function_help: Some(false),
4648
}),
4749
}
4850
}
@@ -100,6 +102,7 @@ impl ConfigManager {
100102
theme: Some("githubLight".to_string()),
101103
font_size: Some(14),
102104
show_line_numbers: Some(true),
105+
show_function_help: Some(false),
103106
});
104107
println!("读取配置 -> 添加默认 editor 配置");
105108
}
@@ -205,6 +208,7 @@ impl ConfigManager {
205208
theme: Some("githubLight".to_string()),
206209
font_size: Some(14),
207210
show_line_numbers: Some(true),
211+
show_function_help: Some(false),
208212
}),
209213
}
210214
}

src/components/setting/Editor.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<Switch v-model="editorConfig.show_line_numbers"/>
99
</Label>
1010

11+
<Label label="是否显示函数帮助信息">
12+
<Switch v-model="editorConfig.show_function_help"/>
13+
</Label>
14+
1115
<Label label="缩进空格数">
1216
<Number v-model="editorConfig.tab_size" :min="1" :max="8" placeholder="缩进空格数"/>
1317
</Label>

src/composables/useCodeMirrorEditor.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { invoke } from '@tauri-apps/api/core'
6161
import { useToast } from '../plugins/toast'
6262
import { StreamLanguage } from '@codemirror/language'
6363
import { EditorConfig } from '../types/app.ts'
64-
import { EditorView } from '@codemirror/view'
64+
import { EditorView, hoverTooltip } from '@codemirror/view'
6565

6666
interface 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,

src/types/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ export interface EditorConfig
5252
tab_size?: number
5353
font_size?: number
5454
show_line_numbers?: boolean
55+
show_function_help?: boolean
5556
}

0 commit comments

Comments
 (0)