Skip to content

Commit

Permalink
lsp: Pass uri and line number between client and webview
Browse files Browse the repository at this point in the history
This aligns the language server with recent changes to enable
synchronised scrolling across multiple source files.

Additionaly, rather than send a custom `editor/scroll` notification to
the client, the server will now send a `window/showDocument` request
taking advantage of the `selection` parameter.

This *in theory* should reduce the amount of integration work required
to enable sync scrolling in new clients.
  • Loading branch information
alcarney committed Jul 11, 2024
1 parent 2fb0b46 commit db7de68
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/esbonio/changes/784.enhancement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Synchronised scrolling now works with files that have been `.. included::`, as well as autodoc docstrings
1 change: 1 addition & 0 deletions lib/esbonio/changes/786.enhancement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The resolution of sync scrolling has been improved with the webview also better handling the case where the requested line does not exactly match a known source location
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ async def on_build(self, client: SphinxClient, result):
self.logger.debug("Refreshing preview")
self.webview.reload()

async def scroll_view(self, line: int):
async def scroll_view(self, uri: str, line: int):
"""Scroll the webview to the given line number."""

if self.webview is None:
return

self.webview.scroll(line)
self.webview.scroll(uri, line)

async def preview_file(self, params, retry=True):
if self.preview is None:
Expand Down Expand Up @@ -224,7 +224,7 @@ def esbonio_setup(

@esbonio.feature("view/scroll")
async def on_scroll(ls: server.EsbonioLanguageServer, params):
await manager.scroll_view(params.line)
await manager.scroll_view(params.uri, params.line)

@esbonio.command("esbonio.server.previewFile")
async def preview_file(ls: server.EsbonioLanguageServer, *args):
Expand Down
21 changes: 18 additions & 3 deletions lib/esbonio/esbonio/server/features/preview_manager/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket
import typing

from lsprotocol import types
from pygls.protocol import JsonRPCProtocol
from pygls.protocol import default_converter
from pygls.server import Server
Expand Down Expand Up @@ -52,6 +53,9 @@ def __init__(self, logger: logging.Logger, config: PreviewConfig, *args, **kwarg
self._view_in_control: Optional[asyncio.Task] = None
"""If set, the view is in control and the editor should not emit scroll events"""

self._current_uri: Optional[str] = None
"""If set, indicates the current uri the editor and view are scrolling."""

def __await__(self):
"""Makes the server await-able"""
if self._startup_task is None:
Expand Down Expand Up @@ -80,7 +84,7 @@ def reload(self):
if self.connected:
self.lsp.notify("view/reload", {})

def scroll(self, line: int):
def scroll(self, uri: str, line: int):
"""Called by the editor to scroll the current webview."""
if not self.connected or self._view_in_control:
return
Expand All @@ -89,8 +93,9 @@ def scroll(self, line: int):
if self._editor_in_control:
self._editor_in_control.cancel()

self._current_uri = uri
self._editor_in_control = asyncio.create_task(self.cooldown("editor"))
self.lsp.notify("view/scroll", {"line": line})
self.lsp.notify("view/scroll", {"uri": uri, "line": line})

async def cooldown(self, name: str):
"""Create a cooldown."""
Expand Down Expand Up @@ -164,6 +169,16 @@ def on_scroll(ls: WebviewServer, params):
server._view_in_control.cancel()

server._view_in_control = asyncio.create_task(server.cooldown("view"))
esbonio.lsp.notify("editor/scroll", dict(line=params.line))

esbonio.lsp.show_document(
types.ShowDocumentParams(
uri=params.uri,
external=False,
selection=types.Range(
start=types.Position(line=params.line - 1, character=0),
end=types.Position(line=params.line, character=0),
),
)
)

return server

0 comments on commit db7de68

Please sign in to comment.