• Stars
    star
    132
  • Rank 272,934 (Top 6 %)
  • Language
    Lua
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimal implementation of Golang development plugin for Neovim

nvim-go

build

A minimal implementation of Golang development plugin written in Lua for Neovim.

Introduction

Neovim (from v0.5) embeds a built-in Language Server Protocol (LSP) client. And Debug Adapter Protocol (DAP) are also brought into Neovim as a general debugger. With the power of them, we are able to easily turn Neovim into a powerful editor.

nvim-go is designed to collaborate with them, provides sufficient features, and leverages community toolchains to get Golang development done.

Features

  • Auto format with :GoFormat (via goimports, gofmt, gofumpt and lsp) when saving.
  • Run linters with :GoLint (via revive) automatically.
  • Quickly test with :GoTest, :GoTestFunc, :GoTestFile and :GoTestAll. Generate test with :GoAddTest.
  • Import packages with :GoGet and :GoImport.
  • Modify struct tags with :GoAddTags, :GoRemoveTags, :GoClearTags, :GoAddTagOptions, :GoRemoveTagOptions and :GoClearTagOptions.
  • Generates JSON models with :GoQuickType (via quicktype).
  • Generate if err based on function return values with :GoIfErr (via iferr).

Screenshots

This section can be regarded as a guide or common practice to develop with nvim-go, LSP (gopls) and DAP. If you are familiar with these tools or other equivalent, you may skip this chapter.

<Show/Hide the guide>

Language Server

Language server provides vital language features to make Golang development easy. We highly recommend you to use LSP client together with nvim-go.

  1. Setup gopls with neovim/nvim-lspconfig.
  2. Setup your favorite completion engine such as nvim-cmp.
  3. Setup and map the following methods based on what you need:
  • Declaration: vim.lsp.buf.declaration()
  • Definition: vim.lsp.buf.definition() and vim.lsp.buf.type_definition()
  • Implementation: vim.lsp.buf.implementation()
  • Hover: vim.lsp.buf.hover()
  • Signature: vim.lsp.buf.signature_help()
  • References: vim.lsp.buf.reference()
  • Symbols: vim.lsp.buf.document_symbol() and vim.lsp.buf.workspace_symbol()
  • Rename: vim.lsp.buf.rename()
  • Format: vim.lsp.buf.format(), also works with GoFormat.
  • Diagnostic: vim.diagnostic will also show lint issues with Virtual Text, which runs go/analysis. You may disable auto_lint if this works well with your project.

For details of gopls, please refer to https://github.com/golang/tools/blob/master/gopls/doc/design/design.md#features.

Debugger

Installation

Prerequisites:

  • Neovim (>= 0.7)
  • npm (for quicktype)

Install with your favorite package manager:

" dependencies
use('nvim-lua/plenary.nvim')

" nvim-go
use('crispgm/nvim-go')

" (optional) if you enable nvim-notify
use('rcarriga/nvim-notify')

" (recommend) LSP config
use('neovim/nvim-lspconfig')

Finally, run :GoInstallBinaries after plugin installed.

Install quicktype with yarn or pnpm:

nvim-go install quicktype with npm by default, you may replace it with yarn or pnpm.

require('go').config.update_tool('quicktype', function(tool)
    tool.pkg_mgr = 'yarn'
end)

Usage

Setup

-- setup nvim-go
require('go').setup({})

-- setup lsp client
require('lspconfig').gopls.setup({})

Defaults

require('go').setup({
    -- notify: use nvim-notify
    notify = false,
    -- auto commands
    auto_format = true,
    auto_lint = true,
    -- linters: revive, errcheck, staticcheck, golangci-lint
    linter = 'revive',
    -- linter_flags: e.g., {revive = {'-config', '/path/to/config.yml'}}
    linter_flags = {},
    -- lint_prompt_style: qf (quickfix), vt (virtual text)
    lint_prompt_style = 'qf',
    -- formatter: goimports, gofmt, gofumpt, lsp
    formatter = 'goimports',
    -- maintain cursor position after formatting loaded buffer
    maintain_cursor_pos = false,
    -- test flags: -count=1 will disable cache
    test_flags = {'-v'},
    test_timeout = '30s',
    test_env = {},
    -- show test result with popup window
    test_popup = true,
    test_popup_auto_leave = false,
    test_popup_width = 80,
    test_popup_height = 10,
    -- test open
    test_open_cmd = 'edit',
    -- struct tags
    tags_name = 'json',
    tags_options = {'json=omitempty'},
    tags_transform = 'snakecase',
    tags_flags = {'-skip-unexported'},
    -- quick type
    quick_type_flags = {'--just-types'},
})

Manual

Display within Neovim with:

:help nvim-go

