Skip to content

Commit

Permalink
feat: introduce rules (#10)
Browse files Browse the repository at this point in the history
* feat: introduce rules

* docs: rules example
  • Loading branch information
xhyrom authored Aug 18, 2024
1 parent 3c74a0f commit affb5df
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ You can configure state, details and git integration by changing Discord Presenc
// URL for small image
"small_image": "{base_icons_url}/zed.png",
"small_text": "Zed",

// Rules - disable presence in some workspaces
"rules": {
"mode": "blacklist" // or whitelist
"paths": [
"absolute path"
]
},

"git_integration": true
}
}
}
}
```

You can also use `null` to unset the option. Possible for everything except `base_icons_url` and `git_integration`
You can also use `null` to unset the option. Possible for everything except `base_icons_url`, `rules` and `git_integration`
58 changes: 58 additions & 0 deletions lsp/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@

use serde_json::Value;

#[derive(Debug, PartialEq)]
pub enum RulesMode {
Whitelist,
Blacklist,
}

#[derive(Debug)]
pub struct Rules {
pub mode: RulesMode,
pub paths: Vec<String>,
}

impl Default for Rules {
fn default() -> Self {
Rules {
mode: RulesMode::Blacklist,
paths: Vec::new(),
}
}
}

impl Rules {
pub fn suitable(&self, path: &str) -> bool {
let contains = self.paths.contains(&path.to_string());

if self.mode == RulesMode::Blacklist {
!contains
} else {
contains
}
}
}

#[derive(Debug)]
pub struct Configuration {
pub base_icons_url: String,
Expand All @@ -31,6 +64,8 @@ pub struct Configuration {
pub small_image: Option<String>,
pub small_text: Option<String>,

pub rules: Rules,

pub git_integration: bool,
}

Expand Down Expand Up @@ -66,6 +101,7 @@ impl Configuration {
large_text: Some(String::from("{language:u}")),
small_image: Some(String::from("{base_icons_url}/zed.png")),
small_text: Some(String::from("Zed")),
rules: Rules::default(),
git_integration: true,
}
}
Expand All @@ -80,6 +116,28 @@ impl Configuration {
set_option!(self, options, small_image, "small_image");
set_option!(self, options, small_text, "small_text");

if let Some(rules) = options.get("rules") {
self.rules.mode = rules.get("mode").and_then(|m| m.as_str()).map_or(
RulesMode::Blacklist,
|mode| match mode {
"whitelist" => RulesMode::Whitelist,
"blacklist" => RulesMode::Blacklist,
_ => RulesMode::Blacklist,
},
);

self.rules.paths =
rules
.get("paths")
.and_then(|p| p.as_array())
.map_or(Vec::new(), |paths| {
paths
.iter()
.filter_map(|p| p.as_str().map(|s| s.to_string()))
.collect()
});
}

if let Some(git_integration) = options.get("git_integration") {
self.git_integration = git_integration.as_bool().unwrap_or(true);
}
Expand Down
16 changes: 13 additions & 3 deletions lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::ffi::OsStr;
use std::fmt::Debug;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::sync::{Mutex, MutexGuard};

use configuration::Configuration;
Expand Down Expand Up @@ -155,9 +156,6 @@ impl Backend {
#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
// Connect discord client
self.discord.connect();

// Set workspace name
let root_uri = params.root_uri.expect("Failed to get root uri");
let workspace_path = Path::new(root_uri.path());
Expand All @@ -178,6 +176,18 @@ impl LanguageServer for Backend {
let mut config = self.config.lock().unwrap();
config.set(params.initialization_options);

if config.rules.suitable(
workspace_path
.to_str()
.expect("Failed to transform workspace path to str"),
) {
// Connect discord client
self.discord.connect();
} else {
// Exit LSP
exit(0);
}

Ok(InitializeResult {
server_info: Some(ServerInfo {
name: env!("CARGO_PKG_NAME").into(),
Expand Down

0 comments on commit affb5df

Please sign in to comment.