0

How can I change formatter used for specific file type in doom-emacs format module? So far I've been happy with using mostly doom defaults and haven't gotten into writing custom configurations and seriously using Elisp, so I'm a bit lost.

I'm trying to use doom-emacs as an IDE for Laravel development and I'm running into problems with Blade files. The main one being wrong code auto formatting. Prettier run from the command line has no problem formating the files, but doom defaults do. Eg.:

Input:

@section('content')
 @foreach ($posts as $post)
          <article class="article">
     </article>
       @endforeach
@endsection

Result:

@section('content')
@foreach ($posts as $post)
<article class="article">
</article>
@endforeach
@endsection

Expected:

@section('content')
    @foreach ($posts as $post)
        <article class="article">
        </article>
    @endforeach
@endsection

I'm using +onsave option. The files are opened in web-mode and the extension is *.blade.php. And I think it's using lsp formatting by default.

I've tried using (set-formatter! 'prettier "prettier" :modes '(web-mode)) and setting format-with-lsp to nil just to see if I can force the web-mode itself to use prettier. But no luck.

  1. How can I change the formatter used?
  2. How can I specify on which file extension it should be used?

I've been searching format-all, Doom, Googleing and trying find something in the source code, but no luck so far.

2 Answers 2

0

The package behind the format module is format-all. Going down the rabbit hole, its method of determining the current buffer's language is delegated to language-id, which has a nice DSL for detecting web-mode peculiarities. Adding a entry for blade may cut it:

(after! format-all
  (add-to-list 'language-id--definitions
               '("PHP"
                 php-mode
                 (web-mode
                  (web-mode-engine "blade")))))

Or, you could use web-mode-buffer-indent :)

5
  • web-mode-buffer-indent does the same thing :/ I've tried the code you provided, but it seems that the 'language-id--definitions variable is not defined in Doom. Commented Aug 10, 2022 at 13:59
  • What do you mean by "does the same thing"? What happens when you use it? Also, try wrapping the snippet with (after! format-all ...), maybe the reason it's not defined for you is related to doom's lazy module loading Commented Aug 10, 2022 at 15:02
  • 1. Using web-mode-buffer-indent function formats the code in the same way. 2. That just break doom and prevents it from starting (almost) at all. It seems you get a theme loaded and some modules in the background, but other than that just a blank screen and read-only buffer. It looks like the var is not defined anywhere, if you do describe-variable in web-mode and look for it it's nowhere to be found :( Commented Aug 10, 2022 at 20:43
  • My bad, the language-id thingy was added after the format-all version pinned by Doom. Try adding (unpin! format-all) to your packages.el file Commented Aug 11, 2022 at 10:10
  • Still no dice. Error in private config: config.el, (void-variable language-id--definitions). Just to be sure the code ((after! ...) should be in config.el, right?! I'm just not sure what else can be wrong... Commented Aug 11, 2022 at 11:45
0

Doom has now switched from using format-all to the apheleia package to handle formatting, and prettier is already the default formatter for web-mode buffers, so for the purposes of this question it is only necessary to ensure that web-mode is enabled for *.blade.php files, see: how do I enable web-mode .blade.php files? for details in how to do that in case it is not already configured in Doom (which I think it is).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.