Skip to content

Commit 0b0fa65

Browse files
authored
ide: internal cleanup goto def & find ref tests (#1080)
this should help when we add support for extension stubs
1 parent c6d7a98 commit 0b0fa65

3 files changed

Lines changed: 90 additions & 78 deletions

File tree

β€Žcrates/squawk_ide/src/builtins.rsβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use url::Url;
66

77
use crate::db::File;
88

9-
pub(crate) const BUILTINS_SQL: &str = include_str!("generated/builtins.sql");
9+
const BUILTINS_SQL: &str = include_str!("generated/builtins.sql");
1010

1111
#[salsa::tracked]
1212
pub fn builtins_file(db: &dyn Db) -> File {
@@ -15,13 +15,15 @@ pub fn builtins_file(db: &dyn Db) -> File {
1515

1616
#[cfg(not(target_arch = "wasm32"))]
1717
#[salsa::tracked]
18-
pub fn builtins_url(_db: &dyn Db) -> Option<Url> {
18+
pub fn builtins_url(db: &dyn Db) -> Option<Url> {
1919
let strategy = etcetera::base_strategy::choose_base_strategy().ok()?;
2020
let config_dir = strategy.config_dir();
2121
let cache_dir = config_dir.join("squawk/stubs");
2222
let path = cache_dir.join("builtins.sql");
2323
std::fs::create_dir_all(&cache_dir).ok()?;
24-
std::fs::write(&path, BUILTINS_SQL).ok()?;
24+
let builtins = builtins_file(db);
25+
let builtins_sql = builtins.content(db);
26+
std::fs::write(&path, builtins_sql.as_ref()).ok()?;
2527
Url::from_file_path(path).ok()
2628
}
2729

β€Žcrates/squawk_ide/src/find_references.rsβ€Ž

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,60 +77,65 @@ fn find_target_defs(
7777

7878
#[cfg(test)]
7979
mod test {
80-
use crate::builtins::{BUILTINS_SQL, builtins_file};
80+
use crate::builtins::builtins_file;
8181
use crate::db::{Database, File};
8282
use crate::find_references::find_references;
8383
use crate::test_utils::fixture;
8484
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
8585
use insta::assert_snapshot;
8686
use rowan::TextRange;
87+
use rustc_hash::FxHashMap;
8788

8889
#[track_caller]
8990
fn find_refs(sql: &str) -> String {
9091
let (mut offset, sql) = fixture(sql);
9192
offset = offset.checked_sub(1.into()).unwrap_or_default();
9293
let db = Database::default();
93-
let file = File::new(&db, sql.clone().into());
94-
assert_eq!(crate::db::parse(&db, file).errors(), vec![]);
95-
96-
let references = find_references(&db, file, offset);
94+
let current_file = File::new(&db, sql.clone().into());
95+
assert_eq!(crate::db::parse(&db, current_file).errors(), vec![]);
9796

97+
let references = find_references(&db, current_file, offset);
9898
let offset_usize: usize = offset.into();
9999

100-
let mut current_refs = vec![];
101-
let mut builtin_refs = vec![];
102-
let builtins_file = builtins_file(&db);
100+
let mut file_paths = FxHashMap::default();
101+
file_paths.insert(current_file, "current.sql");
102+
file_paths.insert(builtins_file(&db), "builtins.sql");
103+
104+
let mut refs_by_file: FxHashMap<File, Vec<(usize, TextRange)>> = FxHashMap::default();
103105
for (i, location) in references.iter().enumerate() {
104-
let label_index = i + 1;
105-
if location.file == file {
106-
current_refs.push((label_index, location.range));
107-
} else if location.file == builtins_file {
108-
builtin_refs.push((label_index, location.range));
109-
}
106+
refs_by_file
107+
.entry(location.file)
108+
.or_default()
109+
.push((i + 1, location.range));
110110
}
111111

112-
let has_builtins = !builtin_refs.is_empty();
112+
let multi_file = refs_by_file.len() > 1 || !refs_by_file.contains_key(&current_file);
113113

114114
let mut snippet = Snippet::source(&sql).fold(true);
115-
if has_builtins {
116-
snippet = snippet.path("current.sql");
115+
if multi_file {
116+
snippet = snippet.path(*file_paths.get(&current_file).unwrap());
117117
}
118118
snippet = snippet.annotation(
119119
AnnotationKind::Context
120120
.span(offset_usize..offset_usize + 1)
121121
.label("0. query"),
122122
);
123-
snippet = annotate_refs(snippet, current_refs);
123+
if let Some(current_refs) = refs_by_file.remove(&current_file) {
124+
snippet = annotate_refs(snippet, current_refs);
125+
}
124126

125127
let mut groups = vec![Level::INFO.primary_title("references").element(snippet)];
126128

127-
if has_builtins {
128-
let builtins_snippet = Snippet::source(BUILTINS_SQL).path("builtin.sql").fold(true);
129-
let builtins_snippet = annotate_refs(builtins_snippet, builtin_refs);
129+
for (ref_file, refs) in refs_by_file {
130+
let path = file_paths.get(&ref_file).unwrap();
131+
let other_snippet = Snippet::source(ref_file.content(&db).as_ref())
132+
.path(*path)
133+
.fold(true);
134+
let other_snippet = annotate_refs(other_snippet, refs);
130135
groups.push(
131136
Level::INFO
132137
.primary_title("references")
133-
.element(builtins_snippet),
138+
.element(other_snippet),
134139
);
135140
}
136141

@@ -429,7 +434,7 @@ select now();
429434
β”‚ ─── 2. reference
430435
β•°β•΄
431436
432-
β•­β–Έ builtin.sql:11089:28
437+
β•­β–Έ builtins.sql:11089:28
433438
β”‚
434439
11089 β”‚ create function pg_catalog.now() returns timestamp with time zone
435440
β•°β•΄ ─── 3. reference

β€Žcrates/squawk_ide/src/goto_definition.rsβ€Ž

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,15 @@ fn find_following_commit_or_rollback(
149149

150150
#[cfg(test)]
151151
mod test {
152-
use crate::builtins::{BUILTINS_SQL, builtins_file};
152+
use crate::builtins::builtins_file;
153153
use crate::db::{Database, File};
154154
use crate::goto_definition::goto_definition;
155155
use crate::test_utils::fixture;
156156
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
157157
use insta::assert_snapshot;
158158
use log::info;
159159
use rowan::TextRange;
160+
use rustc_hash::FxHashMap;
160161

161162
#[track_caller]
162163
fn goto(sql: &str) -> String {
@@ -171,60 +172,64 @@ mod test {
171172
// marker after the item we're trying to go to def on.
172173
offset = offset.checked_sub(1.into()).unwrap_or_default();
173174
let db = Database::default();
174-
let file = File::new(&db, sql.clone().into());
175-
assert_eq!(crate::db::parse(&db, file).errors(), vec![]);
176-
let results = goto_definition(&db, file, offset);
177-
if !results.is_empty() {
178-
let offset: usize = offset.into();
179-
let mut current_dests = vec![];
180-
let mut builtin_dests = vec![];
181-
let builtins_file = builtins_file(&db);
182-
for (i, location) in results.iter().enumerate() {
183-
let label_index = i + 2;
184-
if location.file == file {
185-
current_dests.push((label_index, location.range));
186-
} else if location.file == builtins_file {
187-
builtin_dests.push((label_index, location.range));
188-
}
189-
}
190-
191-
let has_builtins = !builtin_dests.is_empty();
175+
let current_file = File::new(&db, sql.into());
176+
assert_eq!(crate::db::parse(&db, current_file).errors(), vec![]);
177+
let results = goto_definition(&db, current_file, offset);
178+
if results.is_empty() {
179+
return None;
180+
}
192181

193-
let mut snippet = Snippet::source(&sql).fold(true);
194-
if has_builtins {
195-
// only show the current file when we have two file types, aka current and builtins
196-
snippet = snippet.path("current.sql");
197-
} else {
198-
snippet = annotate_destinations(snippet, current_dests);
199-
}
200-
snippet = snippet.annotation(
201-
AnnotationKind::Context
202-
.span(offset..offset + 1)
203-
.label("1. source"),
204-
);
182+
let mut file_paths = FxHashMap::default();
183+
file_paths.insert(current_file, "current.sql");
184+
file_paths.insert(builtins_file(&db), "builtins.sql");
185+
186+
let offset: usize = offset.into();
187+
let mut dests_by_file: FxHashMap<File, Vec<(usize, TextRange)>> = FxHashMap::default();
188+
for (i, location) in results.iter().enumerate() {
189+
dests_by_file
190+
.entry(location.file)
191+
.or_default()
192+
.push((i + 2, location.range));
193+
}
205194

206-
let mut groups = vec![Level::INFO.primary_title("definition").element(snippet)];
195+
let multi_file = dests_by_file.len() > 1 || !dests_by_file.contains_key(&current_file);
207196

208-
if has_builtins {
209-
let builtins_snippet = Snippet::source(BUILTINS_SQL).path("builtin.sql").fold(true);
210-
let builtins_snippet = annotate_destinations(builtins_snippet, builtin_dests);
211-
groups.push(
212-
Level::INFO
213-
.primary_title("definition")
214-
.element(builtins_snippet),
215-
);
216-
}
197+
let mut snippet = Snippet::source(current_file.content(&db).as_ref()).fold(true);
198+
if multi_file {
199+
snippet = snippet.path(*file_paths.get(&current_file).unwrap());
200+
}
201+
if let Some(current_dests) = dests_by_file.remove(&current_file) {
202+
snippet = annotate_destinations(snippet, current_dests);
203+
}
204+
snippet = snippet.annotation(
205+
AnnotationKind::Context
206+
.span(offset..offset + 1)
207+
.label("1. source"),
208+
);
217209

218-
let renderer = Renderer::plain().decor_style(DecorStyle::Unicode);
219-
return Some(
220-
renderer
221-
.render(&groups)
222-
.to_string()
223-
// hacky cleanup to make the text shorter
224-
.replace("info: definition", ""),
210+
let mut groups = vec![Level::INFO.primary_title("definition").element(snippet)];
211+
212+
for (dest_file, dests) in dests_by_file {
213+
let path = file_paths.get(&dest_file).unwrap();
214+
let other_snippet = Snippet::source(dest_file.content(&db).as_ref())
215+
.path(*path)
216+
.fold(true);
217+
let other_snippet = annotate_destinations(other_snippet, dests);
218+
groups.push(
219+
Level::INFO
220+
.primary_title("definition")
221+
.element(other_snippet),
225222
);
226223
}
227-
None
224+
225+
let renderer = Renderer::plain().decor_style(DecorStyle::Unicode);
226+
Some(
227+
renderer
228+
.render(&groups)
229+
.to_string()
230+
// hacky cleanup to make the text shorter
231+
.replace("info: definition", ""),
232+
)
228233
}
229234

230235
fn goto_not_found(sql: &str) {
@@ -870,7 +875,7 @@ select now$0();
870875
β”‚ ─ 1. source
871876
β•°β•΄
872877
873-
β•­β–Έ builtin.sql:11089:28
878+
β•­β–Έ builtins.sql:11089:28
874879
β”‚
875880
11089 β”‚ create function pg_catalog.now() returns timestamp with time zone
876881
β•°β•΄ ─── 2. destination
@@ -888,7 +893,7 @@ select current_timestamp$0;
888893
β”‚ ─ 1. source
889894
β•°β•΄
890895
891-
β•­β–Έ builtin.sql:11089:28
896+
β•­β–Έ builtins.sql:11089:28
892897
β”‚
893898
11089 β”‚ create function pg_catalog.now() returns timestamp with time zone
894899
β•°β•΄ ─── 2. destination
@@ -989,7 +994,7 @@ select * from t where current_timestamp$0 > t.created_at;
989994
β”‚ ─ 1. source
990995
β•°β•΄
991996
992-
β•­β–Έ builtin.sql:11089:28
997+
β•­β–Έ builtins.sql:11089:28
993998
β”‚
994999
11089 β”‚ create function pg_catalog.now() returns timestamp with time zone
9951000
β•°β•΄ ─── 2. destination

0 commit comments

Comments
Β (0)