Skip to content

Commit ce16e54

Browse files
authored
Merge pull request #42 from emacs-lsp/use-hooks-on-features
Use hooks on features
2 parents ecad300 + 7450f9c commit ce16e54

8 files changed

+154
-86
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ jobs:
1414
strategy:
1515
matrix:
1616
emacs-version:
17-
- 25.2
18-
- 25.3
1917
- 26.1
2018
- 26.2
2119
- 26.3

lsp-dart-closing-labels.el

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ be sent with information to render editor closing labels."
4040
:type 'float
4141
:group 'lsp-dart)
4242

43-
(lsp-defun lsp-dart-closing-labels-handle (_workspace (&ClosingLabelsNotification :uri :labels))
43+
(lsp-defun lsp-dart--closing-labels-check ((&ClosingLabelsNotification :uri :labels))
4444
"Closing labels notification handler."
4545
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
4646
(with-current-buffer buffer
@@ -58,5 +58,16 @@ be sent with information to render editor closing labels."
5858
'cursor t
5959
'font-lock-face 'font-lock-comment-face))))))))
6060

61+
(define-minor-mode lsp-dart-closing-labels-mode
62+
"Mode for displaying flutter closing labels on the end of methods/contructors."
63+
nil nil nil
64+
(cond
65+
(lsp-dart-closing-labels-mode
66+
(add-hook 'lsp-dart-closing-labels-arrived-hook #'lsp-dart--closing-labels-check nil t))
67+
(t
68+
(progn
69+
(remove-overlays (point-min) (point-max) 'lsp-dart-closing-labels t)
70+
(remove-hook 'lsp-dart-closing-labels-arrived-hook #'lsp-dart--closing-labels-check t)))))
71+
6172
(provide 'lsp-dart-closing-labels)
6273
;;; lsp-dart-closing-labels.el ends here

lsp-dart-code-lens.el

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,18 @@ NAMES arg is optional and are the group of tests representing a test name."
175175
(unless (seq-empty-p children)
176176
(lsp-dart-code-lens--add-test buffer children concatened-names)))))
177177

178-
179-
;; Public
180-
181-
(lsp-defun lsp-dart-code-lens-check-main (uri (&Outline :children))
178+
(lsp-defun lsp-dart--main-code-lens-check ((&OutlineNotification :uri :outline (&Outline :children)))
182179
"Check URI and outline for main method adding lens to it."
183180
(-let* ((buffer (find-buffer-visiting (lsp--uri-to-path uri)))
184181
(main-outline (lsp-dart-code-lens--find-main-outline children)))
185182
(when buffer
186183
(with-current-buffer buffer
187184
(remove-overlays (point-min) (point-max) 'lsp-dart-main-code-lens t)
188-
(save-excursion
189-
(when main-outline
185+
(when main-outline
186+
(save-excursion
190187
(lsp-dart-code-lens--build-main-overlay buffer main-outline)))))))
191188

192-
(lsp-defun lsp-dart-code-lens-check-test (uri (&Outline :children))
189+
(lsp-defun lsp-dart--test-code-lens-check ((&OutlineNotification :uri :outline (&Outline :children)))
193190
"Check URI and outline for test adding lens to it."
194191
(when (lsp-dart-test-file-p uri)
195192
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
@@ -198,5 +195,30 @@ NAMES arg is optional and are the group of tests representing a test name."
198195
(save-excursion
199196
(lsp-dart-code-lens--add-test buffer children))))))
200197

198+
199+
;; Public
200+
201+
(define-minor-mode lsp-dart-main-code-lens-mode
202+
"Mode for displaying code lens on main methods."
203+
nil nil nil
204+
(cond
205+
(lsp-dart-main-code-lens-mode
206+
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--main-code-lens-check nil t))
207+
(t
208+
(progn
209+
(remove-overlays (point-min) (point-max) 'lsp-dart-main-code-lens t)
210+
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--main-code-lens-check t)))))
211+
212+
(define-minor-mode lsp-dart-test-code-lens-mode
213+
"Mode for displaying code lens on main methods."
214+
nil nil nil
215+
(cond
216+
(lsp-dart-test-code-lens-mode
217+
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--test-code-lens-check nil t))
218+
(t
219+
(progn
220+
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
221+
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--test-code-lens-check t)))))
222+
201223
(provide 'lsp-dart-code-lens)
202224
;;; lsp-dart-code-lens.el ends here

lsp-dart-flutter-daemon.el

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@
3737

3838
(defun lsp-dart-flutter-daemon--log (level msg &rest args)
3939
"Log for LEVEL, MSG with ARGS adding lsp-dart-flutter-daemon prefix."
40-
(apply #'lsp-dart-custom-log (concat "[FLUTTER " (upcase level) "] ")
41-
msg
42-
args))
40+
(unless (string= "STATUS" (upcase level))
41+
(apply #'lsp-dart-custom-log (concat "[FLUTTER " (upcase level) "] ")
42+
msg
43+
args)))
4344

4445
(defun lsp-dart-flutter-daemon--generate-command-id ()
4546
"Generate a random command id."

lsp-dart-flutter-fringe.el renamed to lsp-dart-flutter-fringe-colors.el

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
;;; lsp-dart-flutter-fringe.el --- Dart fringe tools -*- lexical-binding: t; -*-
1+
;;; lsp-dart-flutter-fringe-colors.el --- Flutter fringe colors -*- lexical-binding: t; -*-
22
;;
33
;; This program is free software; you can redistribute it and/or modify
44
;; it under the terms of the GNU General Public License as published by
@@ -15,7 +15,7 @@
1515

1616
;;; Commentary:
1717

18-
;; LSP dart support for fringe
18+
;; Flutter support for fringe colors
1919

2020
;;; Code:
2121

@@ -102,11 +102,5 @@
102102
(remove-overlays (point-min) (point-max) 'lsp-dart-flutter-fringe-colors t)
103103
(remove-hook 'lsp-on-change-hook #'lsp-dart-flutter-fringe--update-colors t)))))
104104

105-
(when lsp-dart-flutter-fringe-colors
106-
(add-hook 'lsp-after-open-hook
107-
(lambda ()
108-
(when (lsp-find-workspace 'dart_analysis_server nil)
109-
(lsp-dart-flutter-fringe-colors-mode)))))
110-
111-
(provide 'lsp-dart-flutter-fringe)
112-
;;; lsp-dart-flutter-fringe.el ends here
105+
(provide 'lsp-dart-flutter-fringe-colors)
106+
;;; lsp-dart-flutter-fringe-colors.el ends here

lsp-dart-flutter-widget-guide.el

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,18 @@
5050

5151
(defun lsp-dart-flutter-widget-guide--first-non-whitespace-pos (line)
5252
"Return the first non whitepaces position at LINE."
53-
(save-excursion
54-
(goto-char (point-min))
55-
(forward-line line)
56-
(back-to-indentation)
57-
(lsp-make-position :line line
58-
:character (current-column))))
53+
(goto-char (point-min))
54+
(forward-line line)
55+
(back-to-indentation)
56+
(lsp-make-position :line line
57+
:character (current-column)))
5958

6059
(defun lsp-dart-flutter-widget-guide--last-col-at (line)
6160
"Return the last col at LINE."
62-
(save-excursion
63-
(goto-char (point-min))
64-
(forward-line line)
65-
(end-of-line)
66-
(current-column)))
61+
(goto-char (point-min))
62+
(forward-line line)
63+
(end-of-line)
64+
(current-column))
6765

6866
(lsp-defun lsp-dart-flutter-widget-guide--outline->guide ((&FlutterOutline :kind :children :range
6967
(&Range :start
@@ -112,7 +110,8 @@ Return nil if the widget guilde does not apply."
112110
SIZE is the length of the characters list.
113111
LAST-LINE-CHAR is the last column position of LINE.
114112
ANCHOR is the anchor point of the widget guide at LINE."
115-
(let ((chars (make-list size lsp-dart-flutter-widget-guide-space)))
113+
(let ((chars (make-list size lsp-dart-flutter-widget-guide-space))
114+
(max-lisp-eval-depth 1800))
116115
(seq-doseq (guide guide-lines)
117116
(-let* (((&Range :start (&Position :character start-char)
118117
:end (&Position :line end-line :character end-char)) guide)
@@ -141,37 +140,34 @@ ANCHOR is the anchor point of the widget guide at LINE."
141140

142141
(lsp-defun lsp-dart-flutter-widget-guide-check ((&FlutterOutlineNotification :uri :outline))
143142
"Check if there is any widget guide on buffer from uri of OUTLINE-PARAMS."
144-
(-when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
143+
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
145144
(with-current-buffer buffer
146145
(remove-overlays (point-min) (point-max) 'category 'lsp-dart-flutter-widget-guide)
147-
(let* ((guides (lsp-dart-flutter-widget-guide--outline->guides outline))
148-
(guides-by-line (lsp-dart-flutter-widget-guide--guides->guides-by-line guides)))
149-
(lsp-dart-plist-each
150-
(lambda (line guide-lines)
151-
(let* ((first-guide-char (-min (--map (min (-> it lsp:range-start lsp:position-character)
152-
(-> it lsp:range-end lsp:position-character)) guide-lines)))
153-
(last-guide-char (-max (--map (max (-> it lsp:range-start lsp:position-character)
154-
(-> it lsp:range-end lsp:position-character)) guide-lines)))
155-
(last-line-char (lsp-dart-flutter-widget-guide--last-col-at line))
156-
(anchor (max 0 (if (< last-line-char first-guide-char) 0 first-guide-char)))
157-
(chars (lsp-dart-flutter-widget-guide--build-chars line guide-lines last-guide-char last-line-char anchor)))
158-
(--each-indexed chars (lsp-dart-flutter-widget-guide--add-overlay-to buffer line (+ it-index anchor) it))))
159-
guides-by-line)))))
146+
(save-excursion
147+
(->> outline
148+
(lsp-dart-flutter-widget-guide--outline->guides)
149+
(lsp-dart-flutter-widget-guide--guides->guides-by-line)
150+
(lsp-dart-plist-each
151+
(lambda (line guide-lines)
152+
(let* ((first-guide-char (-min (--map (min (-> it lsp:range-start lsp:position-character)
153+
(-> it lsp:range-end lsp:position-character)) guide-lines)))
154+
(last-guide-char (-max (--map (max (-> it lsp:range-start lsp:position-character)
155+
(-> it lsp:range-end lsp:position-character)) guide-lines)))
156+
(last-line-char (lsp-dart-flutter-widget-guide--last-col-at line))
157+
(anchor (max 0 (if (< last-line-char first-guide-char) 0 first-guide-char)))
158+
(chars (lsp-dart-flutter-widget-guide--build-chars line guide-lines last-guide-char last-line-char anchor)))
159+
(--each-indexed chars (lsp-dart-flutter-widget-guide--add-overlay-to buffer line (+ it-index anchor) it))))))))))
160160

161161
(define-minor-mode lsp-dart-flutter-widget-guides-mode
162162
"Mode for displaying flutter widget guide lines."
163163
nil nil nil
164164
(cond
165165
(lsp-dart-flutter-widget-guides-mode
166-
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart-flutter-widget-guide-check nil t))
166+
(add-hook 'lsp-dart-flutter-outline-arrived-hook #'lsp-dart-flutter-widget-guide-check nil t))
167167
(t
168168
(progn
169169
(remove-overlays (point-min) (point-max) 'category 'lsp-dart-flutter-widget-guide)
170-
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart-flutter-widget-guide-check t)))))
171-
172-
(add-hook 'lsp-before-open-hook (lambda ()
173-
(when lsp-dart-flutter-widget-guides
174-
(lsp-dart-flutter-widget-guides-mode 1))))
170+
(remove-hook 'lsp-dart-flutter-outline-arrived-hook #'lsp-dart-flutter-widget-guide-check t)))))
175171

