feat(vscode): PyCharm 启动命令可配置(支持 ~/绝对路径),抽 expandTilde 复用

GUI 启动的 VS Code 扩展进程 PATH 常不含 JetBrains 启动器,裸 pycharm 易 ENOENT。
新增设置 pycharmCommand(默认 pycharm),贯穿 store/settings handler/messages/
KanbanPanel 与 webview,「钩子」分组在选 PyCharm 时显示「PyCharm 启动命令」输入框。
handleOpenWorktree 用该命令并经 expandTilde 展开 ~。expandTilde 从 resolveWorktreePath
抽出共用。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-24 05:29:19 +08:00
co-authored by Claude Opus 4.7
parent cacc60eb0e
commit 02b0aca891
10 changed files with 84 additions and 9 deletions
+10 -5
View File
@@ -10,16 +10,21 @@ import { execFile } from 'node:child_process'
import * as os from 'node:os' import * as os from 'node:os'
import * as path from 'node:path' import * as path from 'node:path'
/**
* 把开头的 `~` 展开为用户 home 目录。execFile 不走 shell,传给它的命令/路径
* 里的 `~` 不会被展开,需要我们自己处理。
*/
export function expandTilde(p: string): string {
return p.startsWith('~') ? path.join(os.homedir(), p.slice(1)) : p
}
/** /**
* 把存储的 worktreePath 解析成可用的绝对路径。三态合一: * 把存储的 worktreePath 解析成可用的绝对路径。三态合一:
* 开头 `~` → 展开为 homegit/execFile 不走 shell,自己不展开 `~`, * 开头 `~` → 展开为 home存量工单里可能存了带 `~` 的字面量);已是绝对 → 原样;
* 存量工单里可能存了带 `~` 的字面量);已是绝对 → 原样;
* 相对 → 拼到 workspaceRoot 下(旧的 .claude/worktrees/<hash> 方案)。 * 相对 → 拼到 workspaceRoot 下(旧的 .claude/worktrees/<hash> 方案)。
*/ */
export function resolveWorktreePath(stored: string, workspaceRoot: string): string { export function resolveWorktreePath(stored: string, workspaceRoot: string): string {
let p = stored const p = expandTilde(stored)
if (p.startsWith('~'))
p = path.join(os.homedir(), p.slice(1))
return path.isAbsolute(p) ? p : path.join(workspaceRoot, p) return path.isAbsolute(p) ? p : path.join(workspaceRoot, p)
} }
+2
View File
@@ -842,6 +842,7 @@ export class KanbanWebviewPanel {
autoBuildBranch: s.autoBuildBranch, autoBuildBranch: s.autoBuildBranch,
worktreeDirectory: s.worktreeDirectory, worktreeDirectory: s.worktreeDirectory,
worktreeOpenWith: s.worktreeOpenWith, worktreeOpenWith: s.worktreeOpenWith,
pycharmCommand: s.pycharmCommand,
worktreePostCreateScript: s.worktreePostCreateScript, worktreePostCreateScript: s.worktreePostCreateScript,
worktreePreRemoveScript: s.worktreePreRemoveScript, worktreePreRemoveScript: s.worktreePreRemoveScript,
implTabPreCreateScript: s.implTabPreCreateScript, implTabPreCreateScript: s.implTabPreCreateScript,
@@ -903,6 +904,7 @@ export class KanbanWebviewPanel {
autoBuildBranch: s.autoBuildBranch, autoBuildBranch: s.autoBuildBranch,
worktreeDirectory: s.worktreeDirectory, worktreeDirectory: s.worktreeDirectory,
worktreeOpenWith: s.worktreeOpenWith, worktreeOpenWith: s.worktreeOpenWith,
pycharmCommand: s.pycharmCommand,
worktreePostCreateScript: s.worktreePostCreateScript, worktreePostCreateScript: s.worktreePostCreateScript,
worktreePreRemoveScript: s.worktreePreRemoveScript, worktreePreRemoveScript: s.worktreePreRemoveScript,
implTabPreCreateScript: s.implTabPreCreateScript, implTabPreCreateScript: s.implTabPreCreateScript,
+7
View File
@@ -27,6 +27,7 @@ export async function handleSettingsSave(panel: KanbanWebviewPanel, payload: {
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
@@ -48,6 +49,9 @@ export async function handleSettingsSave(panel: KanbanWebviewPanel, payload: {
// worktree dir template: '' = "use default", so don't preserve blanks // worktree dir template: '' = "use default", so don't preserve blanks
// specially — getSettings falls back to the default at read time. // specially — getSettings falls back to the default at read time.
const trimmedWorktreeDir = payload.worktreeDirectory.trim() const trimmedWorktreeDir = payload.worktreeDirectory.trim()
// pycharm launcher command: '' = "use default `pycharm`", getSettings falls
// back at read time, so just strip whitespace here.
const trimmedPycharmCmd = payload.pycharmCommand.trim()
// Hook script paths: keep '' meaningful (= "use default // Hook script paths: keep '' meaningful (= "use default
// .spx/*.sh"). Just strip whitespace. // .spx/*.sh"). Just strip whitespace.
const trimmedPostCreate = payload.worktreePostCreateScript.trim() const trimmedPostCreate = payload.worktreePostCreateScript.trim()
@@ -78,6 +82,7 @@ export async function handleSettingsSave(panel: KanbanWebviewPanel, payload: {
autoBuildBranch: trimmedAutoBuildBranch, autoBuildBranch: trimmedAutoBuildBranch,
worktreeDirectory: trimmedWorktreeDir || prev.worktreeDirectory, worktreeDirectory: trimmedWorktreeDir || prev.worktreeDirectory,
worktreeOpenWith: payload.worktreeOpenWith || prev.worktreeOpenWith, worktreeOpenWith: payload.worktreeOpenWith || prev.worktreeOpenWith,
pycharmCommand: trimmedPycharmCmd || prev.pycharmCommand,
worktreePostCreateScript: trimmedPostCreate, worktreePostCreateScript: trimmedPostCreate,
worktreePreRemoveScript: trimmedPreRemove, worktreePreRemoveScript: trimmedPreRemove,
implTabPreCreateScript: trimmedImplPre, implTabPreCreateScript: trimmedImplPre,
@@ -98,6 +103,7 @@ export async function handleSettingsSave(panel: KanbanWebviewPanel, payload: {
autoBuildBranch: trimmedAutoBuildBranch, autoBuildBranch: trimmedAutoBuildBranch,
worktreeDirectory: trimmedWorktreeDir, worktreeDirectory: trimmedWorktreeDir,
worktreeOpenWith: payload.worktreeOpenWith, worktreeOpenWith: payload.worktreeOpenWith,
pycharmCommand: trimmedPycharmCmd,
worktreePostCreateScript: trimmedPostCreate, worktreePostCreateScript: trimmedPostCreate,
worktreePreRemoveScript: trimmedPreRemove, worktreePreRemoveScript: trimmedPreRemove,
implTabPreCreateScript: trimmedImplPre, implTabPreCreateScript: trimmedImplPre,
@@ -179,6 +185,7 @@ export async function handleEditSettingsRequest(panel: KanbanWebviewPanel): Prom
autoBuildBranch: s.autoBuildBranch, autoBuildBranch: s.autoBuildBranch,
worktreeDirectory: s.worktreeDirectory, worktreeDirectory: s.worktreeDirectory,
worktreeOpenWith: s.worktreeOpenWith, worktreeOpenWith: s.worktreeOpenWith,
pycharmCommand: s.pycharmCommand,
worktreePostCreateScript: s.worktreePostCreateScript, worktreePostCreateScript: s.worktreePostCreateScript,
worktreePreRemoveScript: s.worktreePreRemoveScript, worktreePreRemoveScript: s.worktreePreRemoveScript,
implTabPreCreateScript: s.implTabPreCreateScript, implTabPreCreateScript: s.implTabPreCreateScript,
+7 -4
View File
@@ -4,7 +4,7 @@ import { execFile } from 'node:child_process'
import { commands, Uri, window, workspace } from 'vscode' import { commands, Uri, window, workspace } from 'vscode'
import { getToken } from '../../auth/secrets' import { getToken } from '../../auth/secrets'
import { detectRepo } from '../../git/remote' import { detectRepo } from '../../git/remote'
import { resolveWorktreePath } from '../../git/worktree' import { expandTilde, resolveWorktreePath } from '../../git/worktree'
import { import {
runImplTabPostCloseHook, runImplTabPostCloseHook,
runImplTabPreCreateHook, runImplTabPreCreateHook,
@@ -32,13 +32,16 @@ export async function handleOpenWorktree(panel: KanbanWebviewPanel, relPath: str
// PyCharm 分支用 JetBrains 启动器打开目录。错误在回调里自行处理,不要落进 // PyCharm 分支用 JetBrains 启动器打开目录。错误在回调里自行处理,不要落进
// 下面 openFolder 的 catch。 // 下面 openFolder 的 catch。
const { worktreeOpenWith } = getSettings(panel.context) const { worktreeOpenWith, pycharmCommand } = getSettings(panel.context)
if (worktreeOpenWith === 'pycharm') { if (worktreeOpenWith === 'pycharm') {
execFile('pycharm', [abs], (err) => { // GUI 启动的 VS Code 进程 PATH 常不含 JetBrains 启动器,故命令可配置;
// 允许填 `~`/绝对路径,execFile 不走 shell`~` 需自己展开。
const cmd = expandTilde(pycharmCommand)
execFile(cmd, [abs], (err) => {
if (err) { if (err) {
const code = (err as NodeJS.ErrnoException).code ?? 'unknown' const code = (err as NodeJS.ErrnoException).code ?? 'unknown'
const message = code === 'ENOENT' const message = code === 'ENOENT'
? '找不到 pycharm 命令,请在 JetBrains Toolbox 里启用命令行启动器或把 pycharm 加入 PATH' ? `找不到 ${cmd} 命令,请在「PyCharm 启动命令」里填启动器的完整路径,或确保它在 PATH 中`
: err.message : err.message
void window.showErrorMessage(`用 PyCharm 打开失败: ${message}`) void window.showErrorMessage(`用 PyCharm 打开失败: ${message}`)
} }
+2
View File
@@ -63,6 +63,7 @@ export type ExtensionToWebview
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
@@ -119,6 +120,7 @@ export type WebviewToExtension
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
+14
View File
@@ -100,6 +100,13 @@ export interface Settings {
* `'vscode'` (default) opens a new VS Code window. * `'vscode'` (default) opens a new VS Code window.
*/ */
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
/**
* Command used to launch PyCharm when `worktreeOpenWith === 'pycharm'`.
* A bare name relies on PATH, which a GUI-launched VS Code often lacks, so
* an absolute path (or a `~`-prefixed one) is the reliable choice. Empty
* means "use the default `pycharm`".
*/
pycharmCommand: string
/** /**
* Path to a user-provided shell script that runs *after* the extension * Path to a user-provided shell script that runs *after* the extension
* creates a worktree via `git worktree add`. Empty string means use the * creates a worktree via `git worktree add`. Empty string means use the
@@ -173,6 +180,7 @@ function defaults(ctx: ExtensionContext): Settings {
autoBuildBranch: '', autoBuildBranch: '',
worktreeDirectory: '~/Sources/worktree/$project_name/$feature_name', worktreeDirectory: '~/Sources/worktree/$project_name/$feature_name',
worktreeOpenWith: 'vscode', worktreeOpenWith: 'vscode',
pycharmCommand: 'pycharm',
worktreePostCreateScript: '', worktreePostCreateScript: '',
worktreePreRemoveScript: '', worktreePreRemoveScript: '',
implTabPreCreateScript: '', implTabPreCreateScript: '',
@@ -226,6 +234,11 @@ export function getSettings(ctx: ExtensionContext): Settings {
const worktreeOpenWith = stored.worktreeOpenWith === 'pycharm' || stored.worktreeOpenWith === 'vscode' const worktreeOpenWith = stored.worktreeOpenWith === 'pycharm' || stored.worktreeOpenWith === 'vscode'
? stored.worktreeOpenWith ? stored.worktreeOpenWith
: base.worktreeOpenWith : base.worktreeOpenWith
// pycharm launcher command: blank means "use the default `pycharm`", so fall
// back to base rather than preserving an empty string.
const pycharmCommand = typeof stored.pycharmCommand === 'string' && stored.pycharmCommand.length > 0
? stored.pycharmCommand
: base.pycharmCommand
// worktree hook script paths: '' is meaningful (= "use default // worktree hook script paths: '' is meaningful (= "use default
// .spx/worktree-*.sh"), so don't coerce. Non-string legacy values fall // .spx/worktree-*.sh"), so don't coerce. Non-string legacy values fall
// back to ''. The hook runner resolves '' to the default at execution // back to ''. The hook runner resolves '' to the default at execution
@@ -266,6 +279,7 @@ export function getSettings(ctx: ExtensionContext): Settings {
autoBuildBranch, autoBuildBranch,
worktreeDirectory, worktreeDirectory,
worktreeOpenWith, worktreeOpenWith,
pycharmCommand,
worktreePostCreateScript, worktreePostCreateScript,
worktreePreRemoveScript, worktreePreRemoveScript,
implTabPreCreateScript, implTabPreCreateScript,
+1
View File
@@ -447,6 +447,7 @@ export function App() {
initialAutoBuildBranch={settings?.autoBuildBranch ?? ''} initialAutoBuildBranch={settings?.autoBuildBranch ?? ''}
initialWorktreeDirectory={settings?.worktreeDirectory ?? ''} initialWorktreeDirectory={settings?.worktreeDirectory ?? ''}
initialWorktreeOpenWith={settings?.worktreeOpenWith ?? 'vscode'} initialWorktreeOpenWith={settings?.worktreeOpenWith ?? 'vscode'}
initialPycharmCommand={settings?.pycharmCommand ?? 'pycharm'}
initialWorktreePostCreateScript={settings?.worktreePostCreateScript ?? ''} initialWorktreePostCreateScript={settings?.worktreePostCreateScript ?? ''}
initialWorktreePreRemoveScript={settings?.worktreePreRemoveScript ?? ''} initialWorktreePreRemoveScript={settings?.worktreePreRemoveScript ?? ''}
initialImplTabPreCreateScript={settings?.implTabPreCreateScript ?? ''} initialImplTabPreCreateScript={settings?.implTabPreCreateScript ?? ''}
@@ -25,6 +25,7 @@ interface SubmitValues {
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
@@ -63,6 +64,7 @@ export interface SettingsModalProps {
initialAutoBuildBranch: string initialAutoBuildBranch: string
initialWorktreeDirectory: string initialWorktreeDirectory: string
initialWorktreeOpenWith: 'vscode' | 'pycharm' initialWorktreeOpenWith: 'vscode' | 'pycharm'
initialPycharmCommand: string
initialWorktreePostCreateScript: string initialWorktreePostCreateScript: string
initialWorktreePreRemoveScript: string initialWorktreePreRemoveScript: string
initialImplTabPreCreateScript: string initialImplTabPreCreateScript: string
@@ -134,6 +136,7 @@ export function SettingsModal({
initialAutoBuildBranch, initialAutoBuildBranch,
initialWorktreeDirectory, initialWorktreeDirectory,
initialWorktreeOpenWith, initialWorktreeOpenWith,
initialPycharmCommand,
initialWorktreePostCreateScript, initialWorktreePostCreateScript,
initialWorktreePreRemoveScript, initialWorktreePreRemoveScript,
initialImplTabPreCreateScript, initialImplTabPreCreateScript,
@@ -159,6 +162,7 @@ export function SettingsModal({
const [autoBuildBranch, setAutoBuildBranch] = useState(initialAutoBuildBranch) const [autoBuildBranch, setAutoBuildBranch] = useState(initialAutoBuildBranch)
const [worktreeDirectory, setWorktreeDirectory] = useState(initialWorktreeDirectory) const [worktreeDirectory, setWorktreeDirectory] = useState(initialWorktreeDirectory)
const [worktreeOpenWith, setWorktreeOpenWith] = useState(initialWorktreeOpenWith) const [worktreeOpenWith, setWorktreeOpenWith] = useState(initialWorktreeOpenWith)
const [pycharmCommand, setPycharmCommand] = useState(initialPycharmCommand)
const [worktreePostCreateScript, setWorktreePostCreateScript] = useState(initialWorktreePostCreateScript) const [worktreePostCreateScript, setWorktreePostCreateScript] = useState(initialWorktreePostCreateScript)
const [worktreePreRemoveScript, setWorktreePreRemoveScript] = useState(initialWorktreePreRemoveScript) const [worktreePreRemoveScript, setWorktreePreRemoveScript] = useState(initialWorktreePreRemoveScript)
const [implTabPreCreateScript, setImplTabPreCreateScript] = useState(initialImplTabPreCreateScript) const [implTabPreCreateScript, setImplTabPreCreateScript] = useState(initialImplTabPreCreateScript)
@@ -263,6 +267,8 @@ export function SettingsModal({
// worktree 目录模板:trim 后留空 = 用默认模板(getSettings 读时回退) // worktree 目录模板:trim 后留空 = 用默认模板(getSettings 读时回退)
worktreeDirectory: worktreeDirectory.trim(), worktreeDirectory: worktreeDirectory.trim(),
worktreeOpenWith, worktreeOpenWith,
// pycharm 启动命令:trim 后留空 = 用默认 `pycharm`getSettings 读时回退)
pycharmCommand: pycharmCommand.trim(),
// 钩子路径保持原样 trim(前后空格无意义;空串 = 用默认 .spx/*.sh // 钩子路径保持原样 trim(前后空格无意义;空串 = 用默认 .spx/*.sh
worktreePostCreateScript: worktreePostCreateScript.trim(), worktreePostCreateScript: worktreePostCreateScript.trim(),
worktreePreRemoveScript: worktreePreRemoveScript.trim(), worktreePreRemoveScript: worktreePreRemoveScript.trim(),
@@ -669,6 +675,35 @@ export function SettingsModal({
</select> </select>
</Field> </Field>
{worktreeOpenWith === 'pycharm' && (
<Field
label="PyCharm 启动命令"
hint={(
<>
{' '}
<code>~</code>
{' '}
{' '}
<code>~/.local/share/JetBrains/Toolbox/scripts/pycharm</code>
GUI VS Code
{' '}
<code>pycharm</code>
</>
)}
>
<input
type="text"
value={pycharmCommand}
onChange={e => setPycharmCommand(e.target.value)}
placeholder="pycharm"
className={inputClass}
/>
</Field>
)}
<Field <Field
label="worktree 创建后脚本" label="worktree 创建后脚本"
hint={( hint={(
+4
View File
@@ -35,6 +35,7 @@ export interface SettingsValues {
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
@@ -59,6 +60,7 @@ export interface SettingsOverlayState {
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
@@ -271,6 +273,7 @@ export function useIssues(): UseIssuesResult {
autoBuildBranch: values.autoBuildBranch, autoBuildBranch: values.autoBuildBranch,
worktreeDirectory: values.worktreeDirectory, worktreeDirectory: values.worktreeDirectory,
worktreeOpenWith: values.worktreeOpenWith, worktreeOpenWith: values.worktreeOpenWith,
pycharmCommand: values.pycharmCommand,
worktreePostCreateScript: values.worktreePostCreateScript, worktreePostCreateScript: values.worktreePostCreateScript,
worktreePreRemoveScript: values.worktreePreRemoveScript, worktreePreRemoveScript: values.worktreePreRemoveScript,
implTabPreCreateScript: values.implTabPreCreateScript, implTabPreCreateScript: values.implTabPreCreateScript,
@@ -598,6 +601,7 @@ export function useIssues(): UseIssuesResult {
autoBuildBranch: msg.autoBuildBranch, autoBuildBranch: msg.autoBuildBranch,
worktreeDirectory: msg.worktreeDirectory, worktreeDirectory: msg.worktreeDirectory,
worktreeOpenWith: msg.worktreeOpenWith, worktreeOpenWith: msg.worktreeOpenWith,
pycharmCommand: msg.pycharmCommand,
worktreePostCreateScript: msg.worktreePostCreateScript, worktreePostCreateScript: msg.worktreePostCreateScript,
worktreePreRemoveScript: msg.worktreePreRemoveScript, worktreePreRemoveScript: msg.worktreePreRemoveScript,
implTabPreCreateScript: msg.implTabPreCreateScript, implTabPreCreateScript: msg.implTabPreCreateScript,
+2
View File
@@ -78,6 +78,7 @@ export type ExtensionToWebview
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string
@@ -134,6 +135,7 @@ export type WebviewToExtension
autoBuildBranch: string autoBuildBranch: string
worktreeDirectory: string worktreeDirectory: string
worktreeOpenWith: 'vscode' | 'pycharm' worktreeOpenWith: 'vscode' | 'pycharm'
pycharmCommand: string
worktreePostCreateScript: string worktreePostCreateScript: string
worktreePreRemoveScript: string worktreePreRemoveScript: string
implTabPreCreateScript: string implTabPreCreateScript: string