Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve use of lsp hooks in user configuration options #186

Open
wants to merge 3 commits into
base: regexp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lua/venv-selector/cached_venv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ function M.retrieve()
if venv_cache ~= nil and venv_cache[vim.fn.getcwd()] ~= nil then
local venv = require("venv-selector.venv")
local venv_info = venv_cache[vim.fn.getcwd()]
if venv_info.value == path.current_python_path then
return
end

log.debug("Activating venv `" .. venv_info.value .. "` from cache.")
venv.activate(venv_info.value, venv_info.type, false)
Expand Down
3 changes: 1 addition & 2 deletions lua/venv-selector/config.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local hooks = require("venv-selector.hooks")
local log = require("venv-selector.logger")

local M = {}
Expand Down Expand Up @@ -177,7 +176,7 @@ M.default_settings = {
cache = {
file = "~/.cache/venv-selector/venvs2.json",
},
hooks = { hooks.basedpyright_hook, hooks.pyright_hook, hooks.pylance_hook, hooks.pylsp_hook },
lsp_hooks = { basedpyright = true, pyright = true, pylance = true, pylsp = true },
options = {
on_venv_activate_callback = nil, -- callback function for after a venv activates
enable_default_searches = true, -- switches all default searches on/off
Expand Down
14 changes: 12 additions & 2 deletions lua/venv-selector/hooks.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local config = require("venv-selector.config")
local log = require("venv-selector.logger")

local M = {}
Expand Down Expand Up @@ -29,7 +30,6 @@ function M.set_python_path_for_client(client_name, venv_python)
return
end

local config = require("venv-selector.config")
if client.settings then
client.settings = vim.tbl_deep_extend("force", client.settings, {
python = {
Expand Down Expand Up @@ -67,7 +67,6 @@ end
function M.pylsp_hook(venv_python)
local client_name = "pylsp"
return M.execute_for_client(client_name, function(client)
local config = require("venv-selector.config")
local settings = vim.tbl_deep_extend("force", (client.settings or client.config.settings), {
pylsp = {
plugins = {
Expand All @@ -89,6 +88,17 @@ function M.pylsp_hook(venv_python)
end)
end

function M.notify_lsp(venv_python)
local count = 0
local hooks = config.user_settings.lsp_hooks
for name, state in pairs(hooks) do
if state then
count = count + M[name .. "_hook"](venv_python)
end
end
return count
end

function M.execute_for_client(name, callback)
-- get_active_clients deprecated in neovim v0.10
local client = (vim.lsp.get_clients or vim.lsp.get_active_clients)({ name = name })[1]
Expand Down
7 changes: 5 additions & 2 deletions lua/venv-selector/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ local venv = require("venv-selector.venv")
local path = require("venv-selector.path")
local ws = require("venv-selector.workspace")

local function on_lsp_attach()
local function on_lsp_attach(args)
if vim.bo.filetype == "python" then
local cache = require("venv-selector.cached_venv")
if config.user_settings.options.cached_venv_automatic_activation == true then
cache.retrieve()
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client and config.user_settings.lsp_hooks[client.name] then
cache.retrieve()
end
end
end
end
Expand Down
9 changes: 0 additions & 9 deletions lua/venv-selector/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ function M.table_has_content(t)
return next(t) ~= nil
end

function M.merge_user_settings(user_settings)
log.debug("User plugin settings: ", user_settings.settings, "")
M.user_settings = vim.tbl_deep_extend("force", M.default_settings, user_settings.settings)
M.user_settings.detected = {
system = vim.loop.os_uname().sysname,
}
log.debug("Complete user settings:", M.user_settings, "")
end

-- split a string
function M.split_string(str)
local result = {}
Expand Down
12 changes: 3 additions & 9 deletions lua/venv-selector/venv.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
local path = require("venv-selector.path")
local config = require("venv-selector.config")
local hooks = require("venv-selector.hooks")
local log = require("venv-selector.logger")

local M = {}

M.current_source = nil -- contains the name of the search, like anaconda, pipx etc.

function M.stop_lsp_servers()
local hooks = require("venv-selector.config").user_settings.hooks
for _, hook in pairs(hooks) do
hook(nil)
end
hooks.notify_lsp(nil)
end

function M.set_source(source)
Expand Down Expand Up @@ -43,11 +41,7 @@ function M.activate(python_path, type, check_lsp)
path.current_venv_path = path.get_base(python_path)

-- Inform lsp servers
local count = 0
local hooks = require("venv-selector.config").user_settings.hooks
for _, hook in pairs(hooks) do
count = count + hook(python_path)
end
local count = hooks.notify_lsp(python_path)

if check_lsp and count == 0 and config.user_settings.options.require_lsp_activation == true then
local message =
Expand Down