Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SublimeText Typescript Format on Save

Using Microsoft's Typescript plugin for sublime, I can format a file using ^T ^F as mentioned in features.

Is they a way to run this command automatically when saving a file? I'm after something akin to Sublime-HTMLPrettify Beautify on Save feature.

Thanks.

like image 913
robstarbuck Avatar asked Feb 14 '17 18:02

robstarbuck


People also ask

What is typescript Sublime Text plugin?

TypeScript Plugin for Sublime Text The plugin uses an IO wrapper around the TypeScript language services to provide an enhanced Sublime Text experience when working with TypeScript code.

What's new in Sublime Text 4?

The Sublime Text API has been updated to Python 3.8, while keeping backwards compatibility with packages built for Sublime Text 3. The API has been significantly expanded, adding features that allow plugins like LSP to work better than ever. Read the revamped documentation here . Sublime Text 4 is fully compatible with version 3.

What happened to Sublime Text license keys?

A huge thanks goes out to all the beta testers on discord and all the contributors to our packages. Sublime Text license keys are no longer tied to a single major version, instead they are now valid for all updates within 3 years of purchase.

How long is a Sublime Text license good for?

Sublime Text license keys are no longer tied to a single major version, instead they are now valid for all updates within 3 years of purchase. After that, you will still have full access to every version of Sublime Text released within the 3 year window, but newer builds will required a license upgrade.


2 Answers

There is likely an existing plugin on PackgeControl that allows you to do something like this, although I'm not aware of any off the top of my head and the site is not responsive at the moment.

In the meantime, simplistically you can accomplish this via some simple plugin code:

import sublime
import sublime_plugin

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

To use this, select Tools > Developer > New Plugin... from the menu, then replace the entire contents of the template document with this code, and save it (name doesn't matter, only the extension).

This catches a save that's about to happen and runs the appropriate command, so long as the current file is a TypeScript file, as determined by the currently set syntax.

like image 74
OdatNurd Avatar answered Oct 06 '22 18:10

OdatNurd


I am using HTML-CSS-JS Prettify plugin.

After installing the package, open HTMLPrettify.sublime-settings under the menu Preferences > Package Settings > HTML/CSS/JS Prettify > Plugin Options - User and paste this setting below:

{
    "format_on_save": true,
    "global_file_rules":
    {
        "js":
        {
            "allowed_file_extensions": ["js", "jsx", "ts"],
            "allowed_file_syntaxes": ["javascript", "typescript", "ecma", "react", "babel"],
            "disallowed_file_patterns": []
        },
    }
}
like image 2
Kiry Meas Avatar answered Oct 06 '22 19:10

Kiry Meas