Skip to content

Simplify find_width_of_character_at_span. #111560

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
May 15, 2023
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
1 change: 1 addition & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![feature(min_specialization)]
#![feature(rustc_attrs)]
#![feature(let_chains)]
#![feature(round_char_boundary)]
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]

Expand Down
31 changes: 7 additions & 24 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,36 +1019,19 @@ impl SourceMap {

let src = local_begin.sf.external_src.borrow();

// We need to extend the snippet to the end of the src rather than to end_index so when
// searching forwards for boundaries we've got somewhere to search.
let snippet = if let Some(ref src) = local_begin.sf.src {
&src[start_index..]
let snippet = if let Some(src) = &local_begin.sf.src {
src
} else if let Some(src) = src.get_source() {
&src[start_index..]
src
} else {
return 1;
};
debug!("snippet=`{:?}`", snippet);

let mut target = if forwards { end_index + 1 } else { end_index - 1 };
debug!("initial target=`{:?}`", target);

while !snippet.is_char_boundary(target - start_index) && target < source_len {
target = if forwards {
target + 1
} else {
match target.checked_sub(1) {
Some(target) => target,
None => {
break;
}
}
};
debug!("target=`{:?}`", target);
if forwards {
(snippet.ceil_char_boundary(end_index + 1) - end_index) as u32
} else {
(end_index - snippet.floor_char_boundary(end_index - 1)) as u32
}
debug!("final target=`{:?}`", target);

if forwards { (target - end_index) as u32 } else { (end_index - target) as u32 }
}

pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
Expand Down