176172
(provide 'lsp-dart-flutter-widget-guide)
177173
;;; lsp-dart-flutter-widget-guide.el ends here

lsp-dart-outline.el

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
(require 'lsp-dart-protocol)
2525
(require 'lsp-dart-utils)
26-
(require 'lsp-dart-code-lens)
2726

2827
(defcustom lsp-dart-outline t
2928
"Enable the analysis server outline custom method.
@@ -60,6 +59,12 @@ Defaults to side following treemacs default."
6059

6160
;;; Internal
6261

62+
(defvar-local lsp-dart-current-outline nil)
63+
(defvar-local lsp-dart-current-flutter-outline nil)
64+
65+
(defconst lsp-dart--outline-buffer-name "*Dart Outline*")
66+
(defconst lsp-dart--flutter-outline-buffer-name "*Flutter Outline*")
67+
6368
(defun lsp-dart-outline--set-metadata (workspace params key-prefix)
6469
"Save in WORKSPACE the PARAMS metadata with KEY-PREFIX.
6570
The key is composed of the KEY-PREFIX with PARAMS uri path."
@@ -176,7 +181,7 @@ OUTLINES are the outline items."
176181
(lsp-dart-outline--outlines->tree uri outlines)
177182
"Outline"
178183
t
179-
"*Dart Outline*")))
184+
lsp-dart--outline-buffer-name)))
180185

