Skip to content

Commit

Permalink
Workaround file URI matching issue for now
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed May 16, 2021
1 parent 309b3fa commit f3e7288
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..virtual_documents_shadow import (
EditableFile,
ShadowFilesystemError,
_strip_file_protocol_prefix,
extract_or_none,
setup_shadow_filesystem,
)
Expand Down Expand Up @@ -111,6 +112,12 @@ async def test_shadow(shadow_path, message_func, content, expected_content):
assert f.read() == expected_content


def test_strip_file_prefix():
assert _strip_file_protocol_prefix("file:///test") == "test"
assert _strip_file_protocol_prefix("file://test") == "test"
assert _strip_file_protocol_prefix("http://test") == "http://test"


@pytest.mark.asyncio
async def test_shadow_failures(shadow_path):

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ class ShadowFilesystemError(ValueError):
"""Error in the shadow file system."""


# TODO: remove once https://github.com/unshiftio/url-parse/pull/204 gets merged and released
def _strip_file_protocol_prefix(uri):
if uri.startswith("file:///"):
return uri[8:]
if uri.startswith("file://"):
return uri[7:]
return uri


def setup_shadow_filesystem(virtual_documents_uri):

if not virtual_documents_uri.startswith("file:/"):
Expand All @@ -117,6 +126,8 @@ def setup_shadow_filesystem(virtual_documents_uri):
# create again
shadow_filesystem.mkdir(parents=True, exist_ok=True)

stripped_virtual_documents_uri = _strip_file_protocol_prefix(virtual_documents_uri)

@lsp_message_listener("client")
async def shadow_virtual_documents(scope, message, language_server, manager):
"""Intercept a message with document contents creating a shadow file for it.
Expand All @@ -138,7 +149,9 @@ async def shadow_virtual_documents(scope, message, language_server, manager):
if not uri:
raise ShadowFilesystemError("Could not get URI from: {}".format(message))

if not uri.startswith(virtual_documents_uri):
if not _strip_file_protocol_prefix(uri).startswith(
stripped_virtual_documents_uri
):
return

path = file_uri_to_path(uri)
Expand Down

0 comments on commit f3e7288

Please sign in to comment.