Skip to content
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

Experiment with multi-file schema handling in PSL #4243

Merged
merged 6 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions prisma-fmt/src/code_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec

let datasource = config.datasources.first();

for source in validated_schema.db.ast().sources() {
relation_mode::edit_referential_integrity(&mut actions, &params, validated_schema.db.source(), source)
for source in validated_schema.db.ast_assert_single().sources() {
relation_mode::edit_referential_integrity(
&mut actions,
&params,
validated_schema.db.source_assert_single(),
source,
)
}

// models AND views
Expand All @@ -45,21 +50,27 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec
multi_schema::add_schema_block_attribute_model(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
config,
model,
);

multi_schema::add_schema_to_schemas(&mut actions, &params, validated_schema.db.source(), config, model);
multi_schema::add_schema_to_schemas(
&mut actions,
&params,
validated_schema.db.source_assert_single(),
config,
model,
);
}

if matches!(datasource, Some(ds) if ds.active_provider == "mongodb") {
mongodb::add_at_map_for_id(&mut actions, &params, validated_schema.db.source(), model);
mongodb::add_at_map_for_id(&mut actions, &params, validated_schema.db.source_assert_single(), model);

mongodb::add_native_for_auto_id(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
model,
datasource.unwrap(),
);
Expand All @@ -71,7 +82,7 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec
multi_schema::add_schema_block_attribute_enum(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
config,
enumerator,
)
Expand All @@ -88,15 +99,15 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec
relations::add_referenced_side_unique(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
complete_relation,
);

if relation.is_one_to_one() {
relations::add_referencing_side_unique(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
complete_relation,
);
}
Expand All @@ -105,7 +116,7 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec
relations::add_index_for_relation_fields(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
complete_relation.referencing_field(),
);
}
Expand All @@ -114,7 +125,7 @@ pub(crate) fn available_actions(schema: String, params: CodeActionParams) -> Vec
relation_mode::replace_set_default_mysql(
&mut actions,
&params,
validated_schema.db.source(),
validated_schema.db.source_assert_single(),
complete_relation,
config,
)
Expand Down
2 changes: 1 addition & 1 deletion prisma-fmt/src/code_actions/multi_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub(super) fn add_schema_to_schemas(
formatted_attribute,
true,
// todo: update spans so that we can just append to the end of the _inside_ of the array. Instead of needing to re-append the `]` or taking the span end -1
Span::new(span.start, span.end - 1),
Span::new(span.start, span.end - 1, psl::parser_database::FileId::ZERO),
params,
)
}
Expand Down
2 changes: 1 addition & 1 deletion prisma-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ pub(crate) fn range_to_span(range: Range, document: &str) -> ast::Span {
let start = position_to_offset(&range.start, document).unwrap();
let end = position_to_offset(&range.end, document).unwrap();

ast::Span::new(start, end)
ast::Span::new(start, end, psl::parser_database::FileId::ZERO)
}

/// Gives the LSP position right after the given span.
Expand Down
6 changes: 3 additions & 3 deletions prisma-fmt/src/text_document_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) fn completion(schema: String, params: CompletionParams) -> Completion

let db = {
let mut diag = Diagnostics::new();
ParserDatabase::new(source_file, &mut diag)
ParserDatabase::new_single_file(source_file, &mut diag)
};

let ctx = CompletionContext {
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'a> CompletionContext<'a> {
}

fn push_ast_completions(ctx: CompletionContext<'_>, completion_list: &mut CompletionList) {
match ctx.db.ast().find_at_position(ctx.position) {
match ctx.db.ast_assert_single().find_at_position(ctx.position) {
ast::SchemaPosition::Model(
_model_id,
ast::ModelPosition::Field(_, ast::FieldPosition::Attribute("relation", _, Some(attr_name))),
Expand Down Expand Up @@ -190,7 +190,7 @@ fn ds_has_prop(ctx: CompletionContext<'_>, prop: &str) -> bool {

fn push_namespaces(ctx: CompletionContext<'_>, completion_list: &mut CompletionList) {
for (namespace, _) in ctx.namespaces() {
let insert_text = if add_quotes(ctx.params, ctx.db.source()) {
let insert_text = if add_quotes(ctx.params, ctx.db.source_assert_single()) {
format!(r#""{namespace}""#)
} else {
namespace.to_string()
Expand Down
2 changes: 1 addition & 1 deletion prisma-fmt/src/text_document_completion/datasource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub(super) fn url_env_db_completion(completion_list: &mut CompletionList, kind:
_ => unreachable!(),
};

let insert_text = if add_quotes(ctx.params, ctx.db.source()) {
let insert_text = if add_quotes(ctx.params, ctx.db.source_assert_single()) {
format!(r#""{text}""#)
} else {
text.to_owned()
Expand Down
8 changes: 4 additions & 4 deletions prisma-fmt/tests/code_actions/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn parse_schema_diagnostics(file: impl Into<SourceFile>) -> Option<Vec<Diagnosti
severity: Some(DiagnosticSeverity::WARNING),
message: warn.message().to_owned(),
range: lsp_types::Range {
start: offset_to_position(warn.span().start, schema.db.source()),
end: offset_to_position(warn.span().end, schema.db.source()),
start: offset_to_position(warn.span().start, schema.db.source_assert_single()),
end: offset_to_position(warn.span().end, schema.db.source_assert_single()),
},
..Default::default()
});
Expand All @@ -31,8 +31,8 @@ fn parse_schema_diagnostics(file: impl Into<SourceFile>) -> Option<Vec<Diagnosti
severity: Some(DiagnosticSeverity::ERROR),
message: error.message().to_owned(),
range: lsp_types::Range {
start: offset_to_position(error.span().start, schema.db.source()),
end: offset_to_position(error.span().end, schema.db.source()),
start: offset_to_position(error.span().start, schema.db.source_assert_single()),
end: offset_to_position(error.span().end, schema.db.source_assert_single()),
},
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion psl/diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ mod warning;
pub use collection::Diagnostics;
pub use error::DatamodelError;
pub use native_type_error_factory::NativeTypeErrorFactory;
pub use span::Span;
pub use span::{FileId, Span};
pub use warning::DatamodelWarning;
26 changes: 21 additions & 5 deletions psl/diagnostics/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
/// The stable identifier for a PSL file.
#[derive(Debug, PartialEq, Clone, Copy, Hash, Eq, PartialOrd, Ord)]
pub struct FileId(pub u32); // we can't encapsulate because it would be a circular crate
// dependency between diagnostics and parser-database

impl FileId {
pub const ZERO: FileId = FileId(0);
pub const MAX: FileId = FileId(u32::MAX);
}

/// Represents a location in a datamodel's text representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
pub file_id: FileId,
}

impl Span {
/// Constructor.
pub fn new(start: usize, end: usize) -> Span {
Span { start, end }
pub fn new(start: usize, end: usize, file_id: FileId) -> Span {
Span { start, end, file_id }
}

/// Creates a new empty span.
pub fn empty() -> Span {
Span { start: 0, end: 0 }
Span {
start: 0,
end: 0,
file_id: FileId::ZERO,
}
}

/// Is the given position inside the span? (boundaries included)
Expand All @@ -27,11 +42,12 @@ impl Span {
}
}

impl From<pest::Span<'_>> for Span {
fn from(s: pest::Span<'_>) -> Self {
impl From<(FileId, pest::Span<'_>)> for Span {
fn from((file_id, s): (FileId, pest::Span<'_>)) -> Self {
Span {
start: s.start(),
end: s.end(),
file_id,
}
}
}
Loading
Loading