Home

Awesome

sg.nvim

[!IMPORTANT]
This plugin is currently experimental and is not actively being maintained.

Table of Contents

sg.nvim is a plugin focused on bringing many of the features of sourcegraph.com and Cody into Neovim.

Setup

To login, either:

See :help sg.auth for more information.

You can check that you're logged in by then running :checkhealth sg

Autocomplete

Currently, sg.nvim only supports nvim-cmp. For setup information, see: :help cody.complete.

If you have other completion sources you would like added, please let me know in the issue tracker!

Installation

Requirements

Requires:

(By default, sg.nvim downloads released binaries from Github. If you prefer to build the plugin yourself, you'll need cargo to build)

Install

Regardless of installation method, you must call require("sg").setup { ... } in your config.

<details> <summary><code>lazy.nvim</code></summary>
-- Use your favorite package manager to install, for example in lazy.nvim
--  Optionally, you can also install nvim-telescope/telescope.nvim to use some search functionality.
return {
  {
    "sourcegraph/sg.nvim",
    dependencies = { "nvim-lua/plenary.nvim", --[[ "nvim-telescope/telescope.nvim ]] },

    -- If you have a recent version of lazy.nvim, you don't need to add this!
    build = "nvim -l build/init.lua",
  },
}
</details> <details> <summary><code>packer.nvim</code></summary>
-- Packer.nvim, also make sure to install nvim-lua/plenary.nvim
use { 'sourcegraph/sg.nvim', run = 'nvim -l build/init.lua' }

-- You'll also need plenary.nvim
use { 'nvim-lua/plenary.nvim' }

-- And optionally, you can install telescope for some search functionality
--  "nvim-lua/plenary.nvim", --[[ "nvim-telescope/telescope.nvim ]]
</details> <details> <summary><code>vim-plug</code></summary>
" Using vim-plug
Plug 'sourcegraph/sg.nvim', { 'do': 'nvim -l build/init.lua' }

" Required for various utilities
Plug 'nvim-lua/plenary.nvim'

" Required if you want to use some of the search functionality
Plug 'nvim-telescope/telescope.nvim'
</details>

After installation, run :checkhealth sg.

(Nix instructions at the end of the readme)

Configuration:

-- Sourcegraph configuration. All keys are optional
require("sg").setup {
  -- Pass your own custom attach function
  --    If you do not pass your own attach function, then the following maps are provide:
  --        - gd -> goto definition
  --        - gr -> goto references
  on_attach = your_custom_lsp_attach_function
}
" Example mapping for doing searches from within neovim (may change) using telescope.
" (requires telescope.nvim to be installed)
nnoremap <space>ss <cmd>lua require('sg.extensions.telescope').fuzzy_search_results()<CR>

Demos:

Features:

Cody:

Sourcegraph Browsing:

Nix(OS)

The project is packaged as a Nix Flake. Consume it as you normally would. For reference, see:

sg = let
  system = "x86_64-linux";
  package = inputs.sg-nvim.packages.${system}.default;
in {
  inherit package;
  init = pkgs.writeTextFile {
    name = "sg.lua";
    text = ''
      return function()
        package.cpath = package.cpath .. ";" .. "${package}/lib/?.so"
      end
    '';
  };
};

For Nix contributors and maintainers:

Development

Loading the plugin

To be able to test our changes we need to tell our favourite plugin manager to load the plugin locally rather than clone it from GitHub. Below is a snippet on how to do it with lazy.nvim

{
  --- The dir specified here is the absolute path to the sg.nvim repository
  dir = "~/code/path-to-sg-nvim-repo",
  dependencies = { "nvim-lua/plenary.nvim" }
}

Dynamically switch to loading the plugin from the repository

For ease for development it can be useful to automatically switch to loading the plugin from this repository if we enter this directory. We can do this by doing the following:

  1. In your plugin manager configuration, create the following function
local function load_sg()
  if vim.env.SG_NVIM_DEV then
    return { dir = vim.fn.getcwd(), dependencies = { "nvim-lua/plenary.nvim" } }
  else
    --- This is the configuration that lazy.nvim expects, but you can change it to whatever configuration your plugin manager expects
    return {
      "sourcegraph/sg.nvim",
      dependencies = { "nvim-lua/plenary.nvim" },
    }
  end
end
  1. Update your configuration - example (lazy.nvim):
require("lazy").setup({
  "example/other-plugin",
  load_sg(),
}
  1. Finally, we need the SG_NVIM_DEV variable to exist in our environment as soon as we enter the repository. We can do that by using direnv which automatically loads .envrc if it exists. Let's edit the current .envrc
export SG_NVIM_DEV="true"
# If nix-shell available, then nix is installed. We're going to use nix-direnv.
# for automatic devshell injection after opt-in `direnv allow`
if command -v nix-shell &> /dev/null
then
    use flake
fi

With the above changes, as soon as we enter this repository directory direnv will run .envrc which exports our SG_NVIM_DEV variable. Once we open Neovim and our plugins are loaded our load_sg function will get executed and see the SG_NVIM_DEV varialbe in the environment and rather load the sg.nvim plugin from the current working directory!