Skip to content

Commit

Permalink
fix: return None for non file: URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
alcarney committed Oct 5, 2024
1 parent f7b6ba5 commit 7b09f31
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
5 changes: 5 additions & 0 deletions docs/source/howto/migrate-to-v2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Python Support

*pygls v2* removes support for Python 3.8 and adds support for Python 3.13 (with the GIL, you are welcome to try the free-threaded version just note that it has not been tested yet!)

URI Handling
------------

The :func:`pygls.uri.to_fs_path` will now return ``None`` for URIs that do not have a ``file:`` scheme.


Removed Deprecated Functions
----------------------------
Expand Down
10 changes: 7 additions & 3 deletions pygls/uris.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
https://github.com/Microsoft/vscode-uri/blob/e59cab84f5df6265aed18ae5f43552d3eef13bb9/lib/index.ts
"""
from __future__ import annotations

from typing import Optional, Tuple

import re
Expand Down Expand Up @@ -75,20 +77,22 @@ def from_fs_path(path: str):
return None


def to_fs_path(uri: str):
def to_fs_path(uri: str) -> str | None:
"""
Returns the filesystem path of the given URI.
Will handle UNC paths and normalize windows drive letters to lower-case.
Also uses the platform specific path separator. Will *not* validate the
path for invalid characters and semantics.
Will *not* look at the scheme of this URI.
"""
try:
# scheme://netloc/path;parameters?query#fragment
scheme, netloc, path, _, _, _ = urlparse(uri)

if netloc and path and scheme == "file":
if scheme != "file":
return None

if netloc and path:
# unc path: file://shares/c$/far/boo
value = f"//{netloc}{path}"

Expand Down
12 changes: 6 additions & 6 deletions pygls/workspace/text_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from lsprotocol import types

from pygls.uris import to_fs_path
from pygls.uris import urlparse
from .position_codec import PositionCodec

# TODO: this is not the best e.g. we capture numbers
Expand All @@ -47,7 +47,7 @@ def __init__(
):
self.uri = uri
self.version = version
path = to_fs_path(uri)
_, _, path, *_ = urlparse(uri)
if path is None:
raise Exception("`path` cannot be None")
self.path = path
Expand Down Expand Up @@ -177,10 +177,10 @@ def offset_at_position(self, client_position: types.Position) -> int:

@property
def source(self) -> str:
if self._source is None:
with io.open(self.path, "r", encoding="utf-8") as f:
return f.read()
return self._source
if self._source is None and self.path is not None:
return pathlib.Path(self.path).read_text(encoding="utf-8")

return self._source or ""

def word_at_position(
self,
Expand Down
16 changes: 9 additions & 7 deletions pygls/workspace/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ def __init__(
self._root_uri = root_uri
if self._root_uri is not None:
self._root_uri_scheme = uri_scheme(self._root_uri)
root_path = to_fs_path(self._root_uri)
if root_path is None:
raise Exception("Couldn't get `root_path` from `root_uri`")
self._root_path = root_path
self._root_path = to_fs_path(self._root_uri)
else:
self._root_path = None
self._sync_kind = sync_kind
Expand Down Expand Up @@ -151,9 +148,14 @@ def get_text_document(self, doc_uri: str) -> TextDocument:
return self._text_documents.get(doc_uri) or self._create_text_document(doc_uri)

def is_local(self):
return (
self._root_uri_scheme == "" or self._root_uri_scheme == "file"
) and os.path.exists(self._root_path)

if self._root_uri_scheme not in {"", "file"}:
return False

if (path := self._root_path) is None:
return False

return os.path.exists(path)

def put_notebook_document(self, params: types.DidOpenNotebookDocumentParams):
notebook = params.notebook_document
Expand Down

0 comments on commit 7b09f31

Please sign in to comment.