Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 6761a21

Browse files
floklidbarnett
authored andcommitted
Support formatting Nix
This adds support for formatting Nix code with the nixpkgs-fmt tool.
1 parent d418de0 commit 6761a21

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via
2828
* TypeScript (clang-format)
2929
* Shell (shfmt)
3030
* [Vue](http://vuejs.org) (prettier)
31+
* Nix (nixpkgs-fmt)
3132

3233
# Commands
3334

autoload/codefmt/nixpkgs_fmt.vim

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
" Copyright 2017 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
19+
""
20+
" @private
21+
"
22+
" Formatter provider for .nix files using nixpkgs-fmt.
23+
function! codefmt#nixpkgs_fmt#GetFormatter() abort
24+
let l:formatter = {
25+
\ 'name': 'nixpkgs-fmt',
26+
\ 'setup_instructions': 'Install nixpkgs-fmt. ' .
27+
\ '(https://github.com/nix-community/nixpkgs-fmt).'}
28+
29+
function l:formatter.IsAvailable() abort
30+
return executable(s:plugin.Flag('nixpkgs_fmt_executable'))
31+
endfunction
32+
33+
function l:formatter.AppliesToBuffer() abort
34+
return &filetype is# 'nix'
35+
endfunction
36+
37+
""
38+
" Reformat the current buffer with nixpkgs-fmt or the binary named in
39+
" @flag(nixpkgs_fmt_executable)
40+
" @throws ShellError
41+
function l:formatter.Format() abort
42+
let l:cmd = [ s:plugin.Flag('nixpkgs_fmt_executable') ]
43+
44+
" nixpkgs-fmt does not support range formatting.
45+
call codefmt#formatterhelpers#Format(l:cmd)
46+
endfunction
47+
48+
return l:formatter
49+
endfunction

instant/flags.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,7 @@ call s:plugin.Flag('zprint_executable', 'zprint')
168168
""
169169
" The path to the fish_indent executable.
170170
call s:plugin.Flag('fish_indent_executable', 'fish_indent')
171+
172+
""
173+
" The path to the nixpkgs-fmt executable.
174+
call s:plugin.Flag('nixpkgs_fmt_executable', 'nixpkgs-fmt')

plugin/register.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
3838
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
3939
call s:registry.AddExtension(codefmt#zprint#GetFormatter())
4040
call s:registry.AddExtension(codefmt#fish_indent#GetFormatter())
41+
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())

vroom/nixpkgs_fmt.vroom

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
The built-in nixpkgs-fmt formatter knows how to format Nix code.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
18+
The nixpkgs-fmt formatter expects the nixpkgs-fmt executable to be installed on
19+
your system.
20+
21+
% f()
22+
:FormatCode nixpkgs-fmt
23+
! nixpkgs-fmt .*
24+
$ f()
25+
26+
The name or path of the nixpkgs-fmt executable can be configured via the
27+
nixpkgs_fmt_executable flag if the default of "nixpkgs-fmt" doesn't work.
28+
29+
:Glaive codefmt nixpkgs_fmt_executable='nixpkgsfmt'
30+
:FormatCode nixpkgs-fmt
31+
! nixpkgsfmt .*
32+
$ f()
33+
:Glaive codefmt nixpkgs_fmt_executable='nixpkgs-fmt'
34+
35+
36+
You can format any buffer with nixpkgs-fmt specifying the formatter explicitly.
37+
Here's an example:
38+
39+
@clear
40+
% { foo = "bar" ;}
41+
42+
:FormatCode nixpkgs-fmt
43+
! nixpkgs-fmt .*
44+
$ {
45+
$ foo = "bar";
46+
$ }
47+
{
48+
foo = "bar";
49+
}
50+
@end

0 commit comments

Comments
 (0)