181186
(defun lsp-dart-outline--render-flutter-outline-tree (uri outlines)
182187
"Render an Flutter outline view with the source URI and OUTLINES data."
@@ -185,65 +190,84 @@ OUTLINES are the outline items."
185190
(lsp-dart-outline--flutter-outline->tree uri outlines)
186191
"Flutter Outline"
187192
t
188-
"*Flutter Outline*")))
193+
lsp-dart--flutter-outline-buffer-name)))
189194

190-
(defun lsp-dart-outline--show-outline (buffer ignore-focus?)
195+
(defun lsp-dart-outline--show-outline (ignore-focus?)
191196
"Show an outline tree for BUFFER.
192197
Focus on it if IGNORE-FOCUS? is nil."
193-
(-when-let ((&OutlineNotification? :uri :outline (&Outline :children)) (lsp-dart-outline--get-metadata buffer "current-outline"))
198+
(-if-let ((&OutlineNotification? :uri :outline (&Outline :children)) lsp-dart-current-outline)
194199
(-let* ((tree-buffer (lsp-dart-outline--render-outline-tree uri children))
195200
(window (display-buffer-in-side-window tree-buffer lsp-dart-outline-position-params)))
196201
(unless ignore-focus?
197202
(select-window window)
198-
(set-window-dedicated-p window t)))))
203+
(set-window-dedicated-p window t)))
204+
(lsp-dart-log "No Dart outline data found")))
199205

200-
(defun lsp-dart-outline--show-flutter-outline (buffer ignore-focus?)
206+
(defun lsp-dart-outline--show-flutter-outline (ignore-focus?)
201207
"Show a Flutter outline tree for BUFFER.
202208
Focus on it if IGNORE-FOCUS? is nil."
203-
(-when-let ((&FlutterOutlineNotification? :uri :outline (&FlutterOutline :children)) (lsp-dart-outline--get-metadata buffer "current-flutter-outline"))
209+
(-if-let ((&FlutterOutlineNotification? :uri :outline (&FlutterOutline :children)) lsp-dart-current-flutter-outline)
204210
(-let* ((tree-buffer (lsp-dart-outline--render-flutter-outline-tree uri children))
205211
(window (display-buffer-in-side-window tree-buffer lsp-dart-flutter-outline-position-params)))
206212
(unless ignore-focus?
207213
(select-window window)
208-
(set-window-dedicated-p window t)))))
214+
(set-window-dedicated-p window t)))
215+
(lsp-dart-log "No Flutter outline data found")))
209216

