A plugin that allows applying code formatting with minimum configuration.
Includes support for running the following formatters:
- Ruby’s RuboCop
- Ruby’s rubyfmt
- ESLint
- Prettier
- Golang’s gofmt
- Golang’s goimports
- Python’s autopep8
- Python’s yapf
- PHP Code Standards Fixer
- Rust’s rustfmt
- SVGO
- Flutter
- Dart’s dartfmt
- Crystal
- Follow the instructions from https://sublime.fnando.com.
- Open the command pallete, run “Package Control: Install Package“, then search for “Codefmt“.
Clone this repository into the Sublime Text “Packages” directory, which is located where ever the “Preferences” -> “Browse Packages” option in sublime takes you.
By default, Codefmt is ran whenever you save a supported code file (i.e. a
formatter is configured and enabled). You can disable this behaviour by changing
the settings under “Sublime Text -> Preferences -> Package Settings -> Codefmt
-> Settings” or by using the command palette (super+shift+p
) and searching for
“Codefmt: Settings”.
You can also trigger commands using the command pallete by searching for “Code Formatter: Format File”. You can add a custom shortcut by using the following command:
[{ "keys": ["super+k", "super+f"], "command": "format_code_file" }]
When auto saving is disabled, you can set up alternative keybindings so you can
use super+s
to save and format the current file, and another shortcut to
bypass auto formatting. The following keybindings show how to do that for macOS:
[
{
"keys": ["super+s"],
"command": "format_code_file"
},
{
"keys": ["ctrl+s"],
"command": "save",
"args": { "async": true }
}
]
You add new formatters or override the settings for an existing one. All you
have to do is a new key that identifiers the formatter to formatters
, and the
related configuration using the same key. So, let’s say you want add a formatter
called txtfmt
. The user configuration file could be something like this:
{
overrides: {
txtfmt: {
// The command that will be executed for format files.
// The special variables are:
//
// - `$config`: the full path to the configuration file we found.
// - `$file`: the full path to the file that’s being formatted.
command: ["txtfmt", "--fix", "--config", "$config", "$file"],
// The scopes that will be considered when formatting
scopes: ["text.plain"],
// Additional flags when running in debug mode.
// If the formatter doesn’t have a debug mode, you may set this to an empty
// array.
debug: ["--debug"],
// When no custom config file exists, use a default config file.
default_config: null,
// Config files that will be looked up on the root of the project.
// If the formatter doesn’t require a config file (or is automatically
// inferred by the formatter), you may want to set this to an empty array.
config_files: ["txtfmt.config.json"],
},
},
formatters: ["gofmt", "rubocop", "prettier", "eslint", "txtfmt"],
}
Note
When overriding existing commands, you only need to define the keys that are changing; there's no need to add all options.
If you use version managers like asdf, you may need to set the command to the full path. The following example shows how to override RuboCop's command to use the shim:
{
"overrides": {
"rubocop": {
"command": [
"/Users/fnando/.asdf/shims/rubocop",
"--auto-correct-all",
"--config",
"$config",
"$file"
]
}
}
}
Note
Commands are always executed from the root directory of your project.
Some commands may require additional environment variables, like ASDF's golang
plugin and the ASDF_GOLANG_MOD_VERSION_ENABLED
environment variable. You can
use the setting env
to specify that.
You can override any setting per project. Here's an .sublime-project
example:
{
"folders": [
{
"path": "/home/fnando/example"
}
],
"settings": {
"Codefmt": {
"env": {
"ASDF_GOLANG_MOD_VERSION_ENABLED": "true"
},
"debug": true,
"formatters": ["gofmt"]
}
}
}
Copyright (c) 2020 Nando Vieira
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.