Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/migrating_to_lsp_plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ from LSP.plugin import uri_handler
def on_open_foo_uri(self, uri: DocumentUri, flags: sublime.NewFileFlags) -> Promise[sublime.Sheet | None]:
title, content, syntax = render_foo_uri(uri)
if session := self.weaksession():
return session.open_scratch_buffer(title, content, syntax, uri, None, flags)
return session.open_scratch_buffer(title, content, syntax, uri, None, flags).then(_: None)
return Promise.resolve(None)
```

Expand Down
19 changes: 9 additions & 10 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
from typing import Union
from typing_extensions import TypeAlias
from typing_extensions import TypeGuard
from urllib.parse import urldefrag
from weakref import WeakSet
import itertools
import mdpopups
Expand Down Expand Up @@ -1534,9 +1535,9 @@ def _open_uri_with_plugin_async(
group: int,
) -> Promise[sublime.View | None] | None:
# I cannot type-hint an unpacked tuple
pair: PackagedTask[tuple[str | None, str, str]] = Promise.packaged_task()
pair: PackagedTask[tuple[str, str, str]] = Promise.packaged_task()
# It'd be nice to have automatic tuple unpacking continuations
callback = lambda a, b, c: pair[1]((a, b, c)) # noqa: E731
callback = lambda a, b, c: pair[1]((a or 'Untitled', b, c)) # noqa: E731
Comment thread
jwortmann marked this conversation as resolved.
Outdated
if plugin.on_open_uri_async(uri, callback):
result: PackagedTask[sublime.View | None] = Promise.packaged_task()
pair[0].then(lambda tup: self.open_scratch_buffer(*tup, uri, r, flags, group)).then(result[1])
Expand All @@ -1545,21 +1546,18 @@ def _open_uri_with_plugin_async(

def open_scratch_buffer(
self,
title: str | None,
title: str,
content: str,
syntax: str,
uri: DocumentUri,
Comment thread
rchl marked this conversation as resolved.
Outdated
r: Range | None,
flags: sublime.NewFileFlags = sublime.NewFileFlags.NONE,
group: int = -1,
) -> Promise[sublime.View | None]:
task: PackagedTask[sublime.View | None] = Promise.packaged_task()
) -> Promise[sublime.View]:
task: PackagedTask[sublime.View] = Promise.packaged_task()
promise, resolve = task

def continue_on_main_thread() -> None:
if title is None:
resolve(None)
return
if group > -1:
self.window.focus_group(group)
view = self.window.new_file(syntax=syntax, flags=flags)
Expand All @@ -1569,7 +1567,7 @@ def continue_on_main_thread() -> None:
view.set_name(title)
view.run_command("append", {"characters": content})
view.set_read_only(True)
self._on_sheet_opened(view.sheet(), uri, r).then(resolve)
self._on_sheet_opened(view.sheet(), uri, r).then(lambda _: resolve(view))

sublime.set_timeout(continue_on_main_thread)
return promise
Expand All @@ -1578,7 +1576,8 @@ def _on_sheet_opened(
self, sheet: sublime.Sheet | None, uri: DocumentUri, r: Range | None
) -> Promise[sublime.View | None]:
if sheet and (view := sheet.view()):
view.settings().set('lsp_uri', uri) # Preserve original URI given by the language server
uri_no_fragment = urldefrag(uri).url
view.settings().set('lsp_uri', uri_no_fragment)
Comment thread
rchl marked this conversation as resolved.
if r:
center_selection(view, r)
return Promise.resolve(view)
Expand Down
Loading