LuaFormatter icon indicating copy to clipboard operation
LuaFormatter copied to clipboard

Feature Request: Preserve Line Breaks

Open mqnc opened this issue 4 years ago • 5 comments

I am often annoyed if formatters don't give me the option to put line breaks where I want. For instance, let's say I want to define a matrix like this:

local mtx33 = {
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
}

I haven't found a formatter that lets me do this without hacks like putting empty comments in the end of each line. And even then, it often messes up the formatting of the following lines.

I would like to have the option that the formatter just doesn't touch line breaks at all.

Would that be possible?

mqnc avatar Feb 05 '22 11:02 mqnc

I think it should be possible but won't be easy.

Koihik avatar Feb 07 '22 02:02 Koihik

I found a dirty hack to make this work in C++, should be the same with lua: Before formatting, replace every "\n" in the code with "--\n", then format, then replace every "--\n" with "\n" and done :) So far I implemented this as a macro in vscode but it is probably easy to embed into formatters as well. I am aware how ugly this is tho, so I don't expect you to do it like this. Just if anyone stumbles upon this post, that's how they can patch this.

mqnc avatar Mar 04 '22 14:03 mqnc

Would it be possible to just use some comments to disable and enable formatting?

-- @format-disable
local mtx33 = {
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
}
-- @format-enable

Nick-Mazuk avatar May 16 '22 15:05 Nick-Mazuk

I also really want this too for situations where the number of arguments passed to a function is long and 1 may be an inline function. It looks weird when it gets moved onto its own line:

After formatting:

local listener = em:GetEventListenerByID("OnChange") or
                   em:CreateEventListenerWithID(
                     "OnChange", function()  
    local canListen = CanListenToChanges();

    if (canListen) then
      -- do stuff
    else       
      data.disabled = true;
      handleDisable();
    end
  end);

I'd prefer it to look like this:

local listener = 
  em:GetEventListenerByID("OnChange") or
    em:CreateEventListenerWithID("OnChange", function()  
      local canListen = CanListenToChanges();

      if (canListen) then
        -- do stuff
      else       
        data.disabled = true;
        handleDisable();
      end
    end);

Mayron avatar Sep 17 '22 09:09 Mayron