neovim / pynvim

Python client and plugin host for Nvim
http://pynvim.readthedocs.io/en/latest/
Apache License 2.0
1.47k stars 118 forks source link

Access output of lua commands #561

Closed m-anish-gupta closed 4 months ago

m-anish-gupta commented 4 months ago

I am new to pynvim and I'm trying to replicate the use of LSP related commands using pynvim but I'm not able to determine how to get it to return the outputs of each commands. Below is a small repro for anyone to use:

I start a nvim instance using NVIM_LISTEN_ADDRESS=/tmp/nvim nvim in a different terminal

import pynvim

# Create a python API session attached to unix domain socket created above:
nvim = pynvim.attach('socket', path='/tmp/nvim')

nvim.chdir("root-path-to-my-system/oraladder") # the github repo is: https://github.com/ubitux/oraladder
# We clear the buffer and load a new file in it.
nvim.command("bd!")
nvim.command("e laddertools/test_glicko.py")
print(nvim.current.buffer[:])  # --> this works perfectly

nvim.current.window.cursor = (17, 17)
print(f"Cursor is at: {nvim.current.window.cursor}")
print(f"Current line: {nvim.current.line}")

nvim.exec_lua("vim.lsp.buf.definition()", async_=True)  
# after this I should get some indication that the symbol was resolved but the cursor and line doesn't change when I try to print 
# them but when I go and see the attached UI, I can notice that it has resolved the reference to the correct file.
print(f"Cursor is at: {nvim.current.window.cursor}")
print(f"Current line: {nvim.current.line}")
print(f"The buffer becomes: {nvim.current.buffer[:]}")

Can anyone help as to how I can get these outputs from pynvim?

justinmk commented 4 months ago
nvim.exec_lua("vim.lsp.buf.definition()", async_=True)  
# after this I should get some indication that the symbol was resolved but the cursor and line doesn't change when I try to print 
# them but when I go and see the attached UI, I can notice that it has resolved the reference to the correct file.
print(f"Cursor is at: {nvim.current.window.cursor}")

You specified async_=True, so of course the behavior won't be synchronous.

m-anish-gupta commented 4 months ago

Ah, sorry for that. I used async_ = False only but I was observing the same behaviour

m-anish-gupta commented 4 months ago

Here's the output log for reference:

['import pytest', 'from .rankings.glicko import RankingGlicko, _RatingGlicko', '', 'import numpy', '', '', 'def test_glicko():', '    # Based on example from https://www.glicko.net/glicko/glicko2.pdf', '    me = _RatingGlicko(1500, 200, .06)', '    opponents = [', '        _RatingGlicko(1400, 30, .06),', '        _RatingGlicko(1550, 100, .06),', '        _RatingGlicko(1700, 300, .06),', '    ]', '    outcomes = [1, 0, 0]  # I won, then lost two', '', '    new_rating = RankingGlicko.compute_new_rating(', '        me, opponents, outcomes, tau=0.5, eps=1e-6', '    )', '', '    numpy.testing.assert_almost_equal(1464.06, new_rating.r, decimal=2)', '    numpy.testing.assert_almost_equal(151.52, new_rating.RD, decimal=2)', '    numpy.testing.assert_almost_equal(0.05999, new_rating.std, decimal=5)', '', '', 'def test_glicko_empty_period_will_increase_RD():', '    me = _RatingGlicko(1500, 345, std=1.0)', '    opponents = outcomes = []', '    new_rating = RankingGlicko.compute_new_rating(me, opponents, outcomes)', '    assert new_rating.RD == 350', '    assert new_rating.r == me.r  # should remain unchanged... Only increase RD']
Cursor is at: (17, 17)
Current line:     new_rating = RankingGlicko.compute_new_rating(
Cursor is at: (17, 17)
Current line:     new_rating = RankingGlicko.compute_new_rating(
The buffer becomes: ['import pytest', 'from .rankings.glicko import RankingGlicko, _RatingGlicko', '', 'import numpy', '', '', 'def test_glicko():', '    # Based on example from https://www.glicko.net/glicko/glicko2.pdf', '    me = _RatingGlicko(1500, 200, .06)', '    opponents = [', '        _RatingGlicko(1400, 30, .06),', '        _RatingGlicko(1550, 100, .06),', '        _RatingGlicko(1700, 300, .06),', '    ]', '    outcomes = [1, 0, 0]  # I won, then lost two', '', '    new_rating = RankingGlicko.compute_new_rating(', '        me, opponents, outcomes, tau=0.5, eps=1e-6', '    )', '', '    numpy.testing.assert_almost_equal(1464.06, new_rating.r, decimal=2)', '    numpy.testing.assert_almost_equal(151.52, new_rating.RD, decimal=2)', '    numpy.testing.assert_almost_equal(0.05999, new_rating.std, decimal=5)', '', '', 'def test_glicko_empty_period_will_increase_RD():', '    me = _RatingGlicko(1500, 345, std=1.0)', '    opponents = outcomes = []', '    new_rating = RankingGlicko.compute_new_rating(me, opponents, outcomes)', '    assert new_rating.RD == 350', '    assert new_rating.r == me.r  # should remain unchanged... Only increase RD']

After the invocation too the buffer and the cursor positions dont change in the sdk whereas in the connected UI I can see the new file opened and the cursor is at the resolved class reference.