textmate2

How to use a code formatter with textmate 2?


I use TM2 to create and compile lualatex code. I'm working on a Macbook Air (M1). I installed stylua which works with the terminal, then I tried to install it in the lua bundle. The script is

[![#!/usr/bin/env bash
stylua "$TM_FILEPATH"]

I have to fill in this form

enter image description here

Before to use stylua

enter image description here

After

enter image description here

Is it possible to obtain only the result without the old code?

Is there another formatter code for lua with textmate?


Solution

  • Update: 3/15/2025

    The issue is that stylua reformats the file directly, rather than outputting the fixed file. The diff comes from TextMate looking at the file in memory and the file on disk, and thinks an external process changed the file, so it offers you a diff.

    I found a simple solution was to modify your script to copy the file to be formatted to a different location, format it, and then read it back to TextMate:

    #!/usr/bin/env bash
    cp "$TM_FILEPATH" /tmp/stlua.tmp.lua
    /opt/homebrew/bin/stylua /tmp/stlua.tmp.lua
    cat /tmp/stlua.tmp.lua
    

    There might be other solutions, like a flag for stylua to tell it to output the results to stdout, or a TextMate method to tell the editor to reload the file from disk, but this seemed to work.

    The rest of your bundle seemed good, but I'll leave my original reply in case it's helpful to you or someone else.

    Original post:

    Referencing the "Strip trailing whitespace on save" action from the Avian Missing bundle:

    Strip trailing whitespace on save" action from the Avian Missing bundle:

    I believe you should set the semantic class to callback.document.export.

    It also seems that Save: Nothing might help you avoid the conflict you're seeing.

    The editor should save regardless of this setting, if you're saving the document.

    While not necessary, I do suggest that you use Caret Placement: Line Interpolation as well.

    Write back if it doesn't work :)

    Hope this helps!