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

Fix path completions #6002

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@
MarkupContent,
MarkupKind,
Position,
Range,
RenameParams,
SignatureHelp,
SignatureHelpOptions,
SymbolInformation,
TextDocumentIdentifier,
TextDocumentPositionParams,
TextEdit,
WorkspaceEdit,
WorkspaceSymbolParams,
)
Expand Down Expand Up @@ -368,8 +370,8 @@ def positron_completion(

# Don't add jedi completions if completing an explicit magic command
if not trimmed_line.startswith(_LINE_MAGIC_PREFIX):
jedi_completion_items = [
jedi_utils.lsp_completion_item(
for completion in completions_jedi:
jedi_completion_item = jedi_utils.lsp_completion_item(
completion=completion,
char_before_cursor=char_before_cursor,
char_after_cursor=char_after_cursor,
Expand All @@ -378,9 +380,19 @@ def positron_completion(
markup_kind=markup_kind,
sort_append_text=completion.name,
)
for completion in completions_jedi
]
completion_items.extend(jedi_completion_items)

# If Jedi knows how to complete the expression, use its suggestion.
if completion.complete is not None:
# Using the text_edit attribute (instead of insert_text used in
# lsp_completion_item) notifies the client to use the text as is.
# See https://github.com/posit-dev/positron/issues/5193.
jedi_completion_item.text_edit = TextEdit(
# Use a range that starts and ends at the cursor position will to insert
# text at the cursor.
Range(params.position, params.position),
completion.complete,
)
completion_items.append(jedi_completion_item)

# Don't add magic completions if:
# - completing an object's attributes e.g `numpy.<cursor>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
MarkupContent,
MarkupKind,
Position,
Range,
TextDocumentIdentifier,
TextEdit,
)
from positron_ipykernel._vendor.pygls.workspace.text_document import TextDocument
from positron_ipykernel.help_comm import ShowHelpTopicParams
Expand Down Expand Up @@ -108,10 +110,12 @@ def prop(self) -> str:
def _completions(
source: str,
namespace: Dict[str, Any],
character: Optional[int] = None,
) -> List[CompletionItem]:
lines = source.splitlines()
line = len(lines) - 1
character = len(lines[line])
if character is None:
character = len(lines[line])
params = CompletionParams(TextDocumentIdentifier("file:///foo.py"), Position(line, character))
server = mock_server(params.text_document.uri, source, namespace)

Expand Down Expand Up @@ -169,6 +173,52 @@ def test_positron_completion_contains(
assert expected_label in completion_labels


def test_path_completion(tmp_path) -> None:
# See https://github.com/posit-dev/positron/issues/5193.

dir_ = tmp_path / "my-notebooks.new"
dir_.mkdir()

file = dir_ / "weather-report.ipynb"
file.write_text("")

cwd = os.getcwd()

def assert_has_path_completion(source: str, completion: str, chars_from_end=1):
# Replace separators for testing cross-platform.
source = source.replace("/", os.path.sep)
completion = completion.replace("/", os.path.sep)

chars_from_end = len(source) - chars_from_end
cursor = Position(0, chars_from_end)
completions = _completions(source, {}, cursor.character)
assert len(completions) == 1
assert completions[0].text_edit == TextEdit(Range(cursor, cursor), completion)

try:
os.chdir(tmp_path)

# Check directory completions at various points around symbols.
assert_has_path_completion('""', f"my-notebooks.new/")
# Quotes aren't automatically closed for directories, since the user may want a file.
assert_has_path_completion('"', "my-notebooks.new/", 0)
assert_has_path_completion('"my"', "-notebooks.new/")
assert_has_path_completion('"my-notebooks"', ".new/")
assert_has_path_completion('"my-notebooks."', "new/")
assert_has_path_completion('"my-notebooks.new"', "/")

# Check file completions at various points around symbols.
assert_has_path_completion('"my-notebooks.new/"', "weather-report.ipynb")
# Quotes are automatically closed for files, since they end the completion.
assert_has_path_completion('"my-notebooks.new/', 'weather-report.ipynb"', 0)
assert_has_path_completion('"my-notebooks.new/weather"', "-report.ipynb")
assert_has_path_completion('"my-notebooks.new/weather-report"', ".ipynb")
assert_has_path_completion('"my-notebooks.new/weather-report."', "ipynb")
assert_has_path_completion('"my-notebooks.new/weather-report.ipynb"', "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice tests!

finally:
os.chdir(cwd)


_pd_df = pd.DataFrame({"a": [0]})
_pl_df = pl.DataFrame({"a": [0]})

Expand Down
Loading