Skip to content

Commit 637ea73

Browse files
authored
Merge pull request #41 from qianmoQ/dev-25.0.2
feat (ui): 支持编辑器行号配置
2 parents 3b25246 + 940c2e3 commit 637ea73

File tree

5 files changed

+105
-36
lines changed

5 files changed

+105
-36
lines changed

src-tauri/src/config.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +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>, // 编辑器字体大小
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>, // 是否显示函数帮助
1921
}
2022

2123
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -41,6 +43,8 @@ impl Default for AppConfig {
4143
tab_size: Some(2),
4244
theme: Some("githubLight".to_string()),
4345
font_size: Some(14),
46+
show_line_numbers: Some(true),
47+
show_function_help: Some(false),
4448
}),
4549
}
4650
}
@@ -97,6 +101,8 @@ impl ConfigManager {
97101
tab_size: Some(2),
98102
theme: Some("githubLight".to_string()),
99103
font_size: Some(14),
104+
show_line_numbers: Some(true),
105+
show_function_help: Some(false),
100106
});
101107
println!("读取配置 -> 添加默认 editor 配置");
102108
}
@@ -201,6 +207,8 @@ impl ConfigManager {
201207
tab_size: Some(2),
202208
theme: Some("githubLight".to_string()),
203209
font_size: Some(14),
210+
show_line_numbers: Some(true),
211+
show_function_help: Some(false),
204212
}),
205213
}
206214
}

src/components/setting/Editor.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
<Switch v-model="editorConfig.indent_with_tab"/>
55
</Label>
66

7+
<Label label="是否显示行号">
8+
<Switch v-model="editorConfig.show_line_numbers"/>
9+
</Label>
10+
11+
<Label label="是否显示函数帮助信息">
12+
<Switch v-model="editorConfig.show_function_help"/>
13+
</Label>
14+
715
<Label label="缩进空格数">
816
<Number v-model="editorConfig.tab_size" :min="1" :max="8" placeholder="缩进空格数"/>
917
</Label>

src/composables/useCodeMirrorEditor.ts

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,8 @@ import {
6060
import { invoke } from '@tauri-apps/api/core'
6161
import { useToast } from '../plugins/toast'
6262
import { 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

7266
interface 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+
}

src/composables/useEditorConfig.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ import { ref, watch } from 'vue'
22
import { debounce } from 'lodash-es'
33
import { invoke } from '@tauri-apps/api/core'
44
import { useToast } from '../plugins/toast'
5-
6-
interface EditorConfig
7-
{
8-
tab_size?: number
9-
theme?: string
10-
indent_with_tab?: boolean
11-
font_size?: number
12-
}
5+
import { EditorConfig } from '../types/app.ts'
136

147
interface ThemeOption
158
{

src/types/app.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ export interface AppInfo
4444
platform: string
4545
arch: string
4646
}
47+
48+
export interface EditorConfig
49+
{
50+
theme?: string
51+
indent_with_tab?: boolean
52+
tab_size?: number
53+
font_size?: number
54+
show_line_numbers?: boolean
55+
show_function_help?: boolean
56+
}

0 commit comments

Comments
 (0)