]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/edit.el
Add 2012 to FSF copyright years for Emacs files
[gnu-emacs] / lisp / cedet / semantic / edit.el
1 ;;; semantic/edit.el --- Edit Management for Semantic
2
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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 ;; In Semantic 1.x, changes were handled in a simplistic manner, where
25 ;; tags that changed were reparsed one at a time. Any other form of
26 ;; edit were managed through a full reparse.
27 ;;
28 ;; This code attempts to minimize the number of times a full reparse
29 ;; needs to occur. While overlays and tags will continue to be
30 ;; recycled in the simple case, new cases where tags are inserted
31 ;; or old tags removed from the original list are handled.
32 ;;
33
34 ;;; NOTES FOR IMPROVEMENT
35 ;;
36 ;; Work done by the incremental parser could be improved by the
37 ;; following:
38 ;;
39 ;; 1. Tags created could have as a property an overlay marking a region
40 ;; of themselves that can be edited w/out affecting the definition of
41 ;; that tag.
42 ;;
43 ;; 2. Tags w/ positioned children could have a property of an
44 ;; overlay marking the region in themselves that contain the
45 ;; children. This could be used to better improve splicing near
46 ;; the beginning and end of the child lists.
47 ;;
48
49 ;;; BUGS IN INCREMENTAL PARSER
50 ;;
51 ;; 1. Changes in the whitespace between tags could extend a
52 ;; following tag. These will be marked as merely unmatched
53 ;; syntax instead.
54 ;;
55 ;; 2. Incremental parsing while a new function is being typed in
56 ;; sometimes gets a chance only when lists are incomplete,
57 ;; preventing correct context identification.
58
59 ;;
60 (require 'semantic)
61
62 ;;; Code:
63 (defvar semantic-after-partial-cache-change-hook nil
64 "Normal hook run after the buffer cache has been updated.
65
66 This hook will run when the cache has been partially reparsed.
67 Partial reparses are incurred when a user edits a buffer, and only the
68 modified sections are rescanned.
69
70 Hook functions must take one argument, which is the list of tags
71 updated in the current buffer.
72
73 For language specific hooks, make sure you define this as a local hook.")
74
75 (defvar semantic-change-hooks
76 '(semantic-edits-change-function-handle-changes)
77 "Abnormal hook run when semantic detects a change in a buffer.
78 Each hook function must take three arguments, identical to the
79 common hook `after-change-functions'.")
80
81 (defvar semantic-reparse-needed-change-hook nil
82 "Hooks run when a user edit is detected as needing a reparse.
83 For language specific hooks, make sure you define this as a local hook.
84 Not used yet; part of the next generation reparse mechanism.")
85
86 (defvar semantic-no-reparse-needed-change-hook nil
87 "Hooks run when a user edit is detected as not needing a reparse.
88 If the hook returns non-nil, then declare that a reparse is needed.
89 For language specific hooks, make sure you define this as a local hook.
90 Not used yet; part of the next generation reparse mechanism.")
91
92 (defvar semantic-edits-new-change-hooks nil
93 "Abnormal hook run when a new change is found.
94 Functions must take one argument representing an overlay on that change.")
95
96 (defvar semantic-edits-delete-change-hooks nil
97 "Abnormal hook run before a change overlay is deleted.
98 Deleted changes occur when multiple changes are merged.
99 Functions must take one argument representing an overlay being deleted.")
100
101 (defvar semantic-edits-move-change-hook nil
102 "Abnormal hook run after a change overlay is moved.
103 Changes move when a new change overlaps an old change. The old change
104 will be moved.
105 Functions must take one argument representing an overlay being moved.")
106
107 (defvar semantic-edits-reparse-change-hooks nil
108 "Abnormal hook run after a change results in a reparse.
109 Functions are called before the overlay is deleted, and after the
110 incremental reparse.")
111
112 (defvar semantic-edits-incremental-reparse-failed-hook nil
113 "Hook run after the incremental parser fails.
114 When this happens, the buffer is marked as needing a full reparse.")
115
116 (semantic-varalias-obsolete 'semantic-edits-incremental-reparse-failed-hooks
117 'semantic-edits-incremental-reparse-failed-hook "23.2")
118
119 (defcustom semantic-edits-verbose-flag nil
120 "Non-nil means the incremental parser is verbose.
121 If nil, errors are still displayed, but informative messages are not."
122 :group 'semantic
123 :type 'boolean)
124
125 ;;; Change State management
126 ;;
127 ;; Manage a series of overlays that define changes recently
128 ;; made to the current buffer.
129 ;;;###autoload
130 (defun semantic-change-function (start end length)
131 "Provide a mechanism for semantic tag management.
132 Argument START, END, and LENGTH specify the bounds of the change."
133 (setq semantic-unmatched-syntax-cache-check t)
134 (let ((inhibit-point-motion-hooks t)
135 )
136 (run-hook-with-args 'semantic-change-hooks start end length)
137 ))
138
139 (defun semantic-changes-in-region (start end &optional buffer)
140 "Find change overlays which exist in whole or in part between START and END.
141 Optional argument BUFFER is the buffer to search for changes in."
142 (save-excursion
143 (if buffer (set-buffer buffer))
144 (let ((ol (semantic-overlays-in (max start (point-min))
145 (min end (point-max))))
146 (ret nil))
147 (while ol
148 (when (semantic-overlay-get (car ol) 'semantic-change)
149 (setq ret (cons (car ol) ret)))
150 (setq ol (cdr ol)))
151 (sort ret #'(lambda (a b) (< (semantic-overlay-start a)
152 (semantic-overlay-start b)))))))
153
154 (defun semantic-edits-change-function-handle-changes (start end length)
155 "Run whenever a buffer controlled by `semantic-mode' change.
156 Tracks when and how the buffer is re-parsed.
157 Argument START, END, and LENGTH specify the bounds of the change."
158 ;; We move start/end by one so that we can merge changes that occur
159 ;; just before, or just after. This lets simple typing capture everything
160 ;; into one overlay.
161 (let ((changes-in-change (semantic-changes-in-region (1- start) (1+ end)))
162 )
163 (semantic-parse-tree-set-needs-update)
164 (if (not changes-in-change)
165 (let ((o (semantic-make-overlay start end)))
166 (semantic-overlay-put o 'semantic-change t)
167 ;; Run the hooks safely. When hooks blow it, our dirty
168 ;; function will be removed from the list of active change
169 ;; functions.
170 (condition-case nil
171 (run-hook-with-args 'semantic-edits-new-change-hooks o)
172 (error nil)))
173 (let ((tmp changes-in-change))
174 ;; Find greatest bounds of all changes
175 (while tmp
176 (when (< (semantic-overlay-start (car tmp)) start)
177 (setq start (semantic-overlay-start (car tmp))))
178 (when (> (semantic-overlay-end (car tmp)) end)
179 (setq end (semantic-overlay-end (car tmp))))
180 (setq tmp (cdr tmp)))
181 ;; Move the first found overlay, recycling that overlay.
182 (semantic-overlay-move (car changes-in-change) start end)
183 (condition-case nil
184 (run-hook-with-args 'semantic-edits-move-change-hooks
185 (car changes-in-change))
186 (error nil))
187 (setq changes-in-change (cdr changes-in-change))
188 ;; Delete other changes. They are now all bound here.
189 (while changes-in-change
190 (condition-case nil
191 (run-hook-with-args 'semantic-edits-delete-change-hooks
192 (car changes-in-change))
193 (error nil))
194 (semantic-overlay-delete (car changes-in-change))
195 (setq changes-in-change (cdr changes-in-change))))
196 )))
197
198 (defsubst semantic-edits-flush-change (change)
199 "Flush the CHANGE overlay."
200 (condition-case nil
201 (run-hook-with-args 'semantic-edits-delete-change-hooks
202 change)
203 (error nil))
204 (semantic-overlay-delete change))
205
206 (defun semantic-edits-flush-changes ()
207 "Flush the changes in the current buffer."
208 (let ((changes (semantic-changes-in-region (point-min) (point-max))))
209 (while changes
210 (semantic-edits-flush-change (car changes))
211 (setq changes (cdr changes))))
212 )
213
214 (defun semantic-edits-change-in-one-tag-p (change hits)
215 "Return non-nil of the overlay CHANGE exists solely in one leaf tag.
216 HITS is the list of tags that CHANGE is in. It can have more than
217 one tag in it if the leaf tag is within a parent tag."
218 (and (< (semantic-tag-start (car hits))
219 (semantic-overlay-start change))
220 (> (semantic-tag-end (car hits))
221 (semantic-overlay-end change))
222 ;; Recurse on the rest. If this change is inside all
223 ;; of these tags, then they are all leaves or parents
224 ;; of the smallest tag.
225 (or (not (cdr hits))
226 (semantic-edits-change-in-one-tag-p change (cdr hits))))
227 )
228
229 ;;; Change/Tag Query functions
230 ;;
231 ;; A change (region of space) can effect tags in different ways.
232 ;; These functions perform queries on a buffer to determine different
233 ;; ways that a change effects a buffer.
234 ;;
235 ;; NOTE: After debugging these, replace below to no longer look
236 ;; at point and mark (via comments I assume.)
237 (defsubst semantic-edits-os (change)
238 "For testing: Start of CHANGE, or smaller of (point) and (mark)."
239 (if change (semantic-overlay-start change)
240 (if (< (point) (mark)) (point) (mark))))
241
242 (defsubst semantic-edits-oe (change)
243 "For testing: End of CHANGE, or larger of (point) and (mark)."
244 (if change (semantic-overlay-end change)
245 (if (> (point) (mark)) (point) (mark))))
246
247 (defun semantic-edits-change-leaf-tag (change)
248 "A leaf tag which completely encompasses CHANGE.
249 If change overlaps a tag, but is not encompassed in it, return nil.
250 Use `semantic-edits-change-overlap-leaf-tag'.
251 If CHANGE is completely encompassed in a tag, but overlaps sub-tags,
252 return nil."
253 (let* ((start (semantic-edits-os change))
254 (end (semantic-edits-oe change))
255 (tags (nreverse
256 (semantic-find-tag-by-overlay-in-region
257 start end))))
258 ;; A leaf is always first in this list
259 (if (and tags
260 (<= (semantic-tag-start (car tags)) start)
261 (> (semantic-tag-end (car tags)) end))
262 ;; Ok, we have a match. If this tag has children,
263 ;; we have to do more tests.
264 (let ((chil (semantic-tag-components (car tags))))
265 (if (not chil)
266 ;; Simple leaf.
267 (car tags)
268 ;; For this type, we say that we encompass it if the
269 ;; change occurs outside the range of the children.
270 (if (or (not (semantic-tag-with-position-p (car chil)))
271 (> start (semantic-tag-end (nth (1- (length chil)) chil)))
272 (< end (semantic-tag-start (car chil))))
273 ;; We have modifications to the definition of this parent
274 ;; so we have to reparse the whole thing.
275 (car tags)
276 ;; We actually modified an area between some children.
277 ;; This means we should return nil, as that case is
278 ;; calculated by someone else.
279 nil)))
280 nil)))
281
282 (defun semantic-edits-change-between-tags (change)
283 "Return a cache list of tags surrounding CHANGE.
284 The returned list is the CONS cell in the master list pointing to
285 a tag just before CHANGE. The CDR will have the tag just after CHANGE.
286 CHANGE cannot encompass or overlap a leaf tag.
287 If CHANGE is fully encompassed in a tag that has children, and
288 this change occurs between those children, this returns non-nil.
289 See `semantic-edits-change-leaf-tag' for details on parents."
290 (let* ((start (semantic-edits-os change))
291 (end (semantic-edits-oe change))
292 (tags (nreverse
293 (semantic-find-tag-by-overlay-in-region
294 start end)))
295 (list-to-search nil)
296 (found nil))
297 (if (not tags)
298 (setq list-to-search semantic--buffer-cache)
299 ;; A leaf is always first in this list
300 (if (and (< (semantic-tag-start (car tags)) start)
301 (> (semantic-tag-end (car tags)) end))
302 ;; We are completely encompassed in a tag.
303 (if (setq list-to-search
304 (semantic-tag-components (car tags)))
305 ;; Ok, we are completely encompassed within the first tag
306 ;; entry, AND that tag has children. This means that change
307 ;; occurred outside of all children, but inside some tag
308 ;; with children.
309 (if (or (not (semantic-tag-with-position-p (car list-to-search)))
310 (> start (semantic-tag-end
311 (nth (1- (length list-to-search))
312 list-to-search)))
313 (< end (semantic-tag-start (car list-to-search))))
314 ;; We have modifications to the definition of this parent
315 ;; and not between it's children. Clear the search list.
316 (setq list-to-search nil)))
317 ;; Search list is nil.
318 ))
319 ;; If we have a search list, let's go. Otherwise nothing.
320 (while (and list-to-search (not found))
321 (if (cdr list-to-search)
322 ;; We end when the start of the CDR is after the end of our
323 ;; asked change.
324 (if (< (semantic-tag-start (cadr list-to-search)) end)
325 (setq list-to-search (cdr list-to-search))
326 (setq found t))
327 (setq list-to-search nil)))
328 ;; Return it. If it is nil, there is a logic bug, and we need
329 ;; to avoid this bit of logic anyway.
330 list-to-search
331 ))
332
333 (defun semantic-edits-change-over-tags (change)
334 "Return a cache list of tags surrounding a CHANGE encompassing tags.
335 CHANGE must not only include all overlapped tags (excepting possible
336 parent tags) in their entirety. In this case, the change may be deleting
337 or moving whole tags.
338 The return value is a vector.
339 Cell 0 is a list of all tags completely encompassed in change.
340 Cell 1 is the cons cell into a master parser cache starting with
341 the cell which occurs BEFORE the first position of CHANGE.
342 Cell 2 is the parent of cell 1, or nil for the buffer cache.
343 This function returns nil if any tag covered by change is not
344 completely encompassed.
345 See `semantic-edits-change-leaf-tag' for details on parents."
346 (let* ((start (semantic-edits-os change))
347 (end (semantic-edits-oe change))
348 (tags (nreverse
349 (semantic-find-tag-by-overlay-in-region
350 start end)))
351 (parent nil)
352 (overlapped-tags nil)
353 inner-start inner-end
354 (list-to-search nil))
355 ;; By the time this is already called, we know that it is
356 ;; not a leaf change, nor a between tag change. That leaves
357 ;; an overlap, and this condition.
358
359 ;; A leaf is always first in this list.
360 ;; Is the leaf encompassed in this change?
361 (if (and tags
362 (>= (semantic-tag-start (car tags)) start)
363 (<= (semantic-tag-end (car tags)) end))
364 (progn
365 ;; We encompass one whole change.
366 (setq overlapped-tags (list (car tags))
367 inner-start (semantic-tag-start (car tags))
368 inner-end (semantic-tag-end (car tags))
369 tags (cdr tags))
370 ;; Keep looping while tags are inside the change.
371 (while (and tags
372 (>= (semantic-tag-start (car tags)) start)
373 (<= (semantic-tag-end (car tags)) end))
374
375 ;; Check if this new all-encompassing tag is a parent
376 ;; of that which went before. Only check end because
377 ;; we know that start is less than inner-start since
378 ;; tags was sorted on that.
379 (if (> (semantic-tag-end (car tags)) inner-end)
380 ;; This is a parent. Drop the children found
381 ;; so far.
382 (setq overlapped-tags (list (car tags))
383 inner-start (semantic-tag-start (car tags))
384 inner-end (semantic-tag-end (car tags))
385 )
386 ;; It is not a parent encompassing tag
387 (setq overlapped-tags (cons (car tags)
388 overlapped-tags)
389 inner-start (semantic-tag-start (car tags))))
390 (setq tags (cdr tags)))
391 (if (not tags)
392 ;; There are no tags left, and all tags originally
393 ;; found are encompassed by the change. Setup our list
394 ;; from the cache
395 (setq list-to-search semantic--buffer-cache);; We have a tag outside the list. Check for
396 ;; We know we have a parent because it would
397 ;; completely cover the change. A tag can only
398 ;; do that if it is a parent after we get here.
399 (when (and tags
400 (< (semantic-tag-start (car tags)) start)
401 (> (semantic-tag-end (car tags)) end))
402 ;; We have a parent. Stuff in the search list.
403 (setq parent (car tags)
404 list-to-search (semantic-tag-components parent))
405 ;; If the first of TAGS is a parent (see above)
406 ;; then clear out the list. All other tags in
407 ;; here must therefore be parents of the car.
408 (setq tags nil)
409 ;; One last check, If start is before the first
410 ;; tag or after the last, we may have overlap into
411 ;; the characters that make up the definition of
412 ;; the tag we are parsing.
413 (when (or (semantic-tag-with-position-p (car list-to-search))
414 (< start (semantic-tag-start
415 (car list-to-search)))
416 (> end (semantic-tag-end
417 (nth (1- (length list-to-search))
418 list-to-search))))
419 ;; We have a problem
420 (setq list-to-search nil
421 parent nil))))
422
423 (when list-to-search
424
425 ;; Ok, return the vector only if all TAGS are
426 ;; confirmed as the lineage of `overlapped-tags'
427 ;; which must have a value by now.
428
429 ;; Loop over the search list to find the preceding CDR.
430 ;; Fortunately, (car overlapped-tags) happens to be
431 ;; the first tag positionally.
432 (let ((tokstart (semantic-tag-start (car overlapped-tags))))
433 (while (and list-to-search
434 ;; Assume always (car (cdr list-to-search)).
435 ;; A thrown error will be captured nicely, but
436 ;; that case shouldn't happen.
437
438 ;; We end when the start of the CDR is after the
439 ;; end of our asked change.
440 (cdr list-to-search)
441 (< (semantic-tag-start (car (cdr list-to-search)))
442 tokstart)
443 (setq list-to-search (cdr list-to-search)))))
444 ;; Create the return vector
445 (vector overlapped-tags
446 list-to-search
447 parent)
448 ))
449 nil)))
450
451 ;;; Default Incremental Parser
452 ;;
453 ;; Logic about how to group changes for effective reparsing and splicing.
454
455 (defun semantic-parse-changes-failed (&rest args)
456 "Signal that Semantic failed to parse changes.
457 That is, display a message by passing all ARGS to `format', then throw
458 a 'semantic-parse-changes-failed exception with value t."
459 (when semantic-edits-verbose-flag
460 (message "Semantic parse changes failed: %S"
461 (apply 'format args)))
462 (throw 'semantic-parse-changes-failed t))
463
464 (defsubst semantic-edits-incremental-fail ()
465 "When the incremental parser fails, we mark that we need a full reparse."
466 ;;(debug)
467 (semantic-parse-tree-set-needs-rebuild)
468 (when semantic-edits-verbose-flag
469 (message "Force full reparse (%s)"
470 (buffer-name (current-buffer))))
471 (run-hooks 'semantic-edits-incremental-reparse-failed-hook))
472
473 ;;;###autoload
474 (defun semantic-edits-incremental-parser ()
475 "Incrementally reparse the current buffer.
476 Incremental parser allows semantic to only reparse those sections of
477 the buffer that have changed. This function depends on
478 `semantic-edits-change-function-handle-changes' setting up change
479 overlays in the current buffer. Those overlays are analyzed against
480 the semantic cache to see what needs to be changed."
481 (let ((changed-tags
482 ;; Don't use `semantic-safe' here to explicitly catch errors
483 ;; and reset the parse tree.
484 (catch 'semantic-parse-changes-failed
485 (if debug-on-error
486 (semantic-edits-incremental-parser-1)
487 (condition-case err
488 (semantic-edits-incremental-parser-1)
489 (error
490 (message "incremental parser error: %S"
491 (error-message-string err))
492 t))))))
493 (when (eq changed-tags t)
494 ;; Force a full reparse.
495 (semantic-edits-incremental-fail)
496 (setq changed-tags nil))
497 changed-tags))
498
499 (defmacro semantic-edits-assert-valid-region ()
500 "Assert that parse-start and parse-end are sorted correctly."
501 ;;; (if (> parse-start parse-end)
502 ;;; (error "Bug is %s !> %d! Buff min/max = [ %d %d ]"
503 ;;; parse-start parse-end
504 ;;; (point-min) (point-max)))
505 )
506
507 (defun semantic-edits-incremental-parser-1 ()
508 "Incrementally reparse the current buffer.
509 Return the list of tags that changed.
510 If the incremental parse fails, throw a 'semantic-parse-changes-failed
511 exception with value t, that can be caught to schedule a full reparse.
512 This function is for internal use by `semantic-edits-incremental-parser'."
513 (let* ((changed-tags nil)
514 (debug-on-quit t) ; try to find this annoying bug!
515 (changes (semantic-changes-in-region
516 (point-min) (point-max)))
517 (tags nil) ;tags found at changes
518 (newf-tags nil) ;newfound tags in change
519 (parse-start nil) ;location to start parsing
520 (parse-end nil) ;location to end parsing
521 (parent-tag nil) ;parent of the cache list.
522 (cache-list nil) ;list of children within which
523 ;we incrementally reparse.
524 (reparse-symbol nil) ;The ruled we start at for reparse.
525 (change-group nil) ;changes grouped in this reparse
526 (last-cond nil) ;track the last case used.
527 ;query this when debugging to find
528 ;source of bugs.
529 )
530 (or changes
531 ;; If we were called, and there are no changes, then we
532 ;; don't know what to do. Force a full reparse.
533 (semantic-parse-changes-failed "Don't know what to do"))
534 ;; Else, we have some changes. Loop over them attempting to
535 ;; patch things up.
536 (while changes
537 ;; Calculate the reparse boundary.
538 ;; We want to take some set of changes, and group them
539 ;; together into a small change group. One change forces
540 ;; a reparse of a larger region (the size of some set of
541 ;; tags it encompasses.) It may contain several tags.
542 ;; That region may have other changes in it (several small
543 ;; changes in one function, for example.)
544 ;; Optimize for the simple cases here, but try to handle
545 ;; complex ones too.
546
547 (while (and changes ; we still have changes
548 (or (not parse-start)
549 ;; Below, if the change we are looking at
550 ;; is not the first change for this
551 ;; iteration, and it starts before the end
552 ;; of current parse region, then it is
553 ;; encompassed within the bounds of tags
554 ;; modified by the previous iteration's
555 ;; change.
556 (< (semantic-overlay-start (car changes))
557 parse-end)))
558
559 ;; REMOVE LATER
560 (if (eq (car changes) (car change-group))
561 (semantic-parse-changes-failed
562 "Possible infinite loop detected"))
563
564 ;; Store this change in this change group.
565 (setq change-group (cons (car changes) change-group))
566
567 (cond
568 ;; Is this is a new parse group?
569 ((not parse-start)
570 (setq last-cond "new group")
571 (let (tmp)
572 (cond
573
574 ;;;; Are we encompassed all in one tag?
575 ((setq tmp (semantic-edits-change-leaf-tag (car changes)))
576 (setq last-cond "Encompassed in tag")
577 (setq tags (list tmp)
578 parse-start (semantic-tag-start tmp)
579 parse-end (semantic-tag-end tmp)
580 )
581 (semantic-edits-assert-valid-region))
582
583 ;;;; Did the change occur between some tags?
584 ((setq cache-list (semantic-edits-change-between-tags
585 (car changes)))
586 (setq last-cond "Between and not overlapping tags")
587 ;; The CAR of cache-list is the tag just before
588 ;; our change, but wasn't modified. Hmmm.
589 ;; Bound our reparse between these two tags
590 (setq tags nil
591 parent-tag
592 (car (semantic-find-tag-by-overlay
593 parse-start)))
594 (cond
595 ;; A change at the beginning of the buffer.
596 ;; Feb 06 -
597 ;; IDed when the first cache-list tag is after
598 ;; our change, meaning there is nothing before
599 ;; the change.
600 ((> (semantic-tag-start (car cache-list))
601 (semantic-overlay-end (car changes)))
602 (setq last-cond "Beginning of buffer")
603 (setq parse-start
604 ;; Don't worry about parents since
605 ;; there there would be an exact
606 ;; match in the tag list otherwise
607 ;; and the routine would fail.
608 (point-min)
609 parse-end
610 (semantic-tag-start (car cache-list)))
611 (semantic-edits-assert-valid-region)
612 )
613 ;; A change stuck on the first surrounding tag.
614 ((= (semantic-tag-end (car cache-list))
615 (semantic-overlay-start (car changes)))
616 (setq last-cond "Beginning of Tag")
617 ;; Reparse that first tag.
618 (setq parse-start
619 (semantic-tag-start (car cache-list))
620 parse-end
621 (semantic-overlay-end (car changes))
622 tags
623 (list (car cache-list)))
624 (semantic-edits-assert-valid-region)
625 )
626 ;; A change at the end of the buffer.
627 ((not (car (cdr cache-list)))
628 (setq last-cond "End of buffer")
629 (setq parse-start (semantic-tag-end
630 (car cache-list))
631 parse-end (point-max))
632 (semantic-edits-assert-valid-region)
633 )
634 (t
635 (setq last-cond "Default")
636 (setq parse-start
637 (semantic-tag-end (car cache-list))
638 parse-end
639 (semantic-tag-start (car (cdr cache-list)))
640 )
641 (semantic-edits-assert-valid-region))))
642
643 ;;;; Did the change completely overlap some number of tags?
644 ((setq tmp (semantic-edits-change-over-tags
645 (car changes)))
646 (setq last-cond "Overlap multiple tags")
647 ;; Extract the information
648 (setq tags (aref tmp 0)
649 cache-list (aref tmp 1)
650 parent-tag (aref tmp 2))
651 ;; We can calculate parse begin/end by checking
652 ;; out what is in TAGS. The one near start is
653 ;; always first. Make sure the reparse includes
654 ;; the `whitespace' around the snarfed tags.
655 ;; Since cache-list is positioned properly, use it
656 ;; to find that boundary.
657 (if (eq (car tags) (car cache-list))
658 ;; Beginning of the buffer!
659 (let ((end-marker (nth (length tags)
660 cache-list)))
661 (setq parse-start (point-min))
662 (if end-marker
663 (setq parse-end
664 (semantic-tag-start end-marker))
665 (setq parse-end (semantic-overlay-end
666 (car changes))))
667 (semantic-edits-assert-valid-region)
668 )
669 ;; Middle of the buffer.
670 (setq parse-start
671 (semantic-tag-end (car cache-list)))
672 ;; For the end, we need to scoot down some
673 ;; number of tags. We 1+ the length of tags
674 ;; because we want to skip the first tag
675 ;; (remove 1-) then want the tag after the end
676 ;; of the list (1+)
677 (let ((end-marker (nth (1+ (length tags)) cache-list)))
678 (if end-marker
679 (setq parse-end (semantic-tag-start end-marker))
680 ;; No marker. It is the last tag in our
681 ;; list of tags. Only possible if END
682 ;; already matches the end of that tag.
683 (setq parse-end
684 (semantic-overlay-end (car changes)))))
685 (semantic-edits-assert-valid-region)
686 ))
687
688 ;;;; Unhandled case.
689 ;; Throw error, and force full reparse.
690 ((semantic-parse-changes-failed "Unhandled change group")))
691 ))
692 ;; Is this change inside the previous parse group?
693 ;; We already checked start.
694 ((< (semantic-overlay-end (car changes)) parse-end)
695 (setq last-cond "in bounds")
696 nil)
697 ;; This change extends the current parse group.
698 ;; Find any new tags, and see how to append them.
699 ((semantic-parse-changes-failed
700 (setq last-cond "overlap boundary")
701 "Unhandled secondary change overlapping boundary"))
702 )
703 ;; Prepare for the next iteration.
704 (setq changes (cdr changes)))
705
706 ;; By the time we get here, all TAGS are children of
707 ;; some parent. They should all have the same start symbol
708 ;; since that is how the multi-tag parser works. Grab
709 ;; the reparse symbol from the first of the returned tags.
710 ;;
711 ;; Feb '06 - If reparse-symbol is nil, then they are top level
712 ;; tags. (I'm guessing.) Is this right?
713 (setq reparse-symbol
714 (semantic--tag-get-property (car (or tags cache-list))
715 'reparse-symbol))
716 ;; Find a parent if not provided.
717 (and (not parent-tag) tags
718 (setq parent-tag
719 (semantic-find-tag-parent-by-overlay
720 (car tags))))
721 ;; We can do the same trick for our parent and resulting
722 ;; cache list.
723 (unless cache-list
724 (if parent-tag
725 (setq cache-list
726 ;; We need to get all children in case we happen
727 ;; to have a mix of positioned and non-positioned
728 ;; children.
729 (semantic-tag-components parent-tag))
730 ;; Else, all the tags since there is no parent.
731 ;; It sucks to have to use the full buffer cache in
732 ;; this case because it can be big. Failure to provide
733 ;; however results in a crash.
734 (setq cache-list semantic--buffer-cache)
735 ))
736 ;; Use the boundary to calculate the new tags found.
737 (setq newf-tags (semantic-parse-region
738 parse-start parse-end reparse-symbol))
739 ;; Make sure all these tags are given overlays.
740 ;; They have already been cooked by the parser and just
741 ;; need the overlays.
742 (let ((tmp newf-tags))
743 (while tmp
744 (semantic--tag-link-to-buffer (car tmp))
745 (setq tmp (cdr tmp))))
746
747 ;; See how this change lays out.
748 (cond
749
750 ;;;; Whitespace change
751 ((and (not tags) (not newf-tags))
752 ;; A change that occurred outside of any existing tags
753 ;; and there are no new tags to replace it.
754 (when semantic-edits-verbose-flag
755 (message "White space changes"))
756 nil
757 )
758
759 ;;;; New tags in old whitespace area.
760 ((and (not tags) newf-tags)
761 ;; A change occurred outside existing tags which added
762 ;; a new tag. We need to splice these tags back
763 ;; into the cache at the right place.
764 (semantic-edits-splice-insert newf-tags parent-tag cache-list)
765
766 (setq changed-tags
767 (append newf-tags changed-tags))
768
769 (when semantic-edits-verbose-flag
770 (message "Inserted tags: (%s)"
771 (semantic-format-tag-name (car newf-tags))))
772 )
773
774 ;;;; Old tags removed
775 ((and tags (not newf-tags))
776 ;; A change occurred where pre-existing tags were
777 ;; deleted! Remove the tag from the cache.
778 (semantic-edits-splice-remove tags parent-tag cache-list)
779
780 (setq changed-tags
781 (append tags changed-tags))
782
783 (when semantic-edits-verbose-flag
784 (message "Deleted tags: (%s)"
785 (semantic-format-tag-name (car tags))))
786 )
787
788 ;;;; One tag was updated.
789 ((and (= (length tags) 1) (= (length newf-tags) 1))
790 ;; One old tag was modified, and it is replaced by
791 ;; One newfound tag. Splice the new tag into the
792 ;; position of the old tag.
793 ;; Do the splice.
794 (semantic-edits-splice-replace (car tags) (car newf-tags))
795 ;; Add this tag to our list of changed toksns
796 (setq changed-tags (cons (car tags) changed-tags))
797 ;; Debug
798 (when semantic-edits-verbose-flag
799 (message "Update Tag Table: %s"
800 (semantic-format-tag-name (car tags) nil t)))
801 ;; Flush change regardless of above if statement.
802 )
803
804 ;;;; Some unhandled case.
805 ((semantic-parse-changes-failed "Don't know what to do")))
806
807 ;; We got this far, and we didn't flag a full reparse.
808 ;; Clear out this change group.
809 (while change-group
810 (semantic-edits-flush-change (car change-group))
811 (setq change-group (cdr change-group)))
812
813 ;; Don't increment change here because an earlier loop
814 ;; created change-groups.
815 (setq parse-start nil)
816 )
817 ;; Mark that we are done with this glop
818 (semantic-parse-tree-set-up-to-date)
819 ;; Return the list of tags that changed. The caller will
820 ;; use this information to call hooks which can fix themselves.
821 changed-tags))
822
823 ;; Make it the default changes parser
824 ;;;###autoload
825 (defalias 'semantic-parse-changes-default
826 'semantic-edits-incremental-parser)
827
828 ;;; Cache Splicing
829 ;;
830 ;; The incremental parser depends on the ability to parse up sections
831 ;; of the file, and splice the results back into the cache. There are
832 ;; three types of splices. A REPLACE, an ADD, and a REMOVE. REPLACE
833 ;; is one of the simpler cases, as the starting cons cell representing
834 ;; the old tag can be used to auto-splice in. ADD and REMOVE
835 ;; require scanning the cache to find the correct location so that the
836 ;; list can be fiddled.
837 (defun semantic-edits-splice-remove (oldtags parent cachelist)
838 "Remove OLDTAGS from PARENT's CACHELIST.
839 OLDTAGS are tags in the current buffer, preferably linked
840 together also in CACHELIST.
841 PARENT is the parent tag containing OLDTAGS.
842 CACHELIST should be the children from PARENT, but may be
843 pre-positioned to a convenient location."
844 (let* ((first (car oldtags))
845 (last (nth (1- (length oldtags)) oldtags))
846 (chil (if parent
847 (semantic-tag-components parent)
848 semantic--buffer-cache))
849 (cachestart cachelist)
850 (cacheend nil)
851 )
852 ;; First in child list?
853 (if (eq first (car chil))
854 ;; First tags in the cache are being deleted.
855 (progn
856 (when semantic-edits-verbose-flag
857 (message "To Remove First Tag: (%s)"
858 (semantic-format-tag-name first)))
859 ;; Find the last tag
860 (setq cacheend chil)
861 (while (and cacheend (not (eq last (car cacheend))))
862 (setq cacheend (cdr cacheend)))
863 ;; The spliceable part is after cacheend.. so move cacheend
864 ;; one more tag.
865 (setq cacheend (cdr cacheend))
866 ;; Splice the found end tag into the cons cell
867 ;; owned by the current top child.
868 (setcar chil (car cacheend))
869 (setcdr chil (cdr cacheend))
870 (when (not cacheend)
871 ;; No cacheend.. then the whole system is empty.
872 ;; The best way to deal with that is to do a full
873 ;; reparse
874 (semantic-parse-changes-failed "Splice-remove failed. Empty buffer?")
875 ))
876 (message "To Remove Middle Tag: (%s)"
877 (semantic-format-tag-name first)))
878 ;; Find in the cache the preceding tag
879 (while (and cachestart (not (eq first (car (cdr cachestart)))))
880 (setq cachestart (cdr cachestart)))
881 ;; Find the last tag
882 (setq cacheend cachestart)
883 (while (and cacheend (not (eq last (car cacheend))))
884 (setq cacheend (cdr cacheend)))
885 ;; Splice the end position into the start position.
886 ;; If there is no start, then this whole section is probably
887 ;; gone.
888 (if cachestart
889 (setcdr cachestart (cdr cacheend))
890 (semantic-parse-changes-failed "Splice-remove failed."))
891
892 ;; Remove old overlays of these deleted tags
893 (while oldtags
894 (semantic--tag-unlink-from-buffer (car oldtags))
895 (setq oldtags (cdr oldtags)))
896 ))
897
898 (defun semantic-edits-splice-insert (newtags parent cachelist)
899 "Insert NEWTAGS into PARENT using CACHELIST.
900 PARENT could be nil, in which case CACHLIST is the buffer cache
901 which must be updated.
902 CACHELIST must be searched to find where NEWTAGS are to be inserted.
903 The positions of NEWTAGS must be synchronized with those in
904 CACHELIST for this to work. Some routines pre-position CACHLIST at a
905 convenient location, so use that."
906 (let* ((start (semantic-tag-start (car newtags)))
907 (newtagendcell (nthcdr (1- (length newtags)) newtags))
908 (end (semantic-tag-end (car newtagendcell)))
909 )
910 (if (> (semantic-tag-start (car cachelist)) start)
911 ;; We are at the beginning.
912 (let* ((pc (if parent
913 (semantic-tag-components parent)
914 semantic--buffer-cache))
915 (nc (cons (car pc) (cdr pc))) ; new cons cell.
916 )
917 ;; Splice the new cache cons cell onto the end of our list.
918 (setcdr newtagendcell nc)
919 ;; Set our list into parent.
920 (setcar pc (car newtags))
921 (setcdr pc (cdr newtags)))
922 ;; We are at the end, or in the middle. Find our match first.
923 (while (and (cdr cachelist)
924 (> end (semantic-tag-start (car (cdr cachelist)))))
925 (setq cachelist (cdr cachelist)))
926 ;; Now splice into the list!
927 (setcdr newtagendcell (cdr cachelist))
928 (setcdr cachelist newtags))))
929
930 (defun semantic-edits-splice-replace (oldtag newtag)
931 "Replace OLDTAG with NEWTAG in the current cache.
932 Do this by recycling OLDTAG's first CONS cell. This effectively
933 causes the new tag to completely replace the old one.
934 Make sure that all information in the overlay is transferred.
935 It is presumed that OLDTAG and NEWTAG are both cooked.
936 When this routine returns, OLDTAG is raw, and the data will be
937 lost if not transferred into NEWTAG."
938 (let* ((oo (semantic-tag-overlay oldtag))
939 (o (semantic-tag-overlay newtag))
940 (oo-props (semantic-overlay-properties oo)))
941 (while oo-props
942 (semantic-overlay-put o (car oo-props) (car (cdr oo-props)))
943 (setq oo-props (cdr (cdr oo-props)))
944 )
945 ;; Free the old overlay(s)
946 (semantic--tag-unlink-from-buffer oldtag)
947 ;; Recover properties
948 (semantic--tag-copy-properties oldtag newtag)
949 ;; Splice into the main list.
950 (setcdr oldtag (cdr newtag))
951 (setcar oldtag (car newtag))
952 ;; This important bit is because the CONS cell representing
953 ;; OLDTAG is now pointing to NEWTAG, but the NEWTAG
954 ;; cell is about to be abandoned. Here we update our overlay
955 ;; to point at the updated state of the world.
956 (semantic-overlay-put o 'semantic oldtag)
957 ))
958
959 (add-hook 'semantic-before-toplevel-cache-flush-hook
960 #'semantic-edits-flush-changes)
961
962 (provide 'semantic/edit)
963
964 ;; Local variables:
965 ;; generated-autoload-file: "loaddefs.el"
966 ;; generated-autoload-load-name: "semantic/edit"
967 ;; End:
968
969 ;;; semantic/edit.el ends here