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

inline_assistant: Respect tabs when selection first row is not indented #14886

Merged

Conversation

arturhoo
Copy link
Contributor

@arturhoo arturhoo commented Jul 21, 2024

When using the inline assistant with a language such as Go that uses tabs, if the user selects a block of text that is correctly formatted and where the first line has no indentation, the suggested_line_indent variable ends up with IndentSize { len: 0, kind: Space }. That's because suggested_line_indent current relies on BufferSnapshot::suggested_indents suggestion for the first line on the selection, but since it is already correctly indented, there are no suggestions and MultiBufferSnapshot::indent_size_for_line is used instead.

let suggested_line_indent = snapshot
.suggested_indents(selection_start.row..selection_start.row + 1, cx)
.into_values()
.next()
.unwrap_or_else(|| snapshot.indent_size_for_line(MultiBufferRow(selection_start.row)));

In this patch, we also take a look at the rest of the selection and detect tabs. If one is encountered, we assume that tabs should always be used. I suppose this isn't perfect, especially if the original file had a mix of spaces and tabs, however it seems better than the status quo.

I considered using BufferSnapshot::language_indent_size_at, but I imagine tabs should be preserved even when a specific language isn't being used.

See screenshot below of the original prompt with this patch.

Tests:

  • New unit test
  • I've also manually tested with a few other cases: selection where all lines are indented and file that only use spaces.

Release Notes:

  • Fixed 'inline_assistant: tabs are overwritten with space characters when first line in selection has no indentation' (#14885).
image

Copy link

cla-bot bot commented Jul 21, 2024

We require contributors to sign our Contributor License Agreement, and we don't have @arturhoo on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@arturhoo
Copy link
Contributor Author

@cla-bot check

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jul 21, 2024
Copy link

cla-bot bot commented Jul 21, 2024

The cla-bot has been summoned, and re-checked this pull request!

@maxdeviant maxdeviant changed the title inline_assistant - respect tabs when selection first row is not indented inline_assistant: Respect tabs when selection first row is not indented Jul 21, 2024
@arturhoo arturhoo force-pushed the inline_assistant/respect-tabs branch 3 times, most recently from 9d4c82d to 9807631 Compare July 21, 2024 16:58
@arturhoo arturhoo force-pushed the inline_assistant/respect-tabs branch from 9807631 to 84f3d19 Compare July 23, 2024 07:36
@SomeoneToIgnore SomeoneToIgnore self-assigned this Jul 24, 2024
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

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

Looks really nice, thank you for the extensive description.

@SomeoneToIgnore SomeoneToIgnore merged commit 5ad3c6b into zed-industries:main Jul 24, 2024
9 checks passed
@arturhoo arturhoo deleted the inline_assistant/respect-tabs branch July 24, 2024 13:32
SomeoneToIgnore added a commit that referenced this pull request Jul 24, 2024
Follow-up of #14886

We do not require the branch to be up-to-date with `main` before
merging, and in 4 days some related test code got reworked so that there
were no conflicts and it slipped.

Release Notes:

- N/A
CharlesChen0823 pushed a commit to CharlesChen0823/zed that referenced this pull request Jul 29, 2024
…ed (zed-industries#14886)

When using the inline assistant with a language such as Go that uses
tabs, if the user selects a block of text that is correctly formatted
and where the first line has no indentation, the `suggested_line_indent`
variable ends up with `IndentSize { len: 0, kind: Space }`. That's
because `suggested_line_indent` current relies on
`BufferSnapshot::suggested_indents` suggestion for the first line on the
selection, but since it is already correctly indented, there are no
suggestions and `MultiBufferSnapshot::indent_size_for_line` is used
instead.


https://github.com/zed-industries/zed/blob/2d96bba61fd9e60951ecfcf697707a974475c1b2/crates/assistant/src/inline_assistant.rs#L2124-L2128

In this patch, we also take a look at the rest of the selection and
detect tabs. If one is encountered, we assume that tabs should always be
used. I suppose this isn't perfect, especially if the original file had
a mix of spaces and tabs, however it seems better than the status quo.

I considered using `BufferSnapshot::language_indent_size_at`, but I
imagine tabs should be preserved even when a specific language isn't
being used.

See screenshot below of the original prompt with this patch.

Tests:

* New unit test
* I've also manually tested with a few other cases: selection where all
lines are indented and file that only use spaces.

Release Notes:

- Fixed 'inline_assistant: tabs are overwritten with space characters
when first line in selection has no indentation'
([zed-industries#14885](zed-industries#14885)).

<img width="942" alt="image"
src="https://github.com/user-attachments/assets/f2c5d7e9-e8bc-400b-bd6f-09e4a89d22c1">
CharlesChen0823 pushed a commit to CharlesChen0823/zed that referenced this pull request Jul 29, 2024
Follow-up of zed-industries#14886

We do not require the branch to be up-to-date with `main` before
merging, and in 4 days some related test code got reworked so that there
were no conflicts and it slipped.

Release Notes:

- N/A
kevmo314 pushed a commit to kevmo314/zed that referenced this pull request Jul 29, 2024
…ed (zed-industries#14886)

When using the inline assistant with a language such as Go that uses
tabs, if the user selects a block of text that is correctly formatted
and where the first line has no indentation, the `suggested_line_indent`
variable ends up with `IndentSize { len: 0, kind: Space }`. That's
because `suggested_line_indent` current relies on
`BufferSnapshot::suggested_indents` suggestion for the first line on the
selection, but since it is already correctly indented, there are no
suggestions and `MultiBufferSnapshot::indent_size_for_line` is used
instead.


https://github.com/zed-industries/zed/blob/2d96bba61fd9e60951ecfcf697707a974475c1b2/crates/assistant/src/inline_assistant.rs#L2124-L2128

In this patch, we also take a look at the rest of the selection and
detect tabs. If one is encountered, we assume that tabs should always be
used. I suppose this isn't perfect, especially if the original file had
a mix of spaces and tabs, however it seems better than the status quo.

I considered using `BufferSnapshot::language_indent_size_at`, but I
imagine tabs should be preserved even when a specific language isn't
being used.

See screenshot below of the original prompt with this patch.

Tests:

* New unit test
* I've also manually tested with a few other cases: selection where all
lines are indented and file that only use spaces.

Release Notes:

- Fixed 'inline_assistant: tabs are overwritten with space characters
when first line in selection has no indentation'
([zed-industries#14885](zed-industries#14885)).

<img width="942" alt="image"
src="https://github.com/user-attachments/assets/f2c5d7e9-e8bc-400b-bd6f-09e4a89d22c1">
kevmo314 pushed a commit to kevmo314/zed that referenced this pull request Jul 29, 2024
Follow-up of zed-industries#14886

We do not require the branch to be up-to-date with `main` before
merging, and in 4 days some related test code got reworked so that there
were no conflicts and it slipped.

Release Notes:

- N/A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants