Skip to content

Commit 47bb071

Browse files
committed
fix: add config for warning on transaction flag #816
1 parent 68336c5 commit 47bb071

File tree

5 files changed

+230
-22
lines changed

5 files changed

+230
-22
lines changed

README.md

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ The language server accepts configuration via LSP initialization options:
220220
"currency_column": 60,
221221
"account_amount_spacing": 2,
222222
"number_currency_spacing": 1
223-
}
223+
},
224+
"diagnostic_flags": ["!"] // Optional: flags that generate warnings (default: ["!"])
224225
}
225226
```
226227

@@ -368,6 +369,46 @@ If the PyO3 embedded checker is not working:
368369

369370
Check your configuration if you need to explicitly set a method.
370371

372+
### Diagnostic Flags Configuration
373+
374+
By default, the language server generates warnings for all flagged transactions (entries with flags like `!`, `P`, etc.). You can configure which flags should generate diagnostics to reduce noise from intentional flags.
375+
376+
| Option | Type | Description | Default |
377+
| ------------------ | -------- | ---------------------------------------------------------------- | ------- |
378+
| `diagnostic_flags` | string[] | List of transaction flags that should generate warning diagnostics | `["!"]` |
379+
380+
**Default behavior** (only `!` flag generates warnings):
381+
382+
```json
383+
{
384+
"diagnostic_flags": ["!"]
385+
}
386+
```
387+
388+
This means padding transactions with `P` flag, or other custom flags, won't generate warnings.
389+
390+
**Include multiple flags:**
391+
392+
```json
393+
{
394+
"diagnostic_flags": ["!", "P"]
395+
}
396+
```
397+
398+
This generates warnings for both `!` (needs attention) and `P` (padding) flags.
399+
400+
**Disable all flag diagnostics:**
401+
402+
```json
403+
{
404+
"diagnostic_flags": []
405+
}
406+
```
407+
408+
This completely disables warnings for flagged transactions.
409+
410+
**Use case**: Some users intentionally use flags like `P` for padding transactions that will persist forever in the ledger. With the default configuration, these won't generate noise in your diagnostics panel, while `!` flags (which typically indicate transactions needing review) will still show warnings.
411+
371412
### Formatting Options
372413

373414
| Option | Type | Description | Default | Bean-format Equivalent |
@@ -465,7 +506,9 @@ This controls the whitespace between numbers and currency codes:
465506
"prefix_width": 30,
466507
"currency_column": 60,
467508
"number_currency_spacing": 1
468-
}
509+
},
510+
// Optional: flags that generate warnings (default: ["!"])
511+
"beancountLangServer.diagnosticFlags": ["!"]
469512
}
470513
```
471514

@@ -493,6 +536,8 @@ return {
493536
init_options = {
494537
-- Optional: Only needed for multi-file projects with include directives
495538
journal_file = "main.bean",
539+
-- Optional: flags that generate warnings (default: ["!"])
540+
diagnostic_flags = { "!" },
496541
},
497542
settings = {
498543
beancount = {
@@ -521,6 +566,8 @@ lspconfig.beancount.setup({
521566
currency_column = 60,
522567
number_currency_spacing = 1,
523568
},
569+
-- Optional: flags that generate warnings (default: {"!"})
570+
diagnostic_flags = { "!" },
524571
},
525572
})
526573

crates/lsp/src/beancount_data.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,9 @@ fn get_note_query() -> &'static tree_sitter::Query {
9595
pub struct FlaggedEntry {
9696
_file: String,
9797
pub line: u32,
98+
pub flag: String,
9899
}
99100

100-
//impl FlaggedEntry {
101-
// pub fn new(file: String, line: u32) -> Self {
102-
// Self { file, line }
103-
// }
104-
//}
105-
106101
#[derive(Clone, Debug)]
107102
pub struct BeancountData {
108103
accounts: Arc<Vec<String>>,
@@ -170,10 +165,16 @@ impl BeancountData {
170165
links_set.insert(text_for_tree_sitter_node(content, &capture.node));
171166
}
172167
idx if idx == flag_idx => {
173-
tracing::debug!("adding flag entry: {:?}", capture.node);
168+
let flag_text = text_for_tree_sitter_node(content, &capture.node);
169+
tracing::debug!(
170+
"adding flag entry: {:?} with flag '{}'",
171+
capture.node,
172+
flag_text
173+
);
174174
flagged_entries.push(FlaggedEntry {
175175
_file: "".to_string(),
176176
line: capture.node.start_position().row as u32,
177+
flag: flag_text,
177178
});
178179
}
179180
idx if idx == account_idx => {

crates/lsp/src/config.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct Config {
1212
pub journal_root: Option<PathBuf>,
1313
pub formatting: FormattingConfig,
1414
pub bean_check: BeancountCheckConfig,
15+
/// Flags that should generate diagnostics (e.g., ["!"] for only exclamation mark)
16+
pub diagnostic_flags: Vec<String>,
1517
}
1618

1719
#[derive(Debug, Clone)]
@@ -62,6 +64,7 @@ impl Config {
6264
journal_root: None,
6365
formatting: FormattingConfig::default(),
6466
bean_check: BeancountCheckConfig::new(),
67+
diagnostic_flags: vec!["!".to_string()],
6568
}
6669
}
6770
pub fn update(&mut self, json: serde_json::Value) -> Result<()> {
@@ -126,6 +129,11 @@ impl Config {
126129
}
127130
}
128131

132+
// Update diagnostic_flags configuration
133+
if let Some(diagnostic_flags) = beancount_lsp_settings.diagnostic_flags {
134+
self.diagnostic_flags = diagnostic_flags;
135+
}
136+
129137
Ok(())
130138
}
131139
}
@@ -135,6 +143,8 @@ pub struct BeancountLspOptions {
135143
pub journal_file: Option<String>,
136144
pub formatting: Option<FormattingOptions>,
137145
pub bean_check: Option<BeancountCheckOptions>,
146+
/// Flags that should generate diagnostics (e.g., ["!"] for only exclamation mark)
147+
pub diagnostic_flags: Option<Vec<String>>,
138148
}
139149

140150
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
@@ -489,4 +499,31 @@ mod tests {
489499
Some(PathBuf::from("/usr/bin/python3"))
490500
);
491501
}
502+
503+
#[test]
504+
fn test_diagnostic_flags_default() {
505+
let config = Config::new(PathBuf::new());
506+
assert_eq!(config.diagnostic_flags, vec!["!".to_string()]);
507+
}
508+
509+
#[test]
510+
fn test_diagnostic_flags_custom() {
511+
let mut config = Config::new(PathBuf::new());
512+
config
513+
.update(serde_json::from_str(r#"{"diagnostic_flags": ["!", "P"]}"#).unwrap())
514+
.unwrap();
515+
assert_eq!(
516+
config.diagnostic_flags,
517+
vec!["!".to_string(), "P".to_string()]
518+
);
519+
}
520+
521+
#[test]
522+
fn test_diagnostic_flags_empty() {
523+
let mut config = Config::new(PathBuf::new());
524+
config
525+
.update(serde_json::from_str(r#"{"diagnostic_flags": []}"#).unwrap())
526+
.unwrap();
527+
assert_eq!(config.diagnostic_flags, Vec::<String>::new());
528+
}
492529
}

0 commit comments

Comments
 (0)