feat(purescript): Configure when the module is shown (#2357)

This makes it possible to configure when the purescript module is shown
based on the contents of a directory. This should make it possible to
be a lot more granular when configuring the module.
This commit is contained in:
Shu Kutsuzawa 2021-02-22 03:53:10 +09:00 committed by GitHub
parent efb82454f2
commit fe6f9eeb4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 17 deletions

View File

@ -2046,19 +2046,22 @@ format = "via [🔹 $version](147 bold) "
## PureScript ## PureScript
The `purescript` module shows the currently installed version of PureScript version. The `purescript` module shows the currently installed version of PureScript version.
The module will be shown if any of the following conditions are met: By default the module will be shown if any of the following conditions are met:
- The current directory contains a `spago.dhall` file - The current directory contains a `spago.dhall` file
- The current directory contains a \*.purs files - The current directory contains a file with the `.purs` extension
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ---------- | ------------------------------------ | ------------------------------------------------------------ | | -------------------- | ------------------------------------ | ------------------------------------------------------------ |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"<=> "` | The symbol used before displaying the version of PureScript. | | `symbol` | `"<=> "` | The symbol used before displaying the version of PureScript. |
| `style` | `"bold white"` | The style for the module. | | `detect_extensions` | `["purs"]` | Which extensions should trigger this moudle. |
| `disabled` | `false` | Disables the `purescript` module. | | `detect_files` | `["spago.dhall"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold white"` | The style for the module. |
| `disabled` | `false` | Disables the `purescript` module. |
### Variables ### Variables

View File

@ -8,6 +8,9 @@ pub struct PureScriptConfig<'a> {
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
} }
impl<'a> RootModuleConfig<'a> for PureScriptConfig<'a> { impl<'a> RootModuleConfig<'a> for PureScriptConfig<'a> {
@ -17,6 +20,9 @@ impl<'a> RootModuleConfig<'a> for PureScriptConfig<'a> {
symbol: "<=> ", symbol: "<=> ",
style: "bold white", style: "bold white",
disabled: false, disabled: false,
detect_extensions: vec!["purs"],
detect_files: vec!["spago.dhall"],
detect_folders: vec![],
} }
} }
} }

View File

@ -4,24 +4,20 @@ use crate::configs::purescript::PureScriptConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
/// Creates a module with the current PureScript version /// Creates a module with the current PureScript version
///
/// Will display the PureScript version if any of the following criteria are met:
/// - Current directory contains a `spago.dhall` file
/// - Current directory contains a `*.purs` files
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("purescript");
let config: PureScriptConfig = PureScriptConfig::try_load(module.config);
let is_purs_project = context let is_purs_project = context
.try_begin_scan()? .try_begin_scan()?
.set_files(&["spago.dhall"]) .set_files(&config.detect_files)
.set_extensions(&["purs"]) .set_folders(&config.detect_folders)
.set_extensions(&config.detect_extensions)
.is_match(); .is_match();
if !is_purs_project { if !is_purs_project {
return None; return None;
} }
let mut module = context.new_module("purescript");
let config: PureScriptConfig = PureScriptConfig::try_load(module.config);
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|variable, _| match variable { .map_meta(|variable, _| match variable {