N0tTheBees

joined 2 years ago
[–] N0tTheBees@sh.itjust.works 3 points 2 weeks ago

Spite really is the best motivator

 

Preview


What

So I really needed my pylsp to use a different pyenv install, and just want to share my basic solution in hopes it'll help someone else.

The basic gist of it is that I use vim.system() to get the version paths through pyenv:

pyenv root
pyenv versions --bare --skip-aliases

and create a telescope picker for selection.

Then I use vim.lsp.client.notify() with the workspace/didChangeConfiguration lsp method to change pylsp's settings (for which options can be found here).

Code

local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local M = {}

M.get_pyenv_root = function()
    if not vim.fn.executable('pyenv') then
        vim.notify("pyenv executable not in path.", vim.log.levels.WARN)
        return nil
    end

    local root_cmd = vim.system({
        'pyenv',
        'root',
    }):wait()

    if root_cmd.code ~= 0 then
        vim.notify('"pyenv root" command returned non zero exit status.', vim.log.levels.WARN)
        return nil
    end

    local pyenv_root = string.gsub(root_cmd.stdout, '%s+', '')
    local pyenv_versions_path = vim.fs.joinpath(pyenv_root, '/versions')

    if not vim.fn.isdirectory(pyenv_versions_path) then
        vim.notify("Failed to find pyenv versions directory, '" .. pyenv_versions_path .. "'.", vim.log.levels.WARN)
        return nil
    end

    return pyenv_versions_path
end

M.get_pyenv_version_paths = function()
    if not vim.fn.executable('pyenv') then
        vim.notify("pyenv executable not in path.", vim.log.levels.WARN)
        return nil
    end

    local cmd = {
        'pyenv',
        'versions',
        '--bare',
        '--skip-aliases'
    }

    local versions_cmd = vim.system(cmd):wait()
    if versions_cmd.code ~= 0 then
        vim.notify('command "' .. vim.inspect(cmd) .. '" returned non zero exit status.', vim.log.levels.WARN)
        return nil
    end

    local versions = vim.fn.split(versions_cmd.stdout, '\n')
    return versions
end

M.get_pyenv_results = function()
    local pyenv_versions_root = M.get_pyenv_root()
    local results = {}

    if pyenv_versions_root == nil then
        return nil
    end

    local pyenv_versions = M.get_pyenv_version_paths()
    if pyenv_versions == nil then
        return nil
    end

    for _, version_path in ipairs(pyenv_versions) do
        table.insert(results, {
            version_path,
            vim.fs.joinpath(pyenv_versions_root, version_path),
        })
    end

    return results
end

M.set_pylsp_environment = function(python_env)
    local pylsp_clients = vim.lsp.get_clients({ name = "pylsp" })
    if pylsp_clients == nil or next(pylsp_clients) == nil then
        vim.notify("No Pylsp clients found.", vim.log.levels.WARN)
        return
    end

    local clients = 0
    for _, client in ipairs(pylsp_clients) do
        client.notify("workspace/didChangeConfiguration", {
            settings = {
                pylsp = {
                    plugins = {
                        jedi = {
                            environment = python_env
                        }
                    }
                }
            }
        })
        clients = clients + 1
    end
    vim.notify("Set python environment to '" .. python_env .. "' for " .. clients .. " pylsp clients.")
end

M.pyenvs = function(opts)
    opts = opts or {}
    local version_results = M.get_pyenv_results()
    if version_results == nil then
        return
    end

    pickers.new(opts, {
        prompt_title = "pyenvs",
        finder = finders.new_table({
            results = version_results,
            entry_maker = function(entry)
                return {
                    value = entry,
                    display = entry[1],
                    ordinal = entry[1],
                }
            end,
        }),
        attach_mappings = function(prompt_bufnr, _)
            actions.select_default:replace(function()
                actions.close(prompt_bufnr)
                local selection = action_state.get_selected_entry()
                M.set_pylsp_environment(selection.value[2])
            end)
            return true
        end,
        sorter = conf.generic_sorter(opts)
    }):find()
end

return M

And now you can easily open the picker with the M.pyenvs() function.

Also nice way to create the keymap for it is to wait for the LspAttach autocmd (which fires when an LSP is attached to a buffer) to fire and only create the keymap on the buffer that pylsp is attached to like so:

(don't forget to change the '<module_name>')

vim.api.nvim_create_autocmd("LspAttach", {
    callback = function(args)
        local client = vim.lsp.get_client_by_id(args.data.client_id)
        if client and client.name == "pylsp" then
            vim.keymap.set("n", "<leader>le", function()
                local pylsp_picker = require("<module_name>")
                pylsp_picker.pyenvs()
            end, {
                desc = "[e]nvironment (python)",
                buffer = args.buf
            })
        end
    end
})
 

Dark Math Films just released the first episode of their series SCP Academy and it’s a banger, go show em some love!

[–] N0tTheBees@sh.itjust.works 1 points 3 months ago

Plus it also means that the government has an actual incentive to keep the insurance companies honest

[–] N0tTheBees@sh.itjust.works 5 points 3 months ago (2 children)

Well yeah but percentage of GDP is just the total spent. The point is that the USA relies primarily on employers paying for the insurance (through a pay cut) whereas in the EU it is generally subsidised with taxes. Which, if you tax fairly, means that the cost of healthcare is better for the average worker (e.g more based on how much any individual earns)

[–] N0tTheBees@sh.itjust.works 7 points 6 months ago

Honestly the French would probably still have a go at it.

[–] N0tTheBees@sh.itjust.works 29 points 7 months ago (2 children)

If they made it malicious, we probably wouldn’t have noticed though

[–] N0tTheBees@sh.itjust.works 28 points 8 months ago* (last edited 8 months ago) (1 children)

:o=3

:o==3

:oc==3

:D c==3

[–] N0tTheBees@sh.itjust.works 7 points 9 months ago

Am I weird for liking it better when people call me? Usually takes less time and effort to discuss something over the phone imo

[–] N0tTheBees@sh.itjust.works 1 points 10 months ago

Ah an avid dreambird user I see

 

This man has spend the past 30+ years perfecting a machine to create beautiful fractals through HD video feedback

https://sh.itjust.works/pictrs/image/edd0a9a9-b07f-46b6-9b44-06ee1b8f2eb3.gif

[–] N0tTheBees@sh.itjust.works 4 points 10 months ago

Hard men make sore buttholes

[–] N0tTheBees@sh.itjust.works 5 points 11 months ago

Can confirm I personally know the guy that hosts 127.0.0.1, total asshole.

 
view more: next ›