Skip to content

Commit a02d834

Browse files
committed
rustell.nvim plugin wip
1 parent d1ce3bf commit a02d834

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

rust/flake.nix

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,58 @@
4747
mkdir -p $out/lib
4848
cp target/${buildTarget}/release/*.wasm $out/lib/
4949
'';
50-
# Disable checks if they only work for WASM
51-
# doCheck = false;
50+
};
51+
mkRustellNvim = exe:
52+
pkgs.vimUtils.buildVimPlugin {
53+
name = "rustell-nvim";
54+
src = pkgs.writeTextDir "plugin/rustell.vim" ''
55+
augroup rust.vim.PreWrite
56+
autocmd!
57+
augroup END
58+
59+
augroup rustell_PreWrite
60+
autocmd!
61+
autocmd BufWritePre *.rs call s:RustellPreWrite()
62+
augroup END
63+
64+
function! s:RustellPreWrite()
65+
if !filereadable(expand('%'))
66+
return
67+
endif
68+
69+
" Read buffer
70+
let l:input = join(getline(1, '$'), "\n")
71+
72+
" --- run rustell first ---
73+
let l:rustell_out = system('${exe}', l:input)
74+
if v:shell_error
75+
echohl ErrorMsg | echom 'rustell failed: ' . l:rustell_out | echohl None
76+
return
77+
endif
78+
79+
" --- now feed result into rustfmt ---
80+
let l:rustfmt_cmd = 'rustfmt'
81+
if exists('g:rustfmt_command')
82+
let l:rustfmt_cmd = g:rustfmt_command
83+
endif
84+
if exists('g:rustfmt_options')
85+
let l:rustfmt_cmd .= ' ' . g:rustfmt_options
86+
endif
87+
88+
let l:rustfmt_out = system(l:rustfmt_cmd, l:rustell_out)
89+
if v:shell_error
90+
echohl ErrorMsg | echom 'rustfmt failed: ' . l:rustfmt_out | echohl None
91+
return
92+
endif
93+
94+
" Replace buffer with formatted output
95+
let l:out_lines = split(l:rustfmt_out, "\n")
96+
call setline(1, l:out_lines)
97+
if line('$') > len(l:out_lines)
98+
execute len(l:out_lines)+1 . ',$delete _'
99+
endif
100+
endfunction
101+
'';
52102
};
53103
in {
54104
devShells.default = pkgs.mkShell {
@@ -64,9 +114,13 @@
64114
wasmtime
65115
];
66116
};
67-
packages = {
117+
packages = rec {
68118
rustell = mkRustPkg "rustell";
119+
rustell-nvim = mkRustellNvim "${rustell}/bin/rustell";
69120
rustell-wasm = mkRustPkgWasm "rustell";
121+
rustell-wasm-nvim = mkRustellNvim "${
122+
pkgs.wasmtime
123+
}/bin/wasmtime ${rustell-wasm}/bin/rustell.wasm";
70124
default = self.packages.${system}.rustell;
71125
};
72126
}

0 commit comments

Comments
 (0)