Setting up Sway in Vim
September 24th, 2023

If you are a swayor and you use vim as your main code editor here I will show you how to configure the sway syntax in vim, so you can develop smart contracts like a ninja.

Let's started

Pre-requisites:

  1. Ensure you have the forc-lsp installed with forc-lsp —version. If not, install the Sway toolchain.

  2. Clone the sway.vim repo

    git clone https://github.com/FuelLabs/sway.vim.git
    

Setting up Neovim

  1. Copy the folders 'ftdetect' and 'syntax' to your .config folder:

    cp -R ~/sway.vim/syntax ~/.config/nvim && cp -R ~/sway.vim/ftdetect ~/.config/nvim
    
  2. If you do not have the ~/.config/nvim/init.lua file, install kickstart.nvim.

  3. Add the following to ~/.config/nvim/init.lua:

    -- Install Sway LSP as a custom	server
    local lspconfig = require 'lspconfig'
    local configs = require 'lspconfig.configs'
    
    -- Check if the config is already defined (useful when reloading this file)
    if not configs.sway_lsp then
       configs.sway_lsp = {
         default_config = {
           cmd = {'forc-lsp'},
           filetypes = {'sway'},
           on_attach = on_attach,
           init_options = { 
             -- Any initialization options
             logging = { level = 'trace' }
           },
           root_dir = function(fname)
             return lspconfig.util.find_git_ancestor(fname)
           end;
           settings = {};
         };
       }
     end
    
    lspconfig.sway_lsp.setup{}
    

    Check that the LSP is installed running :LspInfo

Setting up Vim

To configure sway in vim the steps are very similar to the ones we did to configure in neovim.

  1. Clone the sway.vim repo

    git clone https://github.com/FuelLabs/sway.vim.git
    
  2. Copy the folders 'ftdetect' and 'syntax' to your .config folder:

    cp -R ~/sway.vim/syntax ~/.config/nvim && cp -R ~/sway.vim/ftdetect ~/.config/nvim
    
  3. Add the following code to your ~/.vim/filetype.vim

    if exists("did_load_filetypes")
      finish
    endif
    
    augroup filetypedetect
      au! BufNewFile,BufRead *.[sS][wW] setf sway
    augroup END
    
  4. Install vim.plug with the following command

    curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    
  5. Add the following code to your ~/.vimrc

    call plug#begin()
    
    Plug 'prabirshrestha/vim-lsp'
    
    call plug#end()
    
  6. Create a file called ~/.vim/lsp.vim with the following code

    " vim-lsp for Sway (sway-lsp)
    if executable('sway-lsp')
        au User lsp_setup call lsp#register_server({
            \ 'name': 'sway-lsp',
            \ 'cmd': {server_info->['sway-lsp']},
            \ 'whitelist': ['sway'],
            \ })
    endif
    
  7. Open vim and run :source ~/.vim/lsp.vim

  8. Check that the LSP is installed running :LspStatus

And done!! Now to code with sway in vim or neovim.


That's all for today, I hope this tutorial has worked for you, thanks for making it this far!

Subscribe to andrw
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from andrw

Skeleton

Skeleton

Skeleton