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

Clamp Position::character to line length #18243

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

krobelus
Copy link
Contributor

@krobelus krobelus commented Oct 5, 2024

LSP says about Position::character

If the character value is greater than the line length it defaults back to the line length.

but from_proto::offset() doesn't implement this.

A client might for example request code actions for a whole line by sending
Position::character=99999. I don't think there is ever a reason (besides laziness) why the
client can't specify the line length instead but I guess we should not crash but follow protocol.

Not sure how to update Cargo.lock (lib/README.md doesn't say how).

Fixes #18240

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 5, 2024
)
})?;
if let Some(clamped_length) = clamped_length {
tracing::error!(
Copy link
Contributor

Choose a reason for hiding this comment

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

If LSP allows that, should we warn at it?

The answer probably lies in whether there are some client that deliberately sends longer line lengths. But if not, then maybe we don't want to merge this...

Copy link
Contributor Author

@krobelus krobelus Oct 14, 2024

Choose a reason for hiding this comment

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

The answer probably lies in whether there are some client that deliberately sends longer line lengths

Indirectly yes. But the more direct answer is in the specification (unless we want to deliberately deviate from that which would be surprising).
Ideally I'd prefer following the spec today but also remove this feature from the spec later (assuming no client needs it).
Since it's allowed by the spec I agree we shouldn't error. But I think a warning would be appropriate; if nothing else it will help us find out which client wants to use this feature.

Assuming we want to warn, we can define our own LSP protocol extension that clients can opt into.
Say we call it strict-mode.
Then we can suppress the warning by default and only print it if strict mode is enabled.
LSP is sometimes a bit underspecified, meaning that servers and clients have to implement multiple different ways the protocol could be interpreted. We could use a strict mode as a means to eventually simplify the spec..

I'd also be fine with simply dropping the warning but I guess I have a slight preference for keeping it.

This crash is not necessarily an important issue but I believe I've hit it occasionally over a few years and didn't notice the crash because my client restarts rust-analyzer automatically on crash. I got better error propagation set up now.

Copy link
Member

Choose a reason for hiding this comment

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

I'd say lets keep the log for now, and see if any client is being spammy with this or not

@Veykril
Copy link
Member

Veykril commented Oct 5, 2024

Not sure how to update Cargo.lock (lib/README.md doesn't say how).

We'll need to split the PR, iirc our CI auto publishes the library folder if the versions change so we need the library change first, then a separate PR to bump the dependency.

Cargo.toml Outdated
@@ -40,7 +40,7 @@ debug = 2
# chalk-ir = { path = "../chalk/chalk-ir" }
# chalk-recursive = { path = "../chalk/chalk-recursive" }
# chalk-derive = { path = "../chalk/chalk-derive" }
# line-index = { path = "lib/line-index" }
line-index = { path = "lib/line-index" }
Copy link
Member

@Veykril Veykril Oct 14, 2024

Choose a reason for hiding this comment

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

Please split this PR up into two, one changing the lib/line-index crate, that we then merge and have a patch release done by. Then we can rebase this PR on top to use the new release (or swap things, have this PR contain the lib changes and do a follow up to bump the line-index dep usage)

Comment on lines 139 to 140
/// Transforms the `LineCol` into a `TextSize`. If the column exceeds the line length,
/// the line, it is clamped to that.
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to describe the return types here then, its not immediately obvious what they mean

Comment on lines 183 to 191
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
pub fn lines_in_range(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
Copy link
Member

Choose a reason for hiding this comment

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

This is a semver breaking change to the signatures so we'd need to release a 0.2.0 release then if we rename this function, should be fine to keep it as lines

@Veykril
Copy link
Member

Veykril commented Oct 18, 2024

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Oct 18, 2024

📌 Commit d42a4e6 has been approved by Veykril

It is now in the queue for this repository.

@Veykril
Copy link
Member

Veykril commented Oct 18, 2024

Ah CI is still borked (not from your PR)
@bors r-

@bors
Copy link
Contributor

bors commented Oct 18, 2024

⌛ Testing commit d42a4e6 with merge 129aad2...

bors added a commit that referenced this pull request Oct 18, 2024
Clamp Position::character to line length

LSP says about Position::character

> If the character value is greater than the line length it defaults back to the line length.

but from_proto::offset() doesn't implement this.

A client might for example request code actions for a whole line by sending
Position::character=99999.  I don't think there is ever a reason (besides laziness) why the
client can't specify the line length instead but I guess we should not crash but follow protocol.

Not sure how to update Cargo.lock (lib/README.md doesn't say how).

Fixes #18240
@bors
Copy link
Contributor

bors commented Oct 18, 2024

💔 Test failed - checks-actions

@Veykril
Copy link
Member

Veykril commented Oct 18, 2024

@bors r+

@bors
Copy link
Contributor

bors commented Oct 18, 2024

📌 Commit d42a4e6 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Oct 18, 2024

⌛ Testing commit d42a4e6 with merge 3bda0cc...

@bors
Copy link
Contributor

bors commented Oct 18, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing 3bda0cc to master...

@bors bors merged commit 3bda0cc into rust-lang:master Oct 18, 2024
11 checks passed
bors added a commit that referenced this pull request Oct 18, 2024
Clamp Position::character to line length 2/2

Completes #18243

I don't think I have permissions to target this on the other PR, so we'll need to rebase manually
lnicola pushed a commit to lnicola/rust that referenced this pull request Oct 22, 2024
…r=Veykril

Clamp Position::character to line length 2/2

Completes rust-lang/rust-analyzer#18243

I don't think I have permissions to target this on the other PR, so we'll need to rebase manually
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Position.character is not clamped to line length
5 participants