vim-nix icon indicating copy to clipboard operation
vim-nix copied to clipboard

Add fenced language syntaxes

Open rummik opened this issue 4 years ago • 20 comments

This adds fenced syntax highlighting to Nix files by making use of a leading comment on multiline strings.

Edit: This borrows much of the loading code for fenced languages from https://github.com/plasticboy/vim-markdown/

{
  installPhase = /* sh */ ''
    mkdir -p $out
    cp -r ftdetect ftplugin indent syntax $out
  '';

  vimrc = writeText "vimrc" /* vim */ ''
    filetype off
    set rtp+=${vader}
    set rtp+=${src}
   '';
}

image

rummik avatar Sep 02 '19 13:09 rummik

Additionally, this resolves #27

rummik avatar Sep 02 '19 13:09 rummik

I'm not really sure how to reproduce the error showing up on Travis, considering everything runs fine within a nix shell locally

rummik avatar Sep 02 '19 14:09 rummik

Apparently it runs fine within a nix shell on Travis as well

rummik avatar Sep 02 '19 14:09 rummik

This is neat but I don't really want to add support for this unless the syntax is somewhat standardised by the community.

LnL7 avatar Jun 22 '20 17:06 LnL7

Understandable. I don't know of any community syntax for this though, which is why I went with a leading comment, making it benign for anyone who doesn't use a syntax highlighter with support for it

rummik avatar Jun 23 '20 20:06 rummik

The leading block comment is I think the only sane approach, but could be slightly awkward in the case that someone already is using a block comment prior to a multiline string; maybe make it so that it scans the comment content for something like ft=sh so it is a little more explicit/flexible?

Shados avatar Jun 24 '20 06:06 Shados

Some type of prefix could be handy to make it a bit more explicit. As far as flexibility goes though it's able to load any syntax available to vim itself, so long as you either know the name or have an alias that points to it

rummik avatar Jun 30 '20 14:06 rummik

This is the only sane approach which does not modify the string which would cause rebuilds. It can also be combined with vims included markdown fencing to reuse the same syntax highlighting mapping

let g:vim_nix_fenced_languages = g:markdown_fenced_languages

SuperSandro2000 avatar Mar 15 '21 21:03 SuperSandro2000

I've been using this for a couple of months now and one thing I noticed is syntax highlighting breaking after antristrings with the same quoting as the outer strings and sometimes when the editor is split.

image

dasJ avatar Sep 28 '21 16:09 dasJ

Is it possible to disable this feature for very large buffers? I noticed this patch causes tremendous slowdown on Nixpkgs' all-packages.nix..

doronbehar avatar Dec 21 '22 08:12 doronbehar

This helps me:

commit 490826cd7b80fa4df5ce61a43a2167b9fc7b131a
Author: Doron Behar <[email protected]>
Date:   Sat Apr 22 12:57:07 2023 +0300

    Allow to disable fenced highlighting

diff --git a/ftplugin/nix.vim b/ftplugin/nix.vim
index 134a0e7..2f7fb72 100644
--- a/ftplugin/nix.vim
+++ b/ftplugin/nix.vim
@@ -19,6 +19,10 @@ if get(g:, 'nix_recommended_style', 1)
     \ softtabstop=2
     \ expandtab 
 endif
+ 
+if get(b:, 'nix_disable_fenced_highlight', 0)
+    finish
+endif
 
 " Borrowed from vim-markdown: https://github.com/plasticboy/vim-markdown/
 if exists('g:vim_nix_fenced_languages')

doronbehar avatar Apr 22 '23 09:04 doronbehar

I'm still very interested in this feature, any chance to get it merged?

pinpox avatar May 08 '23 10:05 pinpox

Looking into this now.

equill avatar May 08 '23 12:05 equill

Does lua syntax fencing work for anyone else? For some reason mine really hates right parentheses image image

agentx3 avatar May 16 '23 01:05 agentx3

Thanks a lot for the beautiful work!

I had some thoughts about this and I'm somewhat interested in getting things ready:

  • as @LnL7 already stated, I'm also rather hesitant about merging this as-is considering that this plugin is the de-facto standard for syntax highlighting on nix[1] and I don't feel well with introducing a new standard noone else has agreed on. We should at least discuss this with the maintainers of nix-mode[2] and the vscode integration[3] to cover the vast majority of editors. Perhaps we should even discuss this on nixpkgs itself. That'd also have the nice side-effect that it would be OK to introduce these tags where appropriate (and we could benefit from proper syntax highlighting at way more places). So ccing @matthewbauer @jnoortheen what do you think of this approach? It has the benefit that it would work for any kind of expression (and additionally it's clear which language is in use ;-) ). We should probably open up a discussion in nixpkgs itself for this, but I'd like to hear your opinion first.
  • I can confirm the lua issue @agentx3 has reported, would you mind looking into it @rummik ?
  • I tried this plugin out on all-packages.nix and I didn't get any highlighting at all. Perhaps we should automatically disable this feature for either well-known large files (e.g. yarn.nix, hackage-packages.nix, all-packages.nix) or dynamically switch it off for files >n lines. A static opt-out switch should also exist IMHO.
  • Question: has anybody investigated whether an LSP can be thrown against e.g. a block of Python code? Didn't work out of the box for me (using latest nvim-lspconfig and neovim 0.9.1), but I haven't looked closely, yet.

In the meantime I'll try to take a look at the code itself :)

[1] https://github.com/nixos/nixpkgs/commit/0b5a0cbc69f18f2a123bf4ae0751ee718ef04e53, https://github.com/vim/vim/commit/86b4816766d976a7ecd4403eca1f8bf6b4105800#diff-5202ae314163489b5d9872596ccd1bcd9ec8215c64722447dc7ba3b6cbceadd5 [2] https://github.com/nixos/nix-mode [3] https://github.com/nix-community/vscode-nix-ide

Ma27 avatar Jul 29 '23 12:07 Ma27

A static opt-out switch should also exist IMHO.

Perhaps it's even best to make this opt-in instead. Performing syntax highlighting on string contents makes interpolation much harder to see which I feel like is something more common than its syntax complexity. For large code blocks it seems better to use a dedicated file, which also allows regular linting, lsp, etc. tools to be used.

LnL7 avatar Jul 29 '23 12:07 LnL7

This is actually a very good point. Constructs such as ''${ will actually cause highlighting problems as well.

Ma27 avatar Jul 29 '23 13:07 Ma27

I would love to see this merged in as an opt-in feature. Currently it can be quite difficult to work with language snippets that aren't directly in a mkDerivation phase. Adding such a feature, but not enabling it by default seems like a decent solution to me.

jakehamilton avatar Sep 13 '23 13:09 jakehamilton

I rarely use /* sh */ in my code, due to the problem mentioned above regarding constructs such as ''${, but I enabled this feature in my fork's master branch, with a small addition:

if get(b:, 'nix_disable_fenced_highlight', 0)
    finish
endif

Right before most of the code added here begins. This enables me to disable this feature for large files for example.

doronbehar avatar Sep 13 '23 14:09 doronbehar

FWIW I was able to fix the Lua parentheses problem with rainbow-delimiters.nvim, available in nixpkgs.

donovanglover avatar Apr 02 '24 17:04 donovanglover