|
| 1 | +;;; cider-resolve.el --- Resolve clojure symbols according to current nREPL connection |
| 2 | + |
| 3 | +;; Copyright © 2015 Artur Malabarba |
| 4 | + |
| 5 | +;; Author: Artur Malabarba <[email protected]> |
| 6 | + |
| 7 | +;; This program is free software; you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | + |
| 22 | +;;; Code: |
| 23 | + |
| 24 | +(require 'nrepl-client) |
| 25 | +(require 'cider-interaction) |
| 26 | +(require 'cider-repl) |
| 27 | + |
| 28 | +(defun cider-resolve--get-in (&rest keys) |
| 29 | + "Return (nrepl-dict-get-in cider-repl-ns-cache keys)." |
| 30 | + (when cider-connections |
| 31 | + (nrepl-dict-get-in |
| 32 | + (with-current-buffer (cider-current-connection) |
| 33 | + cider-repl-ns-cache) |
| 34 | + keys))) |
| 35 | + |
| 36 | +(defun cider-resolve-alias (ns alias) |
| 37 | + "Return the namespace that ALIAS refers to in namespace NS. |
| 38 | +If it doesn't point anywhere, returns ALIAS." |
| 39 | + (or (cider-resolve--get-in ns "aliases" alias) |
| 40 | + alias)) |
| 41 | + |
| 42 | +(defun cider-resolve-var (ns var) |
| 43 | + "Return a dict of the metadata of a clojure var VAR in namespace NS. |
| 44 | +VAR is a string. |
| 45 | +Return nil only if VAR cannot be resolved." |
| 46 | + (let* ((prefix-regexp "\\`\\([^/]+\\)/") |
| 47 | + (var-ns (when (string-match prefix-regexp var) |
| 48 | + (cider-resolve-alias ns (match-string 1 var)))) |
| 49 | + (name (replace-regexp-in-string prefix-regexp "" var))) |
| 50 | + (or |
| 51 | + (cider-resolve--get-in (or var-ns ns) "interns" name) |
| 52 | + (unless var-ns |
| 53 | + ;; If the var had no prefix, it might be referred. |
| 54 | + (-if-let (referal (cider-resolve--get-in ns "refers" name)) |
| 55 | + (cider-resolve-var ns referal) |
| 56 | + ;; Or it might be from core. |
| 57 | + (unless (equal ns "clojure.core") |
| 58 | + (cider-resolve-var "clojure.core" name))))))) |
| 59 | + |
| 60 | +(defun cider-match-instrumented-symbol (n face) |
| 61 | + "Return a face specification for font-locking. |
| 62 | +If (match-string N) is an instrumented symbol, return |
| 63 | + (face cider-instrumented-face FACE) |
| 64 | +otherwise, return (face FACE)." |
| 65 | + (cons 'face |
| 66 | + (if (nrepl-dict-get (cider-resolve-var (cider-current-ns) (match-string n)) |
| 67 | + "cider-instrumented") |
| 68 | + `((cider-instrumented-face ,face)) |
| 69 | + (list face)))) |
| 70 | + |
| 71 | +(provide 'cider-resolve) |
| 72 | +;;; cider-resolve.el ends here |
0 commit comments