Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Uri/Path operations for Windows. #274

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
21 changes: 15 additions & 6 deletions snooty/language_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import os
import sys
import threading
import urllib.parse
from urllib.parse import urlparse, unquote
from urllib.request import url2pathname
from collections import defaultdict
from dataclasses import dataclass
from functools import wraps
Expand Down Expand Up @@ -313,27 +314,27 @@ def uri_to_fileid(self, uri: Uri) -> FileId:
if not self.project:
raise TypeError("Cannot map uri to fileid before a project is open")

parsed = urllib.parse.urlparse(uri)
parsed = urlparse(uri)
if parsed.scheme != "file":
raise ValueError("Only file:// URIs may be resolved", uri)

path = Path(parsed.netloc).joinpath(Path(parsed.path)).resolve()
path = self.uriToPath(uri)
return self.project.config.get_fileid(path)

def fileid_to_uri(self, fileid: FileId) -> str:
if not self.project:
raise TypeError("Cannot map fileid to uri before a project is open")

return "file://" + str(self.project.config.source_path.joinpath(fileid))
return self.project.config.source_path.joinpath(fileid).as_uri()

def m_initialize(
self,
processId: Optional[int] = None,
rootUri: Optional[Uri] = None,
**kwargs: object,
) -> SerializableType:
if rootUri:
root_path = Path(rootUri.replace("file://", "", 1))
if rootUri:
root_path = self.uriToPath(rootUri)
self.project = Project(root_path, self.backend, {})
self.notify_diagnostics()

Expand Down Expand Up @@ -363,6 +364,14 @@ def watch_parent_process(pid: int) -> None:

return {"capabilities": {"textDocumentSync": 1}}

@staticmethod
def uriToPath(uri: Uri) -> Path:
parsed = urlparse(uri)
host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc)
return Path(os.path.abspath(
os.path.join(host, url2pathname(unquote(parsed.path)))
))

def m_initialized(self, **kwargs: object) -> None:
# Ignore this message to avoid logging a pointless warning
pass
Expand Down