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: never panic in LSP inlay hints #5534

Merged
merged 2 commits into from
Jul 18, 2024
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: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tooling/lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ license.workspace = true
acvm.workspace = true
codespan-lsp.workspace = true
lsp-types.workspace = true
lsp_types_0_88_0 = { package = "lsp-types", version = "0.88.0" }
nargo.workspace = true
nargo_fmt.workspace = true
nargo_toml.workspace = true
Expand Down
82 changes: 65 additions & 17 deletions tooling/lsp/src/requests/inlay_hint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fm::codespan_files::Files;
use std::future::{self, Future};

use async_lsp::ResponseError;
Expand Down Expand Up @@ -37,29 +38,13 @@
args.files.get_file_id(&path).map(|file_id| {
let file = args.files.get_file(file_id).unwrap();
let source = file.source();
let (parsed_moduled, _errors) = noirc_frontend::parse_program(source);

Check warning on line 41 in tooling/lsp/src/requests/inlay_hint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (moduled)

// The version of codespan_lsp is pretty old and relies on an old version of lsp-types.
// We can't downgrade because we need some types from the latest version, and there's
// no newer version of codespan_lsp... but we just need this type here, so we create
// a copy of a Range to get an older type.
let range = lsp_types_0_88_0::Range {
start: lsp_types_0_88_0::Position {
line: params.range.start.line,
character: params.range.start.character,
},
end: lsp_types_0_88_0::Position {
line: params.range.end.line,
character: params.range.end.character,
},
};

let span = codespan_lsp::range_to_byte_span(args.files, file_id, &range)
.ok()
let span = range_to_byte_span(args.files, file_id, &params.range)
.map(|range| Span::from(range.start as u32..range.end as u32));

let mut collector = InlayHintCollector::new(args.files, file_id, args.interner, span);
collector.collect_in_parsed_module(&parsed_moduled);

Check warning on line 47 in tooling/lsp/src/requests/inlay_hint.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (moduled)
collector.inlay_hints
})
});
Expand Down Expand Up @@ -596,4 +581,67 @@
panic!("Expected InlayHintLabel::LabelParts, got {:?}", inlay_hint.label);
}
}

#[test]
async fn test_do_not_panic_when_given_line_is_too_big() {
let inlay_hints = get_inlay_hints(0, 100000).await;
assert!(!inlay_hints.is_empty());
}
}

// These functions are copied from the codespan_lsp crate, except that they never panic
// (the library will sometimes panic, so functions returning Result are not always accurate)

fn range_to_byte_span(
files: &FileMap,
file_id: FileId,
range: &lsp_types::Range,
) -> Option<std::ops::Range<usize>> {
Some(
position_to_byte_index(files, file_id, &range.start)?
..position_to_byte_index(files, file_id, &range.end)?,
)
}

fn position_to_byte_index(
files: &FileMap,
file_id: FileId,
position: &lsp_types::Position,
) -> Option<usize> {
let Ok(source) = files.source(file_id) else {
return None;
};

let Ok(line_span) = files.line_range(file_id, position.line as usize) else {
return None;
};
let line_str = source.get(line_span.clone())?;

let byte_offset = character_to_line_offset(line_str, position.character)?;

Some(line_span.start + byte_offset)
}

fn character_to_line_offset(line: &str, character: u32) -> Option<usize> {
let line_len = line.len();
let mut character_offset = 0;

let mut chars = line.chars();
while let Some(ch) = chars.next() {
if character_offset == character {
let chars_off = chars.as_str().len();
let ch_off = ch.len_utf8();

return Some(line_len - chars_off - ch_off);
}

character_offset += ch.len_utf16() as u32;
}

// Handle positions after the last character on the line
if character_offset == character {
Some(line_len)
} else {
None
}
}
Loading