-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix: Resolve HIR display length issues and improve adjustment tooltips #20031
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
Open
jnyfah
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
jnyfah:some-branch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,9 +91,9 @@ pub(super) fn hints( | |
match_ast! { | ||
match parent { | ||
ast::Fn(it) => { | ||
// FIXME: this could include parameters, but `HirDisplay` prints too much info | ||
// and doesn't respect the max length either, so the hints end up way too long | ||
(format!("fn {}", it.name()?), it.name().map(name)) | ||
let hint_text = format_function_hint(&it, config.max_length?) | ||
.unwrap_or_else(|| format!("fn {}", it.name().map(|n| n.to_string()).unwrap_or_default())); | ||
(hint_text, it.name().map(name)) | ||
}, | ||
ast::Static(it) => (format!("static {}", it.name()?), it.name().map(name)), | ||
ast::Const(it) => { | ||
|
@@ -156,6 +156,47 @@ pub(super) fn hints( | |
None | ||
} | ||
|
||
fn format_function_hint(func: &ast::Fn, max_length: usize) -> Option<String> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imo we should do this as described in my comment in #19207 (comment) instead |
||
let name = func.name()?; | ||
let name_str = name.to_string(); | ||
|
||
let params = if let Some(param_list) = func.param_list() { | ||
let mut param_parts = Vec::new(); | ||
let mut total_len = 0; | ||
let max_param_len = max_length.saturating_sub(name_str.len() + 4); | ||
|
||
for param in param_list.params() { | ||
let param_text = if let Some(pat) = param.pat() { | ||
if let Some(ty) = param.ty() { format!("{}: {}", pat, ty) } else { pat.to_string() } | ||
} else if let Some(ty) = param.ty() { | ||
format!("_: {}", ty) | ||
} else { | ||
let param_source = param.syntax().text().to_string(); | ||
if param_source.trim() == "..." { "...".to_owned() } else { "_".to_owned() } | ||
}; | ||
|
||
let param_len = param_text.len() + if param_parts.is_empty() { 0 } else { 2 }; | ||
if total_len + param_len > max_param_len { | ||
param_parts.push("...".to_owned()); | ||
break; | ||
} | ||
|
||
total_len += param_len; | ||
param_parts.push(param_text); | ||
} | ||
|
||
if param_parts.is_empty() { | ||
"()".to_owned() | ||
} else { | ||
format!("({})", param_parts.join(", ")) | ||
} | ||
} else { | ||
"()".to_owned() | ||
}; | ||
|
||
Some(format!("fn {}{}", name_str, params)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{ | ||
|
@@ -166,7 +207,11 @@ mod tests { | |
#[test] | ||
fn hints_closing_brace() { | ||
check_with_config( | ||
InlayHintsConfig { closing_brace_hints_min_lines: Some(2), ..DISABLED_CONFIG }, | ||
InlayHintsConfig { | ||
closing_brace_hints_min_lines: Some(2), | ||
max_length: Some(30), | ||
..DISABLED_CONFIG | ||
}, | ||
r#" | ||
fn a() {} | ||
|
||
|
@@ -175,17 +220,17 @@ fn f() { | |
|
||
fn g() { | ||
} | ||
//^ fn g | ||
//^ fn g() | ||
|
||
fn h<T>(with: T, arguments: u8, ...) { | ||
} | ||
//^ fn h | ||
//^ fn h(with: T, arguments: u8, ...) | ||
|
||
trait Tr { | ||
fn f(); | ||
fn g() { | ||
} | ||
//^ fn g | ||
//^ fn g() | ||
} | ||
//^ trait Tr | ||
impl Tr for () { | ||
|
@@ -222,15 +267,19 @@ fn f() { | |
let v = vec![ | ||
]; | ||
} | ||
//^ fn f | ||
//^ fn f() | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn hints_closing_brace_for_block_expr() { | ||
check_with_config( | ||
InlayHintsConfig { closing_brace_hints_min_lines: Some(2), ..DISABLED_CONFIG }, | ||
InlayHintsConfig { | ||
closing_brace_hints_min_lines: Some(2), | ||
max_length: Some(10), | ||
..DISABLED_CONFIG | ||
}, | ||
r#" | ||
fn test() { | ||
'end: { | ||
|
@@ -258,7 +307,40 @@ fn test() { | |
//^ 'a | ||
|
||
} | ||
//^ fn test | ||
//^ fn test() | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn hints_closing_brace_function_parameters() { | ||
check_with_config( | ||
InlayHintsConfig { | ||
closing_brace_hints_min_lines: Some(1), | ||
max_length: Some(50), | ||
..DISABLED_CONFIG | ||
}, | ||
r#" | ||
fn simple() { | ||
let v = vec![ | ||
]; | ||
} | ||
//^ fn simple() | ||
|
||
fn with_params(x: i32, y: String) { | ||
|
||
} | ||
//^ fn with_params(x: i32, y: String) | ||
|
||
fn long_params(very_long_parameter_name: ComplexType, another: AnotherType) { | ||
|
||
} | ||
//^ fn long_params(...) | ||
|
||
fn many_params(a: i32, b: i32, c: i32, d: i32, e: i32) { | ||
|
||
} | ||
//^ fn many_params(a: i32, b: i32, c: i32, d: i32, ...) | ||
"#, | ||
); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's just manually uppercase the string literal instead of doing this