diff --git a/prisma-fmt/src/code_actions.rs b/prisma-fmt/src/code_actions.rs index bacad1ac79b6..8ffd8f1d569c 100644 --- a/prisma-fmt/src/code_actions.rs +++ b/prisma-fmt/src/code_actions.rs @@ -76,7 +76,6 @@ pub(crate) fn empty_code_actions() -> Vec { pub(crate) fn available_actions( schema_files: Vec<(String, SourceFile)>, - initiating_file_name: &str, params: CodeActionParams, ) -> Vec { let mut actions = Vec::new(); @@ -86,7 +85,8 @@ pub(crate) fn available_actions( let config = &validated_schema.configuration; let datasource = config.datasources.first(); - let Some(initiating_file_id) = validated_schema.db.file_id(initiating_file_name) else { + let file_uri = params.text_document.uri.as_str(); + let Some(initiating_file_id) = validated_schema.db.file_id(file_uri) else { warn!("Initiating file name is not found in the schema"); return vec![]; }; @@ -141,14 +141,10 @@ pub(crate) fn available_actions( relations::add_referencing_side_unique(&mut actions, &context, complete_relation); } - if relation.referencing_model().is_defined_in_file(initiating_file_id) { - if validated_schema.relation_mode().is_prisma() { - relations::add_index_for_relation_fields( - &mut actions, - &context, - complete_relation.referencing_field(), - ); - } + if validated_schema.relation_mode().is_prisma() + && relation.referencing_model().is_defined_in_file(initiating_file_id) + { + relations::add_index_for_relation_fields(&mut actions, &context, complete_relation.referencing_field()); } if validated_schema.relation_mode().uses_foreign_keys() { @@ -293,5 +289,5 @@ pub(crate) fn parse_url(url: &str) -> Result> { if result.is_err() { warn!("Could not parse url {url}") } - return Ok(result?); + Ok(result?) } diff --git a/prisma-fmt/src/lib.rs b/prisma-fmt/src/lib.rs index 79979c4ffe47..d44c9b76938e 100644 --- a/prisma-fmt/src/lib.rs +++ b/prisma-fmt/src/lib.rs @@ -19,7 +19,7 @@ use schema_file_input::SchemaFileInput; /// request](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#textDocument_completion). /// Input and output are both JSON, the request being a `CompletionParams` object and the response /// being a `CompletionList` object. -pub fn text_document_completion(schema_files: String, initiating_file_name: &str, params: &str) -> String { +pub fn text_document_completion(schema_files: String, params: &str) -> String { let params = if let Ok(params) = serde_json::from_str::(params) { params } else { @@ -32,13 +32,13 @@ pub fn text_document_completion(schema_files: String, initiating_file_name: &str return serde_json::to_string(&text_document_completion::empty_completion_list()).unwrap(); }; - let completion_list = text_document_completion::completion(input.into(), &initiating_file_name, params); + let completion_list = text_document_completion::completion(input.into(), params); serde_json::to_string(&completion_list).unwrap() } /// This API is modelled on an LSP [code action request](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#textDocument_codeAction=). Input and output are both JSON, the request being a `CodeActionParams` object and the response being a list of `CodeActionOrCommand` objects. -pub fn code_actions(schema_files: String, initiating_file_name: &str, params: &str) -> String { +pub fn code_actions(schema_files: String, params: &str) -> String { let params = if let Ok(params) = serde_json::from_str::(params) { params } else { @@ -51,7 +51,7 @@ pub fn code_actions(schema_files: String, initiating_file_name: &str, params: &s return serde_json::to_string(&text_document_completion::empty_completion_list()).unwrap(); }; - let actions = code_actions::available_actions(input.into(), &initiating_file_name, params); + let actions = code_actions::available_actions(input.into(), params); serde_json::to_string(&actions).unwrap() } diff --git a/prisma-fmt/src/text_document_completion.rs b/prisma-fmt/src/text_document_completion.rs index 8f0c7e9fb649..49151c27b497 100644 --- a/prisma-fmt/src/text_document_completion.rs +++ b/prisma-fmt/src/text_document_completion.rs @@ -20,11 +20,7 @@ pub(crate) fn empty_completion_list() -> CompletionList { } } -pub(crate) fn completion( - schema_files: Vec<(String, SourceFile)>, - initiating_file_name: &str, - params: CompletionParams, -) -> CompletionList { +pub(crate) fn completion(schema_files: Vec<(String, SourceFile)>, params: CompletionParams) -> CompletionList { let config = parse_configuration_multi_file(&schema_files) .ok() .map(|(_, config)| config); @@ -39,7 +35,7 @@ pub(crate) fn completion( ParserDatabase::new(&schema_files, &mut diag) }; - let Some(initiating_file_id) = db.file_id(initiating_file_name) else { + let Some(initiating_file_id) = db.file_id(params.text_document_position.text_document.uri.as_str()) else { warn!("Initiating file name is not found in the schema"); return empty_completion_list(); }; diff --git a/prisma-fmt/tests/code_actions/test_api.rs b/prisma-fmt/tests/code_actions/test_api.rs index f8b19057b5d4..8b060455307a 100644 --- a/prisma-fmt/tests/code_actions/test_api.rs +++ b/prisma-fmt/tests/code_actions/test_api.rs @@ -113,7 +113,6 @@ pub(crate) fn test_scenario(scenario_name: &str) { let result = prisma_fmt::code_actions( serde_json::to_string_pretty(&schema_files).unwrap(), - initiating_file_name, &serde_json::to_string_pretty(¶ms).unwrap(), ); // Prettify the JSON diff --git a/prisma-fmt/tests/regressions/language_tools_1466.rs b/prisma-fmt/tests/regressions/language_tools_1466.rs index a7fada4267cf..bdb86e7fa218 100644 --- a/prisma-fmt/tests/regressions/language_tools_1466.rs +++ b/prisma-fmt/tests/regressions/language_tools_1466.rs @@ -29,7 +29,6 @@ fn code_actions_should_not_crash_on_validation_errors_with_mongodb() { prisma_fmt::code_actions( serde_json::to_string_pretty(&[("schema.prisma", schema.to_owned())]).unwrap(), - "schema.prisma".into(), &serde_json::to_string_pretty(¶ms).unwrap(), ); } diff --git a/prisma-fmt/tests/regressions/language_tools_1473.rs b/prisma-fmt/tests/regressions/language_tools_1473.rs index 604dbe98eae7..274e71873cf7 100644 --- a/prisma-fmt/tests/regressions/language_tools_1473.rs +++ b/prisma-fmt/tests/regressions/language_tools_1473.rs @@ -32,7 +32,6 @@ fn code_actions_should_not_crash_on_validation_errors_with_multi_schema() { prisma_fmt::code_actions( serde_json::to_string_pretty(&[("schema.prisma", schema.to_owned())]).unwrap(), - "schema.prisma".into(), &serde_json::to_string_pretty(¶ms).unwrap(), ); } diff --git a/prisma-fmt/tests/text_document_completion/test_api.rs b/prisma-fmt/tests/text_document_completion/test_api.rs index 8db074e42ea1..122196157e6f 100644 --- a/prisma-fmt/tests/text_document_completion/test_api.rs +++ b/prisma-fmt/tests/text_document_completion/test_api.rs @@ -18,11 +18,11 @@ pub(crate) fn test_scenario(scenario_name: &str) { write!(path, "{SCENARIOS_PATH}/{scenario_name}/result.json").unwrap(); let expected_result = std::fs::read_to_string(&path).unwrap_or_else(|_| String::new()); - let (initiating_file_name, cursor_position, schema_files) = take_cursor(schema_files); + let (initiating_file_uri, cursor_position, schema_files) = take_cursor(schema_files); let params = lsp_types::CompletionParams { text_document_position: lsp_types::TextDocumentPositionParams { text_document: lsp_types::TextDocumentIdentifier { - uri: "https://example.com/meow".parse().unwrap(), + uri: initiating_file_uri.parse().unwrap(), }, // ignored position: cursor_position, }, @@ -35,7 +35,6 @@ pub(crate) fn test_scenario(scenario_name: &str) { let result = prisma_fmt::text_document_completion( serde_json::to_string_pretty(&schema_files).unwrap(), - &initiating_file_name, &serde_json::to_string_pretty(¶ms).unwrap(), ); // Prettify the JSON diff --git a/prisma-schema-wasm/src/lib.rs b/prisma-schema-wasm/src/lib.rs index d0b732d33fcd..4e2797571d4d 100644 --- a/prisma-schema-wasm/src/lib.rs +++ b/prisma-schema-wasm/src/lib.rs @@ -87,9 +87,9 @@ pub fn preview_features() -> String { /// Input and output are both JSON, the request being a `CompletionParams` object and the response /// being a `CompletionList` object. #[wasm_bindgen] -pub fn text_document_completion(schema_files: String, initiating_file_id: u32, params: String) -> String { +pub fn text_document_completion(schema_files: String, params: String) -> String { register_panic_hook(); - prisma_fmt::text_document_completion(schema_files, initiating_file_id, ¶ms) + prisma_fmt::text_document_completion(schema_files, ¶ms) } /// This API is modelled on an LSP [code action diff --git a/psl/parser-database/src/files.rs b/psl/parser-database/src/files.rs index 73cf9dacbed5..b43154927c3c 100644 --- a/psl/parser-database/src/files.rs +++ b/psl/parser-database/src/files.rs @@ -13,7 +13,7 @@ impl Files { /// Create a new Files instance from multiple files. pub fn new(files: &[(String, schema_ast::SourceFile)], diagnostics: &mut Diagnostics) -> Self { let asts = files - .into_iter() + .iter() .enumerate() .map(|(file_idx, (path, source))| { let id = FileId(file_idx as u32); diff --git a/psl/parser-database/src/lib.rs b/psl/parser-database/src/lib.rs index e53cdda640c6..4f6c81257740 100644 --- a/psl/parser-database/src/lib.rs +++ b/psl/parser-database/src/lib.rs @@ -89,7 +89,7 @@ impl ParserDatabase { /// See the docs on [ParserDatabase](/struct.ParserDatabase.html). pub fn new(schemas: &[(String, schema_ast::SourceFile)], diagnostics: &mut Diagnostics) -> Self { - let asts = Files::new(&schemas, diagnostics); + let asts = Files::new(schemas, diagnostics); let mut interner = Default::default(); let mut names = Default::default();