How can I format dart code in sublime text

325 Views Asked by At

I downloaded flutter with dart inside, and start coding on sublime text but when I press formatt code it gives me wrong message which is "formatter for this type file (dart) not found." but when I run this command "dart format . " in the directory the code will be formatted, my question is how can I format code inside sublime instead of write that command in cmd?

note:

  • when I run dart --version in cmd it show its version but if I run dartfmt --version it doesn't recognize it as well as I downloaded codeformatter package
1

There are 1 best solutions below

0
SrPanda On

If you are referring to the plugin formatter specifically, you can't, dart is not supported but you can easily implement it with the instructions on the git page.

If you don't know/want do it yourself just do this two things:

Create the file ${packages}/Formatter/modules/formatter_dartformat.py with this code (the file is located in %APPDATA%/Sublime Text/Packages/Formatter/modules on windows).

import logging
from ..core import common

log = logging.getLogger(__name__)

EXECUTABLES = ['dart']
MODULE_CONFIG = {
    'name': 'DartFormat', 
    'source': 'https://dart.dev/tools/dart-format', 

    'uid': 'dartformat', 
    'type': 'beautifier',                  
    'syntaxes': ['dart'], 

    'executable_path': '',
    'args': ['format'],
}

class DartformatFormatter:
    def __init__(self, *args, **kwargs):
        self.view = kwargs.get('view', None)
        self.uid = kwargs.get('uid', None)
        self.region = kwargs.get('region', None)
        self.is_selected = kwargs.get('is_selected', False)
        self.pathinfo = common.get_pathinfo(self.view.file_name())

    def get_cmd(self):
        executable = common.get_executable(
            self.view, self.uid, EXECUTABLES, runtime_type=None
        )

        if executable is not None: 
            args = common.get_args(self.uid)
            
            cmd = [executable]
            cmd.extend(args or MODULE_CONFIG['args'])
            
            return cmd
        else:
            return None


    def format(self, text):
        cmd = self.get_cmd()
        log.debug('Current arguments: %s', cmd)
        cmd = common.set_fix_cmds(cmd, self.uid)
        if not cmd:
            return None

        try:
            proc = common.exec_cmd(cmd, self.pathinfo['cwd'])
            stdout, stderr = proc.communicate(text.encode('utf-8'))

            errno = proc.returncode
            if errno > 0:
                log.error(
                    'File not formatted due to an error (errno=%d): "%s"', 
                    errno, stderr.decode('utf-8')
                )
            else:
                return stdout.decode('utf-8')
        except OSError:
            log.error(
                'An error occurred while executing the command: %s', 
                ' '.join(cmd)
            )

        return None

Then add the module to the config file at ${packages}/User/Formatter.sublime-settings.

{
    // The other variables

    "formatters": {

        "dartformat": {
            "disable": false,
            "format_on_save": false,
            "syntaxes": ["dart"],
            "args": ["format", "-l", "360"],
            "executable_path": "O:/Flutter/bin/dart",
        },

        // The other formatters that you may have
    }
}