Skip to content

Commit

Permalink
feat: Add support for LSP managers
Browse files Browse the repository at this point in the history
feat: Add support for LSP managers
  • Loading branch information
vyfor authored Apr 27, 2024
2 parents 700e59e + 72df0e9 commit c9c8593
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require('cord').setup({
editing = 'Editing {}', -- Text to display when editing a file
file_browser = 'Browsing files in {}', -- Text to display when browsing files (Empty string to disable)
plugin_manager = 'Managing plugins in {}', -- Text to display when managing plugins (Empty string to disable)
lsp_manager = 'Configuring LSP servers in {}', -- Text to display when managing LSP servers (Empty string to disable)
workspace = 'In {}', -- Text to display when in a workspace (Empty string to disable)
},
buttons = {
Expand All @@ -105,12 +106,12 @@ require('cord').setup({
-- url = 'https://github.com/vyfor/cord.nvim',
-- }
},
assets = {
assets = { -- Custom file icons
-- lazy = { -- Vim filetype or file name or file extension = table or string (see wiki)*
-- name = 'Lazy', -- Optional override for the icon name, redundant for language types
-- icon = 'https://example.com/lazy.png', -- Rich Presence asset name or URL
-- tooltip = 'lazy.nvim',
-- type = 2, -- 0 = language, 1 = file browser, 2 = plugin manager; defaults to language
-- type = 2, -- 0 = language, 1 = file browser, 2 = plugin manager, 3 = lsp manager; defaults to language
-- },
-- ['Cargo.toml'] = 'crates',
},
Expand Down
Binary file added assets/lsp/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions lua/cord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cord.config = {
editing = 'Editing {}',
file_browser = 'Browsing files in {}',
plugin_manager = 'Managing plugins in {}',
lsp_manager = 'Configuring LSP servers in {}',
workspace = 'In {}',
},
buttons = {
Expand Down Expand Up @@ -73,6 +74,7 @@ local function connect(config)
config.text.editing,
config.text.file_browser,
config.text.plugin_manager,
config.text.lsp_manager,
config.text.workspace,
vim.fn.getcwd(),
config.display.show_repository and ffi.new(
Expand Down
1 change: 1 addition & 0 deletions lua/cord/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ local function init_discord(ffi)
const char* editing_text,
const char* file_browser_text,
const char* plugin_manager_text,
const char* lsp_manager_text,
const char* workspace_text,
const char* initial_path,
const Buttons* buttons,
Expand Down
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct Config {
editing_text: String,
file_browser_text: String,
plugin_manager_text: String,
lsp_manager_text: String,
workspace_text: String,
workspace: String,
buttons: Vec<ActivityButton>,
Expand All @@ -61,6 +62,7 @@ pub extern "C" fn init(
editing_text: *const c_char,
file_browser_text: *const c_char,
plugin_manager_text: *const c_char,
lsp_manager_text: *const c_char,
workspace_text: *const c_char,
initial_path: *const c_char,
buttons_ptr: *const Buttons,
Expand Down Expand Up @@ -105,6 +107,7 @@ pub extern "C" fn init(
let editing_text = ptr_to_string(editing_text);
let file_browser_text = ptr_to_string(file_browser_text);
let plugin_manager_text = ptr_to_string(plugin_manager_text);
let lsp_manager_text = ptr_to_string(lsp_manager_text);
let workspace_text = ptr_to_string(workspace_text);
let workspace = find_workspace(&ptr_to_string(initial_path));

Expand Down Expand Up @@ -138,6 +141,7 @@ pub extern "C" fn init(
editing_text: editing_text,
file_browser_text: file_browser_text,
plugin_manager_text: plugin_manager_text,
lsp_manager_text: lsp_manager_text,
workspace_text: workspace_text,
workspace: workspace
.file_name()
Expand Down Expand Up @@ -351,6 +355,35 @@ pub extern "C" fn update_presence_with_assets(

(details, icon, tooltip)
}
Some(AssetType::LSP) => {
let details =
config.lsp_manager_text.replace("{}", &name);

if icon.is_empty() || tooltip.is_empty() {
if let Some((default_icon, default_tooltip)) =
mappings::lsp_manager::get(&filetype)
{
if icon.is_empty() {
icon = format!(
"{}/lsp/{}.png?v=5",
GITHUB_ASSETS_URL, default_icon
)
}
if tooltip.is_empty() {
tooltip = default_tooltip.to_string()
}
} else {
if icon.is_empty() {
return false;
}
if tooltip.is_empty() {
tooltip = name;
}
}
}

(details, icon, tooltip)
}
None => return false,
};

Expand Down
9 changes: 9 additions & 0 deletions src/mappings/lsp_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn get<'a>(filetype: &'a str) -> Option<(&'a str, &'a str)> {
let lsp_manager = match filetype {
"lspinfo" => ("default", "LSP Config"),
"mason" => ("default", "Mason"),
_ => return None,
};

Some(lsp_manager)
}
5 changes: 5 additions & 0 deletions src/mappings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod file_browser;
pub mod language;
pub mod lsp_manager;
pub mod plugin_manager;

pub fn get_by_filetype<'a>(filetype: &'a str, filename: &str) -> Filetype<'a> {
Expand All @@ -12,11 +13,15 @@ pub fn get_by_filetype<'a>(filetype: &'a str, filename: &str) -> Filetype<'a> {
if let Some(plugin_manager) = plugin_manager::get(filetype) {
return Filetype::PluginManager(plugin_manager.0, plugin_manager.1);
}
if let Some(lsp_manager) = lsp_manager::get(filetype) {
return Filetype::LSP(lsp_manager.0, lsp_manager.1);
}
Filetype::Language("text", filetype)
}

pub enum Filetype<'a> {
Language(&'a str, &'a str),
FileBrowser(&'a str, &'a str),
PluginManager(&'a str, &'a str),
LSP(&'a str, &'a str),
}
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub enum AssetType {
Language,
FileBrowser,
PluginManager,
LSP,
}

impl AssetType {
Expand All @@ -11,6 +12,7 @@ impl AssetType {
0 => Some(AssetType::Language),
1 => Some(AssetType::FileBrowser),
2 => Some(AssetType::PluginManager),
3 => Some(AssetType::LSP),
_ => None,
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ pub fn build_presence(
plugin_manager_presence(config, tooltip, icon);
(details, Some(icon), tooltip)
}
Filetype::LSP(icon, tooltip) => {
let (details, icon, tooltip) =
lsp_manager_presence(config, tooltip, icon);
(details, Some(icon), tooltip)
}
}
}

Expand Down Expand Up @@ -256,6 +261,20 @@ fn plugin_manager_presence(
(presence_details, presence_large_image, presence_large_text)
}

#[inline(always)]
fn lsp_manager_presence(
config: &Config,
tooltip: &str,
icon: &str,
) -> (String, String, String) {
let presence_details = config.lsp_manager_text.replace("{}", tooltip);
let presence_large_image =
format!("{}/lsp_manager/{}.png?v=5", GITHUB_ASSETS_URL, icon);
let presence_large_text = tooltip.to_string();

(presence_details, presence_large_image, presence_large_text)
}

#[inline(always)]
fn find_repository(workspace_path: &str) -> Option<String> {
let config_path = format!("{}/{}", workspace_path, ".git/config");
Expand Down

0 comments on commit c9c8593

Please sign in to comment.