alefpereira / pyenv-pyright

Setup a pyenv virtualenv for microsoft pyright in the pyrightconfig.json config file
MIT License
136 stars 10 forks source link

Automatically run the command if pyrightconfig.json not detected #2

Closed zippeurfou closed 1 year ago

zippeurfou commented 1 year ago

Thanks for the plugin @alefpereira this really helps use pyenv with neovim. I am not really sure how other people do it without it... it would be nice to have a nvim plugin that does the command for us if the file does not exist before loading LPS. Something along the line of:

if os.getenv("VIRTUAL_ENV") and pyrightconfig.json does not exist in cwd:
 exec pyright pyright
 reload LSP
alefpereira commented 1 year ago

Thanks for the plugin @alefpereira this really helps use pyenv with neovim. I am not really sure how other people do it without it...

Hi @zippeurfou, thank you for your message, I'm really glad this plugin is helping you.

it would be nice to have a nvim plugin that does the command for us if the file does not exist before loading LPS

This is interesting, sure a good nvim plugin. I don't think right now I could help with a nvim plugin, but maybe you could use a workaround for this. In my setup I have to do some strange stuffs before running nvim so in my shell this command is a function that calls the real nvim.

One of the things I do here is to run nvim inside the poetry venv if it is not activated yet. The solution may be ugly and it IS very ugly, but I got used to it and it actually gave me a lot of flexibility in my setup to also do other stuff more easily.

This is more or less the poetry part of my script, if you feel doing so, you can change it to run pyenv pyright before calling nvim ( also I use fish but you can easily translate to bash):

Main function (use alias nvim=nvimvenv to keep the name):

# Autosource virtualenv; Workarround for nvim

function nvimvenv
  if test ! -e "$VIRTUAL_ENV" and test -n "$(upfind pyproject.toml)" # if $VIRTUAL_ENV not set and has pyproject.toml
    poetry run nvim $argv
    return
  end
  command nvim $argv # Run nvim program, ignore functions, builtins and aliases
end;
Upfind function (in case you need it to search for the pyright config file upwards): # Find file upwards function upfind set CURRENT "$(pwd)" while true find $CURRENT -maxdepth 1 -name "$argv[1]" if [ $CURRENT = "/" ] break end set CURRENT $(dirname "$CURRENT") end end;

Let me know if this helped you. BR.

zippeurfou commented 1 year ago

Here is the script that allows it to happen:

#!/bin/bash
function nvim {
 # If I have VIRTUAL_ENV and a local pyenv (to avoid creating pyrightconfig.json everywhere) but not the pyrightconfig.json create it
  if test -e "$VIRTUAL_ENV" && test -f .python-version && test ! -f pyrightconfig.json; then
    echo "VIRTUAL_ENV detected adding pyrightconfig.json"
    sleep 2
    pyenv pyright
  fi
  command nvim "$@"
}

Feel free to add it in the readme as a FAQ maybe. Closing this one.