Fix handling of end-of-file (#60)

* Fix underflow on empty line

* Update crates/llm-ls/src/main.rs

Co-authored-by: Luc Georges <McPatate@users.noreply.github.com>

* Update crates/llm-ls/src/main.rs

Co-authored-by: Luc Georges <McPatate@users.noreply.github.com>

---------

Co-authored-by: Luc Georges <McPatate@users.noreply.github.com>
This commit is contained in:
Markus Hennerbichler 2024-02-05 17:37:24 +00:00 committed by GitHub
parent 16606e5371
commit c9a44e591c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -57,6 +57,10 @@ impl Display for CompletionType {
fn should_complete(document: &Document, position: Position) -> Result<CompletionType> {
let row = position.line as usize;
let column = position.character as usize;
if document.text.len_chars() == 0 {
warn!("Document is empty");
return Ok(CompletionType::Empty);
}
if let Some(tree) = &document.tree {
let current_node = tree.root_node().descendant_for_point_range(
tree_sitter::Point { row, column },
@ -111,10 +115,11 @@ fn should_complete(document: &Document, position: Position) -> Result<Completion
.text
.try_line_to_char(row)
.map_err(internal_error)?;
// XXX: We treat the end of a document as a newline
let next_char = document
.text
.get_char(start_idx + column)
.ok_or_else(|| internal_error(format!("failed to find char at {}", start_idx + column)))?;
.unwrap_or('\n');
if next_char.is_whitespace() {
Ok(CompletionType::SingleLine)
} else {