Compare commits

...

2 commits

Author SHA1 Message Date
ba4caf2dc0
build: bump lsp to 0.5.0 2024-08-18 20:27:33 +02:00
affb5df12b
feat: introduce rules (#10)
* feat: introduce rules

* docs: rules example
2024-08-18 20:26:57 +02:00
5 changed files with 83 additions and 6 deletions

2
Cargo.lock generated
View file

@ -124,7 +124,7 @@ dependencies = [
[[package]]
name = "discord-presence-lsp"
version = "0.4.2"
version = "0.5.0"
dependencies = [
"discord-rich-presence",
"git2",

View file

@ -34,6 +34,15 @@ 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
}
}
@ -41,4 +50,4 @@ You can configure state, details and git integration by changing Discord Presenc
}
```
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`

View file

@ -1,6 +1,6 @@
[package]
name = "discord-presence-lsp"
version = "0.4.2"
version = "0.5.0"
edition = "2021"
[dependencies]

View file

@ -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,
@ -31,6 +64,8 @@ pub struct Configuration {
pub small_image: Option<String>,
pub small_text: Option<String>,
pub rules: Rules,
pub git_integration: bool,
}
@ -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,
}
}
@ -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);
}

View file

@ -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;
@ -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());
@ -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(),