[ty] Clarify what "cursor" means

This commit does a small refactor to combine the file and
cursor offset into a single type. I think this makes it
clearer that even if there are multiple files in the cursor
test, this one in particular corresponds to the file that
contains the `<CURSOR>` marker.
This commit is contained in:
Andrew Gallant 2025-06-24 10:00:12 -04:00 committed by Andrew Gallant
parent 40731f0589
commit cef1a522dc
4 changed files with 15 additions and 12 deletions

View File

@ -2184,7 +2184,7 @@ importlib.<CURSOR>
} }
fn completions_if(&self, predicate: impl Fn(&str) -> bool) -> String { fn completions_if(&self, predicate: impl Fn(&str) -> bool) -> String {
let completions = completion(&self.db, self.file, self.cursor_offset); let completions = completion(&self.db, self.cursor.file, self.cursor.offset);
if completions.is_empty() { if completions.is_empty() {
return "<No completions found>".to_string(); return "<No completions found>".to_string();
} }
@ -2198,7 +2198,7 @@ importlib.<CURSOR>
#[track_caller] #[track_caller]
fn assert_completions_include(&self, expected: &str) { fn assert_completions_include(&self, expected: &str) {
let completions = completion(&self.db, self.file, self.cursor_offset); let completions = completion(&self.db, self.cursor.file, self.cursor.offset);
assert!( assert!(
completions completions
@ -2210,7 +2210,7 @@ importlib.<CURSOR>
#[track_caller] #[track_caller]
fn assert_completions_do_not_include(&self, unexpected: &str) { fn assert_completions_do_not_include(&self, unexpected: &str) {
let completions = completion(&self.db, self.file, self.cursor_offset); let completions = completion(&self.db, self.cursor.file, self.cursor.offset);
assert!( assert!(
completions completions

View File

@ -833,7 +833,8 @@ f(**kwargs<CURSOR>)
impl CursorTest { impl CursorTest {
fn goto_type_definition(&self) -> String { fn goto_type_definition(&self) -> String {
let Some(targets) = goto_type_definition(&self.db, self.file, self.cursor_offset) let Some(targets) =
goto_type_definition(&self.db, self.cursor.file, self.cursor.offset)
else { else {
return "No goto target found".to_string(); return "No goto target found".to_string();
}; };

View File

@ -737,7 +737,7 @@ mod tests {
fn hover(&self) -> String { fn hover(&self) -> String {
use std::fmt::Write; use std::fmt::Write;
let Some(hover) = hover(&self.db, self.file, self.cursor_offset) else { let Some(hover) = hover(&self.db, self.cursor.file, self.cursor.offset) else {
return "Hover provided no content".to_string(); return "Hover provided no content".to_string();
}; };
@ -769,7 +769,7 @@ mod tests {
); );
diagnostic.annotate( diagnostic.annotate(
Annotation::secondary( Annotation::secondary(
Span::from(source.file()).with_range(TextRange::empty(self.cursor_offset)), Span::from(source.file()).with_range(TextRange::empty(self.cursor.offset)),
) )
.message("Cursor offset"), .message("Cursor offset"),
); );

View File

@ -213,14 +213,17 @@ mod tests {
SearchPathSettings, SearchPathSettings,
}; };
/// A way to create a simple single-file (named `main.py`) cursor test.
///
/// Use cases that require multiple files with a `<CURSOR>` marker
/// in a file other than `main.py` can use `CursorTest::builder()`.
pub(super) fn cursor_test(source: &str) -> CursorTest { pub(super) fn cursor_test(source: &str) -> CursorTest {
CursorTest::builder().source("main.py", source).build() CursorTest::builder().source("main.py", source).build()
} }
pub(super) struct CursorTest { pub(super) struct CursorTest {
pub(super) db: TestDb, pub(super) db: TestDb,
pub(super) cursor_offset: TextSize, pub(super) cursor: Cursor,
pub(super) file: File,
_insta_settings_guard: SettingsBindDropGuard, _insta_settings_guard: SettingsBindDropGuard,
} }
@ -258,6 +261,8 @@ mod tests {
} }
} }
/// The file and offset into that file containing
/// a `<CURSOR>` marker.
pub(super) struct Cursor { pub(super) struct Cursor {
pub(super) file: File, pub(super) file: File,
pub(super) offset: TextSize, pub(super) offset: TextSize,
@ -318,13 +323,10 @@ mod tests {
insta_settings.add_filter(r"@Todo\(.+\)", "@Todo"); insta_settings.add_filter(r"@Todo\(.+\)", "@Todo");
let insta_settings_guard = insta_settings.bind_to_scope(); let insta_settings_guard = insta_settings.bind_to_scope();
let Cursor { file, offset } =
cursor.expect("at least one source to contain `<CURSOR>`");
CursorTest { CursorTest {
db, db,
cursor_offset: offset, cursor: cursor.expect("at least one source to contain `<CURSOR>`"),
file,
_insta_settings_guard: insta_settings_guard, _insta_settings_guard: insta_settings_guard,
} }
} }