Skip to content

Location.relativePath to use None value if rel path is not available #85 #86

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

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
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
16 changes: 4 additions & 12 deletions src/multilspy/language_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ async def request_definition(
new_item: multilspy_types.Location = {}
new_item.update(item)
new_item["absolutePath"] = PathUtils.uri_to_path(new_item["uri"])
new_item["relativePath"] = str(
PurePath(os.path.relpath(new_item["absolutePath"], self.repository_root_path))
)
new_item["relativePath"] = PathUtils.get_relative_path(new_item["absolutePath"], self.repository_root_path)
ret.append(multilspy_types.Location(new_item))
elif (
LSPConstants.ORIGIN_SELECTION_RANGE in item
Expand All @@ -413,9 +411,7 @@ async def request_definition(
new_item: multilspy_types.Location = {}
new_item["uri"] = item[LSPConstants.TARGET_URI]
new_item["absolutePath"] = PathUtils.uri_to_path(new_item["uri"])
new_item["relativePath"] = str(
PurePath(os.path.relpath(new_item["absolutePath"], self.repository_root_path))
)
new_item["relativePath"] = PathUtils.get_relative_path(new_item["absolutePath"], self.repository_root_path)
new_item["range"] = item[LSPConstants.TARGET_SELECTION_RANGE]
ret.append(multilspy_types.Location(**new_item))
else:
Expand All @@ -428,9 +424,7 @@ async def request_definition(
new_item: multilspy_types.Location = {}
new_item.update(response)
new_item["absolutePath"] = PathUtils.uri_to_path(new_item["uri"])
new_item["relativePath"] = str(
PurePath(os.path.relpath(new_item["absolutePath"], self.repository_root_path))
)
new_item["relativePath"] = PathUtils.get_relative_path(new_item["absolutePath"], self.repository_root_path)
ret.append(multilspy_types.Location(**new_item))
else:
assert False, f"Unexpected response from Language Server: {response}"
Expand Down Expand Up @@ -480,9 +474,7 @@ async def request_references(
new_item: multilspy_types.Location = {}
new_item.update(item)
new_item["absolutePath"] = PathUtils.uri_to_path(new_item["uri"])
new_item["relativePath"] = str(
PurePath(os.path.relpath(new_item["absolutePath"], self.repository_root_path))
)
new_item["relativePath"] = PathUtils.get_relative_path(new_item["absolutePath"], self.repository_root_path)
ret.append(multilspy_types.Location(**new_item))

return ret
Expand Down
2 changes: 1 addition & 1 deletion src/multilspy/multilspy_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Location(TypedDict):
uri: DocumentUri
range: Range
absolutePath: str
relativePath: str
relativePath: Union[str, None]

class CompletionItemKind(IntEnum):
"""The kind of a completion entry."""
Expand Down
12 changes: 11 additions & 1 deletion src/multilspy/multilspy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import gzip
import logging
import os
from typing import Tuple
from typing import Tuple, Union
import requests
import shutil
import uuid
Expand Down Expand Up @@ -89,6 +89,16 @@ def uri_to_path(uri: str) -> str:
host = "{0}{0}{mnt}{0}".format(os.path.sep, mnt=parsed.netloc)
return os.path.normpath(os.path.join(host, url2pathname(unquote(parsed.path))))

@staticmethod
def get_relative_path(path: str, base_path: str) -> Union[str, None]:
"""
Gets relative path if it's possible (paths should be on the same drive),
returns `None` otherwise.
"""
if PurePath(path).drive == PurePath(base_path).drive:
return str(PurePath(os.path.relpath(path, base_path)))
return None

class FileUtils:
"""
Utility functions for file operations.
Expand Down
Loading