[!IMPORTANT]
This plugin is currently experimental and is not actively being maintained.
sg.nvim is a plugin focused on bringing many of the features of sourcegraph.com and Cody into Neovim.
To login, either:
:SourcegraphLogin
after following installation instructions for sourcegraph.com
usage.:SourcegraphLogin!
and provide an endpoint and access token to be stored.SRC_ENDPOINT
and SRC_ACCESS_TOKEN
environment variables to manage tokens for enterprise usage.
See :help sg.auth
for more information.
You can check that you're logged in by then running :checkhealth sg
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!
Requires:
cody-agent.js
(By default, sg.nvim downloads released binaries from Github. If you prefer to build the plugin yourself, you'll need cargo
to build)
Regardless of installation method, you must call require("sg").setup { ... }
in your config.
lazy.nvim
packer.nvim
vim-plug
After installation, run :checkhealth sg
.
(Nix instructions at the end of the readme)
-- 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>
Cody:
Sourcegraph Browsing:
:edit <sourcegraph url>
sg.nvim
will automatically add protocols for handling https://sourcegraph.com/*
links.:edit sg://github.com/tjdevries/sam.py/-/src/sam.py
:SourcegraphLink
to get a link for the location under your cursortype:symbol
supportThe 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:
nix flake update
every once in a while to make sure flake.lock
is up-to-datesg.nvim
-integrated neovim package for testing and examplesg.nvim
+ Cody onto nixpkgs:vimPluginsTo 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" }
}
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:
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
require("lazy").setup({
"example/other-plugin",
load_sg(),
}
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!