Skip to content

Commit

Permalink
feat: allow disabling cody (#119)
Browse files Browse the repository at this point in the history
Fixes: #114
  • Loading branch information
tjdevries authored Sep 12, 2023
1 parent 5659dfe commit 009feb0
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 127 deletions.
4 changes: 4 additions & 0 deletions plugin/cody-agent.lua → after/plugin/cody-agent.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if not require("sg.config").enable_cody then
return
end

local protocol = require "sg.cody.protocol"

local augroup_cody = vim.api.nvim_create_augroup("augroup-cody", {})
Expand Down
4 changes: 4 additions & 0 deletions plugin/cody.lua → after/plugin/cody.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

---@config { ["module"] = "cody" }

if not require("sg.config").enable_cody then
return
end

local cody_commands = require "sg.cody.commands"

local M = {}
Expand Down
119 changes: 119 additions & 0 deletions after/plugin/sg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---@tag sg.commands

---@brief [[
--- Default commands for interacting with Sourcegraph
---@brief ]]

local void = require("plenary.async").void

---@command SourcegraphLogin [[
--- Get prompted for endpoint and access_token if you don't
--- want to set them via environment variables.
---@command ]]
vim.api.nvim_create_user_command("SourcegraphLogin", function()
local auth = require "sg.auth"

auth.set_nvim_auth()

vim.notify "[sg-cody] Changes will come into effect after a restart"
end, {
desc = "Login and store credentials for later use (an alternative to the environment variables",
})

---@command SourcegraphBuild [[
--- Rebuild the Sourcegraph crates and required dependencies (in case build failed during installation)
---@command ]]
vim.api.nvim_create_user_command("SourcegraphBuild", function()
local plugin_file = require("plenary.debug_utils").sourced_filepath()
local root = vim.fn.fnamemodify(plugin_file, ":h:h")
local build = require("sg.utils").joinpath(root, "build", "init.lua")
print "Starting sourcegraph build:"

require("sg.utils").system({ "nvim", "-l", build }, { cwd = root, text = true }, function(obj)
print(obj.stdout)
print(obj.stderr)
if obj.code ~= 0 then
error "Sourcegraph Build Failed. Check `:messages`"
else
print "Sourcegraph Build Success! Build log in `:messages`"
end
end)
end, {
desc = "Rebuild the Sourcegraph crates and required dependencies (in case build failed during installation)",
})

---@command SourcegraphDownloadBinaries [[
--- (Re-)Download the sourcegraph binaries. This should happen during installation
--- but you can force redownloading the binaries this way to ensure that sg.nvim
--- is properly installed.
---@command ]]
vim.api.nvim_create_user_command("SourcegraphDownloadBinaries", function()
require("sg.build").download()
end, {
desc = "(Re-)download the sourcegraph binaries",
})

vim.api.nvim_create_user_command("SourcegraphInfo", function()
print "[sg] Attempting to get sourcegraph info..."

void(function()
-- TODO: Would be nice to get the version of the plugin
print "[sg] making request"
local err, info = require("sg.rpc").get_info()
print(err, info)
if err or not info then
error "Could not get sourcegraph info"
end

local contents = vim.split(vim.inspect(info), "\n")

table.insert(contents, 1, "Sourcegraph info:")

vim.cmd.vnew()
vim.api.nvim_buf_set_lines(0, 0, -1, false, contents)
vim.api.nvim_buf_set_option(0, "buflisted", false)
vim.api.nvim_buf_set_option(0, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(0, "modifiable", false)
vim.api.nvim_buf_set_option(0, "modified", false)

vim.schedule(function()
print "[sg] got sourcegraph info. For more information, see `:checkhealth sg`"
end)
end)()
end, {})

---@command SourcegraphLink [[
--- Get a sourcegraph link to the current repo + file + line.
--- Automatically adds it to your '+' register
---@command ]]
vim.api.nvim_create_user_command("SourcegraphLink", function()
local cursor = vim.api.nvim_win_get_cursor(0)
void(function()
print "requesting link..."

local err, link = require("sg.rpc").get_link(vim.api.nvim_buf_get_name(0), cursor[1], cursor[2] + 1)
if err or not link then
print("Failed to get link:", link)
return
end

print("Setting '+' register to:", link)
vim.fn.setreg("+", link)
end)()
end, {
desc = "Get a sourcegraph link to the current location",
})

---@command SourcegraphSearch [[
--- Run a search. For more sourcegraph search syntax, refer to online documentation
---@command ]]
vim.api.nvim_create_user_command("SourcegraphSearch", function(args)
local input = nil
if args.args and #args.args > 0 then
input = args.args
end

require("sg.extensions.telescope").fuzzy_search_results { input = input }
end, {
desc = "Run a search on your connected Sourcegraph instance",
})
2 changes: 2 additions & 0 deletions doc/sg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ sg.config *sg.config*


Configuration Options: ~
{enable_cody} (boolean) Enable/disable cody
integration
{download_binaries} (boolean) Default true, download
latest release from
Github
Expand Down
12 changes: 12 additions & 0 deletions lua/sg/cody/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ M.message_callbacks = {}
---@param opts { force: boolean? }?
---@return VendoredPublicClient?
M.start = function(opts)
if not config.enable_cody then
return nil
end

opts = opts or {}

if M.client and not opts.force then
Expand Down Expand Up @@ -260,6 +264,10 @@ end

--- Shuts down the client by sending a shutdown request and waiting for completion.
M.shutdown = function()
if not M.client then
return
end

local done = false
M.client.request("shutdown", {}, function()
track { type = "shutdown" }
Expand All @@ -272,6 +280,10 @@ M.shutdown = function()
end

M.exit = function()
if not M.client then
return
end

M.notify("exit", {})

-- Force closing the connection.
Expand Down
3 changes: 3 additions & 0 deletions lua/sg/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
local types = require "sg.types"

---@class sg.config
---@field enable_cody boolean: Enable/disable cody integration
---@field download_binaries boolean: Default true, download latest release from Github
---@field node_executable string: path to node executable
---@field cody_agent string: path to the cody-agent js bundle
Expand All @@ -31,6 +32,8 @@ local types = require "sg.types"
local config = {}

config.download_binaries = true

config.enable_cody = true
config.node_executable = "node"
config.cody_agent = vim.api.nvim_get_runtime_file("dist/cody-agent.js", false)[1]

Expand Down
7 changes: 4 additions & 3 deletions lua/tests/cody_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- For some reason this doesn't always get loaded?...
vim.cmd [[runtime! plugin/cody-agent.lua]]
-- local augroup_cody = vim.api.nvim_create_augroup("augroup-cody", { clear = false })
vim.cmd [[runtime! after/plugin/cody.lua]]
vim.cmd [[runtime! after/plugin/cody-agent.lua]]

require("sg.cody.rpc").start()

require("plenary.async").tests.add_to_env()

Expand Down
120 changes: 0 additions & 120 deletions plugin/sg.lua
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
---@tag sg.commands

---@brief [[
--- Default commands for interacting with Sourcegraph
---@brief ]]

local void = require("plenary.async").void

---@command SourcegraphLogin [[
--- Get prompted for endpoint and access_token if you don't
--- want to set them via environment variables.
---@command ]]
vim.api.nvim_create_user_command("SourcegraphLogin", function()
local auth = require "sg.auth"

auth.set_nvim_auth()

vim.notify "[sg-cody] Changes will come into effect after a restart"
end, {
desc = "Login and store credentials for later use (an alternative to the environment variables",
})

---@command SourcegraphBuild [[
--- Rebuild the Sourcegraph crates and required dependencies (in case build failed during installation)
---@command ]]
vim.api.nvim_create_user_command("SourcegraphBuild", function()
local plugin_file = require("plenary.debug_utils").sourced_filepath()
local root = vim.fn.fnamemodify(plugin_file, ":h:h")
local build = require("sg.utils").joinpath(root, "build", "init.lua")
print "Starting sourcegraph build:"

require("sg.utils").system({ "nvim", "-l", build }, { cwd = root, text = true }, function(obj)
print(obj.stdout)
print(obj.stderr)
if obj.code ~= 0 then
error "Sourcegraph Build Failed. Check `:messages`"
else
print "Sourcegraph Build Success! Build log in `:messages`"
end
end)
end, {
desc = "Rebuild the Sourcegraph crates and required dependencies (in case build failed during installation)",
})

---@command SourcegraphDownloadBinaries [[
--- (Re-)Download the sourcegraph binaries. This should happen during installation
--- but you can force redownloading the binaries this way to ensure that sg.nvim
--- is properly installed.
---@command ]]
vim.api.nvim_create_user_command("SourcegraphDownloadBinaries", function()
require("sg.build").download()
end, {
desc = "(Re-)download the sourcegraph binaries",
})

if not require "sg.request" then
return
end
Expand All @@ -74,68 +19,3 @@ vim.api.nvim_create_autocmd("BufReadCmd", {
end,
desc = "Sourcegraph link and protocol handler",
})

vim.api.nvim_create_user_command("SourcegraphInfo", function()
print "[sg] Attempting to get sourcegraph info..."

void(function()
-- TODO: Would be nice to get the version of the plugin
print "[sg] making request"
local err, info = require("sg.rpc").get_info()
print(err, info)
if err or not info then
error "Could not get sourcegraph info"
end

local contents = vim.split(vim.inspect(info), "\n")

table.insert(contents, 1, "Sourcegraph info:")

vim.cmd.vnew()
vim.api.nvim_buf_set_lines(0, 0, -1, false, contents)
vim.api.nvim_buf_set_option(0, "buflisted", false)
vim.api.nvim_buf_set_option(0, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(0, "modifiable", false)
vim.api.nvim_buf_set_option(0, "modified", false)

vim.schedule(function()
print "[sg] got sourcegraph info. For more information, see `:checkhealth sg`"
end)
end)()
end, {})

---@command SourcegraphLink [[
--- Get a sourcegraph link to the current repo + file + line.
--- Automatically adds it to your '+' register
---@command ]]
vim.api.nvim_create_user_command("SourcegraphLink", function()
local cursor = vim.api.nvim_win_get_cursor(0)
void(function()
print "requesting link..."

local err, link = require("sg.rpc").get_link(vim.api.nvim_buf_get_name(0), cursor[1], cursor[2] + 1)
if err or not link then
print("Failed to get link:", link)
return
end

print("Setting '+' register to:", link)
vim.fn.setreg("+", link)
end)()
end, {
desc = "Get a sourcegraph link to the current location",
})

---@command SourcegraphSearch [[
--- Run a search. For more sourcegraph search syntax, refer to online documentation
---@command ]]
vim.api.nvim_create_user_command("SourcegraphSearch", function(args)
local input = nil
if args.args and #args.args > 0 then
input = args.args
end

require("sg.extensions.telescope").fuzzy_search_results { input = input }
end, {
desc = "Run a search on your connected Sourcegraph instance",
})
4 changes: 2 additions & 2 deletions scripts/gendocs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ local input_files = {
"./lua/sg/init.lua",
"./lua/sg/config.lua",
"./lua/sg/auth.lua",
"./plugin/cody.lua",
"./after/plugin/cody.lua",
"./lua/sg/cody/commands.lua",
"./plugin/sg.lua",
"./after/plugin/sg.lua",
"./lua/sg/rpc.lua",
"./lua/sg/extensions/cmp.lua",
}
Expand Down
6 changes: 4 additions & 2 deletions scripts/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ vim.opt.rtp:append { ".", "../plenary.nvim", "../tree-sitter-lua" }

vim.cmd [[runtime! plugin/plenary.vim]]
vim.cmd [[runtime! plugin/sg.lua]]
vim.cmd [[runtime! plugin/cody.lua]]
vim.cmd [[runtime! plugin/cody-agent.lua]]
vim.cmd [[runtime! after/plugin/sg.lua]]
vim.cmd [[runtime! after/plugin/cody.lua]]
vim.cmd [[runtime! after/plugin/cody-agent.lua]]

require("sg").setup {
download_binaries = false,
accept_tos = true,
enable_cody = true,
}

0 comments on commit 009feb0

Please sign in to comment.