Code Monkey home page Code Monkey logo

sublime-codefmt's Introduction

Code Formatter for Sublime Text

A plugin that allows applying code formatting with minimum configuration.

Includes support for running the following formatters:

Installation

Setup Package Control Repository

  1. Follow the instructions from https://sublime.fnando.com.
  2. Open the command pallete, run “Package Control: Install Package“, then search for “Codefmt“.

Git Clone

Clone this repository into the Sublime Text “Packages” directory, which is located where ever the “Preferences” -> “Browse Packages” option in sublime takes you.

Usage

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 }
  }
]

Add new formatters

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.

Using version managers

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.

Using .sublime-project files

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"]
    }
  }
}

License

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.

sublime-codefmt's People

Contributors

fnando avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sublime-codefmt's Issues

Installing on Sublime Text 4 erroring

When trying to install your awesome looking plugin on Sublime Text build 4113 (latest) I get the following error in the console:

reloading python 3.3 plugin Codefmt.utils
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 308, in reload_plugin
    m = importlib.import_module(modulename)
  File "./python3.3/importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python33/sublime_plugin.py", line 1692, in load_module
    exec(compile(source, source_path, 'exec'), mod.__dict__)
  File "/Users/andrew/Library/Application Support/Sublime Text 3/Installed Packages/Codefmt.sublime-package/utils.py", line 4, in <module>
ImportError: No module named 'pathlib'
Unable to parse binding {command: format_code_file, keys: []}
Unknown key not sure
Unable to parse binding {command: yaml_path, keys: [not sure]}

Unable to format with Rubocop

When I use the keyboard shortcut, I get the following error in my console:

Unable to open /Users/andrew/Library/Application Support/Sublime Text 3/Packages/Codefmt/Codefmt.sublime-settings
git: tracking working dir /Users/andrew/Library/Application Support/Sublime Text 3/Packages/User
Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python38/sublime_plugin.py", line 1518, in run_
    return self.run(edit)
  File "/Users/andrew/Library/Application Support/Sublime Text 3/Installed Packages/Codefmt.sublime-package/FormatCodeFile.py", line 7, in run
  File "/Users/andrew/Library/Application Support/Sublime Text 3/Installed Packages/Codefmt.sublime-package/utils.py", line 37, in format_code_file
  File "/Users/andrew/Library/Application Support/Sublime Text 3/Installed Packages/Codefmt.sublime-package/utils.py", line 122, in find_matching_formatters
TypeError: 'NoneType' object is not iterable

My config looks like this:

{
  "format_on_save": true,
  "debug": true,
  "rubocop": {
    // The command that will be executed for format files.
    "command": [
      "/Users/andrew/.asdf/shims/rubocop",
      "--auto-correct-all",
      "--config",
      "$config",
      "$file"
    ],

    // The scopes that will be considered when formatting
    "scopes": ["source.ruby", "source.ruby.rails"],

    // Additional flags when running in debug mode.
    "debug": ["--debug", "--extra-details"],

    // 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.
    "config_files": [".rubocop.yml"]
  },
  "formatters": [
    "rubocop",
  ]
}

However, I've tried running the code with my settings looking like: {}

I believe this issue is actually that it can't find the /Users/andrew/Library/Application Support/Sublime Text 3/Packages/Codefmt/Codefmt.sublime-settings. As the Codefmt is not there for me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.