-
Notifications
You must be signed in to change notification settings - Fork 126
Startup script repl #392
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
Open
mrsalo
wants to merge
7
commits into
neovim:master
Choose a base branch
from
mrsalo:startup_script_repl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Startup script repl #392
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bfe3ef4
add startup script for ipython and python repl
b7cc641
make nvim_startup.py Python2 compatible and Update connection messages
b66e967
update docstring of nvim_startup.py
df6478d
update README.md
15b8ee6
fix section link in README.md
8defc02
update README.md
7307d96
rename NVimAutoAttachException to NvimAutoAttachException
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" Startup script for IPython an Python REPL that will connect to a running | ||
NeoVim instance automatically or ask the user which NeoVim instance to connect | ||
to. After connection there is a variable called "nvim" that gives acccess to | ||
the endpoint. | ||
""" | ||
import atexit | ||
from glob import glob | ||
import pynvim | ||
|
||
|
||
class NVimAutoAttachException(Exception): | ||
'''Exception class that will be raised by this script''' | ||
|
||
|
||
def __let_user_choose_instance(nvim_instances): | ||
for idx, inst in enumerate(nvim_instances): | ||
with pynvim.attach('socket', path=inst) as nvim_endpoint: | ||
nvim_endpoint.command( | ||
'echon "I am Instance Nr {}: " $NVIM_LISTEN_ADDRESS'.format( | ||
idx | ||
) | ||
) | ||
|
||
for idx, inst in enumerate(nvim_instances): | ||
print('Instance Nr {}: {}'.format(idx, inst)) | ||
|
||
while True: | ||
try: | ||
print( | ||
'Which Nvim instance should I connect to? ' | ||
'(Look at the bottom of the vim instance you want to connect ' | ||
'to)' | ||
) | ||
inst_nr = int(input('Connect to Instance Nr: ')) | ||
chosen_inst = nvim_instances[inst_nr] | ||
break | ||
except (ValueError, IndexError): | ||
continue | ||
|
||
return chosen_inst | ||
|
||
|
||
def autoattach_to_nvim(glob_expr='/tmp/nvim*/0'): | ||
'''Returns a nvim endpoint if there is only one instance. | ||
If there are more instances, asks the user which one to pick. | ||
Raises NVimAutoAttachException if there are no NVim instances. | ||
''' | ||
nvim_instances = glob(glob_expr) | ||
if not nvim_instances: | ||
raise NVimAutoAttachException( | ||
'Could not find any running NVim instances.' | ||
) | ||
if len(nvim_instances) > 1: | ||
chosen_inst = __let_user_choose_instance(nvim_instances) | ||
else: | ||
chosen_inst = nvim_instances[0] | ||
return pynvim.attach('socket', path=chosen_inst) | ||
|
||
|
||
nvim = autoattach_to_nvim() | ||
nvim.command('echo "Connected to Python REPL."') | ||
atexit.register(lambda: nvim.command('echo "Python REPL disconnected."')) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.