Skip to content

Commit

Permalink
fix: nameless file crash (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
McPatate authored Feb 19, 2024
1 parent 50b62bd commit 0d03590
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions crates/llm-ls/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Document {
let rope = Rope::from_str(text);
let text_len = rope.len_chars();
let end_idx = idx + text_len;
self.text.insert(idx, text);
self.text.try_insert(idx, text)?;
(
end_idx,
Point {
Expand All @@ -189,7 +189,7 @@ impl Document {
+ (range.end.character as usize);
let slice_size = removal_idx - start_idx;
self.text.try_remove(start_idx..removal_idx)?;
self.text.insert(start_idx, text);
self.text.try_insert(start_idx, text)?;
let rope = Rope::from_str(text);
let text_len = rope.len_chars();
let character_difference = text_len as isize - slice_size as isize;
Expand Down
24 changes: 20 additions & 4 deletions crates/llm-ls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,22 @@ impl LlmService {
) -> LspResult<GetCompletionsResult> {
let request_id = Uuid::new_v4();
let span = info_span!("completion_request", %request_id);

async move {
let document_map = self.document_map.read().await;

let document = document_map
.get(params.text_document_position.text_document.uri.as_str())
.ok_or_else(|| internal_error("failed to find document"))?;
let document =
match document_map.get(params.text_document_position.text_document.uri.as_str()) {
Some(doc) => doc,
None => {
debug!("failed to find document");
return Ok(GetCompletionsResult {
request_id,
completions: vec![],
});
}
};

info!(
document_url = %params.text_document_position.text_document.uri,
cursor_line = ?params.text_document_position.position.line,
Expand Down Expand Up @@ -545,6 +555,9 @@ impl LanguageServer for LlmService {

async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri.to_string();
if uri == "file:///" {
return;
}
match Document::open(
&params.text_document.language_id,
&params.text_document.text,
Expand All @@ -567,6 +580,9 @@ impl LanguageServer for LlmService {

async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.to_string();
if uri == "file:///" {
return;
}
if params.content_changes.is_empty() {
return;
}
Expand All @@ -593,7 +609,7 @@ impl LanguageServer for LlmService {
}
}
} else {
warn!("textDocument/didChange {uri}: document not found");
debug!("textDocument/didChange {uri}: document not found");
}
}

Expand Down

0 comments on commit 0d03590

Please sign in to comment.