Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sellout opened this issue Dec 6, 2022 · 4 comments
Open

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

sellout opened this issue Dec 6, 2022 · 4 comments
Labels
component | style Modifications to the formatting rules roadmap | comment pragmas roadmap | style config status | it is a good thing We agree it is good to implement this type | feature request New feature or request

Comments

@sellout
Copy link

sellout commented Dec 6, 2022

LnL7/vim-nix#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
Copy link

rummik commented Dec 7, 2022

Other community support for hint prefixes can found floating around:

donovanglover added a commit to donovanglover/nix-config that referenced this issue Jun 11, 2023
Note that alejandra currently does not handle these comments properly.

See: kamadorueda/alejandra#366
@MattSturgeon
Copy link

nvim-treesitter also added support in nvim-treesitter/nvim-treesitter#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
Copy link

musjj commented Dec 28, 2023

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
Copy link

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.

@kamadorueda kamadorueda added type | feature request New feature or request component | style Modifications to the formatting rules status | it is a good thing We agree it is good to implement this roadmap | style config roadmap | comment pragmas labels Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component | style Modifications to the formatting rules roadmap | comment pragmas roadmap | style config status | it is a good thing We agree it is good to implement this type | feature request New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants