Use Result for failed text document retrieval in LSP requests (#14579)

## Summary

Ref:
https://github.com/astral-sh/ruff-vscode/issues/644#issuecomment-2496588452

## Test Plan

Not sure how to test this as this is mainly to get more context on the
panic that the server is raising.
This commit is contained in:
Dhruv Manilawala 2024-11-25 15:14:30 +05:30 committed by GitHub
parent 9f6147490b
commit 0c9165fc3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 8 deletions

View file

@ -1,3 +1,4 @@
use anyhow::Context;
use lsp_types::{self as types, request as req};
use types::TextEdit;
@ -64,7 +65,8 @@ pub(super) fn format_document(snapshot: &DocumentSnapshot) -> Result<super::Form
let text_document = snapshot
.query()
.as_single_document()
.expect("format should only be called on text documents or notebook cells");
.context("Failed to get text document for the format request")
.unwrap();
let query = snapshot.query();
format_text_document(
text_document,

View file

@ -1,3 +1,4 @@
use anyhow::Context;
use lsp_types::{self as types, request as req, Range};
use crate::edit::{RangeExt, ToRangeExt};
@ -32,7 +33,8 @@ fn format_document_range(
let text_document = snapshot
.query()
.as_single_document()
.expect("format should only be called on text documents or notebook cells");
.context("Failed to get text document for the format range request")
.unwrap();
let query = snapshot.query();
format_text_document_range(text_document, range, query, snapshot.encoding())
}

View file

@ -1,5 +1,6 @@
use crate::server::{client::Notifier, Result};
use crate::session::DocumentSnapshot;
use anyhow::Context;
use lsp_types::{self as types, request as req};
use regex::Regex;
use ruff_diagnostics::FixAvailability;
@ -33,7 +34,8 @@ pub(crate) fn hover(
let document = snapshot
.query()
.as_single_document()
.expect("hover should only be called on text documents or notebook cells");
.context("Failed to get text document for the hover request")
.unwrap();
let line_number: usize = position
.position
.line