返回技能列表

lsp-format-code

blackwell-systems
更新于 5 days ago
1 次查看
53
2
53
在 GitHub 上查看
general

关于

This skill formats code files or selections using your language server's formatter, applying consistent style like gofmt or Prettier. It's ideal for cleaning up generated code or preparing files for commit, supporting both full-file and range-based formatting. It requires the agent-lsp MCP server to interface with your editor's formatting engine.

快速安装

Claude Code

推荐
主要方式
npx skills add blackwell-systems/agent-lsp -a claude-code
插件命令备选方式
/plugin add https://github.com/blackwell-systems/agent-lsp
Git 克隆备选方式
git clone https://github.com/blackwell-systems/agent-lsp.git ~/.claude/skills/lsp-format-code

在 Claude Code 中复制并粘贴此命令以安装该技能

技能文档

Requires the agent-lsp MCP server.

lsp-format-code

Format a file or selection using the language server's formatter — the same formatting engine your IDE uses. Applies language-specific rules (gofmt, prettier, rustfmt, black) without requiring those tools to be on PATH separately.

When to use

  • Before committing: ensure consistent style across edited files
  • After generating code: clean up AI-generated indentation and spacing
  • After a refactor that shifted indentation levels
  • When a linter flags style violations fixable by the formatter

Use /lsp-safe-edit instead when you are making a logic change and want before/after diagnostic comparison alongside the edit.


Workflow

Step 1 — Check formatting is supported (optional)

If unsure whether the language server supports formatting for this file, check capabilities first:

mcp__lsp__get_server_capabilities({ "file_path": "<file>" })

Look for documentFormattingProvider (full-file) and documentRangeFormattingProvider (range). If neither is present, the server does not support formatting — stop and report.

Skip this step if you know the language supports formatting (Go, TypeScript, Rust, Python all do via their standard servers).

Step 2 — Open the file

mcp__lsp__open_document({ "file_path": "/abs/path/to/file.go", "language_id": "go" })

Step 3 — Request formatting edits

Full file:

mcp__lsp__format_document({ "file_path": "/abs/path/to/file.go" })

Selection only:

mcp__lsp__format_range({
  "file_path": "/abs/path/to/file.go",
  "start_line": <N>,
  "end_line": <M>
})

Both return TextEdit[] — a list of replacements to apply. They do not write to disk. If the list is empty, the file is already correctly formatted.

Step 4 — Apply the edits

Pass the TextEdit[] from Step 3 to apply_edit:

mcp__lsp__apply_edit({ "workspace_edit": <TextEdit[] from Step 3> })

This writes the formatting changes to disk.

Step 5 — Verify (optional but recommended)

Call get_diagnostics to confirm formatting did not introduce any errors:

mcp__lsp__get_diagnostics({ "file_path": "/abs/path/to/file.go" })

Formatting should never introduce errors — if it does, report immediately without committing.


Output format

## Format result: <filename>

Changes applied: N edits
Lines affected: <range or "whole file">
Formatter: <gopls | typescript-language-server | rust-analyzer | ...>

Status: FORMATTED ✓

If no edits were returned:

Status: ALREADY FORMATTED — no changes needed

If formatting is not supported:

Status: NOT SUPPORTED — <server> does not expose documentFormattingProvider
Fallback: run the formatter directly (gofmt, prettier, rustfmt, etc.)

Multi-file formatting

For formatting multiple files (e.g. all files changed in a PR):

  1. Call format_document for each file — these can run in parallel.
  2. Collect all TextEdit[] responses.
  3. Apply each file's edits via apply_edit sequentially.
  4. Report total edits across all files.

Do not apply edits from multiple files in a single apply_edit call — apply per-file to keep changes scoped and reversible.


Decision guide

SituationAction
Formatting a whole file before commitformat_documentapply_edit
Formatting only generated code in a functionformat_range with the function's line range
Empty TextEdit[] returnedFile is already formatted — no action needed
Server doesn't support formattingReport and suggest running CLI formatter directly
Formatting introduces diagnosticsDo not commit — report immediately
Formatting a Go file in a workspace repoEnsure GOWORK=off is set if running via shell fallback

Language notes

LanguageFormatterServer
Gogofmt (via gopls)gopls
TypeScript / JavaScriptprettier or built-in (via typescript-language-server)typescript-language-server
Rustrustfmt (via rust-analyzer)rust-analyzer
Pythonblack or autopep8 (via pyright/pylsp)pyright-langserver or pylsp
C / C++clang-format (via clangd)clangd

The language server delegates to the language's standard formatter — results match what your IDE would produce.

GitHub 仓库

blackwell-systems/agent-lsp
路径: skills/lsp-format-code
0
agentskillsai-agentsai-toolingclaudeclaude-codecode-intelligence

相关推荐技能

content-collections

Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。

查看技能

polymarket

这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。

查看技能

creating-opencode-plugins

该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。

查看技能

sglang

SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。

查看技能