-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #111 - Add JSON formatting to Rust CLI
- Loading branch information
Showing
37 changed files
with
1,128 additions
and
280 deletions.
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
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
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
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,59 @@ | ||
use core::slice::{Iter, IterMut}; | ||
use std::path::PathBuf; | ||
use std::collections::HashMap; | ||
use super::configuration::{ConfigurationDiagnostic, GlobalConfiguration}; | ||
|
||
/// Plugin that can be implemented for use in the CLI. | ||
pub trait Plugin : std::marker::Sync { | ||
/// The name of the plugin. | ||
fn name(&self) -> &'static str; | ||
/// The version of the plugin. | ||
fn version(&self) -> &'static str; | ||
/// Gets the possible keys that can be used in the configuration JSON. | ||
fn config_keys(&self) -> Vec<String>; | ||
/// Initializes the plugin. | ||
fn initialize(&mut self, plugin_config: HashMap<String, String>, global_config: &GlobalConfiguration); | ||
/// Gets whether the specified file should be formatted. | ||
fn should_format_file(&self, file_path: &PathBuf, file_text: &str) -> bool; | ||
/// Gets the configuration as a collection of key value pairs. | ||
fn get_resolved_config(&self) -> String; | ||
/// Gets the configuration diagnostics. | ||
fn get_configuration_diagnostics(&self) -> &Vec<ConfigurationDiagnostic>; | ||
/// Formats the text in memory based on the file path and file text. | ||
fn format_text(&self, file_path: &PathBuf, file_text: &str) -> Result<String, String>; | ||
} | ||
|
||
/// A formatter constructed from a collection of plugins. | ||
pub struct Formatter { | ||
plugins: Vec<Box<dyn Plugin>>, | ||
} | ||
|
||
impl Formatter { | ||
/// Creates a new formatter | ||
pub fn new(plugins: Vec<Box<dyn Plugin>>) -> Formatter { | ||
Formatter { plugins } | ||
} | ||
|
||
/// Iterates over the plugins. | ||
pub fn iter_plugins(&self) -> Iter<'_, Box<dyn Plugin>> { | ||
self.plugins.iter() | ||
} | ||
|
||
/// Iterates over the plugins with a mutable iterator. | ||
pub fn iter_plugins_mut(&mut self) -> IterMut<'_, Box<dyn Plugin>> { | ||
self.plugins.iter_mut() | ||
} | ||
|
||
/// Formats the file text with one of the plugins. | ||
/// | ||
/// Returns the string when a plugin formatted or error. Otherwise None when no plugin was found. | ||
pub fn format_text(&self, file_path: &PathBuf, file_text: &str) -> Result<Option<String>, String> { | ||
for plugin in self.plugins.iter() { | ||
if plugin.should_format_file(file_path, file_text) { | ||
return plugin.format_text(file_path, file_text).map(|x| Some(x)); | ||
} | ||
} | ||
|
||
Ok(None) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
pub mod configuration; | ||
mod format_text; | ||
mod parser; | ||
mod plugin; | ||
|
||
pub use format_text::format_text; | ||
pub use plugin::JsoncPlugin; |
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,55 @@ | ||
use std::collections::HashMap; | ||
use dprint_core::configuration::{ConfigurationDiagnostic, ResolveConfigurationResult, GlobalConfiguration}; | ||
use std::path::PathBuf; | ||
use dprint_core::plugins::*; | ||
use super::configuration::{Configuration, resolve_config}; | ||
use super::format_text::format_text; | ||
|
||
/// JSONC Dprint CLI Plugin. | ||
pub struct JsoncPlugin { | ||
resolve_config_result: Option<ResolveConfigurationResult<Configuration>>, | ||
} | ||
|
||
impl JsoncPlugin { | ||
pub fn new() -> JsoncPlugin { | ||
JsoncPlugin { | ||
resolve_config_result: None, | ||
} | ||
} | ||
|
||
fn get_resolved_config_result(&self) -> &ResolveConfigurationResult<Configuration> { | ||
self.resolve_config_result.as_ref().expect("Plugin must be initialized.") | ||
} | ||
} | ||
|
||
impl Plugin for JsoncPlugin { | ||
fn name(&self) -> &'static str { env!("CARGO_PKG_NAME") } | ||
fn version(&self) -> &'static str { env!("CARGO_PKG_VERSION") } | ||
fn config_keys(&self) -> Vec<String> { vec![String::from("json"), String::from("jsonc")] } | ||
|
||
fn initialize(&mut self, plugin_config: HashMap<String, String>, global_config: &GlobalConfiguration) { | ||
self.resolve_config_result = Some(resolve_config(plugin_config, &global_config)); | ||
} | ||
|
||
fn should_format_file(&self, file_path: &PathBuf, _: &str) -> bool { | ||
if let Some(ext) = file_path.extension().and_then(|e| e.to_str()) { | ||
String::from(ext).to_lowercase() == "json" | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
fn get_resolved_config(&self) -> String { | ||
let config = &self.get_resolved_config_result().config; | ||
serde_json::to_string(config).unwrap() | ||
} | ||
|
||
fn get_configuration_diagnostics(&self) -> &Vec<ConfigurationDiagnostic> { | ||
&self.get_resolved_config_result().diagnostics | ||
} | ||
|
||
fn format_text(&self, _: &PathBuf, file_text: &str) -> Result<String, String> { | ||
let config = &self.get_resolved_config_result().config; | ||
format_text(file_text, config) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "dprint-plugin-typescript" | ||
description = "TypeScript formatter for dprint." | ||
keywords = ["formatting", "formatter", "typescript"] | ||
version = "0.13.2" | ||
version = "0.14.0" | ||
authors = ["David Sherret <[email protected]>"] | ||
edition = "2018" | ||
license = "MIT" | ||
|
@@ -15,6 +15,7 @@ swc_common = "=0.5.9" | |
swc_ecma_ast = "=0.18.1" | ||
swc_ecma_parser = "=0.21.9" | ||
serde = { version = "1.0.88", features = ["derive"] } | ||
serde_json = "1.0" | ||
|
||
[dev-dependencies] | ||
dprint-development = { path = "../rust-development" } | ||
|
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
Oops, something went wrong.