-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fmt): lsp find references #4934
Merged
Merged
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
40da783
prep-work for references
Druue e1b602d
references
Druue dfdc2cd
rmv duplicate name
Druue cbf4605
Added test api for references
Druue 80c08de
test enums on name and as type
Druue 2ef3aa4
test composite types on name and as type
Druue 0c2fa93
test models on name and as type
Druue 891eddd
test datasource on name and as attribute
Druue 7d528fe
Actually use AttributePosition
Druue 69556c7
In the process of adding more granularity for references, fixed a bug…
Druue cf09ea0
find field in model from field in relation fields
Druue 36bbb34
find field in model from field in relation references
Druue cebc7eb
support finding fields from relations in views
Druue 17a8531
find field from block attribute index & unique
Druue e2fe053
find datasource from native type
Druue 65bdf1b
Update docs links
Druue 95625aa
clippy
Druue fffaaee
clean-up
Druue 28a71e4
map should be offered when we aren't in a specific part of an attribute
Druue 43181a6
remove info! logs
Druue 1d337ab
test that mssql default map doesn't show up inside specific sections …
Druue 1cb0fa7
consume split instead of collecting
Druue dc1be2a
correctly retrieve datasource identifier
Druue 5d25b91
clippy
Druue 3b70d69
don't unwrap split
Druue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
use log::*; | ||
use lsp_types::{Location, ReferenceParams, Url}; | ||
use psl::{ | ||
error_tolerant_parse_configuration, | ||
parser_database::ParserDatabase, | ||
schema_ast::ast::{ | ||
CompositeTypePosition, EnumPosition, Field, FieldId, FieldPosition, FieldType, Identifier, ModelPosition, | ||
SchemaPosition, SourcePosition, Top, WithIdentifier, WithName, | ||
}, | ||
Diagnostics, SourceFile, | ||
}; | ||
|
||
use crate::{offsets::position_to_offset, span_to_range, LSPContext}; | ||
|
||
pub(super) type ReferencesContext<'a> = LSPContext<'a, ReferenceParams>; | ||
|
||
pub(crate) fn empty_references() -> Vec<Location> { | ||
Vec::new() | ||
} | ||
|
||
pub(crate) fn references(schema_files: Vec<(String, SourceFile)>, params: ReferenceParams) -> Vec<Location> { | ||
info!("Finding references"); | ||
Druue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let (_, config, _) = error_tolerant_parse_configuration(&schema_files); | ||
|
||
let db = { | ||
let mut diag = Diagnostics::new(); | ||
ParserDatabase::new(&schema_files, &mut diag) | ||
}; | ||
|
||
let Some(initiating_file_id) = db.file_id(params.text_document_position.text_document.uri.as_str()) else { | ||
warn!("Initating file name is not found in the schema"); | ||
return empty_references(); | ||
}; | ||
|
||
let initiating_doc = db.source(initiating_file_id); | ||
|
||
let position = if let Some(pos) = position_to_offset(¶ms.text_document_position.position, initiating_doc) { | ||
pos | ||
} else { | ||
warn!("Received a position outside of the document boundaries in ReferenceParams"); | ||
return empty_references(); | ||
}; | ||
|
||
let target_position = db.ast(initiating_file_id).find_at_position(position); | ||
|
||
let ctx = ReferencesContext { | ||
db: &db, | ||
config: &config, | ||
initiating_file_id, | ||
params: ¶ms, | ||
}; | ||
|
||
reference_locations_for_target(ctx, target_position) | ||
} | ||
|
||
fn reference_locations_for_target(ctx: ReferencesContext<'_>, target: SchemaPosition) -> Vec<Location> { | ||
info!("{:?}", target); | ||
|
||
match target { | ||
// Blocks | ||
SchemaPosition::Model(_, ModelPosition::Name(name)) | ||
| SchemaPosition::Enum(_, EnumPosition::Name(name)) | ||
| SchemaPosition::CompositeType(_, CompositeTypePosition::Name(name)) => { | ||
find_where_used_as_top_name(&ctx, name) | ||
.into_iter() | ||
.chain(find_where_used_as_type(ctx, name)) | ||
.collect() | ||
} | ||
|
||
SchemaPosition::DataSource(_, SourcePosition::Name(name)) => find_where_used_for_native_type(&ctx, name), | ||
|
||
// Fields | ||
SchemaPosition::Model(_, ModelPosition::Field(_, FieldPosition::Type(r#type))) | ||
| SchemaPosition::CompositeType(_, CompositeTypePosition::Field(_, FieldPosition::Type(r#type))) => { | ||
find_where_used_as_top_name(&ctx, r#type) | ||
.into_iter() | ||
.chain(find_where_used_as_type(ctx, r#type)) | ||
.collect() | ||
} | ||
|
||
// Attributes | ||
SchemaPosition::Model(_, ModelPosition::Field(_, FieldPosition::Attribute(name, _, _))) | ||
| SchemaPosition::CompositeType(_, CompositeTypePosition::Field(_, FieldPosition::Attribute(name, _, _))) => { | ||
let ds_name = ctx.datasource().map(|ds| ds.name.as_str()); | ||
|
||
match ds_name { | ||
Some(ds_name) if name.contains(ds_name) => find_where_used_as_top_name(&ctx, ds_name), | ||
_ => empty_references(), | ||
} | ||
} | ||
|
||
_ => empty_references(), | ||
} | ||
} | ||
|
||
fn find_where_used_for_native_type(ctx: &ReferencesContext<'_>, name: &str) -> Vec<Location> { | ||
info!("Get references for native types"); | ||
|
||
let references = ctx | ||
Check failure on line 100 in prisma-fmt/src/references.rs GitHub Actions / clippy linting
|
||
.db | ||
.walk_tops() | ||
.into_iter() | ||
.flat_map(|top| match top.ast_top() { | ||
Top::CompositeType(composite_type) => find_native_type_locations(&ctx, name, composite_type.iter_fields()), | ||
Top::Model(model) => find_native_type_locations(&ctx, name, model.iter_fields()), | ||
|
||
Top::Enum(_) | Top::Source(_) | Top::Generator(_) => empty_references(), | ||
}) | ||
.collect(); | ||
|
||
references | ||
} | ||
|
||
fn find_native_type_locations<'a>( | ||
ctx: &ReferencesContext<'_>, | ||
name: &str, | ||
fields: impl Iterator<Item = (FieldId, &'a Field)>, | ||
) -> Vec<Location> { | ||
let identifiers = fields | ||
.filter_map(|field| { | ||
field | ||
.1 | ||
.attributes | ||
.iter() | ||
.find(|attr| extract_ds_from_native_type(attr.name()) == name) | ||
.map(|attr| attr.identifier()) | ||
}) | ||
.collect(); | ||
|
||
identifiers_to_locations(identifiers, &ctx) | ||
} | ||
|
||
fn extract_ds_from_native_type(attr_name: &str) -> &str { | ||
let split = attr_name.split(".").collect::<Vec<&str>>()[0]; | ||
split | ||
} | ||
|
||
fn find_where_used_as_type(ctx: ReferencesContext<'_>, name: &str) -> Vec<Location> { | ||
info!("Get references for top"); | ||
|
||
let references: Vec<Location> = ctx | ||
Check failure on line 142 in prisma-fmt/src/references.rs GitHub Actions / clippy linting
|
||
.db | ||
.walk_tops() | ||
.into_iter() | ||
.flat_map(|top| match top.ast_top() { | ||
Top::Model(model) => { | ||
info!("Get references in model"); | ||
|
||
let fields = model.iter_fields(); | ||
|
||
let identifiers = get_relevent_identifiers(fields, name); | ||
|
||
identifiers_to_locations(identifiers, &ctx) | ||
} | ||
Top::CompositeType(composite_type) => { | ||
info!("Get references in composite type"); | ||
|
||
let fields = composite_type.iter_fields(); | ||
let ids = get_relevent_identifiers(fields, name); | ||
|
||
identifiers_to_locations(ids, &ctx) | ||
} | ||
Top::Enum(_) | Top::Source(_) | Top::Generator(_) => empty_references(), | ||
}) | ||
.collect(); | ||
|
||
references | ||
} | ||
|
||
// fn find_where_used_in_relation(ctx: &ReferencesContext<'_>, name: &str) -> Vec<Location> { | ||
// empty_references() | ||
// } | ||
|
||
fn find_where_used_as_top_name(ctx: &ReferencesContext<'_>, name: &str) -> Vec<Location> { | ||
fn ident_to_location(id: &Identifier, name: &str, ctx: &ReferencesContext<'_>) -> Vec<Location> { | ||
if id.name == name { | ||
identifiers_to_locations(vec![id], ctx) | ||
} else { | ||
empty_references() | ||
} | ||
} | ||
|
||
let references: Vec<Location> = ctx | ||
Check failure on line 184 in prisma-fmt/src/references.rs GitHub Actions / clippy linting
|
||
.db | ||
.walk_tops() | ||
.into_iter() | ||
.flat_map(|top| match top.ast_top() { | ||
Top::CompositeType(composite_type) => ident_to_location(composite_type.identifier(), name, ctx), | ||
|
||
Top::Enum(enm) => ident_to_location(enm.identifier(), name, ctx), | ||
|
||
Top::Model(model) => ident_to_location(model.identifier(), name, ctx), | ||
|
||
Top::Source(source) => ident_to_location(source.identifier(), name, ctx), | ||
|
||
Top::Generator(_) => empty_references(), | ||
}) | ||
.collect(); | ||
|
||
references | ||
} | ||
|
||
fn get_relevent_identifiers<'a, 'b>( | ||
fields: impl Iterator<Item = (FieldId, &'a Field)>, | ||
name: &str, | ||
) -> Vec<&'a Identifier> { | ||
fields | ||
.filter_map(|(_id, field)| match &field.field_type { | ||
FieldType::Supported(id) => { | ||
if id.name == name { | ||
Some(id) | ||
} else { | ||
None | ||
} | ||
} | ||
FieldType::Unsupported(_, _) => None, | ||
}) | ||
.collect() | ||
} | ||
|
||
fn identifiers_to_locations<'a>(ids: Vec<&'a Identifier>, ctx: &ReferencesContext<'_>) -> Vec<Location> { | ||
ids.iter() | ||
.filter_map(|identifier| { | ||
let file_id = identifier.span.file_id; | ||
|
||
let source = ctx.db.source(file_id); | ||
let range = span_to_range(identifier.span, source); | ||
|
||
let file_name = ctx.db.file_name(file_id); | ||
|
||
let uri = if let Ok(uri) = Url::parse(file_name) { | ||
uri | ||
} else { | ||
warn!("Failed to parse file path: {:?}", file_name); | ||
return None; | ||
}; | ||
|
||
Some(Location { uri, range }) | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mod test_api; | ||
mod tests; |
14 changes: 14 additions & 0 deletions
14
prisma-fmt/tests/references/scenarios/composite_type_as_type/a.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["prismaSchemaFolder", "views"] | ||
} | ||
|
||
datasource db { | ||
provider = "mongodb" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
type Address { | ||
city String | ||
postCode String | ||
} |
5 changes: 5 additions & 0 deletions
5
prisma-fmt/tests/references/scenarios/composite_type_as_type/b.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
model User { | ||
id String @id @map("_id") | ||
authorId String | ||
address Add<|>ress | ||
} |
28 changes: 28 additions & 0 deletions
28
prisma-fmt/tests/references/scenarios/composite_type_as_type/result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
[ | ||
{ | ||
"uri": "file:///path/to/a.prisma", | ||
"range": { | ||
"start": { | ||
"line": 10, | ||
"character": 5 | ||
}, | ||
"end": { | ||
"line": 10, | ||
"character": 12 | ||
} | ||
} | ||
}, | ||
{ | ||
"uri": "file:///path/to/b.prisma", | ||
"range": { | ||
"start": { | ||
"line": 3, | ||
"character": 11 | ||
}, | ||
"end": { | ||
"line": 3, | ||
"character": 18 | ||
} | ||
} | ||
} | ||
] |
14 changes: 14 additions & 0 deletions
14
prisma-fmt/tests/references/scenarios/composite_type_name/a.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
previewFeatures = ["prismaSchemaFolder", "views"] | ||
} | ||
|
||
datasource db { | ||
provider = "mongodb" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
type Add<|>ress { | ||
city String | ||
postCode String | ||
} |
5 changes: 5 additions & 0 deletions
5
prisma-fmt/tests/references/scenarios/composite_type_name/b.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
model User { | ||
id String @id @map("_id") | ||
authorId String | ||
address Address | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where we actually handle finding all the references