Skip to content

Commit

Permalink
Fix clippy and get rid if initiating_file_name as a param
Browse files Browse the repository at this point in the history
  • Loading branch information
SevInf committed May 13, 2024
1 parent 5b5dc06 commit 10c3b38
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 31 deletions.
18 changes: 7 additions & 11 deletions prisma-fmt/src/code_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ pub(crate) fn empty_code_actions() -> Vec<CodeActionOrCommand> {

pub(crate) fn available_actions(
schema_files: Vec<(String, SourceFile)>,
initiating_file_name: &str,
params: CodeActionParams,
) -> Vec<CodeActionOrCommand> {
let mut actions = Vec::new();
Expand All @@ -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![];
};
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -293,5 +289,5 @@ pub(crate) fn parse_url(url: &str) -> Result<Url, Box<dyn std::error::Error>> {
if result.is_err() {
warn!("Could not parse url {url}")
}
return Ok(result?);
Ok(result?)
}
8 changes: 4 additions & 4 deletions prisma-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<lsp_types::CompletionParams>(params) {
params
} else {
Expand All @@ -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::<lsp_types::CodeActionParams>(params) {
params
} else {
Expand All @@ -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()
}

Expand Down
8 changes: 2 additions & 6 deletions prisma-fmt/src/text_document_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
};
Expand Down
1 change: 0 additions & 1 deletion prisma-fmt/tests/code_actions/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params).unwrap(),
);
// Prettify the JSON
Expand Down
1 change: 0 additions & 1 deletion prisma-fmt/tests/regressions/language_tools_1466.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params).unwrap(),
);
}
1 change: 0 additions & 1 deletion prisma-fmt/tests/regressions/language_tools_1473.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&params).unwrap(),
);
}
5 changes: 2 additions & 3 deletions prisma-fmt/tests/text_document_completion/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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(&params).unwrap(),
);
// Prettify the JSON
Expand Down
4 changes: 2 additions & 2 deletions prisma-schema-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &params)
prisma_fmt::text_document_completion(schema_files, &params)
}

/// This API is modelled on an LSP [code action
Expand Down
2 changes: 1 addition & 1 deletion psl/parser-database/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion psl/parser-database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 10c3b38

Please sign in to comment.