LnL7 / vim-nix

Vim configuration files for Nix http://nixos.org/nix
MIT License
288 stars 26 forks source link

Add fenced language syntaxes #28

Open rummik opened 5 years ago

rummik commented 5 years ago

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 commented 5 years ago

Additionally, this resolves #27

rummik commented 5 years ago

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

rummik commented 5 years ago

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

LnL7 commented 4 years ago

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

rummik commented 4 years ago

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

Shados commented 4 years ago

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?

rummik commented 4 years ago

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

SuperSandro2000 commented 3 years ago

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
dasJ commented 3 years ago

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

doronbehar commented 1 year ago

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

doronbehar commented 1 year ago

This helps me:

commit 490826cd7b80fa4df5ce61a43a2167b9fc7b131a
Author: Doron Behar <doron.behar@gmail.com>
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')
pinpox commented 1 year ago

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

equill commented 1 year ago

Looking into this now.

agentx3 commented 1 year ago

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

Ma27 commented 1 year ago

Thanks a lot for the beautiful work!

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

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

LnL7 commented 1 year ago

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.

Ma27 commented 1 year ago

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

jakehamilton commented 1 year ago

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.

doronbehar commented 1 year ago

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.

donovanglover commented 6 months ago

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