From de4b18a2dcd74dd1f78c80405938f2e67f41d30e Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 26 Feb 2019 16:57:14 -0500 Subject: [PATCH] feature/lookup: refactor backend dispatcher Hopefully improves compatibility with async handlers. Still a WIP. --- modules/feature/lookup/autoload/lookup.el | 87 ++++++++++------------- 1 file changed, 39 insertions(+), 48 deletions(-) diff --git a/modules/feature/lookup/autoload/lookup.el b/modules/feature/lookup/autoload/lookup.el index da2b4fc25..f69e8c344 100644 --- a/modules/feature/lookup/autoload/lookup.el +++ b/modules/feature/lookup/autoload/lookup.el @@ -84,7 +84,7 @@ Otherwise, these properties are available to be set: ;; -;; Library +;; Helpers ;; Helpers (defun +lookup--online-provider (&optional force-p namespace) @@ -107,52 +107,44 @@ Otherwise, these properties are available to be set: ((require 'xref nil t) (xref-backend-identifier-at-point (xref-find-backend))))) -(defun +lookup--jump-to (prop identifier &optional other-window) - ;; TODO Refactor me - (let ((origin (point-marker))) - (cl-loop for fn - in (plist-get (list :definition +lookup-definition-functions - :references +lookup-references-functions - :documentation +lookup-documentation-functions - :file +lookup-file-functions) - prop) - for cmd = (or (command-remapping fn) fn) - if (get fn '+lookup-async) - return - (progn - (when other-window - ;; If async, we can't catch the window change or destination buffer - ;; reliably, so we set up the new window ahead of time. - (switch-to-buffer-other-window (current-buffer)) - (goto-char (marker-position origin))) - (call-interactively fn) - t) - if (condition-case e - (save-window-excursion - (when (or (if (commandp cmd) - (call-interactively cmd) - (funcall cmd identifier)) - (/= (point-marker) origin)) - (point-marker))) - (error (ignore (message "%s" e)))) - return - (progn - (funcall (if other-window - #'switch-to-buffer-other-window - #'switch-to-buffer) - (marker-buffer it)) - (goto-char it))))) +(defun +lookup--run-hooks (hook identifier origin &optional other-window) + (condition-case-unless-debug e + (if (get hook '+lookup-async) + (progn + (when other-window + ;; If async, we can't catch the window change or destination buffer + ;; reliably, so we set up the new window ahead of time. + (switch-to-buffer-other-window (current-buffer)) + (goto-char (marker-position origin))) + (call-interactively hook) + t) + (save-window-excursion + (when (or (if (commandp hook) + (call-interactively hook) + (funcall hook identifier)) + (/= (point-marker) origin)) + (point-marker)))) + ((error user-error) + (message "%s" e) + nil))) -(defun +lookup--file-search (identifier) - (unless identifier - (let ((query (rxt-quote-pcre identifier))) - (ignore-errors - (cond ((featurep! :completion ivy) - (+ivy-file-search nil :query query) - t) - ((featurep! :completion helm) - (+helm-file-search nil :query query) - t)))))) +(defun +lookup--jump-to (prop identifier &optional other-window) + (let ((ret (run-hook-wrapped + (plist-get (list :definition '+lookup-definition-functions + :references '+lookup-references-functions + :documentation '+lookup-documentation-functions + :file '+lookup-file-functions) + prop) + '+lookup--run-hooks + identifier (point-marker) other-window))) + (cond ((null ret) + (message "Could not find '%s'" identifier)) + ((markerp ret) + (funcall (if other-window + #'switch-to-buffer-other-window + #'switch-to-buffer) + (marker-buffer ret)) + (goto-char ret))))) ;; @@ -196,8 +188,7 @@ falling back to git-grep)." (defun +lookup-evil-goto-definition-backend (identifier) "Uses `evil-goto-definition' to conduct a text search for IDENTIFIER in the current buffer." - (and (featurep 'evil) - evil-mode + (and (fboundp 'evil-goto-definition) (ignore-errors (cl-destructuring-bind (beg . end) (bounds-of-thing-at-point 'symbol)