Skip to content

Commit

Permalink
Fix handling of end-of-file (#60)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
HennerM and McPatate authored Feb 5, 2024
1 parent 16606e5 commit c9a44e5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion crates/llm-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c9a44e5

Please sign in to comment.