From 6f9b7a2889cfeada5046f791f74b4440ab322402 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 21 Sep 2019 13:54:30 -0400 Subject: [PATCH] lang/nix: add ivy version of helm-nixos-options And make it nix-mode's lookup-documentation handler, so pressing `K` on an option will prefill the ivy search. --- modules/lang/nix/autoload.el | 41 ++++++++++++++++++++++++++++++++++++ modules/lang/nix/config.el | 5 +++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 modules/lang/nix/autoload.el diff --git a/modules/lang/nix/autoload.el b/modules/lang/nix/autoload.el new file mode 100644 index 000000000..bce624513 --- /dev/null +++ b/modules/lang/nix/autoload.el @@ -0,0 +1,41 @@ +;;; lang/nix/autoload.el -*- lexical-binding: t; -*- + +(defun +nix--options-action (candidate) + (switch-to-buffer-other-window + (nixos-options-doc-buffer + (nixos-options-get-documentation-for-option candidate)))) + +;;;###autoload +(defun +nix/lookup-option (&optional initial-input) + "Look up documentation on a nix option." + (interactive + (list + ;; REVIEW Must be a better way to do this + (when (and (looking-at-p "[a-zA-Z0-9-_\\.]") + (not (doom-point-in-string-or-comment-p))) + (buffer-substring-no-properties + (save-excursion + (skip-chars-backward "^ ") + (point)) + (save-excursion + (skip-chars-forward "^ ") + (point)))))) + (cond ((featurep! :completion helm) + (require 'helm-nixos-options) + ;; REVIEW We reimplment `helm-nixos-options' so we can supply + ;; `initial-input'. Maybe use `helm-attrset' instead? + (helm :sources `(,(helm-source-nixos-options-search)) + :buffer "*helm-nixos-options*" + :input initial-input)) + ((featurep! :completion ivy) + (require 'nixos-options) + (ivy-read "NixOS options: " + nixos-options + :require-match t + :initial-input initial-input + :action #'+nix--options-action + :caller '+nix/options)) + ;; TODO Add general `completing-read' support + ((user-error "No search engine is enabled. Enable helm or ivy!"))) + ;; Tell lookup module to let us handle things from here + 'deferred) diff --git a/modules/lang/nix/config.el b/modules/lang/nix/config.el index 40ffe56d5..8c3c3dc8c 100644 --- a/modules/lang/nix/config.el +++ b/modules/lang/nix/config.el @@ -4,6 +4,8 @@ :mode "\\.nix\\'" :config (set-company-backend! 'nix-mode 'company-nixos-options) + (set-lookup-handlers! 'nix-mode + :documentation '(+nix/lookup-option :async t)) (map! :localleader :map nix-mode-map @@ -13,8 +15,7 @@ "s" #'nix-shell "b" #'nix-build "u" #'nix-unpack - (:when (featurep! :completion helm) - "o" #'helm-nixos-options))) + "o" #'+nix/lookup-option)) (use-package! nix-drv-mode :mode "\\.drv\\'")