Skip to content

Commit

Permalink
plugins/openscad: migrate to mkVimPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanLepage committed Dec 17, 2024
1 parent 17905de commit 4f1fe40
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 74 deletions.
2 changes: 1 addition & 1 deletion flake-modules/dev/list-plugins/list-plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"plugins/by-name/none-ls/prettierd.nix",
"plugins/by-name/none-ls/settings.nix",
"plugins/by-name/none-ls/sources.nix",
"plugins/by-name/openscad/fuzzy-finder-plugin-option.nix",
"plugins/by-name/rustaceanvim/renamed-options.nix",
"plugins/by-name/rustaceanvim/settings-options.nix",
"plugins/by-name/startify/options.nix",
Expand Down Expand Up @@ -75,7 +76,6 @@ class State(Enum):
"plugins/by-name/lint/default.nix": (State.OLD, Kind.NEOVIM, False),
"plugins/by-name/lspkind/default.nix": (State.OLD, Kind.NEOVIM, False),
"plugins/by-name/nix-develop/default.nix": (State.OLD, Kind.NEOVIM, False),
"plugins/by-name/openscad/default.nix": (State.OLD, Kind.VIM, False),
"plugins/by-name/rainbow-delimiters/default.nix": (State.OLD, Kind.NEOVIM, False),
"plugins/by-name/treesitter-refactor/default.nix": (State.OLD, Kind.MISC, True),
"plugins/by-name/treesitter-textobjects/default.nix": (
Expand Down
130 changes: 71 additions & 59 deletions plugins/by-name/openscad/default.nix
Original file line number Diff line number Diff line change
@@ -1,81 +1,93 @@
{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
let
defaultFuzzyFinder = "skim";
inherit (lib) types;
inherit (lib.nixvim) defaultNullOpts applyPrefixToAttrs;
inherit (lib.nixvim.vim-plugin) mkSettingsOptionDescription;

name = "openscad";
globalPrefix = "openscad_";
in
{
options.plugins.openscad = {
enable = mkEnableOption "openscad.nvim, a plugin to manage OpenSCAD files";
lib.nixvim.neovim-plugin.mkNeovimPlugin {
inherit name;
packPathName = "openscad.nvim";
package = "openscad-nvim";

maintainers = [ lib.maintainers.GaetanLepage ];

# TODO: Added 2024-12-17; remove after 25.05
optionsRenamedToSettings = import ./renamed-options.nix;

package = lib.mkPackageOption pkgs "openscad.nvim" {
default = [
"vimPlugins"
"openscad-nvim"
];
};
settingsOptions = {
fuzzy_finder = defaultNullOpts.mkStr "skim" ''
Fuzzy finder to find documentation.
fuzzyFinder = helpers.defaultNullOpts.mkEnum [
"skim"
"fzf"
] defaultFuzzyFinder "fuzzy finder to find documentation";
If you set this option explicitly, Nixvim will install the relevant finder plugin.
'';

cheatsheetWindowBlend = helpers.defaultNullOpts.mkNullable (types.ints.between 0 100) 15 "";
cheatsheet_window_blend = defaultNullOpts.mkNullable (types.ints.between 0 100) 15 ''
Transparency level of the cheatsheet window (in %).
'';

loadSnippets = helpers.defaultNullOpts.mkBool false "";
load_snippets = defaultNullOpts.mkBool false ''
Whether to load predefined snippets for OpenSCAD.
'';

autoOpen = helpers.defaultNullOpts.mkBool false "";
auto_open = defaultNullOpts.mkBool false ''
Whether the openscad project automatically be opened on startup.
'';

keymaps = {
enable = mkEnableOption "keymaps for openscad";
default_mappings = defaultNullOpts.mkBool true ''
Whether to enable the default mappings.
'';

cheatsheetToggle = helpers.defaultNullOpts.mkStr "<Enter>" "Toggle cheatsheet window";
cheatsheet_toggle_key = defaultNullOpts.mkStr "<Enter>" ''
Keyboard shortcut for toggling the cheatsheet.
'';

helpTrigger = helpers.defaultNullOpts.mkStr "<A-h>" "Fuzzy find help resource";
help_trig_key = defaultNullOpts.mkStr "<A-h>" ''
Keyboard shortcut for triggering the fuzzy-find help resource.
'';

helpManualTrigger = helpers.defaultNullOpts.mkStr "<A-m>" "Open offline openscad manual in pdf via zathura";
help_manual_trig_key = defaultNullOpts.mkStr "<A-m>" ''
Keyboard shortcut for manually triggering the offline OpenSCAD manual.
'';

execOpenSCADTrigger = helpers.defaultNullOpts.mkStr "<A-o>" "Open file in OpenSCAD";
exec_openscad_trig_key = defaultNullOpts.mkStr "<A-o>" ''
Keyboard shortcut for opening the current file in OpenSCAD.
'';

topToggle = helpers.defaultNullOpts.mkStr "<A-c>" "toggle htop filtered for openscad processes";
};
top_toggle = defaultNullOpts.mkStr "<A-c>" ''
Keyboard shortcut for toggling `htop` filtered for OpenSCAD processes.
'';
};

config =
let
cfg = config.plugins.openscad;
fuzzyFinder = if (cfg.fuzzyFinder == null) then defaultFuzzyFinder else cfg.fuzzyFinder;
in
mkIf cfg.enable {
extraPlugins =
with pkgs.vimPlugins;
[ cfg.package ]
++ (optional (fuzzyFinder == "skim") skim-vim)
++ (optional (fuzzyFinder == "fzf") fzf-vim);

extraConfigLua = ''
require('openscad')
'';

globals = mkMerge [
{
openscad_fuzzy_finder = cfg.fuzzyFinder;
openscad_cheatsheet_window_blend = cfg.cheatsheetWindowBlend;
openscad_load_snippets = cfg.loadSnippets;
}
(mkIf cfg.keymaps.enable {
openscad_default_mappings = true;
openscad_cheatsheet_toggle_key = cfg.keymaps.cheatsheetToggle;
openscad_help_trig_key = cfg.keymaps.helpTrigger;
openscad_help_manual_trig_key = cfg.keymaps.helpManualTrigger;
openscad_exec_openscad_trig_key = cfg.keymaps.execOpenSCADTrigger;
openscad_top_toggle = cfg.keymaps.topToggle;
})
];
};
settingsExample = {
load_snippets = true;
fuzzy_finder = "fzf";
cheatsheet_window_blend = 15;
auto_open = true;
};

settingsDescription = mkSettingsOptionDescription { inherit name globalPrefix; };

callSetup = false;

extraOptions = {
fuzzyFinderPlugin = import ./fuzzy-finder-plugin-option.nix { inherit lib config pkgs; };
};

extraConfig = cfg: {
plugins.openscad.luaConfig.content = ''
require('openscad')
'';

globals = applyPrefixToAttrs globalPrefix cfg.settings;

extraPlugins = [ cfg.fuzzyFinderPlugin ];
};
}
50 changes: 50 additions & 0 deletions plugins/by-name/openscad/fuzzy-finder-plugin-option.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
lib,
config,
pkgs,
}:
let
optionName = "`settings.fuzzy_finder`";

fuzzyFinder = config.plugins.openscad.settings.fuzzy_finder;

defaultPluginName =
{
skim = "skim-vim";
fzf = "fzf-vim";
}
.${fuzzyFinder} or null;

default =
# If the user has not set `settings.fuzzy_finder`, do not pre-install a fuzzy-finder by default.
if fuzzyFinder == null then
null
# Else, the value of `settings.fuzzy_finder` should be one of the supported options
# (`skim` or `fzf`), else he has to provide a value (`null` or a package) to `fuzzyFinderPlugin`.
else if defaultPluginName == null then
throw ''
We cannot automatically select a fuzzy finder plugin from the value given to `${optionName}`: "${fuzzyFinder}".
Please, explicitly provide a value to the `plugins.openscad.fuzzyFinderPlugin`:
- Either the package for the fuzzy finder plugin to be installed
- or `null` if you do not want a plugin to be installed.
''
# Case where we automatically select the default plugin to install.
else
[
"vimPlugins"
defaultPluginName
];

defaultText = lib.literalMD ''
- `pkgs.vimPlugins.skim-vim` if ${optionName} is `"skim"`
- `pkgs.vimPlugins.fzf-vim` if ${optionName} is `"fzf"`
- `null` otherwise
'';
in
lib.mkPackageOption pkgs "fuzzy finder" {
nullable = true;
inherit default;
}
// {
inherit defaultText;
}
48 changes: 48 additions & 0 deletions plugins/by-name/openscad/renamed-options.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[
"fuzzyFinder"
"cheatsheetWindowBlend"
"loadSnippets"
"autoOpen"
{
old = [
"keymaps"
"enabled"
];
new = "default_mappings";
}
{
old = [
"keymaps"
"cheatsheetToggle"
];
new = "cheatsheet_toggle_key";
}
{
old = [
"keymaps"
"helpTrigger"
];
new = "help_trig_key";
}
{
old = [
"keymaps"
"helpManualTrigger"
];
new = "help_manual_trig_key";
}
{
old = [
"keymaps"
"execOpenSCADTrigger"
];
new = "exec_openscad_trig_key";
}
{
old = [
"keymaps"
"topToggle"
];
new = "top_toggle";
}
]
32 changes: 18 additions & 14 deletions tests/test-sources/plugins/by-name/openscad/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@
plugins.openscad = {
enable = true;

fuzzyFinder = "skim";
cheatsheetWindowBlend = 15;
loadSnippets = false;
autoOpen = false;

keymaps.enable = false;
settings = {
fuzzy_finder = "skim";
cheatsheet_window_blend = 15;
load_snippets = false;
auto_open = false;
default_mappings = true;
cheatsheet_toggle_key = "<Enter>";
help_trig_key = "<A-h>";
help_manual_trig_key = "<A-m>";
exec_openscad_trig_key = "<A-o>";
top_toggle = "<A-c>";
};
};
};

keymaps = {
example = {
plugins.openscad = {
enable = true;

keymaps = {
enable = true;
cheatsheetToggle = "<Enter>";
helpTrigger = "<A-h>";
helpManualTrigger = "<A-m>";
execOpenSCADTrigger = "<A-o>";
topToggle = "<A-c>";
settings = {
load_snippets = true;
fuzzy_finder = "fzf";
cheatsheet_window_blend = 15;
auto_open = true;
};
};
};
Expand Down

0 comments on commit 4f1fe40

Please sign in to comment.