Skip to content

Instantly share code, notes, and snippets.

@caub
Last active April 15, 2021 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caub/bc4ab4a7dc2978837fbc3fcc8a823504 to your computer and use it in GitHub Desktop.
Save caub/bc4ab4a7dc2978837fbc3fcc8a823504 to your computer and use it in GitHub Desktop.
VSCode's Format on save for SublimeText

requirement: TypeScript package should be installed

Step 1: Create a new plugin (Tools > Developer > new plugin) with the following, save it as format-on-save.py for example:

import sublime
import sublime_plugin

class FormatTypescriptOnSave(sublime_plugin.EventListener):
  def on_pre_save(self, view): 
    if "TypeScript" in view.settings().get("syntax") or "JavaScript" in view.settings().get("syntax"):
      view.run_command("typescript_format_document")

source: https://stackoverflow.com/a/42237769/3183756

Step 2: Customize additional formatting settings in ~/.config/sublime-text-3/Packages/TypeScript/typescript/libs/editor_client.py, here I added "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": False

    def set_features(self):
        host_info = "Sublime Text version " + str(sublime.version())
        # Preferences Settings
        format_options = {
            "tabSize": self.tab_size,
            "indentSize": self.indent_size,
            "convertTabsToSpaces": self.translate_tab_to_spaces,
            "insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": False,
            "insertSpaceAfterFunctionKeywordForAnonymousFunctions": True, # False by default on SublimeText TypeScript
        }
        self.service.configure(host_info, None, format_options)

source: microsoft/TypeScript-Sublime-Plugin#321 (comment)

create a .vscode/settings.json file (or edit your main settings ctrl+, or ctrl+shift+p Open settings (JSON)) with:

{
    "javascript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces": false,
    "editor.formatOnSave": true, // of course
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment