Skip to content

Commit 07e58fd

Browse files
committed
[IMP] allow multiple paths in diagnostic filters
1 parent dbddeb1 commit 07e58fd

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

server/src/core/config.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,30 @@ impl Default for MergeMethod {
9191
}
9292
}
9393

94+
#[derive(Debug, Deserialize, Serialize, Clone, Copy, JsonSchema)]
95+
#[serde(rename_all = "lowercase")]
96+
pub enum DiagnosticFilterPathType {
97+
IN,
98+
NOT_IN
99+
}
100+
101+
impl Default for DiagnosticFilterPathType {
102+
fn default() -> Self {
103+
DiagnosticFilterPathType::IN
104+
}
105+
}
106+
94107
#[derive(Debug, Clone, JsonSchema)]
95108
#[schemars(deny_unknown_fields)]
96109
pub struct DiagnosticFilter {
97-
#[schemars(with = "String")]
98-
pub paths: Pattern,
110+
#[schemars(default, schema_with = "regex_vec_schema")]
111+
pub paths: Vec<Pattern>,
99112
#[schemars(default, schema_with = "regex_vec_schema")]
100113
pub codes: Vec<Regex>,
101114
#[schemars(default)]
102115
pub types: Vec<DiagnosticSetting>,
103-
#[schemars(skip_deserializing)]
104-
pub negation: bool,
116+
#[schemars(default)]
117+
pub path_type: DiagnosticFilterPathType,
105118
}
106119

107120
/// Serialize the schema as Vec<String> and adds the default
@@ -118,20 +131,21 @@ impl<'de> serde::Deserialize<'de> for DiagnosticFilter {
118131
{
119132
#[derive(serde::Deserialize)]
120133
struct Helper {
121-
paths: String,
134+
paths: Vec<String>,
122135
#[serde(default)]
123136
codes: Vec<String>,
124137
#[serde(default)]
125138
types: Vec<DiagnosticSetting>,
139+
#[serde(default)]
140+
path_type: DiagnosticFilterPathType,
126141
}
127142
let helper = Helper::deserialize(deserializer)?;
128-
let (path_str, negation) = if let Some(stripped) = helper.paths.strip_prefix('!') {
129-
(stripped, true)
130-
} else {
131-
(helper.paths.as_str(), false)
132-
};
133-
let sanitized_path = PathBuf::from(path_str).sanitize();
134-
let path_pattern = Pattern::new(&sanitized_path).map_err(serde::de::Error::custom)?;
143+
let mut paths = vec![];
144+
for path in helper.paths.iter() {
145+
let sanitized_path = PathBuf::from(path).sanitize();
146+
let path_pattern = Pattern::new(&sanitized_path).map_err(serde::de::Error::custom)?;
147+
paths.push(path_pattern);
148+
}
135149
let mut code_regexes = Vec::with_capacity(helper.codes.len());
136150
for code in &helper.codes {
137151
let regex = Regex::new(code).map_err(serde::de::Error::custom)?;
@@ -143,10 +157,10 @@ impl<'de> serde::Deserialize<'de> for DiagnosticFilter {
143157
}
144158
}
145159
Ok(DiagnosticFilter {
146-
paths: path_pattern,
160+
paths: paths,
147161
codes: code_regexes,
148162
types: helper.types,
149-
negation,
163+
path_type: helper.path_type,
150164
})
151165
}
152166
}
@@ -158,14 +172,12 @@ impl Serialize for DiagnosticFilter {
158172
{
159173
use serde::ser::SerializeStruct;
160174
let mut s = serializer.serialize_struct("DiagnosticFilter", 3)?;
161-
let mut path_str = self.paths.as_str().to_string();
162-
if self.negation {
163-
path_str = format!("!{}", path_str);
164-
}
165-
s.serialize_field("paths", &path_str)?;
175+
let paths: Vec<String> = self.paths.iter().map(|p| p.as_str().to_string()).collect();
176+
s.serialize_field("paths", &paths)?;
166177
let codes: Vec<String> = self.codes.iter().map(|r| r.as_str().to_string()).collect();
167178
s.serialize_field("codes", &codes)?;
168179
s.serialize_field("types", &self.types)?;
180+
s.serialize_field("path_type", &self.path_type)?;
169181
s.end()
170182
}
171183
}

server/src/core/file_mgr.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::PathBuf;
1111
use std::str::FromStr;
1212
use std::sync::Arc;
1313
use std::{collections::HashMap, fs};
14-
use crate::core::config::DiagnosticFilter;
14+
use crate::core::config::{DiagnosticFilter, DiagnosticFilterPathType};
1515
use crate::core::diagnostics::{create_diagnostic, DiagnosticCode, DiagnosticSetting};
1616
use crate::features::node_index_ast::IndexedModule;
1717
use crate::threads::SessionInfo;
@@ -298,7 +298,14 @@ impl FileInfo {
298298
}
299299
pub fn update_diagnostic_filters(&mut self, session: &SessionInfo) {
300300
self.diagnostic_filters = session.sync_odoo.config.diagnostic_filters.iter().cloned().filter(|filter| {
301-
(filter.negation && !filter.paths.matches(&self.uri)) || (!filter.negation && filter.paths.matches(&self.uri))
301+
match filter.path_type {
302+
DiagnosticFilterPathType::IN => {
303+
filter.paths.iter().any(|p| p.matches(&self.uri))
304+
}
305+
DiagnosticFilterPathType::NOT_IN => {
306+
!filter.paths.iter().any(|p| p.matches(&self.uri))
307+
}
308+
}
302309
}).collect::<Vec<_>>();
303310
}
304311

0 commit comments

Comments
 (0)