-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit-jump.el
131 lines (110 loc) · 5.14 KB
/
init-jump.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
;;----------------------------------------------------------------------------
;; bookmark
;;----------------------------------------------------------------------------
;; (defun jester/bookmark-set-auto-name ()
;; "Do `bookmark-set', if region is active, use the region as bookmark name."
;; (interactive)
;; )
;; TODO use the macro for "/"
(jester/with-leader
"b l" 'counsel-evil-marks
"b n" 'bookmark-set
"b j" 'bookmark-jump)
;; TODO "SPC '" to cycle evil markers in current buffer (hydra it)
;;----------------------------------------------------------------------------
;; xref
;;----------------------------------------------------------------------------
(defun jester/xref-find-references-at-point ()
"Find references for symbol at point."
(interactive)
(xref-find-references (xref-backend-identifier-at-point (xref-find-backend))))
(general-define-key
:states '(normal motion)
"g d" 'xref-find-definitions
"g p" 'xref-pop-marker-stack)
(general-define-key
:states '(normal motion)
:keymaps '(typescript-mode-map typescript-ts-mode-map web-mode-map js2-mode-map)
"g d" 'lsp-bridge-find-def
"g p" 'lsp-bridge-find-def-return
"g r" 'lsp-bridge-find-references
"g t" 'lsp-bridge-find-type-def)
(jester/with-major-leader '(typescript-mode-map typescript-ts-mode-map web-mode-map js2-mode-map)
"r" 'lsp-bridge-rename)
(jester/with-leader
"e l" 'lsp-bridge-diagnostic-list
"e n" 'lsp-bridge-diagnostic-jump-next
"e p" 'lsp-bridge-diagnostic-jump-prev)
(use-package dumb-jump
:init
(setq dumb-jump-aggressive t
dumb-jump-selector 'ivy)
;; sets the global value, will be overwritten by major mode / lsp
(add-hook 'xref-backend-functions #'dumb-jump-xref-activate))
(use-package ivy-xref
:init
;; xref initialization is different in Emacs 27 - there are two different
;; variables which can be set rather than just one
(when (>= emacs-major-version 27)
(setq xref-show-definitions-function #'ivy-xref-show-defs))
;; Necessary in Emacs <27. In Emacs 27 it will affect all xref-based
;; commands other than xref-find-definitions (e.g. project-find-regexp)
;; as well
(setq xref-show-xrefs-function #'ivy-xref-show-xrefs))
;; (use-package citre
;; :custom ((citre-enable-imenu-integration nil)
;; (citre-tags-file-names '("tagz" ".tags" "tags")))
;; :init
;; (require 'citre-config))
;;----------------------------------------------------------------------------
;; save recent find-def(find-ref ?) targets in a ring, one ring for each project,
;; so they can be looked up with ivy
;;----------------------------------------------------------------------------
(ignore-errors (make-directory (locate-user-emacs-file "var/jester")))
(setq jester-project-symbol-history-file (locate-user-emacs-file "var/jester/project-symbol-history.el"))
(defun jester/read-symbol-history ()
"Read symbol history from `jester-project-symbol-history-file'."
(setq jester-project-symbols-map
(if (file-exists-p jester-project-symbol-history-file)
(with-temp-buffer
(insert-file-contents jester-project-symbol-history-file)
(read (current-buffer)))
(ht-create))))
(jester/read-symbol-history)
(defun jester/write-symbol-history ()
"Save `jester-project-symbols-map.'"
(let ((filename jester-project-symbol-history-file))
(with-temp-buffer
(insert ";;; -*- lisp-data -*-\n")
(let ((print-length nil)
(print-level nil))
(pp jester-project-symbols-map (current-buffer)))
(write-region nil nil filename nil 'silent))))
(defun jester/save-symbol-history (&optional sym-arg)
"Advice for find def, save current symbol to history."
(let* ((project-root (projectile-project-root))
(sym (if sym-arg sym-arg (thing-at-point 'symbol t)))
(sym-map-in-project (or (ht-get jester-project-symbols-map project-root)
(ht-create))))
;; clear text properties
(set-text-properties 0 (length sym) nil sym)
(ht-set! sym-map-in-project sym (list (buffer-file-name) (point)))
(ht-set! jester-project-symbols-map project-root sym-map-in-project)
(jester/write-symbol-history)))
;; TODO we want after but the behavior is still before (is find-def async?)
(advice-add 'xref-find-definitions :after 'jester/save-symbol-history)
(advice-add 'lsp-bridge-find-def :after 'jester/save-symbol-history)
;; FIXME: find type def not work well because key is symbol before jump, but this symbol is var name not type name
;; (advice-add 'lsp-bridge-find-type-def :after 'jester/save-symbol-history)
(defun jester/recent-symbol ()
"Select a recent symbol, goto it (place after find-def)."
(interactive)
(let ((sym-map-in-project (ht-get jester-project-symbols-map (projectile-project-root))))
(ivy-read "recent symbol: "
(ht-map (lambda (sym _) sym)
sym-map-in-project)
:action (lambda (selected-sym) (let ((place-info (ht-get sym-map-in-project selected-sym)))
(find-file (car place-info))
(goto-char (nth 1 place-info)))))))
(jester/with-leader "s l" 'jester/recent-symbol)
(provide 'init-jump)