Sublime Text plugin to format files faster using prettierd
valid_extensions = [
    "cjs", "component.html", "css", "cts", "flow", "gql", "graphql", "hbs", "handlebars", 
    "html", "js", "json", "json5", "jsx", "less", "markdown", "md", "mdx", "mjs", "mts", 
    "pug", "scss", "ts", "tsx", "vue", "yaml", "yml"
]
prettierd_extensions.py
smastrom/sublime-prettierd-format
import sublime
import sublime_plugin

from .prettierd_formatter import format_with_prettierd
from .prettierd_extensions import valid_extensions

def get_settings():
    return sublime.load_settings("prettierd_format.sublime-settings")

class PrettierdFormatCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        file_path = self.view.file_name()
        if not file_path:
            return

        # Get file extension
        file_extension = file_path.split('.')[-1].lower()

        # Fetch additional extensions from settings
        settings = get_settings()
        additional_extensions = settings.get("additional_extensions", [])
        all_extensions = valid_extensions + additional_extensions

        if file_extension not in all_extensions:
            return       

        # If everything is okay, format the file
        print("Formatting file:", file_path)

        current_content = self.view.substr(sublime.Region(0, self.view.size()))
        file_path = self.view.file_name()
        formatted_code = format_with_prettierd(current_content, file_path)
        
        # print("Formatted Code:", formatted_code)

        if formatted_code:
            self.view.run_command('replace_view_content', {'content': formatted_code})

class ReplaceViewContentCommand(sublime_plugin.TextCommand):
    def run(self, edit, content):
        self.view.replace(edit, sublime.Region(0, self.view.size()), content)

prettierd_format.py
smastrom/sublime-prettierd-format
import subprocess
import sublime
import sublime_plugin
import os
import re

from .prettierd_formatter import format_with_prettierd
from .prettierd_extensions import valid_extensions

def get_settings():
    return sublime.load_settings("prettierd_format.sublime-settings")

class PrettierdFormatEventListener(sublime_plugin.EventListener):

    def on_pre_save(self, view):
        settings = get_settings()
        format_on_save = settings.get("format_on_save", True)

        if not format_on_save:
            return

        # If skip_formatting is set on the view, reset it and exit early.
        if view.settings().get("skip_formatting", False):
            view.settings().erase("skip_formatting")
            return

        file_path = view.file_name()
        if not file_path:
            return

        file_extension = file_path.split('.')[-1].lower()

        disabled_extensions_on_save = settings.get("disabled_extensions_on_save", [])
        disabled_directories_on_save = settings.get("disabled_directories_on_save", [])
        
        additional_extensions = settings.get("additional_extensions", [])

        if file_extension in disabled_extensions_on_save:
            return

        # Add the plugin directory to disabled directories
        plugin_dir = os.path.dirname(os.path.realpath(__file__))
        disabled_directories_on_save.append(plugin_dir)

        # Check if the file belongs to a disabled directory
        for directory_pattern in disabled_directories_on_save:
            # Convert wildcard pattern to regex pattern
            regex_pattern = re.escape(directory_pattern).replace("\\*", ".*")
            if re.search(regex_pattern, file_path):
                return

        # Check against all extensions
        all_extensions = valid_extensions + additional_extensions

        if file_extension in all_extensions:
            current_content = view.substr(sublime.Region(0, view.size()))
            formatted_code = format_with_prettierd(current_content, file_path)
            if formatted_code:
                view.run_command('replace_view_content', {'content': formatted_code})

class ReplaceViewContentCommand(sublime_plugin.TextCommand):
    def run(self, edit, content):
        self.view.replace(edit, sublime.Region(0, self.view.size()), content)

prettierd_format_and_save.py
smastrom/sublime-prettierd-format
import subprocess
import sublime

def format_with_prettierd(content, file_path):
    cmd = ["prettierd", "--stdin-filepath", file_path]
    process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    formatted_code, error = process.communicate(input=content.encode('utf-8'))

    if process.returncode == 0:
        return formatted_code.decode('utf-8')
    else:
        print("Error returned by prettierd: {}".format(error.decode('utf-8')))
        sublime.error_message("Error formatting the file with prettierd.")
        return None

prettierd_formatter.py
smastrom/sublime-prettierd-format
import sublime
import sublime_plugin

class PrettierdSaveWithoutFormatCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.settings().set("skip_formatting", True)
        self.view.run_command("save")
prettierd_save_without_format.py
smastrom/sublime-prettierd-format