<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: David</title>
    <description>The latest articles on Forem by David (@drmason13).</description>
    <link>https://forem.com/drmason13</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F263801%2Ffbc69cf0-ce9f-4acb-9ddd-aebaaf0f0177.jpeg</url>
      <title>Forem: David</title>
      <link>https://forem.com/drmason13</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/drmason13"/>
    <language>en</language>
    <item>
      <title>Configure neovim for rust development</title>
      <dc:creator>David</dc:creator>
      <pubDate>Fri, 03 Jan 2020 21:48:13 +0000</pubDate>
      <link>https://forem.com/drmason13/configure-neovim-for-rust-development-1fjn</link>
      <guid>https://forem.com/drmason13/configure-neovim-for-rust-development-1fjn</guid>
      <description>&lt;p&gt;nvim's 0.5.0 release includes a native lsp-client: &lt;em&gt;nvim-lsp&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I have toyed with neovim for a while as a long time vim user and decided to make the switch proper. This is my current setup, and a little advice on putting all the pieces together.&lt;/p&gt;

&lt;p&gt;First of all, the vim config:&lt;/p&gt;

&lt;h2&gt;
  
  
  init.vim
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="k"&gt;call&lt;/span&gt; plug#begin&lt;span class="p"&gt;()&lt;/span&gt;
Plug &lt;span class="s1"&gt;'neovim/nvim-lsp'&lt;/span&gt;
Plug &lt;span class="s1"&gt;'Shougo/deoplete.nvim'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s1"&gt;'do'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;':UpdateRemotePlugins'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
Plug &lt;span class="s1"&gt;'Shougo/deoplete-lsp'&lt;/span&gt;
Plug &lt;span class="s1"&gt;'ervandew/supertab'&lt;/span&gt;
Plug &lt;span class="s1"&gt;'Chiel92/vim-autoformat'&lt;/span&gt;
&lt;span class="k"&gt;call&lt;/span&gt; plug#end&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c"&gt;" setup rust_analyzer LSP (IDE features)&lt;/span&gt;
&lt;span class="k"&gt;lua&lt;/span&gt; require&lt;span class="s1"&gt;'nvim_lsp'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;rust_analyzer&lt;span class="p"&gt;.&lt;/span&gt;setup&lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c"&gt;" Use LSP omni-completion in Rust files&lt;/span&gt;
autocmd Filetype rust &lt;span class="k"&gt;setlocal&lt;/span&gt; &lt;span class="nb"&gt;omnifunc&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="k"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;lua&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;vim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;lsp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;omnifunc&lt;/span&gt;

&lt;span class="c"&gt;" Enable deoplete autocompletion in Rust files&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:deoplete&lt;/span&gt;#enable_at_startup &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="c"&gt;" customise deoplete                                                                                                                                                     " maximum candidate window length&lt;/span&gt;
&lt;span class="k"&gt;call&lt;/span&gt; deoplete#custom#&lt;span class="k"&gt;source&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'max_menu_width'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;" Press Tab to scroll _down_ a list of auto-completions&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:SuperTabDefaultCompletionType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;c-n&amp;gt;"&lt;/span&gt;

&lt;span class="c"&gt;" rustfmt on write using autoformat&lt;/span&gt;
autocmd &lt;span class="nb"&gt;BufWrite&lt;/span&gt; * &lt;span class="p"&gt;:&lt;/span&gt;Autoformat

&lt;span class="c"&gt;"TODO: clippy on write&lt;/span&gt;
autocmd &lt;span class="nb"&gt;BufWrite&lt;/span&gt; * &lt;span class="p"&gt;:&lt;/span&gt;Autoformat

nnoremap &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;leader&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;c&lt;/span&gt; &lt;span class="p"&gt;:!&lt;/span&gt;cargo clippy
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There's a fair amount going on here, but all of this is related to helping format, lint and auto-complete rust code.&lt;/p&gt;

&lt;p&gt;I'm using &lt;a href="https://www.rust-lang.org/"&gt;rust&lt;/a&gt; for this example because I am loving learning rust at the moment but there are many supported languages - if you are setting yourself up for a different language (JS, python and others), try letting &lt;br&gt;
&lt;a href="https://neovim.io/doc/user/lsp.html"&gt;nvim-lsp&lt;/a&gt; install the language server for you. We'll be compiling the language server from source (don't worry, &lt;a href="https://doc.rust-lang.org/cargo/"&gt;cargo&lt;/a&gt; makes it a breeze).&lt;/p&gt;
&lt;h2&gt;
  
  
  Installs
&lt;/h2&gt;

&lt;p&gt;First of all, nvim-lsp at the time of writing is new, and you need at least version &lt;a href="https://github.com/neovim/neovim/releases"&gt;0.5.0&lt;/a&gt; of nvim installed. Your current install is probably stable like mine was, which isn't new enough. So grab 0.5.0, &lt;code&gt;tar -xzf&lt;/code&gt; and whack it somewhere in your path, like this (on linux):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tar -xzf nvim-linux64.tar.gz
sudo cp nvim-linux64/bin/nvim /usr/bin/nvim
nvim # this should load up 0.5.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;If you don't have nvim 0.5.0 or greater nvim-lsp won't exist, let alone work!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;nvim-lsp and &lt;a href="https://github.com/Chiel92/vim-autoformat#how-to-use"&gt;autoformat&lt;/a&gt; rely on external tooling to work their magic, so let's get it installed.&lt;/p&gt;

&lt;p&gt;In your shell, from the top.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# install rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# install rustfmt (for formatting)
rustup component add rustfmt

# install clippy (for semantic linting)
rustup component add clippy

# hardest one last, install rust-analyzer
git clone https://github.com/rust-analyzer/rust-analyzer &amp;amp;&amp;amp; cd rust-analyzer
cargo xtask install --server
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To check it worked (your commit hash will likely vary!)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ra_lsp_server --version
rust-analyzer 823c152
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Back in the world of vim plugins, start up nvim and install your plugins. If you have &lt;a href="https://github.com/junegunn/vim-plug#installation"&gt;vim-plug&lt;/a&gt; that's as easy as&lt;/p&gt;

&lt;p&gt;(inside nvim)&lt;br&gt;
&lt;code&gt;:PlugInstall&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you don't, it's just (in your shell)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

nvim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and then &lt;code&gt;:PlugInstall&lt;/code&gt; as above. If you don't use vim-plug and don't know what a vim plugin manager is, my advice is simple: &lt;a href="https://github.com/junegunn/vim-plug#installation"&gt;vim-plug&lt;/a&gt; is easily good enough and so simple. I highly recommend it.&lt;/p&gt;

&lt;p&gt;Depending on the state of your nvim install, you might need a python module called &lt;code&gt;pynvim&lt;/code&gt;. I think deoplete needs this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pynvim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can edit some rust code!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;auto-completion should just appear (use tab to choose a suggestion, thanks supertab)&lt;/li&gt;
&lt;li&gt;run clippy by pressing &lt;code&gt;&amp;lt;leader&amp;gt;c&lt;/code&gt; (in this case &lt;code&gt;-c&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;on each write, your code will be formatted using rustfmt (never argue about code style again)&lt;/li&gt;
&lt;li&gt;Enjoy!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let me know if you try this and run into issues and I'll update (m)any mistakes.&lt;/p&gt;

&lt;p&gt;Would love to hear what nvim setup you are running, particularly for rust programming. I have loads to learn so let me know what you think, good or bad.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>rust</category>
      <category>ide</category>
      <category>lsp</category>
    </item>
  </channel>
</rss>
