Skip to content

Commit 5e85bb6

Browse files
committed
enhance ignore
1 parent 94345fd commit 5e85bb6

File tree

7 files changed

+108
-3
lines changed

7 files changed

+108
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/emmylua_ls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ serde_yml.workspace = true
3434
itertools.workspace = true
3535
dirs.workspace = true
3636
clap.workspace = true
37+
wax.workspace = true

crates/emmylua_ls/src/context/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{collections::HashMap, future::Future, sync::Arc};
1717
use tokio::sync::{Mutex, RwLock};
1818
use tokio_util::sync::CancellationToken;
1919
pub use workspace_manager::load_emmy_config;
20+
pub use workspace_manager::WorkspaceFileMatcher;
2021
pub use workspace_manager::WorkspaceManager;
2122

2223
pub struct ServerContext {

crates/emmylua_ls/src/context/workspace_manager.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
use std::collections::HashSet;
2+
use std::path::Path;
23
use std::{path::PathBuf, sync::Arc, time::Duration};
34

45
use super::{ClientProxy, FileDiagnostic, StatusBar};
56
use crate::handlers::{init_analysis, ClientConfig};
67
use dirs;
7-
use emmylua_code_analysis::update_code_style;
88
use emmylua_code_analysis::{load_configs, EmmyLuaAnalysis, Emmyrc};
9+
use emmylua_code_analysis::{update_code_style, uri_to_file_path};
910
use log::{debug, info};
1011
use lsp_types::Uri;
1112
use tokio::sync::{Mutex, RwLock};
1213
use tokio_util::sync::CancellationToken;
14+
use wax::Pattern;
1315

1416
pub struct WorkspaceManager {
1517
analysis: Arc<RwLock<EmmyLuaAnalysis>>,
@@ -21,6 +23,7 @@ pub struct WorkspaceManager {
2123
pub workspace_folders: Vec<PathBuf>,
2224
pub watcher: Option<notify::RecommendedWatcher>,
2325
pub current_open_files: HashSet<Uri>,
26+
pub match_file_pattern: WorkspaceFileMatcher,
2427
}
2528

2629
impl WorkspaceManager {
@@ -40,6 +43,7 @@ impl WorkspaceManager {
4043
file_diagnostic,
4144
watcher: None,
4245
current_open_files: HashSet::new(),
46+
match_file_pattern: WorkspaceFileMatcher::default(),
4347
}
4448
}
4549

@@ -163,6 +167,30 @@ impl WorkspaceManager {
163167

164168
Some(())
165169
}
170+
171+
pub fn is_workspace_file(&self, uri: &Uri) -> bool {
172+
if self.workspace_folders.is_empty() {
173+
return true;
174+
}
175+
176+
let Some(file_path) = uri_to_file_path(&uri) else {
177+
return true;
178+
};
179+
180+
let mut file_matched = true;
181+
for workspace in &self.workspace_folders {
182+
if let Ok(relative) = file_path.strip_prefix(workspace) {
183+
file_matched = self.match_file_pattern.is_match(&file_path, relative);
184+
185+
if file_matched {
186+
// If the file matches the include pattern, we can stop checking further.
187+
break;
188+
}
189+
}
190+
}
191+
192+
file_matched
193+
}
166194
}
167195

168196
pub fn load_emmy_config(config_root: Option<PathBuf>, client_config: ClientConfig) -> Arc<Emmyrc> {
@@ -304,3 +332,51 @@ impl ReindexToken {
304332
*need_re_sleep = true;
305333
}
306334
}
335+
336+
#[derive(Debug, Clone)]
337+
pub struct WorkspaceFileMatcher {
338+
include: Vec<String>,
339+
exclude: Vec<String>,
340+
exclude_dir: Vec<PathBuf>,
341+
}
342+
343+
impl WorkspaceFileMatcher {
344+
pub fn new(include: Vec<String>, exclude: Vec<String>, exclude_dir: Vec<PathBuf>) -> Self {
345+
Self {
346+
include,
347+
exclude,
348+
exclude_dir,
349+
}
350+
}
351+
pub fn is_match(&self, path: &Path, relative_path: &Path) -> bool {
352+
if self.exclude_dir.iter().any(|dir| path.starts_with(dir)) {
353+
return false;
354+
}
355+
356+
// let path_str = path.to_string_lossy().to_string().replace("\\", "/");
357+
let exclude_matcher = wax::any(self.exclude.iter().map(|s| s.as_str()));
358+
if let Ok(exclude_set) = exclude_matcher {
359+
if exclude_set.is_match(relative_path) {
360+
return false;
361+
}
362+
} else {
363+
log::error!("Invalid exclude pattern");
364+
}
365+
366+
let include_matcher = wax::any(self.include.iter().map(|s| s.as_str()));
367+
if let Ok(include_set) = include_matcher {
368+
return include_set.is_match(relative_path);
369+
} else {
370+
log::error!("Invalid include pattern");
371+
}
372+
373+
return true;
374+
}
375+
}
376+
377+
impl Default for WorkspaceFileMatcher {
378+
fn default() -> Self {
379+
let include_pattern = vec!["**/*.lua".to_string()];
380+
Self::new(include_pattern, vec![], vec![])
381+
}
382+
}

crates/emmylua_ls/src/handlers/initialized/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ use crate::{
99
cmd_args::CmdArgs,
1010
context::{
1111
get_client_id, load_emmy_config, ClientId, ClientProxy, FileDiagnostic, ProgressTask,
12-
ServerContextSnapshot, StatusBar,
12+
ServerContextSnapshot, StatusBar, WorkspaceFileMatcher,
13+
},
14+
handlers::{
15+
initialized::collect_files::calculate_include_and_exclude,
16+
text_document::register_files_watch,
1317
},
14-
handlers::text_document::register_files_watch,
1518
logger::init_logger,
1619
};
1720
pub use client_config::{get_client_config, ClientConfig};
@@ -59,6 +62,8 @@ pub async fn initialized_handler(
5962
let mut workspace_manager = context.workspace_manager.write().await;
6063
workspace_manager.workspace_folders = workspace_folders.clone();
6164
workspace_manager.client_config = client_config.clone();
65+
let (include, exclude, exclude_dir) = calculate_include_and_exclude(&emmyrc);
66+
workspace_manager.match_file_pattern = WorkspaceFileMatcher::new(include, exclude, exclude_dir);
6267
drop(workspace_manager);
6368
// let file_diagnostic = context.file_diagnostic.clone();
6469

crates/emmylua_ls/src/handlers/text_document/text_document_handler.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ pub async fn on_did_open_text_document(
1414
let mut analysis = context.analysis.write().await;
1515
let uri = params.text_document.uri;
1616
let text = params.text_document.text;
17+
let old_file_id = analysis.get_file_id(&uri);
18+
// check is filter file
19+
if old_file_id.is_none() {
20+
let workspace_manager = context.workspace_manager.read().await;
21+
if !workspace_manager.is_workspace_file(&uri) {
22+
return None;
23+
}
24+
}
1725

1826
let file_id = analysis.update_file_by_uri(&uri, Some(text));
1927
let emmyrc = analysis.get_emmyrc();
@@ -60,6 +68,15 @@ pub async fn on_did_change_text_document(
6068
let mut analysis = context.analysis.write().await;
6169
let uri = params.text_document.uri;
6270
let text = params.content_changes.first()?.text.clone();
71+
let old_file_id = analysis.get_file_id(&uri);
72+
// check is filter file
73+
if old_file_id.is_none() {
74+
let workspace_manager = context.workspace_manager.read().await;
75+
if !workspace_manager.is_workspace_file(&uri) {
76+
return None;
77+
}
78+
}
79+
6380
let file_id = analysis.update_file_by_uri(&uri, Some(text));
6481
let emmyrc = analysis.get_emmyrc();
6582
let interval = emmyrc.diagnostics.diagnostic_interval.unwrap_or(500);

crates/emmylua_ls/src/handlers/text_document/watched_file_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub async fn on_did_change_watched_files(
2424
}
2525

2626
if !workspace.current_open_files.contains(&file_event.uri) {
27+
if !workspace.is_workspace_file(&file_event.uri) {
28+
continue;
29+
}
30+
2731
collect_lua_files(
2832
&mut watched_lua_files,
2933
file_event.uri,

0 commit comments

Comments
 (0)