-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplantuml-xref.el
67 lines (62 loc) · 2.26 KB
/
plantuml-xref.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
;;; plantuml-xref.el -- plantuml xref backend -*- lexical-binding: t -*-
(require 'xref)
(require 'cl-lib)
(require 'plantuml-keywords)
(defun plantuml-xref-backend ()
"xref backend for plantuml files"
'plantuml)
(cl-defmethod xref-backend-identifier-at-point ((_ (eql plantuml)))
"Return the identifier to lookup"
;; (message "plantuml-xref (%s): Identifier %s (point=%d)" (buffer-name) (symbol-at-point) ( point ))
(symbol-name (symbol-at-point))
)
;; xref-backend
(cl-defmethod xref-backend-identifier-completion-table ((_ (eql plantuml)))
"Return list of terms for completion from the current buffer"
(plantuml--find-definitions nil))
(cl-defmethod xref-backend-definitions ((_ (eql plantuml)) symbol)
;; (message "plantuml-xref (%s) : %s" (buffer-name) symbol)
(plantuml--find-definitions symbol t))
(cl-defmethod xref-backend-references ((_ (eql plantuml)) symbol)
"List of references matching symbol"
(plantuml--find-references symbol))
;; xref helper funtion
(defun plantuml--find-references (symbol)
"Find references of symbol in the buffer"
(let ((case-fold-search t)
(regexp (rx-to-string `(seq bow ,symbol eow)))
(matches))
(save-excursion
(save-restriction
(widen)
(while (re-search-forward regexp nil t)
(push
(xref-make
(buffer-substring (line-beginning-position) (line-end-position))
(xref-make-buffer-location (current-buffer) (match-beginning 0)))
matches))))
matches))
(defun plantuml--find-definitions (symbol &optional ref)
"Find definitions in buffer if 'REF' is + retun matches"
(let ((case-fold-search t)
(regexp (if symbol
(rx-to-string `(seq bow (or ,@plantuml--component-types) eow (+ space) bow (group ,symbol) eow))
(rx-to-string `(seq bow (or ,@plantuml--component-types) eow (+ space) bow (group (+ (any word "_"))) eow)))))
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
(let (matches)
(while (re-search-forward regexp nil t)
(push (if ref
(xref-make
(buffer-substring-no-properties (line-beginning-position) (line-end-position))
(xref-make-buffer-location (current-buffer) (match-beginning 1)))
(match-string-no-properties 1))
matches))
matches)
))
)
)
(provide 'plantuml-xref)
;;; plantuml-xref.el -- Ends here