[ty] Refactor Range to/from TextRange conversion as prep for notebook support (#21230)

This commit is contained in:
Micha Reiser 2025-11-05 14:24:03 +01:00 committed by GitHub
parent f79044478c
commit 7009d60260
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 386 additions and 280 deletions

View file

@ -9,7 +9,6 @@ use rustc_hash::FxHashMap;
use ruff_db::diagnostic::{Annotation, Severity, SubDiagnostic};
use ruff_db::files::FileRange;
use ruff_db::source::{line_index, source_text};
use ruff_db::system::SystemPathBuf;
use ty_project::{Db, ProjectDatabase};
@ -279,11 +278,9 @@ pub(super) fn to_lsp_diagnostic(
) -> Diagnostic {
let range = if let Some(span) = diagnostic.primary_span() {
let file = span.expect_ty_file();
let index = line_index(db, file);
let source = source_text(db, file);
span.range()
.map(|range| range.to_lsp_range(&source, &index, encoding))
.map(|range| range.as_lsp_range(db, file, encoding).to_local_range())
.unwrap_or_default()
} else {
Range::default()
@ -365,7 +362,7 @@ fn annotation_to_related_information(
let annotation_message = annotation.get_message()?;
let range = FileRange::try_from(span).ok()?;
let location = range.to_location(db, encoding)?;
let location = range.as_lsp_range(db, encoding).to_location()?;
Some(DiagnosticRelatedInformation {
location,
@ -383,7 +380,7 @@ fn sub_diagnostic_to_related_information(
let span = primary_annotation.get_span();
let range = FileRange::try_from(span).ok()?;
let location = range.to_location(db, encoding)?;
let location = range.as_lsp_range(db, encoding).to_location()?;
Some(DiagnosticRelatedInformation {
location,