210-
(lsp-defun lsp-dart-outline-handle-outline (workspace (notification &as &OutlineNotification :uri :outline))
217+
(lsp-defun lsp-dart--outline-check ((notification &as &OutlineNotification :uri))
211218
"Outline notification handling from WORKSPACE.
212219
NOTIFICATION is outline notification data received from server.
213220
It updates the outline view if it already exists."
214-
(lsp-dart-outline--set-metadata workspace notification "current-outline")
215-
(when lsp-dart-main-code-lens
216-
(lsp-dart-code-lens-check-main uri outline))
217-
(when lsp-dart-test-code-lens
218-
(lsp-dart-code-lens-check-test uri outline))
219-
(when (get-buffer-window "*Dart Outline*")
220-
(lsp-dart-outline--show-outline (find-buffer-visiting (lsp--uri-to-path uri)) t)))
221-
222-
(lsp-defun lsp-dart-outline-handle-flutter-outline (workspace (notification &as &FlutterOutlineNotification :uri))
221+
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
222+
(with-current-buffer buffer
223+
(setq lsp-dart-current-outline notification)
224+
(when (get-buffer-window lsp-dart--outline-buffer-name)
225+
(lsp-dart-outline--show-outline t)))))
226+
227+
(lsp-defun lsp-dart--flutter-outline-check ((notification &as &FlutterOutlineNotification :uri))
223228
"Flutter outline notification handling from WORKSPACE.
224229
NOTIFICATION is Flutter outline notification data received from server.
225230
It updates the Flutter outline view if it already exists."
226-
(lsp-dart-outline--set-metadata workspace notification "current-flutter-outline")
227-
(run-hook-with-args 'lsp-dart-outline-arrived-hook notification)
228-
(when (get-buffer-window "*Flutter Outline*")
229-
(lsp-dart-outline--show-flutter-outline (find-buffer-visiting (lsp--uri-to-path uri)) t)))
231+
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
232+
(with-current-buffer buffer
233+
(setq lsp-dart-current-flutter-outline notification)
234+
(when (get-buffer-window lsp-dart--flutter-outline-buffer-name)
235+
(lsp-dart-outline--show-flutter-outline t)))))
230236

231237

232238
;;; Public interface
233239

240+
(define-minor-mode lsp-dart-outline-mode
241+
"Mode for updating outline."
242+
nil nil nil
243+
(cond
244+
(lsp-dart-outline-mode
245+
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--outline-check nil t))
246+
(t
247+
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--outline-check t))))
248+
249+
(define-minor-mode lsp-dart-flutter-outline-mode
250+
"Mode for updating flutter outline."
251+
nil nil nil
252+
(cond
253+
(lsp-dart-flutter-outline-mode
254+
(add-hook 'lsp-dart-flutter-outline-arrived-hook #'lsp-dart--flutter-outline-check nil t))
255+
(t
256+
(remove-hook 'lsp-dart-flutter-outline-arrived-hook #'lsp-dart--flutter-outline-check t))))
257+
234258
;;;###autoload
235259
(defun lsp-dart-show-outline (ignore-focus?)
236260
"Show an outline tree and focus on it if IGNORE-FOCUS? is nil."
237261
(interactive "P")
238262
(lsp-dart-assert-sdk-min-version "2.8.0")
239-
(lsp-dart-outline--show-outline (current-buffer) ignore-focus?))
263+
(lsp-dart-outline--show-outline ignore-focus?))
240264

241265
;;;###autoload
242266
(defun lsp-dart-show-flutter-outline (ignore-focus?)
243267
"Show a Flutter outline tree and focus on it if IGNORE-FOCUS? is nil."
244268
(interactive "P")
245269
(lsp-dart-assert-sdk-min-version "2.8.0")
246-
(lsp-dart-outline--show-flutter-outline (current-buffer) ignore-focus?))
270+
(lsp-dart-outline--show-flutter-outline ignore-focus?))
247271

248272
(provide 'lsp-dart-outline)
249273
;;; lsp-dart-outline.el ends here

0 commit comments

Comments
 (0)