Skip to content

Commit eb8a178

Browse files
authored
refactor: replace nvim_out_write/nvim_err_write with nvim_echo #620
Problem: nvim_out_write and nvim_err_write are deprecated since Nvim 0.11. Solution: - Replace internal usages `nvim_echo`. Use `ErrorMsg` highlight group instead of `opts.err`, for back-compat with older Nvim. - Mark out_write() and err_write() as deprecated. Close #588
1 parent 99236bf commit eb8a178

File tree

6 files changed

+20
-10
lines changed

6 files changed

+20
-10
lines changed

docs/usage/remote-plugins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Rplugins follow the structure of this example:
3030
3131
@pynvim.autocmd('BufEnter', pattern='*.py', eval='expand("<afile>")', sync=True)
3232
def on_bufenter(self, filename):
33-
self.nvim.out_write('testplugin is in ' + filename + '\n')
33+
self.nvim.api.echo([['testplugin is in ' + filename]], True, {})
3434
3535
If ``sync=True`` is supplied Neovim will wait for the handler to finish
3636
(this is required for function return values),

pynvim/api/nvim.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from typing import (Any, AnyStr, Callable, Dict, Iterator, List, Literal, Optional,
1313
TYPE_CHECKING, Union)
1414

15+
if sys.version_info >= (3, 13):
16+
from warnings import deprecated
17+
else:
18+
from typing_extensions import deprecated
19+
1520
from msgpack import ExtType
1621

1722
from pynvim.api.buffer import Buffer
@@ -177,12 +182,12 @@ def request(self, name: str, *args: Any, **kwargs: Any) -> Any:
177182
functions have python wrapper functions. The `api` object can
178183
be also be used to call API functions as methods:
179184
180-
vim.api.err_write('ERROR\n', async_=True)
185+
vim.api.echo([['ERROR', 'ErrorMsg']], True, {}, async_=True)
181186
vim.current.buffer.api.get_mark('.')
182187
183188
is equivalent to
184189
185-
vim.request('nvim_err_write', 'ERROR\n', async_=True)
190+
vim.request('nvim_echo', [['ERROR', 'ErrorMsg']], True, {}, async_=True)
186191
vim.request('nvim_buf_get_mark', vim.current.buffer, '.')
187192
188193
@@ -417,13 +422,15 @@ def replace_termcodes(
417422
return self.request('nvim_replace_termcodes', string,
418423
from_part, do_lt, special)
419424

425+
@deprecated('Use nvim.api.echo() instead.')
420426
def out_write(self, msg: str, **kwargs: Any) -> None:
421427
r"""Print `msg` as a normal message.
422428
423429
The message is buffered (won't display) until linefeed ("\n").
424430
"""
425431
return self.request('nvim_out_write', msg, **kwargs)
426432

433+
@deprecated('Use nvim.api.echo() instead.')
427434
def err_write(self, msg: str, **kwargs: Any) -> None:
428435
r"""Print `msg` as an error message.
429436

pynvim/plugin/host.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def __init__(self, nvim: Nvim):
6868

6969
def _on_async_err(self, msg: str) -> None:
7070
# uncaught python exception
71-
self.nvim.err_write(msg, async_=True)
71+
self.nvim.request('nvim_echo', [[msg, 'ErrorMsg']], True, {}, async_=True)
7272

7373
def _on_error_event(self, kind: Any, msg: str) -> None:
7474
# error from nvim due to async request
7575
# like nvim.command(..., async_=True)
76-
errmsg = "{}: Async request caused an error:\n{}\n".format(
76+
errmsg = "{}: Async request caused an error:\n{}".format(
7777
self.name, decode_if_bytes(msg))
78-
self.nvim.err_write(errmsg, async_=True)
78+
self.nvim.request('nvim_echo', [[errmsg, 'ErrorMsg']], True, {}, async_=True)
7979
return errmsg
8080

8181
def start(self, plugins):
@@ -279,7 +279,7 @@ def _on_specs_request(self, path):
279279
path = decode_if_bytes(path)
280280
path = pathlib.Path(os.path.normpath(path)).as_posix() # normalize path
281281
if path in self._load_errors:
282-
self.nvim.out_write(self._load_errors[path] + '\n')
282+
self.nvim.request('nvim_echo', [[self._load_errors[path]]], True, {})
283283
return self._specs.get(path, 0)
284284

285285
def _configure_nvim_for(self, obj):

pynvim/plugin/script_host.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def setup(self, nvim):
6464
info('redirect sys.stdout and sys.stderr')
6565
self.saved_stdout = sys.stdout
6666
self.saved_stderr = sys.stderr
67+
# TODO(justinmk): out_write, err_write are deprecated, but
68+
# script_host.py itself is deprecated by:
69+
# https://github.com/neovim/pynvim/issues/567
6770
sys.stdout = RedirectStream(lambda data: nvim.out_write(data))
6871
sys.stderr = RedirectStream(lambda data: nvim.err_write(data))
6972

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ requires-python = ">=3.10"
1515
dependencies = [
1616
"msgpack>=1.0.0",
1717
"greenlet>=3.0; python_implementation != 'PyPy'",
18-
"typing-extensions>=4.5; python_version < '3.12'",
18+
"typing-extensions>=4.5; python_version < '3.13'",
1919
]
2020

2121
classifiers = [

test/test_host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ def test_host_async_error(vim):
5454
vim.command("lolwut", async_=True)
5555
event = vim.next_message()
5656
assert event[1] == 'nvim_error_event'
57-
assert 'rplugin-host: Async request caused an error:\nboom\n' \
58-
in h._on_error_event(None, 'boom')
57+
errmsg = h._on_error_event(None, 'boom')
58+
assert 'Async request caused an error:\nboom' in errmsg

0 commit comments

Comments
 (0)