forked from emacs-helm/helm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-semantic.el
137 lines (117 loc) · 4.94 KB
/
helm-semantic.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
132
133
134
135
136
137
;;; helm-semantic.el --- Helm interface for Semantic -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2014 Daniel Hackney <[email protected]>
;; Author: Daniel Hackney <[email protected]>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Uses `candidates-in-buffer' for speed.
;;; Code:
(require 'cl-lib)
(require 'semantic)
(require 'helm-imenu)
(declare-function pulse-momentary-highlight-one-line "pulse.el" (point &optional face))
(defun helm-semantic-init-candidates (tags depth &optional class)
"Write the contents of TAGS to the current buffer."
(let ((class class) cur-type)
(cl-dolist (tag tags)
(when (listp tag)
(cl-case (setq cur-type (semantic-tag-class tag))
((function variable type)
(let ((spaces (make-string (* depth 2) ?\s))
(type-p (eq cur-type 'type)))
(unless (and (> depth 0) (not type-p))
(setq class nil))
(insert
(if (and class (not type-p))
(format "%s%sClass(%s) "
spaces (if (< depth 2) "" "├►") class)
spaces)
;; Save the tag for later
(propertize (semantic-format-tag-summarize tag nil t)
'semantic-tag tag)
"\n")
(and type-p (setq class (car tag)))
;; Recurse to children
(helm-semantic-init-candidates
(semantic-tag-components tag) (1+ depth) class)))
;; Don't do anything with packages or includes for now
((package include))
;; Catch-all
(t))))))
(defun helm-semantic-default-action (_candidate &optional persistent)
;; By default, helm doesn't pass on the text properties of the selection.
;; Fix this.
(helm-log-run-hook 'helm-goto-line-before-hook)
(with-current-buffer helm-buffer
(when (looking-at " ")
(goto-char (next-single-property-change
(point-at-bol) 'semantic-tag nil (point-at-eol))))
(let ((tag (get-text-property (point) 'semantic-tag)))
(semantic-go-to-tag tag)
(unless persistent
(pulse-momentary-highlight-one-line (point))))))
(defun helm-semantic--maybe-set-needs-update ()
(with-helm-current-buffer
(let ((tick (buffer-modified-tick)))
(unless (eq helm-cached-imenu-tick tick)
(setq helm-cached-imenu-tick tick)
(semantic-parse-tree-set-needs-update)))))
(defvar helm-source-semantic
'((name . "Semantic Tags")
(init . (lambda ()
(helm-semantic--maybe-set-needs-update)
(let ((tags (semantic-fetch-tags)))
(with-current-buffer (helm-candidate-buffer 'global)
(helm-semantic-init-candidates tags 0)))))
(candidates-in-buffer)
(allow-dups)
(get-line . buffer-substring)
(persistent-action . (lambda (elm)
(helm-semantic-default-action elm t)
(helm-highlight-current-line)))
(persistent-help . "Show this entry")
(action . helm-semantic-default-action)
"Source to search tags using Semantic from CEDET."))
;;;###autoload
(defun helm-semantic ()
"Preconfigured `helm' for `semantic'."
(interactive)
(let ((str (thing-at-point 'symbol)))
(helm :sources 'helm-source-semantic
:default (list (concat "\\_<" str "\\_>") str)
:buffer "*helm semantic*")))
;;;###autoload
(defun helm-semantic-or-imenu ()
"Run `helm' with `semantic' or `imenu'.
If `semantic-mode' is active in the current buffer, then use
semantic for generating tags, otherwise fall back to `imenu'.
Fill in the symbol at point by default."
(interactive)
(let* ((source (if (semantic-active-p)
'helm-source-semantic
'helm-source-imenu))
(imenu-p (eq source 'helm-source-imenu))
(str (thing-at-point 'symbol))
(imenu-auto-rescan imenu-p)
(helm-execute-action-at-once-if-one
(and imenu-p
helm-imenu-execute-action-at-once-if-one)))
(helm :sources source
:default (list (concat "\\_<" str "\\_>") str)
:buffer "*helm semantic/imenu*"
:preselect (unless imenu-p (thing-at-point 'symbol)))))
(provide 'helm-semantic)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions obsolete)
;; coding: utf-8
;; indent-tabs-mode: nil
;; End:
;;; helm-semantic.el ends here