Skip to content

Commit e2e6315

Browse files
committed
In progress
1 parent ff22007 commit e2e6315

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

eldoc-meta-net.el

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
;; Description: Eldoc support for meta-net
88
;; Keyword: eldoc c# dotnet sdk
99
;; Version: 0.1.0
10-
;; Package-Requires: ((emacs "24.3") (meta-net "1.1.0"))
10+
;; Package-Requires: ((emacs "25.1") (meta-net "1.1.0"))
1111
;; URL: https://github.com/emacs-vs/eldoc-meta-net
1212

1313
;; This file is NOT part of GNU Emacs.
@@ -32,6 +32,8 @@
3232

3333
;;; Code:
3434

35+
(require 'subr-x)
36+
3537
(require 'eldoc)
3638
(require 'meta-net)
3739

@@ -41,5 +43,121 @@
4143
:group 'tool
4244
:link '(url-link :tag "Repository" "https://github.com/emacs-vs/eldoc-meta-net"))
4345

46+
;;
47+
;; (@* "Util" )
48+
;;
49+
50+
(defun eldoc-meta-net--inside-comment-p ()
51+
"Return non-nil if it's inside comment."
52+
(nth 4 (syntax-ppss)))
53+
54+
;;
55+
;; (@* "Core" )
56+
;;
57+
58+
(defun eldoc-meta-net--possible-function-point ()
59+
"This function get called infront of the opening curly bracket.
60+
61+
For example,
62+
63+
SomeFunction<TypeA, TypeB>(a, b, c);
64+
^
65+
66+
This function also ignore generic type between < and >."
67+
(let ((start (point))
68+
(normal (save-excursion (forward-symbol -1) (point)))
69+
(generic (save-excursion (search-backward ">" nil t)
70+
(forward-char 1)
71+
(forward-sexp -1)
72+
(forward-symbol -1)
73+
(point)))
74+
result search-pt)
75+
(when (<= generic (line-beginning-position))
76+
(setq generic nil))
77+
;; Make sure the result is number to avoid error
78+
(setq normal (or normal (point))
79+
generic (or generic (point))
80+
result (min normal generic))
81+
(goto-char result) ; here suppose to be the start of the function name
82+
;; We check to see if there is comma right behind the symbol
83+
(save-excursion
84+
(forward-symbol 1)
85+
(setq search-pt (point))
86+
(when (and (re-search-forward "[^,]*" nil t)
87+
(string-empty-p (string-trim (buffer-substring search-pt (point)))))
88+
(setq result start)))
89+
(goto-char result)))
90+
91+
(defun eldoc-meta-net--function-at-point ()
92+
"Return the function name at point."
93+
(let* ((right 0) (left 0))
94+
(while (and (<= left right) (re-search-backward "\\((\\|)\\)" nil t))
95+
(if (equal (buffer-substring-no-properties (point) (+ (point) 1)) "(")
96+
(setq left (+ left 1))
97+
(setq right (+ right 1))))
98+
(while (equal (buffer-substring-no-properties (- (point) 1) (point)) " ")
99+
(goto-char (- (point) 1))))
100+
(eldoc-meta-net--possible-function-point)
101+
(unless (eldoc-meta-net--inside-comment-p) (thing-at-point 'symbol)))
102+
103+
(defun eldoc-meta-net--arg-string ()
104+
"Return argument string."
105+
(when (search-forward "(" nil t)
106+
(forward-char -1)
107+
(let ((beg (point)) arg-string)
108+
(forward-sexp 1)
109+
(setq arg-string (buffer-substring-no-properties beg (point)))
110+
;; Here we remove everything inside nested arguments
111+
;;
112+
;; For example,
113+
;;
114+
;; Add(a, b, (x, y, z) => { }, c)
115+
;;
116+
;; should return,
117+
;;
118+
;; (a, b, => { }, c)
119+
;;
120+
;; Of course, if you are inside the x y z scope then it would just
121+
;; return (x, y, z). This is fine since ElDoc should just only need
122+
;; to know the first layer's function.
123+
(with-temp-buffer
124+
(insert arg-string)
125+
(goto-char (1+ (point-min)))
126+
(while (search-forward "(" nil t)
127+
(forward-char -1)
128+
(let ((start (point)))
129+
(forward-sexp 1)
130+
(delete-region start (point))))
131+
(buffer-string)))))
132+
133+
(defun eldoc-meta-net-function ()
134+
"Main eldoc entry."
135+
(save-excursion
136+
(when-let ((function-name (eldoc-meta-net--function-at-point))
137+
(arg-string (eldoc-meta-net--arg-string)))
138+
(jcs-print function-name)
139+
(jcs-print arg-string)
140+
)))
141+
142+
(defun eldoc-meta-net--turn-on ()
143+
"Start the `eldoc-meta-net' worker."
144+
(unless meta-net-csproj-current (meta-net-read-project))
145+
(add-function :before-until (local 'eldoc-documentation-function) #'eldoc-meta-net-function)
146+
(eldoc-mode 1))
147+
148+
;;;###autoload
149+
(defun eldoc-meta-net-enable ()
150+
"Turn on `eldoc-meta-net'."
151+
(interactive)
152+
(add-hook 'csharp-mode-hook #'eldoc-meta-net--turn-on)
153+
(add-hook 'csharp-tree-sitter-mode #'eldoc-meta-net--turn-on))
154+
155+
;;;###autoload
156+
(defun eldoc-meta-net-disable ()
157+
"Turn off `eldoc-meta-net'."
158+
(interactive)
159+
(remove-hook 'csharp-mode-hook #'eldoc-meta-net--turn-on)
160+
(remove-hook 'csharp-tree-sitter-mode #'eldoc-meta-net--turn-on))
161+
44162
(provide 'eldoc-meta-net)
45163
;;; eldoc-meta-net.el ends here

0 commit comments

Comments
 (0)