@0x706b/coc-prettier 中文文档教程
Prettier formatter for coc.nvim
使用 Prettier 格式化 JavaScript / TypeScript / CSS / JSON 的 Coc 扩展。
prettier-vscode 的一个fork
Installation
运行vim 命令:
:CocInstall coc-prettier
Usage
Setup Prettier
command in your init.vim
or .vimrc
command! -nargs=0 Prettier :CocCommand prettier.formatFile
然后你可以使用:Prettier
来格式化current缓冲。
Remap keys for range format in your init.vim
or .vimrc
vmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)
然后你可以
进行范围格式。
Prettier range 格式只支持 languageId 包括:javascript
, javascriptreact
、typescript
、typescriptreact
、json
和 graphql
。
Update your coc-settings.json
for format on save.
使用以下方式打开设置文件:
:CocConfig
添加:
"coc.preferences.formatOnSaveFiletypes": ["css", "markdown"],
设置要在保存时设置格式的语言。
注意: prettier extension 有较低的优先级,如果文档有其他 注册文档格式提供者,prettier 将无法工作。
要检查格式是否由 prettier 完成,请在之后检查输出消息 格式,其中应包含 Formatted by prettier
。
要使格式更漂亮,请使用命令 :CocCommand prettier.formatFile
而不格式化,请使用 :noa w
Settings
Prettier's Settings
保存
- Prettier configuration file
.editorconfig
要 不存在更漂亮的配置文件
.editorconfig
- Coc prettier's settings (described below with their default)
prettier.printWidth (default: 80)
在此行限制内调整代码
prettier.tabWidth (default: 2)
每个选项卡应使用的空格数
prettier.singleQuote (default: false)
如果为真,将使用单引号而不是双引号
prettier.trailingComma (default: 'none')
尽可能控制尾随逗号的打印。 有效选项:
- "none" - No trailing commas
- "es5" - Trailing commas where valid in ES5 (objects, arrays, etc)
- "all" - Trailing commas wherever possible (function arguments)
prettier.bracketSpacing (default: true)
控制对象文字内空格的打印
prettier.jsxBracketSameLine (default: false)
如果为真,则将多行 jsx 元素的 >
放在最后一行的末尾,而不是单独放在下一
prettier.parser (default: 'babylon') - JavaScript only
行使用。 有效选项是“flow”和“babylon”。
prettier.semi (default: true)
是否在每行末尾添加分号(semi:true), 或仅在可能引入 ASI 故障的行的开头(半:假)
prettier.useTabs (default: false)
如果为真,则使用制表符(Markdown)缩进行
prettier.proseWrap (default: 'preserve')
将散文包装在多行上。
prettier.arrowParens (default: 'avoid')
在单独的箭头函数参数周围包含括号
Coc specific settings
这些设置特定于 Coc,需要在 Coc 设置文件中进行设置。 请参阅文档了解如何执行此操作。
prettier.formatterPriority (default: 1) - priority of formatter
如果您不希望 prettier 的优先级高于 其他语言服务器提供的格式化程序。
prettier.eslintIntegration (default: false) - JavaScript and TypeScript only
使用 prettier-eslint 而不是 prettier。 其他设置只会在无法从 ESLint 规则中推断出来的情况下作为回退。
prettier.tslintIntegration (default: false) - JavaScript and TypeScript only
使用 prettier-tslint 而不是 prettier。 其他设置只会在无法从 TSLint 规则中推断出来的情况下作为回退。
prettier.stylelintIntegration (default: false) - CSS, SCSS and LESS only
使用 prettier-stylelint 而不是 prettier。 其他设置只会在无法从 stylelint 规则推断出来的情况下作为回退。
prettier.requireConfig (default: false)
需要 'prettierconfig' 来格式化
prettier.ignorePath (default: .prettierignore)
提供忽略文件的路径,例如 .gitignore
或 .prettierignore
。 匹配的文件将不会被格式化。 设置为 null
不读取忽略文件。 需要重新启动。
prettier.disableLanguages (default: ["vue"])
禁用此扩展的语言 ID 列表。 需要重新启动。 注意:禁用在父文件夹中启用的语言将阻止格式化,而不是让任何其他格式化程序运行
prettier.statusItemText (default: "Prettier")
指示当前缓冲区的状态项文本可以由 prettier 格式化。
prettier.onlyUseLocalVersion (default: false)
仅使用客户端安装的 prettier 版本,忽略与 coc-prettier 捆绑的版本
prettier.disableSuccessMessage (default: false)
禁用每次成功格式化文件时都会回显的 'Formatted by prettier' 消息
Prettier resolution
此扩展将使用项目本地依赖项中的 prettier。 如果 prettier 不与项目的依赖项一起安装在本地,则副本将与扩展捆绑在一起。
ESLint and TSLint Integration
prettier-eslint
和 prettier-tslint
包含在这个扩展的安装中。 无需为功能单独安装本地或全局安装。
eslint
、tslint
以及您的特定配置所需的所有对等依赖项都必须在本地安装。 全局安装将不会被识别。
Contribute
随意打开问题或 PRs!
Prettier formatter for coc.nvim
Coc extension to format your JavaScript / TypeScript / CSS / JSON using Prettier.
A fork of prettier-vscode
Installation
Run vim command:
:CocInstall coc-prettier
Usage
Setup Prettier
command in your init.vim
or .vimrc
command! -nargs=0 Prettier :CocCommand prettier.formatFile
Then you can use :Prettier
to format current buffer.
Remap keys for range format in your init.vim
or .vimrc
vmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)
Then you can <leader>f
for range format.
Prettier range format only support languageId including: javascript
, javascriptreact
, typescript
, typescriptreact
, json
and graphql
.
Update your coc-settings.json
for format on save.
Open settings file with:
:CocConfig
Add:
"coc.preferences.formatOnSaveFiletypes": ["css", "markdown"],
to setup the languages which you want to format on save.
Note: prettier extension have lower priority, if the document have other registered document format provider, prettier would not work.
To check the format is done by prettier, check out the output message after format, which should contains Formatted by prettier
.
To make prettier do the format, use command :CocCommand prettier.formatFile
To save without formatting, use :noa w
Settings
Prettier's Settings
Settings will be read from (listed by priority):
- Prettier configuration file
.editorconfig
Or if no prettier configuration file exist
.editorconfig
- Coc prettier's settings (described below with their default)
prettier.printWidth (default: 80)
Fit code within this line limit
prettier.tabWidth (default: 2)
Number of spaces it should use per tab
prettier.singleQuote (default: false)
If true, will use single instead of double quotes
prettier.trailingComma (default: 'none')
Controls the printing of trailing commas wherever possible. Valid options:
- "none" - No trailing commas
- "es5" - Trailing commas where valid in ES5 (objects, arrays, etc)
- "all" - Trailing commas wherever possible (function arguments)
prettier.bracketSpacing (default: true)
Controls the printing of spaces inside object literals
prettier.jsxBracketSameLine (default: false)
If true, puts the >
of a multi-line jsx element at the end of the last line instead of being alone on the next line
prettier.parser (default: 'babylon') - JavaScript only
Which parser to use. Valid options are 'flow' and 'babylon'.
prettier.semi (default: true)
Whether to add a semicolon at the end of every line (semi: true), or only at the beginning of lines that may introduce ASI failures (semi: false)
prettier.useTabs (default: false)
If true, indent lines with tabs
prettier.proseWrap (default: 'preserve')
(Markdown) wrap prose over multiple lines.
prettier.arrowParens (default: 'avoid')
Include parentheses around a sole arrow function parameter
Coc specific settings
These settings are specific to Coc and need to be set in the Coc settings file. See the documentation for how to do that.
prettier.formatterPriority (default: 1) - priority of formatter
Change it to -1
if you don't want prettier to have higher priority than formatter provided by other language server.
prettier.eslintIntegration (default: false) - JavaScript and TypeScript only
Use prettier-eslint instead of prettier. Other settings will only be fallbacks in case they could not be inferred from ESLint rules.
prettier.tslintIntegration (default: false) - JavaScript and TypeScript only
Use prettier-tslint instead of prettier. Other settings will only be fallbacks in case they could not be inferred from TSLint rules.
prettier.stylelintIntegration (default: false) - CSS, SCSS and LESS only
Use prettier-stylelint instead of prettier. Other settings will only be fallbacks in case they could not be inferred from stylelint rules.
prettier.requireConfig (default: false)
Require a 'prettierconfig' to format
prettier.ignorePath (default: .prettierignore)
Supply the path to an ignore file such as .gitignore
or .prettierignore
. Files which match will not be formatted. Set to null
to not read ignore files. Restart required.
prettier.disableLanguages (default: ["vue"])
A list of languages IDs to disable this extension on. Restart required. Note: Disabling a language enabled in a parent folder will prevent formatting instead of letting any other formatter to run
prettier.statusItemText (default: "Prettier")
Text of status item indicating current buffer can be formatted by prettier.
prettier.onlyUseLocalVersion (default: false)
Only use the version of prettier installed by the client, ignoring the version bundled with coc-prettier
prettier.disableSuccessMessage (default: false)
Disable the 'Formatted by prettier' message which is echoed every time a file is successfully formatted
Prettier resolution
This extension will use prettier from your project's local dependencies. Should prettier not be installed locally with your project's dependencies, a copy will be bundled with the extension.
ESLint and TSLint Integration
prettier-eslint
and prettier-tslint
are included with the installation of this extension. There is no need for a separate local or global install of either for functionality.
eslint
, tslint
, and all peer dependencies required by your specific configuration must be installed locally. Global installations will not be recognized.
Contribute
Feel free to open issues or PRs!