Skip to content

Commit e2dc587

Browse files
committed
refactor(src-tauri/src, src/components): 重构编辑器命令和设置组件
优化了编辑器命令和设置组件的代码结构,提高了代码的可读性和可维护性。
1 parent e3e662e commit e2dc587

2 files changed

Lines changed: 56 additions & 18 deletions

File tree

src-tauri/src/commands/editor.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,49 @@ fn open_in_external_editor_impl(path: &str, editor: &str) -> Result<()> {
4343
}
4444
};
4545

46-
let status = command.status().map_err(|e| AppError::Io(e))?;
47-
if !status.success() {
48-
// If failed, try using 'open -a' as a fallback for Mac
49-
let app_name = match editor {
50-
"code" => Some("Visual Studio Code"),
51-
"cursor" => Some("Cursor"),
52-
"idea" => Some("IntelliJ IDEA"),
53-
"webstorm" => Some("WebStorm"),
54-
"sublime" => Some("Sublime Text"),
55-
_ => None,
46+
// Try to run the command directly
47+
let status_result = command.status();
48+
49+
// Check if command failed to start (IO error) or returned non-success status
50+
let should_try_fallback = match status_result {
51+
Ok(status) => !status.success(),
52+
Err(_) => true,
53+
};
54+
55+
if should_try_fallback {
56+
let mut fallback_apps = Vec::new();
57+
58+
match editor {
59+
"code" => {
60+
fallback_apps.push("Visual Studio Code");
61+
fallback_apps.push("Visual Studio Code - Insiders");
62+
fallback_apps.push("Cursor");
63+
},
64+
"cursor" => fallback_apps.push("Cursor"),
65+
"idea" => fallback_apps.push("IntelliJ IDEA"),
66+
"webstorm" => fallback_apps.push("WebStorm"),
67+
"sublime" => fallback_apps.push("Sublime Text"),
68+
_ => {},
5669
};
5770

58-
if let Some(app) = app_name {
59-
let status = Command::new("open")
71+
let mut success = false;
72+
for app in fallback_apps {
73+
let status = Command::new("open")
6074
.arg("-a")
6175
.arg(app)
6276
.arg(path)
63-
.status()
64-
.map_err(|e| AppError::Io(e))?;
77+
.status();
6578

66-
if !status.success() {
67-
return Err(AppError::InvalidInput(format!("Failed to open editor: {}", editor)));
79+
if let Ok(s) = status {
80+
if s.success() {
81+
success = true;
82+
break;
83+
}
6884
}
69-
} else {
70-
return Err(AppError::InvalidInput(format!("Failed to open editor: {}", editor)));
85+
}
86+
87+
if !success {
88+
return Err(AppError::InvalidInput(format!("Failed to open editor: {}. Please check if the editor is installed.", editor)));
7189
}
7290
}
7391
}

src/components/Settings.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,27 @@ export function Settings({ isOpen, onClose }: SettingsProps) {
342342
{editor.label}
343343
</Button>
344344
))}
345+
<Button
346+
type="button"
347+
variant={!['code', 'cursor', 'idea', 'webstorm', 'sublime'].includes(externalEditor || '') ? 'default' : 'outline'}
348+
onClick={() => setExternalEditor('custom')}
349+
className="justify-start px-3"
350+
>
351+
自定义
352+
</Button>
345353
</div>
354+
355+
{!['code', 'cursor', 'idea', 'webstorm', 'sublime'].includes(externalEditor || '') && (
356+
<div className="mt-2">
357+
<Label className="text-xs mb-1.5 block">自定义命令 / 路径</Label>
358+
<Input
359+
value={externalEditor === 'custom' ? '' : (externalEditor || '')}
360+
onChange={(e) => setExternalEditor(e.target.value)}
361+
placeholder="例如: code-insiders, vim, /usr/local/bin/subl"
362+
/>
363+
</div>
364+
)}
365+
346366
<p className="text-xs text-muted-foreground mt-1">
347367
选择在查看文件时使用的外部编辑器。
348368
</p>

0 commit comments

Comments
 (0)