]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/edit.el
ff4064195bffd5a2b8d3e1ebc4de3ad687838d1a
[gnu-emacs] / lisp / cedet / semantic / edit.el
1 ;;; semantic/edit.el --- Edit Management for Semantic
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; In Semantic 1.x, changes were handled in a simplistic manner, where
26 ;; tags that changed were reparsed one at a time. Any other form of
27 ;; edit were managed through a full reparse.
28 ;;
29 ;; This code attempts to minimize the number of times a full reparse
30 ;; needs to occur. While overlays and tags will continue to be
31 ;; recycled in the simple case, new cases where tags are inserted
32 ;; or old tags removed from the original list are handled.
33 ;;
34
35 ;;; NOTES FOR IMPROVEMENT
36 ;;
37 ;; Work done by the incremental parser could be improved by the
38 ;; following:
39 ;;
40 ;; 1. Tags created could have as a property an overlay marking a region
41 ;; of themselves that can be edited w/out affecting the definition of
42 ;; that tag.
43 ;;
44 ;; 2. Tags w/ positioned children could have a property of an
45 ;; overlay marking the region in themselves that contain the
46 ;; children. This could be used to better improve splicing near
47 ;; the beginning and end of the child lists.
48 ;;
49
50 ;;; BUGS IN INCREMENTAL PARSER
51 ;;
52 ;; 1. Changes in the whitespace between tags could extend a
53 ;; following tag. These will be marked as merely unmatched
54 ;; syntax instead.
55 ;;
56 ;; 2. Incremental parsing while a new function is being typed in
57 ;; sometimes gets a chance only when lists are incomplete,
58 ;; preventing correct context identification.
59
60 ;;
61 (require 'semantic)
62
63 ;;; Code:
64 (defvar semantic-after-partial-cache-change-hook nil
65 "Normal hook run after the buffer cache has been updated.
66
67 This hook will run when the cache has been partially reparsed.
68 Partial reparses are incurred when a user edits a buffer, and only the
69 modified sections are rescanned.
70
71 Hook functions must take one argument, which is the list of tags
72 updated in the current buffer.
73
74 For language specific hooks, make sure you define this as a local hook.")
75
76 (defvar semantic-change-hooks
77 '(semantic-edits-change-function-handle-changes)
78 "Abnormal hook run when semantic detects a change in a buffer.
79 Each hook function must take three arguments, identical to the
80 common hook `after-change-functions'.")
81
82 (defvar semantic-reparse-needed-change-hook nil
83 "Hooks run when a user edit is detected as needing a reparse.
84 For language specific hooks, make sure you define this as a local hook.
85 Not used yet; part of the next generation reparse mechanism.")
86
87 (defvar semantic-no-reparse-needed-change-hook nil
88 "Hooks run when a user edit is detected as not needing a reparse.
89 If the hook returns non-nil, then declare that a reparse is needed.
90 For language specific hooks, make sure you define this as a local hook.
91 Not used yet; part of the next generation reparse mechanism.")
92
93 (defvar semantic-edits-new-change-hooks nil
94 "Abnormal hook run when a new change is found.
95 Functions must take one argument representing an overlay on that change.")
96
97 (defvar semantic-edits-delete-change-hooks nil
98 "Abnormal hook run before a change overlay is deleted.
99 Deleted changes occur when multiple changes are merged.
100 Functions must take one argument representing an overlay being deleted.")
101
102 (defvar semantic-edits-move-change-hook nil
103 "Abnormal hook run after a change overlay is moved.
104 Changes move when a new change overlaps an old change. The old change
105 will be moved.
106 Functions must take one argument representing an overlay being moved.")
107
108 (defvar semantic-edits-reparse-change-hooks nil
109 "Abnormal hook run after a change results in a reparse.
110 Functions are called before the overlay is deleted, and after the
111 incremental reparse.")
112
113 (defvar semantic-edits-incremental-reparse-failed-hook nil
114 "Hook run after the incremental parser fails.
115 When this happens, the buffer is marked as needing a full reparse.")
116
117 (semantic-varalias-obsolete 'semantic-edits-incremental-reparse-failed-hooks
118 'semantic-edits-incremental-reparse-failed-hook "23.2")
119
120 (defcustom semantic-edits-verbose-flag nil
121 "Non-nil means the incremental parser is verbose.
122 If nil, errors are still displayed, but informative messages are not."
123 :group 'semantic
124 :type 'boolean)
125
126 ;;; Change State management
127 ;;
128 ;; Manage a series of overlays that define changes recently
129 ;; made to the current buffer.
130 ;;;###autoload
131 (defun semantic-change-function (start end length)
132 "Provide a mechanism for semantic tag management.
133 Argument START, END, and LENGTH specify the bounds of the change."
134 (setq semantic-unmatched-syntax-cache-check t)
135 (let ((inhibit-point-motion-hooks t)
136 )
137 (run-hook-with-args 'semantic-change-hooks start end length)
138 ))
139
140 (defun semantic-changes-in-region (start end &optional buffer)
141 "Find change overlays which exist in whole or in part between START and END.
142 Optional argument BUFFER is the buffer to search for changes in."
143 (save-excursion
144 (if buffer (set-buffer buffer))
145 (let ((ol (semantic-overlays-in (max start (point-min))
146 (min end (point-max))))
147 (ret nil))
148 (while ol
149 (when (semantic-overlay-get (car ol) 'semantic-change)
150 (setq ret (cons (car ol) ret)))
151 (setq ol (cdr ol)))
152 (sort ret #'(lambda (a b) (< (semantic-overlay-start a)
153 (semantic-overlay-start b)))))))
154
155 (defun semantic-edits-change-function-handle-changes (start end length)
156 "Run whenever a buffer controlled by `semantic-mode' change.
157 Tracks when and how the buffer is re-parsed.
158 Argument START, END, and LENGTH specify the bounds of the change."
159 ;; We move start/end by one so that we can merge changes that occur
160 ;; just before, or just after. This lets simple typing capture everything
161 ;; into one overlay.
162 (let ((changes-in-change (semantic-changes-in-region (1- start) (1+ end)))
163 )
164 (semantic-parse-tree-set-needs-update)
165 (if (not changes-in-change)
166 (let ((o (semantic-make-overlay start end)))
167 (semantic-overlay-put o 'semantic-change t)
168 ;; Run the hooks safely. When hooks blow it, our dirty
169 ;; function will be removed from the list of active change
170 ;; functions.
171 (condition-case nil
172 (run-hook-with-args 'semantic-edits-new-change-hooks o)
173 (error nil)))
174 (let ((tmp changes-in-change))
175 ;; Find greatest bounds of all changes
176 (while tmp
177 (when (< (semantic-overlay-start (car tmp)) start)
178 (setq start (semantic-overlay-start (car tmp))))
179 (when (> (semantic-overlay-end (car tmp)) end)
180 (setq end (semantic-overlay-end (car tmp))))
181 (setq tmp (cdr tmp)))
182 ;; Move the first found overlay, recycling that overlay.
183 (semantic-overlay-move (car changes-in-change) start end)
184 (condition-case nil
185 (run-hook-with-args 'semantic-edits-move-change-hooks
186 (car changes-in-change))
187 (error nil))
188 (setq changes-in-change (cdr changes-in-change))
189 ;; Delete other changes. They are now all bound here.
190 (while changes-in-change
191 (condition-case nil
192 (run-hook-with-args 'semantic-edits-delete-change-hooks
193 (car changes-in-change))
194 (error nil))
195 (semantic-overlay-delete (car changes-in-change))
196 (setq changes-in-change (cdr changes-in-change))))
197 )))
198
199 (defsubst semantic-edits-flush-change (change)
200 "Flush the CHANGE overlay."
201 (condition-case nil
202 (run-hook-with-args 'semantic-edits-delete-change-hooks
203 change)
204 (error nil))
205 (semantic-overlay-delete change))
206
207 (defun semantic-edits-flush-changes ()
208 "Flush the changes in the current buffer."
209 (let ((changes (semantic-changes-in-region (point-min) (point-max))))
210 (while changes
211 (semantic-edits-flush-change (car changes))
212 (setq changes (cdr changes))))
213 )
214
215 (defun semantic-edits-change-in-one-tag-p (change hits)
216 "Return non-nil of the overlay CHANGE exists solely in one leaf tag.
217 HITS is the list of tags that CHANGE is in. It can have more than
218 one tag in it if the leaf tag is within a parent tag."
219 (and (< (semantic-tag-start (car hits))
220 (semantic-overlay-start change))
221 (> (semantic-tag-end (car hits))
222 (semantic-overlay-end change))
223 ;; Recurse on the rest. If this change is inside all
224 ;; of these tags, then they are all leaves or parents
225 ;; of the smallest tag.
226 (or (not (cdr hits))
227 (semantic-edits-change-in-one-tag-p change (cdr hits))))
228 )
229
230 ;;; Change/Tag Query functions
231 ;;
232 ;; A change (region of space) can effect tags in different ways.
233 ;; These functions perform queries on a buffer to determine different
234 ;; ways that a change effects a buffer.
235 ;;
236 ;; NOTE: After debugging these, replace below to no longer look
237 ;; at point and mark (via comments I assume.)
238 (defsubst semantic-edits-os (change)
239 "For testing: Start of CHANGE, or smaller of (point) and (mark)."
240 (if change (semantic-overlay-start change)
241 (if (< (point) (mark)) (point) (mark))))
242
243 (defsubst semantic-edits-oe (change)
244 "For testing: End of CHANGE, or larger of (point) and (mark)."
245 (if change (semantic-overlay-end change)
246 (if (> (point) (mark)) (point) (mark))))
247
248 (defun semantic-edits-change-leaf-tag (change)
249 "A leaf tag which completely encompasses CHANGE.
250 If change overlaps a tag, but is not encompassed in it, return nil.
251 Use `semantic-edits-change-overlap-leaf-tag'.
252 If CHANGE is completely encompassed in a tag, but overlaps sub-tags,
253 return nil."
254 (let* ((start (semantic-edits-os change))
255 (end (semantic-edits-oe change))
256 (tags (nreverse
257 (semantic-find-tag-by-overlay-in-region
258 start end))))
259 ;; A leaf is always first in this list
260 (if (and tags
261 (<= (semantic-tag-start (car tags)) start)
262 (> (semantic-tag-end (car tags)) end))
263 ;; Ok, we have a match. If this tag has children,
264 ;; we have to do more tests.
265 (let ((chil (semantic-tag-components (car tags))))
266 (if (not chil)
267 ;; Simple leaf.
268 (car tags)
269 ;; For this type, we say that we encompass it if the
270 ;; change occurs outside the range of the children.
271 (if (or (not (semantic-tag-with-position-p (car chil)))
272 (> start (semantic-tag-end (nth (1- (length chil)) chil)))
273 (< end (semantic-tag-start (car chil))))
274 ;; We have modifications to the definition of this parent
275 ;; so we have to reparse the whole thing.
276 (car tags)
277 ;; We actually modified an area between some children.
278 ;; This means we should return nil, as that case is
279 ;; calculated by someone else.
280 nil)))
281 nil)))
282
283 (defun semantic-edits-change-between-tags (change)
284 "Return a cache list of tags surrounding CHANGE.
285 The returned list is the CONS cell in the master list pointing to
286 a tag just before CHANGE. The CDR will have the tag just after CHANGE.
287 CHANGE cannot encompass or overlap a leaf tag.
288 If CHANGE is fully encompassed in a tag that has children, and
289 this change occurs between those children, this returns non-nil.
290 See `semantic-edits-change-leaf-tag' for details on parents."
291 (let* ((start (semantic-edits-os change))
292 (end (semantic-edits-oe change))
293 (tags (nreverse
294 (semantic-find-tag-by-overlay-in-region
295 start end)))
296 (list-to-search nil)
297 (found nil))
298 (if (not tags)
299 (setq list-to-search semantic--buffer-cache)
300 ;; A leaf is always first in this list
301 (if (and (< (semantic-tag-start (car tags)) start)
302 (> (semantic-tag-end (car tags)) end))
303 ;; We are completely encompassed in a tag.
304 (if (setq list-to-search
305 (semantic-tag-components (car tags)))
306 ;; Ok, we are completely encompassed within the first tag
307 ;; entry, AND that tag has children. This means that change
308 ;; occurred outside of all children, but inside some tag
309 ;; with children.
310 (if (or (not (semantic-tag-with-position-p (car list-to-search)))
311 (> start (semantic-tag-end
312 (nth (1- (length list-to-search))
313 list-to-search)))
314 (< end (semantic-tag-start (car list-to-search))))
315 ;; We have modifications to the definition of this parent
316 ;; and not between it's children. Clear the search list.
317 (setq list-to-search nil)))
318 ;; Search list is nil.
319 ))
320 ;; If we have a search list, lets go. Otherwise nothing.
321 (while (and list-to-search (not found))
322 (if (cdr list-to-search)
323 ;; We end when the start of the CDR is after the end of our
324 ;; asked change.
325 (if (< (semantic-tag-start (cadr list-to-search)) end)
326 (setq list-to-search (cdr list-to-search))
327 (setq found t))
328 (setq list-to-search nil)))
329 ;; Return it. If it is nil, there is a logic bug, and we need
330 ;; to avoid this bit of logic anyway.
331 list-to-search
332 ))
333
334 (defun semantic-edits-change-over-tags (change)
335 "Return a cache list of tags surrounding a CHANGE encompassing tags.
336 CHANGE must not only include all overlapped tags (excepting possible
337 parent tags) in their entirety. In this case, the change may be deleting
338 or moving whole tags.
339 The return value is a vector.
340 Cell 0 is a list of all tags completely encompassed in change.
341 Cell 1 is the cons cell into a master parser cache starting with
342 the cell which occurs BEFORE the first position of CHANGE.
343 Cell 2 is the parent of cell 1, or nil for the buffer cache.
344 This function returns nil if any tag covered by change is not
345 completely encompassed.
346 See `semantic-edits-change-leaf-tag' for details on parents."
347 (let* ((start (semantic-edits-os change))
348 (end (semantic-edits-oe change))
349 (tags (nreverse
350 (semantic-find-tag-by-overlay-in-region
351 start end)))
352 (parent nil)
353 (overlapped-tags nil)
354 inner-start inner-end
355 (list-to-search nil))
356 ;; By the time this is already called, we know that it is
357 ;; not a leaf change, nor a between tag change. That leaves
358 ;; an overlap, and this condition.
359
360 ;; A leaf is always first in this list.
361 ;; Is the leaf encompassed in this change?
362 (if (and tags
363 (>= (semantic-tag-start (car tags)) start)
364 (<= (semantic-tag-end (car tags)) end))
365 (progn
366 ;; We encompass one whole change.
367 (setq overlapped-tags (list (car tags))
368 inner-start (semantic-tag-start (car tags))
369 inner-end (semantic-tag-end (car tags))
370 tags (cdr tags))
371 ;; Keep looping while tags are inside the change.
372 (while (and tags
373 (>= (semantic-tag-start (car tags)) start)
374 (<= (semantic-tag-end (car tags)) end))
375
376 ;; Check if this new all-encompassing tag is a parent
377 ;; of that which went before. Only check end because
378 ;; we know that start is less than inner-start since
379 ;; tags was sorted on that.
380 (if (> (semantic-tag-end (car tags)) inner-end)
381 ;; This is a parent. Drop the children found
382 ;; so far.
383 (setq overlapped-tags (list (car tags))
384 inner-start (semantic-tag-start (car tags))
385 inner-end (semantic-tag-end (car tags))
386 )
387 ;; It is not a parent encompassing tag
388 (setq overlapped-tags (cons (car tags)
389 overlapped-tags)
390 inner-start (semantic-tag-start (car tags))))
391 (setq tags (cdr tags)))
392 (if (not tags)
393 ;; There are no tags left, and all tags originally
394 ;; found are encompassed by the change. Setup our list
395 ;; from the cache
396 (setq list-to-search semantic--buffer-cache);; We have a tag ouside the list. Check for
397 ;; We know we have a parent because it would
398 ;; completely cover the change. A tag can only
399 ;; do that if it is a parent after we get here.
400 (when (and tags
401 (< (semantic-tag-start (car tags)) start)
402 (> (semantic-tag-end (car tags)) end))
403 ;; We have a parent. Stuff in the search list.
404 (setq parent (car tags)
405 list-to-search (semantic-tag-components parent))
406 ;; If the first of TAGS is a parent (see above)
407 ;; then clear out the list. All other tags in
408 ;; here must therefore be parents of the car.
409 (setq tags nil)
410 ;; One last check, If start is before the first
411 ;; tag or after the last, we may have overlap into
412 ;; the characters that make up the definition of
413 ;; the tag we are parsing.
414 (when (or (semantic-tag-with-position-p (car list-to-search))
415 (< start (semantic-tag-start
416 (car list-to-search)))
417 (> end (semantic-tag-end
418 (nth (1- (length list-to-search))
419 list-to-search))))
420 ;; We have a problem
421 (setq list-to-search nil
422 parent nil))))
423
424 (when list-to-search
425
426 ;; Ok, return the vector only if all TAGS are
427 ;; confirmed as the lineage of `overlapped-tags'
428 ;; which must have a value by now.
429
430 ;; Loop over the search list to find the preceeding CDR.
431 ;; Fortunatly, (car overlapped-tags) happens to be
432 ;; the first tag positionally.
433 (let ((tokstart (semantic-tag-start (car overlapped-tags))))
434 (while (and list-to-search
435 ;; Assume always (car (cdr list-to-search)).
436 ;; A thrown error will be captured nicely, but
437 ;; that case shouldn't happen.
438
439 ;; We end when the start of the CDR is after the
440 ;; end of our asked change.
441 (cdr list-to-search)
442 (< (semantic-tag-start (car (cdr list-to-search)))
443 tokstart)
444 (setq list-to-search (cdr list-to-search)))))
445 ;; Create the return vector
446 (vector overlapped-tags
447 list-to-search
448 parent)
449 ))
450 nil)))
451
452 ;;; Default Incremental Parser
453 ;;
454 ;; Logic about how to group changes for effective reparsing and splicing.
455
456 (defun semantic-parse-changes-failed (&rest args)
457 "Signal that Semantic failed to parse changes.
458 That is, display a message by passing all ARGS to `format', then throw
459 a 'semantic-parse-changes-failed exception with value t."
460 (when semantic-edits-verbose-flag
461 (message "Semantic parse changes failed: %S"
462 (apply 'format args)))
463 (throw 'semantic-parse-changes-failed t))
464
465 (defsubst semantic-edits-incremental-fail ()
466 "When the incremental parser fails, we mark that we need a full reparse."
467 ;;(debug)
468 (semantic-parse-tree-set-needs-rebuild)
469 (when semantic-edits-verbose-flag
470 (message "Force full reparse (%s)"
471 (buffer-name (current-buffer))))
472 (run-hooks 'semantic-edits-incremental-reparse-failed-hook))
473
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 encompases.) 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 ;; encompased 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 chnge.
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 reprase 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 repase-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 splicable 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 preceeding 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 ;; arch-tag: 91c7fbf0-a418-4220-a90a-b58c74b450e3
970 ;;; semantic/edit.el ends here