Advanced Configuration

Statusline Count

vim-airline:

function! LintIssuesCount()
    if exists('g:nvim_go#lint_issues_count')
        return g:nvim_go#lint_issues_count
    endif
endfunction
call airline#parts#define_function('nvim_go', 'LintIssuesCount')
call airline#parts#define_condition('nvim_go', '&filetype == "go"')
let g:airline_section_warning = airline#section#create_right(['nvim_go'])

lightline:

function! LintIssuesCount()
    if exists('g:nvim_go#lint_issues_count') && &filetype == 'go'
        return g:nvim_go#lint_issues_count
    endif
endfunction
let g:lightline = {
  \ 'colorscheme': 'wombat',
  \ 'active': {
  \   'left': [ [ 'mode', 'paste' ],
  \             [ 'readonly', 'filename', 'modified', 'lintcount' ] ]
  \ },
  \ 'component_function': {
  \   'lintcount': 'LintIssuesCount'
  \ },
  \ }

nvim-hardline:

require('hardline').setup({
    -- ...
    sections = {
        {
            class = 'error',
            item = function()
                if
                    vim.bo.filetype == 'go'
                    and vim.g['nvim_go#lint_issues_count'] ~= nil
                then
                    return vim.g['nvim_go#lint_issues_count']
                else
                    return ''
                end
            end,
        },
    -- ...
    }

Show Lint Issues without Focusing

augroup NvimGo
  autocmd!
  autocmd User NvimGoLintPopupPost wincmd p
augroup END

License

MIT

More Repositories

1

resume

A minimalist resume template for Jekyll and Hexo
HTML
298
star
2

awesome-engineering-blogs

A curated list of awesome engineering blogs.
248
star
3

telescope-heading.nvim

An extension for telescope.nvim that allows you to switch between headings
Lua
118
star
4

dotfiles

dotfiles to provision a new macOS with cosy dev setups
Lua
101
star
5

nvim-tabline

nvim port of tabline.vim with Lua
Lua
75
star
6

cmp-beancount

nvim-cmp source for beancount accounts
Lua
32
star
7

arch-linux-dotfiles

dotfiles for Arch Linux
Python
27
star
8

alfred-markdown-table

Generate Markdown Table with Alfred Workflow
Go
25
star
9

alfred-nord

Nord theme for Alfred
23
star
10

olympia

📷 Control Olympus cameras via Wi-Fi like a boss (WIP)
Ruby
19
star
11

crispgm.com

CrispDev, My Web Home Page
HTML
15
star
12

alfred-cron

Convert Cron expression to human readable text
Go
9
star
13

minimal

A minimalism theme for Jekyll
SCSS
9
star
14

weekly

A personal curation of tech articles
Ruby
9
star
15

holy-images

🕶 A simple image sharing social network
Ruby
8
star
16

gsm

Gem Sources Manager
Ruby
7
star
17

wiki-theme

Wiki theme for Jekyll
CSS
6
star
18

go-van

Simple project files watcher and deployer
Go
6
star
19

kicker-cli

Foosball data analyzer for Kickertool
Go
5
star
20

caravan

Simple project files watcher and deployer
Ruby
5
star
21

dockerfiles

Dockerfiles: FROM crisp
Dockerfile
5
star
22

heelbot

[Experimental] Build and run awesome bots to enable work automations
Ruby
5
star
23

alfred-strlen

Show string length with Alfred
Go
4
star
24

markdown-table-formatter

Format tables in GitHub Flavored Markdown
Ruby
4
star
25

spacifier

A simple & handy tool to insert a space between a Chinese character and a western character
Ruby
4
star
26

nvim-auto-ime

Automatically switch Input Method (only on macOS)
Lua
3
star
27

resume-hexo-example

Crisp Minimal Resume example for Hexo
HTML
3
star
28

foosball-referee

A web application for foosball referee
HTML
3
star
29

read-track

Personal reading list
Go
2
star
30

go-g

Go utility packages
Go
2
star
31

apartment

Source of website for my apartment
HTML
2
star
32

moderu

Photographer's album theme for モデル (Model)
CSS
2
star
33

dottie

Yet another .dotfiles manager written in Rust
Rust
2
star
34

black-white-blue

Simple and minimal project home page theme for Jekyll
HTML
1
star
35

festival.js

Super simple festival detector
JavaScript
1
star
36

crispgm

1
star
37

clutters

Code Clutters
JavaScript
1
star
38

github-meta-badges

Simple GitHub Metadata Badges (e.g. stars, issues, and more)
Ruby
1
star
39

dslr

Simple DSL in Ruby
Ruby
1
star
40

word-kanban

📘 A simple Kanban-like word book
JavaScript
1
star