From 0dbeb774ba9ea618ff1e92b63ab31e5caf3003dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 13 Sep 2021 20:19:10 +0200 Subject: feat(fmt): add support for configuration file (#11944) This commit adds support for configuration file for "deno fmt" subcommand. It is also respected by LSP when formatting files. Example configuration: { "fmt": { "files": { "include": ["src/"], "exclude": ["src/testdata/"] }, "options": { "useTabs": true, "lineWidth": 80, "indentWidth": 4, "singleQuote": true, "textWrap": "preserve" } } } --- cli/config_file.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) (limited to 'cli/config_file.rs') diff --git a/cli/config_file.rs b/cli/config_file.rs index 94373334a..dd002ca97 100644 --- a/cli/config_file.rs +++ b/cli/config_file.rs @@ -277,7 +277,7 @@ pub struct LintRulesConfig { #[derive(Clone, Debug, Default, Deserialize)] #[serde(default, deny_unknown_fields)] -pub struct LintFilesConfig { +pub struct FilesConfig { pub include: Vec, pub exclude: Vec, } @@ -286,7 +286,32 @@ pub struct LintFilesConfig { #[serde(default, deny_unknown_fields)] pub struct LintConfig { pub rules: LintRulesConfig, - pub files: LintFilesConfig, + pub files: FilesConfig, +} + +#[derive(Clone, Copy, Debug, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] +pub enum ProseWrap { + Always, + Never, + Preserve, +} + +#[derive(Clone, Debug, Default, Deserialize)] +#[serde(default, deny_unknown_fields, rename_all = "camelCase")] +pub struct FmtOptionsConfig { + pub use_tabs: Option, + pub line_width: Option, + pub indent_width: Option, + pub single_quote: Option, + pub prose_wrap: Option, +} + +#[derive(Clone, Debug, Default, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct FmtConfig { + pub options: FmtOptionsConfig, + pub files: FilesConfig, } #[derive(Clone, Debug, Deserialize)] @@ -294,6 +319,7 @@ pub struct LintConfig { pub struct ConfigFileJson { pub compiler_options: Option, pub lint: Option, + pub fmt: Option, } #[derive(Clone, Debug)] @@ -374,6 +400,16 @@ impl ConfigFile { Ok(None) } } + + pub fn to_fmt_config(&self) -> Result, AnyError> { + if let Some(config) = self.json.fmt.clone() { + let fmt_config: FmtConfig = serde_json::from_value(config) + .context("Failed to parse \"fmt\" configuration")?; + Ok(Some(fmt_config)) + } else { + Ok(None) + } + } } #[cfg(test)] @@ -441,6 +477,19 @@ mod tests { "tags": ["recommended"], "include": ["ban-untagged-todo"] } + }, + "fmt": { + "files": { + "include": ["src/"], + "exclude": ["src/testdata/"] + }, + "options": { + "useTabs": true, + "lineWidth": 80, + "indentWidth": 4, + "singleQuote": true, + "proseWrap": "preserve" + } } }"#; let config_path = PathBuf::from("/deno/tsconfig.json"); @@ -474,6 +523,17 @@ mod tests { Some(vec!["recommended".to_string()]) ); assert!(lint_config.rules.exclude.is_none()); + + let fmt_config = config_file + .to_fmt_config() + .expect("error parsing fmt object") + .expect("fmt object should be defined"); + assert_eq!(fmt_config.files.include, vec!["src/"]); + assert_eq!(fmt_config.files.exclude, vec!["src/testdata/"]); + assert_eq!(fmt_config.options.use_tabs, Some(true)); + assert_eq!(fmt_config.options.line_width, Some(80)); + assert_eq!(fmt_config.options.indent_width, Some(4)); + assert_eq!(fmt_config.options.single_quote, Some(true)); } #[test] -- cgit v1.2.3