lsp-safe-edit
关于
This skill safely applies code edits by first previewing changes and comparing LSP diagnostics before writing to disk. It only commits edits if they don't introduce new errors, and surfaces quick fixes when issues arise. Developers should use it for risk-free refactoring and multi-file modifications with built-in validation.
快速安装
Claude Code
推荐npx skills add blackwell-systems/agent-lsp -a claude-code/plugin add https://github.com/blackwell-systems/agent-lspgit clone https://github.com/blackwell-systems/agent-lsp.git ~/.claude/skills/lsp-safe-edit在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Requires the agent-lsp MCP server.
lsp-safe-edit
Wrap any code edit with a before/after diagnostic comparison. Speculatively previews the change in-memory before touching disk, then diffs errors introduced vs. resolved after applying. If errors appear, surfaces code actions to fix them.
Prerequisites
LSP must be running for the target workspace. If not yet initialized, call
mcp__lsp__start_lsp with the workspace root before proceeding.
Auto-init note: agent-lsp supports workspace auto-inference from file paths.
Explicit start_lsp is only needed when switching workspace roots.
Input
- target file(s): One or more files to be edited (absolute paths).
- description of change: What you intend to edit and why.
Workflow
Step 1 — Open target file(s)
Call mcp__lsp__open_document for each file that will be edited:
mcp__lsp__open_document(file_path: "/abs/path/to/file.go", language_id: "go")
Step 2 — Capture baseline diagnostics (BEFORE)
Call mcp__lsp__get_diagnostics for each target file. Store as BEFORE.
For multi-file edits, collect diagnostics for all files involved.
BEFORE = mcp__lsp__get_diagnostics(file_path: "/abs/path/to/file.go")
If the server returns an empty list immediately after open, wait briefly and retry — LSP analysis is async.
Step 3 — Speculative preview (preview_edit)
Before touching disk, call mcp__lsp__preview_edit to preview the
error delta of the intended change:
mcp__lsp__preview_edit(
file_path: "/abs/path/to/file.go",
start_line: <N>,
start_column: <col>,
end_line: <N>,
end_column: <col>,
new_text: "<replacement text>"
)
Returns net_delta (new errors introduced minus errors resolved) without
writing to disk.
Decision:
net_delta | Action |
|---|---|
| ≤ 0 | Proceed — edit improves or does not worsen error state |
| > 0 | Pause. Report introduced errors to user and ask: "Proceed anyway? [y/n]" |
If net_delta > 0 and user says "n", stop. Do not apply the edit.
Multi-file edits: preview_edit covers one file at a time. For
edits spanning multiple files, run it for each file independently and sum the
deltas. If any file shows net_delta > 0, pause before continuing.
When to skip Step 3: If the intended change is a new file (Write), there is no existing file to simulate against. Skip to Step 4.
Step 3b — Refactor preview with simulate_chain (renames and signature changes)
Use this step instead of or after Step 3 when the change is a rename, signature change, or any edit with dependent follow-on edits (e.g., updating all call sites after adding a parameter).
simulate_chain applies a sequence of speculative edits in-memory and reports
whether the cumulative change is safe — without touching disk:
mcp__lsp__simulate_chain({
"workspace_root": "/abs/path/to/workspace",
"language": "go",
"edits": [
{
"file_path": "/abs/path/to/file.go",
"start_line": <N>, "start_column": <col>,
"end_line": <N>, "end_column": <col>,
"new_text": "<replacement>"
},
// additional dependent edits (e.g. call site updates) ...
]
})
Returns:
cumulative_delta— net error change across all stepssafe_to_apply_through_step— how many steps are safe to apply in sequence
Decision:
cumulative_delta | safe_to_apply_through_step | Action |
|---|---|---|
| ≤ 0 | = total steps | All steps safe. Proceed to Step 4. |
| ≤ 0 | < total steps | Safe up to that step. Review remaining steps. |
| > 0 | any | Net regression. Report to user before proceeding. |
When to use Step 3b:
- Renaming an exported symbol and updating its call sites
- Adding/removing a parameter and updating all callers
- Any multi-file refactor where edits are order-dependent
When to skip Step 3b:
- Simple in-place edits with no dependent follow-on edits (Step 3 is sufficient)
- New file creation (no existing text to simulate against)
Step 4 — Apply the edit to disk
Apply the change using the Edit or Write tool:
- Use
Editfor targeted replacements in an existing file. - Use
Writeonly when creating a new file or doing a full rewrite.
Edit(file_path: "/abs/path/to/file.go", old_string: "...", new_string: "...")
For multi-file edits, apply each file's changes before collecting post-edit diagnostics (Step 5). If any individual edit fails, stop and report before applying remaining files.
Step 5 — Capture post-edit diagnostics (AFTER)
Call mcp__lsp__get_diagnostics again for each edited file. Store as AFTER.
AFTER = mcp__lsp__get_diagnostics(file_path: "/abs/path/to/file.go")
For multi-file edits, collect diagnostics for all files and merge the results.
Step 6 — Compute the diagnostic diff
Compare BEFORE and AFTER:
- Introduced = diagnostics in AFTER not in BEFORE (new problems).
- Resolved = diagnostics in BEFORE not in AFTER (fixed problems).
Match by (file, line, message) tuple to handle line-number shifts. Treat
error and warning severity separately.
Step 7 — Surface code actions if errors were introduced
If any new error-severity diagnostics appear, call mcp__lsp__suggest_fixes
at each error location to surface quick fixes:
mcp__lsp__suggest_fixes(
file_path: "<file>",
start_line: <error line>,
start_column: 1,
end_line: <error line>,
end_column: 999
)
Report available code actions to the user:
Errors introduced (2):
file.go:34 — undefined: MyType
→ Code action: Import "mypackage" (quickfix)
file.go:51 — cannot use int as string
→ No code actions available
Apply code actions? [y/n/select]
If the user accepts, apply the code action's WorkspaceEdit via
mcp__lsp__apply_edit, then re-collect diagnostics and re-diff.
Step 8 — Format (optional)
If the diagnostic diff is clean (net change ≤ 0), offer to format the edited file via the language server:
mcp__lsp__format_document({ "file_path": "/abs/path/to/file" })
Returns TextEdit[]. If non-empty, apply immediately:
mcp__lsp__apply_edit({ "workspace_edit": <TextEdit[]> })
Skip if the user did not ask for formatting, or if there are unresolved errors (fix errors before formatting).
Step 9 — Report using DiagnosticDiffFormat
Output the final summary:
## Edit Summary
Files changed: N
Errors introduced: A → Errors resolved: B (net: A-B)
Warnings introduced: C → Warnings resolved: D
### Introduced errors
- file.go:34 — undefined: MyType
### Resolved errors
- file.go:12 — unused variable: x
Decision Guide
| Net change | Action |
|---|---|
| 0 | Safe. No new errors. |
| Negative | Net improvement — errors resolved. Safe. |
| Positive (after code actions) | Do NOT commit. Offer to revert. |
When net change > 0 after code actions:
- Show the full list of remaining introduced errors.
- Offer to revert using the original
old_stringin a follow-upEditcall. - Wait for user decision before proceeding.
Do not commit or stage files when net change > 0.
Multi-file workflow
For edits spanning multiple files (e.g., changing a function signature and all its call sites):
- Open all files in Step 1.
- Collect BEFORE diagnostics for all files.
- Simulate each file independently in Step 3 — sum
net_deltavalues. - Apply edits file by file in Step 4 — stop on first failure.
- Collect AFTER diagnostics for all files and merge.
- Check code actions on any file showing new errors.
Report the combined diagnostic diff across all files in the final summary.
GitHub 仓库
相关推荐技能
executing-plans
设计该Skill用于当开发者提供完整实施计划时,以受控批次方式执行代码实现。它会先审阅计划并提出疑问,然后分批次执行任务(默认每批3个任务),并在批次间暂停等待审查。关键特性包括分批次执行、内置检查点和架构师审查机制,确保复杂系统实现的可控性。
requesting-code-review
设计该Skill可在完成任务、实现主要功能或合并代码前自动调度代码审查子代理,确保实现符合需求和计划。它支持通过指定git SHA范围进行精准的代码变更审查,帮助开发者在关键节点及时发现潜在问题。核心原则是"早审查、勤审查",适用于开发流程的各个关键阶段。
connect-mcp-server
设计这个Skill指导开发者如何将MCP服务器连接到Claude Code,支持HTTP、stdio和SSE三种传输协议。它涵盖了从安装配置到认证安全的完整流程,适用于集成GitHub、Notion、数据库等外部服务。当开发者需要添加集成、配置外部工具或提及MCP相关功能时,这个Skill能提供实用的操作指南。
web-cli-teleport
设计该Skill帮助开发者根据任务特性选择Claude Code的Web或CLI界面,并指导如何在两种环境间无缝迁移会话。它能分析任务复杂度、迭代需求等要素,推荐最优工作界面和工作流。关键特性包括会话状态管理、环境切换指导和上下文优化建议。
