bcomnes / sublime-standard-format

:sparkles: Runs standard --fix against the javascript in your ST3 window on save or manually.
https://packagecontrol.io/packages/StandardFormat
MIT License
60 stars 21 forks source link

format in html script tags #58

Closed johannes-z closed 7 years ago

johannes-z commented 7 years ago

I'm not sure if this is an issue with sublime-standard-format or it's dependencies, but the formatting doesn't work for me in .html files with a script tag. If I extract the script tag content to another file, save the file with a .js extension and then run the formatter, it works as expected. There is no error output when I try to run the formatter on a .html file.

I even explicitly added .html extensions as such: "extensions": ["js", "html"]. Is there another option for html script tags?

bcomnes commented 7 years ago

Oh hrmm. Yeah I think that maybe standard --fix might not have support for html files. Adding html to the extensions array should attempt to pipe the contents of the view through the formatting command, but will abort the format if the command doesn't return 0.

johannes-z commented 7 years ago

That's what I suspected. But this also means that it's either the responsibility of standard-format or sublime-standard-format to handle html files correctly, right? Are there any plans or should I post this issue on standard-format.

What would be nice as some sort of work around is a standard format: current selection. Then I could just select the tags content, format, and be done with it.

johannes-z commented 7 years ago

Actually the sublime linter works perfectly fine with script tags. It requires the syntax (.sublime-syntax) to set the scope:source.js though. I just updated another syntax and the linter works for the other syntax as well. Maybe I can take a look at it and do a pull request, but my python sucks.

From the SublimeLinter\lint\linter.py

    # If a linter can be used with embedded code, you need to tell SublimeLinter
    # which portions of the source code contain the embedded code by specifying
    # the embedded scope selectors. This attribute maps syntax names
    # to embedded scope selectors.
    #
    # For example, the HTML syntax uses the scope `source.js.embedded.html`
    # for embedded JavaScript. To allow a JavaScript linter to lint that embedded
    # JavaScript, you would set this attribute to {'html': 'source.js.embedded.html'}.
    selectors = {}

This is how the standard linter does it:

    syntax = ('javascript', 'html', 'javascriptnext', 'javascript 6to5', 'javascript (babel)', 'vue')
    selectors = {
        'html': 'source.js.embedded.html',
        'vue': 'source.js.embedded.html'
    }

The SublimeLinter provides an interface vor both the syntax and the selectors. StandardFormat could use similar variables and only pass the scope's content to standard --fix

bcomnes commented 7 years ago

I never released this! Gah doing that now.

bcomnes commented 7 years ago

OK it is done!