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

Let prisma_fmt::format() take parameters for tab size #2538

Merged
merged 1 commit into from
Dec 23, 2021
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
30 changes: 26 additions & 4 deletions prisma-fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ mod native;
mod preview;
mod text_document_completion;

use log::*;

/// The API is modelled on an LSP [completion
/// 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: &str, params: String) -> String {
let params = if let Ok(params) = serde_json::from_str::<lsp_types::CompletionParams>(&params) {
pub fn text_document_completion(schema: &str, params: &str) -> String {
let params = if let Ok(params) = serde_json::from_str::<lsp_types::CompletionParams>(params) {
params
} else {
warn!("Failed to parse params to text_document_completion() as CompletionParams.");
return serde_json::to_string(&text_document_completion::empty_completion_list()).unwrap();
};

Expand All @@ -20,9 +23,28 @@ pub fn text_document_completion(schema: &str, params: String) -> String {
serde_json::to_string(&completion_list).unwrap()
}

pub fn format(schema: String) -> String {
/// The two parameters are:
/// - The Prisma schema to reformat, as a string.
/// - An LSP
/// [DocumentFormattingParams](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#textDocument_formatting) object, as JSON.
///
/// The function returns the formatted schema, as a string.
///
/// Of the DocumentFormattingParams, we only take into account tabSize, at the moment.
pub fn format(schema: &str, params: &str) -> String {
use datamodel::ast::reformat::Reformatter;
Reformatter::new(&schema).reformat_to_string()

let params: lsp_types::DocumentFormattingParams = match serde_json::from_str(params) {
Ok(params) => params,
Err(err) => {
warn!("Error parsing DocumentFormattingParams params: {}", err);
return schema.to_owned();
}
};

let mut out = Vec::with_capacity(schema.len() / 2);
Reformatter::new(schema).reformat_to(&mut out, params.options.tab_size as usize);
String::from_utf8_lossy(&out).into_owned()
}

pub fn lint(schema: String) -> String {
Expand Down
2 changes: 1 addition & 1 deletion prisma-fmt/tests/text_document_completion/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) fn test_scenario(scenario_name: &str) {
context: None,
};

let result = prisma_fmt::text_document_completion(&schema, serde_json::to_string_pretty(&params).unwrap());
let result = prisma_fmt::text_document_completion(&schema, &serde_json::to_string_pretty(&params).unwrap());
// Prettify the JSON
let result =
serde_json::to_string_pretty(&serde_json::from_str::<lsp_types::CompletionList>(&result).unwrap()).unwrap();
Expand Down