lisp/cedet/semantic/symref/filter.el: New file.
[bpt/emacs.git] / lisp / cedet / semantic / symref / filter.el
CommitLineData
ea041226
CY
1;;; semantic/symref/filter.el --- Filter symbol reference hits for accuracy.
2
3;;; Copyright (C) 2009 Free Software Foundation, Inc.
4
5;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22;;; Commentary:
23;;
24;; Filter symbol reference hits for accuracy.
25;;
26;; Most symbol referencing tools, such as find/grep only find matching
27;; strings, but cannot determine the difference between an actual use,
28;; and something else with a similar name, or even a string in a comment.
29;;
30;; This file provides utilities for filtering down to accurate matches
31;; starting at a basic filter level that doesn't use symref, up to filters
32;; across symref results.
33
34;;; Code:
35
36(require 'semantic)
37
38;;; FILTERS
39;;
40(defun semantic-symref-filter-hit (target &optional position)
41 "Determine if the tag TARGET is used at POSITION in the current buffer.
42Return non-nil for a match."
43 (semantic-analyze-current-symbol
44 (lambda (start end prefix)
45 (let ((tag (car (nreverse prefix))))
46 (and (semantic-tag-p tag)
47 (semantic-equivalent-tag-p target tag))))
48 position))
49
50;;; IN-BUFFER FILTERING
51
52;; The following does filtering in-buffer only, and not against
53;; a symref results object.
54
55(defun semantic-symref-hits-in-region (target hookfcn start end)
56 "Find all occurances of the symbol TARGET that match TARGET the tag.
57For each match, call HOOKFCN.
58HOOKFCN takes three arguments that match
59`semantic-analyze-current-symbol's use of HOOKfCN.
60 ( START END PREFIX )
61
62Search occurs in the current buffer between START and END."
63 (save-excursion
64 (goto-char start)
65 (let* ((str (semantic-tag-name target))
66 (case-fold-search semantic-case-fold)
67 (regexp (concat "\\<" (regexp-quote str) "\\>")))
68 (while (re-search-forward regexp end t)
69 (when (semantic-idle-summary-useful-context-p)
70 (semantic-analyze-current-symbol
71 (lambda (start end prefix)
72 (let ((tag (car (nreverse prefix))))
73 ;; check for semantic match on the text match.
74 (when (and (semantic-tag-p tag)
75 (semantic-equivalent-tag-p target tag))
76 (save-excursion
77 (funcall hookfcn start end prefix)))))
78 (point)))))))
79
80;;;###autoload
81(defun semantic-symref-test-count-hits-in-tag ()
82 "Lookup in the current tag the symbol under point.
83the count all the other references to the same symbol within the
84tag that contains point, and return that."
85 (interactive)
86 (let* ((ctxt (semantic-analyze-current-context))
87 (target (car (reverse (oref ctxt prefix))))
88 (tag (semantic-current-tag))
89 (start (current-time))
90 (Lcount 0))
91 (when (semantic-tag-p target)
92 (semantic-symref-hits-in-region
93 target (lambda (start end prefix) (setq Lcount (1+ Lcount)))
94 (semantic-tag-start tag)
95 (semantic-tag-end tag))
96 (when (interactive-p)
97 (message "Found %d occurances of %s in %.2f seconds"
98 Lcount (semantic-tag-name target)
99 (semantic-elapsed-time start (current-time))))
100 Lcount)))
101
102(defun semantic-symref-rename-local-variable ()
103 "Fancy way to rename the local variable under point.
104Depends on the SRecode Field editing API."
105 (interactive)
106 ;; Do the replacement as needed.
107 (let* ((ctxt (semantic-analyze-current-context))
108 (target (car (reverse (oref ctxt prefix))))
109 (tag (semantic-current-tag))
110 )
111
112 (when (or (not target)
113 (not (semantic-tag-with-position-p target)))
114 (error "Cannot identify symbol under point"))
115
116 (when (not (semantic-tag-of-class-p target 'variable))
117 (error "Can only rename variables"))
118
119 (when (or (< (semantic-tag-start target) (semantic-tag-start tag))
120 (> (semantic-tag-end target) (semantic-tag-end tag)))
121 (error "Can only rename variables declared in %s"
122 (semantic-tag-name tag)))
123
124 ;; I think we're good for this example. Give it a go through
125 ;; our fancy interface from SRecode.
126 (require 'srecode-fields)
127
128 ;; Make sure there is nothing active.
129 (let ((ar (srecode-active-template-region)))
130 (when ar (srecode-delete ar)))
131
132 (let ((srecode-field-archive nil)
133 (region nil)
134 )
135 (semantic-symref-hits-in-region
136 target (lambda (start end prefix)
137 ;; For every valid hit, create one field.
138 (srecode-field "LOCAL" :name "LOCAL" :start start :end end))
139 (semantic-tag-start tag) (semantic-tag-end tag))
140
141 ;; Now that the fields are setup, create the region.
142 (setq region (srecode-template-inserted-region
143 "REGION" :start (semantic-tag-start tag)
144 :end (semantic-tag-end tag)))
145
146 ;; Activate the region.
147 (srecode-overlaid-activate region)
148
149 )
150 ))
151
152(provide 'semantic/symref/filter)
153
154;; Local variables:
155;; generated-autoload-file: "../loaddefs.el"
156;; generated-autoload-feature: semantic/loaddefs
157;; generated-autoload-load-name: "semantic/symref/filter"
158;; End:
159
160;;; semantic/symref/filter.el ends here