kamadorueda / alejandra

The Uncompromising Nix Code Formatter
https://kamadorueda.github.io/alejandra/
The Unlicense
811 stars 39 forks source link

`/* … */` always split into multiple lines #366

Open sellout opened 1 year ago

sellout commented 1 year ago

https://github.com/LnL7/vim-nix/pull/28 proposes using inline comments as editor hints for highlighting foreign code blocks. But Alejandra doesn’t currently behave well with that. Here’s a before/after Alejandra excerpt from my experiment using that style:

-      initExtra = /* sh */ ''
-        source "${pkgs.darcs}/share/bash-completion/completions/darcs"
-      '';
+      initExtra =
+        /*
+        sh
+        */
+        ''
+          source "${pkgs.darcs}/share/bash-completion/completions/darcs"
+        '';

This isn’t yet a settled convention, so raising it here for consideration as to whether supporting one-line /* … */ in Alejandra is the way to go, or if there should be some other approach for handling foreign code blocks.

rummik commented 1 year ago

Other community support for hint prefixes can found floating around:

MattSturgeon commented 11 months ago

nvim-treesitter also added support in https://github.com/nvim-treesitter/nvim-treesitter/pull/3842

This format appears to be gaining traction and personally I'm a fan of it. Perhaps Alejandra could allow "short" comments to be kept inline?

A simple definition of "short" might be the comment does not contain multiple words. Or alternatively perhaps a max char length?

Some additional examples:

mapping = {
  "<CR>" = /* lua */ "cmp.mapping.confirm({ select = true })";
  "C-y" = /* lua */ "cmp.mapping.confirm({ select = true })";
  "<Tab>" = /* lua */ ''
    function(fallback)
      -- ...
    end
  '';
  "<S-Tab>" = /* lua */ ''
    function(fallback)
      -- ...
    end
  '';
};
musjj commented 9 months ago

Would love support for this too. For now I'm working around this by using linewise comments instead:

{
  attribute =
    # bash
    ''
      echo "hello world"
    '';
}

It saves two whole lines compared to the way that inline comments are currently formatted, with a plus of better readability. But it still wastes two extra lines compared to the ideal format:

{
  attribute = /* bash */ ''
    echo "hello world"
  '';
}
ian-h-chamberlain commented 2 months ago

To make another case where avoiding the line break is useful, I have started to get in the habit of making block-comments that are togglable via a single line-comment, for example:

{
  /* <-- Toggling a # line comment at the beginning of this line toggles the whole block
    services.xserver = {
      enable = true;
      layout = "us";
      libinput.enable = true;

      # Enable the KDE Desktop Environment.
      displayManager.sddm.enable = true;
      desktopManager.plasma5.enable = true;
    };
  # */
}

Upon reformatting, Alejandra breaks the # */ on the last line apart (as well as breaking up the first line), making the quick one-line-toggle functionality no longer work. Now the final */ and initial comment text appears on their own lines and would produce syntax errors:

{
  # /*
    <-- Toggling a # line comment on this line toggles the whole block
    services.xserver = {
      enable = true;
      layout = "us";
      libinput.enable = true;

      # Enable the KDE Desktop Environment.
      displayManager.sddm.enable = true;
      desktopManager.plasma5.enable = true;
    };
  #
  */
}

I'm not sure if/how this would fit with the proposed "short comment" logic, but it's a really convenient way of quickly toggling on/off large code blocks which would be nice to support with the formatter.