Skip to content

Commit

Permalink
Added globset to helix-core. Moved globset building to config parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ontley committed Feb 9, 2024
1 parent 51d40c1 commit 9909082
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ textwrap = "0.16.0"

nucleo.workspace = true
parking_lot = "0.12"
globset = "0.4.14"

[dev-dependencies]
quickcheck = { version = "1", default-features = false }
Expand Down
26 changes: 23 additions & 3 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
use ahash::RandomState;
use arc_swap::{ArcSwap, Guard};
use bitflags::bitflags;
use globset::GlobSet;
use hashbrown::raw::RawTable;
use slotmap::{DefaultKey as LayerId, HopSlotMap};

Expand Down Expand Up @@ -358,6 +359,22 @@ where
serializer.end()
}

fn deserialize_required_root_patterns<'de, D>(deserializer: D) -> Result<Option<GlobSet>, D::Error>
where
D: serde::Deserializer<'de>,
{
let patterns = Vec::<String>::deserialize(deserializer)?;
if patterns.is_empty() {
return Ok(None);
}
let mut builder = globset::GlobSetBuilder::new();
for pattern in patterns {
let glob = globset::Glob::new(&pattern).map_err(serde::de::Error::custom)?;
builder.add(glob);
}
builder.build().map(Some).map_err(serde::de::Error::custom)
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct LanguageServerConfiguration {
Expand All @@ -371,9 +388,12 @@ pub struct LanguageServerConfiguration {
pub config: Option<serde_json::Value>,
#[serde(default = "default_timeout")]
pub timeout: u64,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub required_root_patterns: Vec<String>,
#[serde(
default,
skip_serializing,
deserialize_with = "deserialize_required_root_patterns"
)]
pub required_root_patterns: Option<GlobSet>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
41 changes: 8 additions & 33 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,40 +910,15 @@ fn start_client(
let root_path = root.clone().unwrap_or_else(|| workspace.clone());
let root_uri = root.and_then(|root| lsp::Url::from_file_path(root).ok());

let mut globset = globset::GlobSetBuilder::new();
let required_root_patterns = &ls_config.required_root_patterns;
if !required_root_patterns.is_empty() {
for required_root_pattern in required_root_patterns {
match globset::Glob::new(required_root_pattern) {
Ok(glob) => {
globset.add(glob);
}
Err(err) => {
log::warn!(
"Failed to build glob '{}' for language server '{}'",
required_root_pattern,
name
);
log::warn!("{}", err);
}
};
if let Some(globset) = &ls_config.required_root_patterns {
if !root_path
.read_dir()?
.flatten()
.map(|entry| entry.file_name())
.any(|entry| globset.is_match(entry))
{
return Ok(None);
}
match globset.build() {
Ok(glob) => {
if !root_path
.read_dir()?
.flatten()
.map(|entry| entry.file_name())
.any(|entry| glob.is_match(entry))
{
return Ok(None);
}
}
Err(err) => {
log::warn!("Failed to build globset for language server {name}");
log::warn!("{}", err);
}
};
}

let (client, incoming, initialize_notify) = Client::start(
Expand Down

0 comments on commit 9909082

Please sign in to comment.