Skip to content

Commit 35e3ca2

Browse files
committed
Migrate outline feature to use lsp-protocol
1 parent 124aa57 commit 35e3ca2

File tree

2 files changed

+71
-82
lines changed

2 files changed

+71
-82
lines changed

lsp-dart-outline.el

Lines changed: 59 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ Defaults to side following treemacs default."
6464
(defun lsp-dart-outline--set-metadata (workspace params key-prefix)
6565
"Save in WORKSPACE the PARAMS metadata with KEY-PREFIX.
6666
The key is composed of the KEY-PREFIX with PARAMS uri path."
67-
(-let* (((&hash "uri") params)
68-
(uri-path (lsp--uri-to-path uri))
67+
(-let* ((uri-path (-> params lsp:outline-notification-uri lsp--uri-to-path))
6968
(key (concat key-prefix "--" uri-path)))
7069
(lsp-workspace-set-metadata key params workspace)))
7170

@@ -109,92 +108,90 @@ URI is the source of the item.
109108
RANGE is the range of positions to where this item should point."
110109
(interactive)
111110
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
112-
(goto-char (lsp--position-to-point (gethash "start" range)))
111+
(goto-char (lsp--position-to-point (lsp:range-start range)))
113112
(run-hooks 'xref-after-jump-hook))
114113

115-
(defun lsp-dart-outline--outline->tree (uri items)
114+
(defun lsp-dart-outline--outlines->tree (uri outlines)
116115
"Builds an outline tree.
117116
URI is the source of the outline.
118-
ITEMS is the outline items data."
119-
(seq-map (-lambda ((&hash "children"
120-
"element" (&hash "kind" "name" "parameters" "range")))
121-
(let ((label (concat name (when parameters
122-
(propertize (concat " " parameters)
117+
OUTLINES are the outline items."
118+
(seq-map (-lambda ((&Outline :children
119+
:element (&Element :kind :name :range :parameters?)))
120+
(let ((label (concat name (when parameters?
121+
(propertize (concat " " parameters?)
123122
'face 'lsp-lens-face)))))
124123
(list :key label
125124
:label label
126125
:icon (lsp-dart-outline--outline-kind->icon kind)
127126
:children (lambda (&rest _)
128127
(unless (seq-empty-p children)
129-
(lsp-dart-outline--outline->tree uri children)))
128+
(lsp-dart-outline--outlines->tree uri children)))
130129
:ret-action (lambda (&rest _) (lsp-dart-outline--outline-tree-ret-action uri range)))))
131-
items))
130+
outlines))
132131

133132
(defun lsp-dart-outline--build-flutter-outline-widget-actions (uri range)
134133
"Build the action options for the Flutter outline tree view.
135134
URI is the source of the outline.
136135
RANGE is the range for currently build item."
137136
(-when-let (buffer (lsp--buffer-for-file (lsp--uri-to-path uri)))
138137
(with-current-buffer buffer
139-
(goto-char (-> range lsp--range-to-region car))
140-
(->> (lsp-code-actions-at-point)
141-
(-filter (-lambda ((&hash "kind"))
142-
(and kind (equal "refactor" kind))))
143-
(-map (-lambda ((action &as &hash "title"))
144-
`[,title (lsp-execute-code-action ,action)]))))))
145-
146-
(defun lsp-dart-outline--flutter-outline->tree (uri items)
138+
(goto-char (-> range lsp--range-to-region car))
139+
(->> (lsp-code-actions-at-point)
140+
(-filter (-lambda ((&CodeAction :kind?))
141+
(and kind? (equal "refactor" kind?))))
142+
(-map (-lambda ((action &as &CodeAction :title))
143+
`[,title (lsp-execute-code-action ,action)]))))))
144+
145+
(defun lsp-dart-outline--flutter-outline->tree (uri outlines)
147146
"Builds a Flutter outline tree.
148147
URI is the source of the outline.
149-
ITEMS is the outline items data."
150-
(seq-map (lambda (item)
151-
(-let* (((&hash "children" "kind"
152-
"dartElement" element "className" class-name "label" extra-label "codeRange" range) item)
153-
(widget? (not (string= kind "DART_ELEMENT")))
154-
(label (if widget?
155-
(concat class-name " " extra-label)
156-
(concat (gethash "name" element)
157-
(when (gethash "parameters" element)
158-
(propertize (concat " " (gethash "parameters" element))
159-
'face 'lsp-lens-face)))))
160-
(icon (if widget?
161-
'flutter
162-
(lsp-dart-outline--outline-kind->icon (gethash "kind" element)))))
163-
(list :key label
164-
:label label
165-
:icon icon
166-
:children (lambda (&rest _)
167-
(unless (seq-empty-p children)
168-
(lsp-dart-outline--flutter-outline->tree uri children)))
169-
:ret-action (lambda (&rest _) (lsp-dart-outline--outline-tree-ret-action uri range))
170-
:actions (when widget?
171-
(lsp-dart-outline--build-flutter-outline-widget-actions uri range))
172-
:uri uri)))
173-
items))
174-
175-
(defun lsp-dart-outline--render-outline-tree (uri outline)
176-
"Render an outline view with the source URI and an OUTLINE data."
148+
OUTLINES are the outline items."
149+
(seq-map
150+
(-lambda ((&FlutterOutline :children :kind :code-range :class-name? :label? :dart-element?))
151+
(-let* ((widget? (not (string= kind "DART_ELEMENT")))
152+
(label (if widget?
153+
(concat class-name? " " label?)
154+
(concat (lsp:element-name dart-element?)
155+
(when (lsp:element-parameters? dart-element?)
156+
(propertize (concat " " (lsp:element-parameters? dart-element?))
157+
'face 'lsp-lens-face)))))
158+
(icon (if widget?
159+
'flutter
160+
(lsp-dart-outline--outline-kind->icon (lsp:element-kind dart-element?)))))
161+
(list :key label
162+
:label label
163+
:icon icon
164+
:children (lambda (&rest _)
165+
(unless (seq-empty-p children)
166+
(lsp-dart-outline--flutter-outline->tree uri children)))
167+
:ret-action (lambda (&rest _) (lsp-dart-outline--outline-tree-ret-action uri code-range))
168+
:actions (when widget?
169+
(lsp-dart-outline--build-flutter-outline-widget-actions uri code-range))
170+
:uri uri)))
171+
outlines))
172+
173+
(defun lsp-dart-outline--render-outline-tree (uri outlines)
174+
"Render an outline view with the source URI and OUTLINES data."
177175
(save-excursion
178176
(lsp-treemacs-render
179-
(lsp-dart-outline--outline->tree uri outline)
177+
(lsp-dart-outline--outlines->tree uri outlines)
180178
"Outline"
181179
t
182180
"*Dart Outline*")))
183181

184-
(defun lsp-dart-outline--render-flutter-outline-tree (uri outline)
185-
"Render an Flutter outline view with the source URI and an OUTLINE data."
182+
(defun lsp-dart-outline--render-flutter-outline-tree (uri outlines)
183+
"Render an Flutter outline view with the source URI and OUTLINES data."
186184
(save-excursion
187185
(lsp-treemacs-render
188-
(lsp-dart-outline--flutter-outline->tree uri outline)
186+
(lsp-dart-outline--flutter-outline->tree uri outlines)
189187
"Flutter Outline"
190188
t
191189
"*Flutter Outline*")))
192190

193191
(defun lsp-dart-outline--show-outline (buffer ignore-focus?)
194192
"Show an outline tree for BUFFER.
195193
Focus on it if IGNORE-FOCUS? is nil."
196-
(-let* ((current-outline (lsp-dart-outline--get-metadata buffer "current-outline"))
197-
((&hash "uri" "outline" (&hash "children")) current-outline)
194+
(-let* (((&OutlineNotification :uri :outline (&Outline :children)) (lsp-dart-outline--get-metadata buffer "current-outline"))
198195
(tree-buffer (lsp-dart-outline--render-outline-tree uri children))
199196
(window (display-buffer-in-side-window tree-buffer lsp-dart-outline-position-params)))
200197
(unless ignore-focus?
@@ -204,17 +201,16 @@ Focus on it if IGNORE-FOCUS? is nil."
204201
(defun lsp-dart-outline--show-flutter-outline (buffer ignore-focus?)
205202
"Show a Flutter outline tree for BUFFER.
206203
Focus on it if IGNORE-FOCUS? is nil."
207-
(-let* ((current-outline (lsp-dart-outline--get-metadata buffer "current-flutter-outline"))
208-
((&hash "uri" "outline" (&hash "children")) current-outline)
209-
(buffer (lsp-dart-outline--render-flutter-outline-tree uri children))
210-
(window (display-buffer-in-side-window buffer lsp-dart-flutter-outline-position-params)))
204+
(-let* (((&FlutterOutlineNotification :uri :outline (&FlutterOutline :children)) (lsp-dart-outline--get-metadata buffer "current-flutter-outline"))
205+
(tree-buffer (lsp-dart-outline--render-flutter-outline-tree uri children))
206+
(window (display-buffer-in-side-window tree-buffer lsp-dart-flutter-outline-position-params)))
211207
(unless ignore-focus?
212208
(select-window window)
213209
(set-window-dedicated-p window t))))
214210

215211
(lsp-defun lsp-dart-outline-handle-outline (workspace (notification &as &OutlineNotification :uri :outline))
216212
"Outline notification handling from WORKSPACE.
217-
PARAMS outline notification data sent.
213+
NOTIFICATION is outline notification data received from server.
218214
It updates the outline view if it already exists."
219215
(lsp-dart-outline--set-metadata workspace notification "current-outline")
220216
(when lsp-dart-main-code-lens
@@ -224,28 +220,15 @@ It updates the outline view if it already exists."
224220
(when (get-buffer-window "*Dart Outline*")
225221
(lsp-dart-outline--show-outline (lsp--buffer-for-file (lsp--uri-to-path uri)) t)))
226222

227-
;; (defun lsp-dart-outline-handle-outline (workspace params)
228-
;; "Outline notification handling from WORKSPACE.
229-
;; PARAMS outline notification data sent.
230-
;; It updates the outline view if it already exists."
231-
;; (lsp-dart-outline--set-metadata workspace params "current-outline")
232-
;; (-let (((&hash "uri" "outline") params))
233-
;; (when lsp-dart-main-code-lens
234-
;; (lsp-dart-code-lens-check-main uri outline))
235-
;; (when lsp-dart-test-code-lens
236-
;; (lsp-dart-code-lens-check-test uri outline))
237-
;; (when (get-buffer-window "*Dart Outline*")
238-
;; (lsp-dart-outline--show-outline (lsp--buffer-for-file (lsp--uri-to-path uri)) t))))
239-
240-
(defun lsp-dart-outline-handle-flutter-outline (workspace params)
223+
(lsp-defun lsp-dart-outline-handle-flutter-outline (workspace (notification &as &FlutterOutlineNotification :uri))
241224
"Flutter outline notification handling from WORKSPACE.
242-
PARAMS Flutter outline notification data sent.
225+
NOTIFICATION is Flutter outline notification data received from server.
243226
It updates the Flutter outline view if it already exists."
244-
(lsp-dart-outline--set-metadata workspace params "current-flutter-outline")
227+
(lsp-dart-outline--set-metadata workspace notification "current-flutter-outline")
245228
(when lsp-dart-flutter-widget-guides
246-
(lsp-dart-flutter-widget-guide-check params))
229+
(lsp-dart-flutter-widget-guide-check notification))
247230
(when (get-buffer-window "*Flutter Outline*")
248-
(lsp-dart-outline--show-flutter-outline (lsp--buffer-for-file (lsp--uri-to-path (gethash "uri" params))) t)))
231+
(lsp-dart-outline--show-flutter-outline (lsp--buffer-for-file (lsp--uri-to-path uri)) t)))
249232

250233

251234
;;; Public interface

lsp-dart-protocol.el

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
;;; lsp-dart-protocol.el --- lsp-dart custom protocol -*- lexical-binding: t; -*-
1+
;;; lsp-dart-protocol.el --- lsp-dart custom protocol definitions -*- lexical-binding: t; -*-
22
;;
3-
; This program is free software; you can redistribute it and/or modify
3+
;; 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
55
;; the Free Software Foundation, either version 3 of the License, or
66
;; (at your option) any later version.
@@ -13,17 +13,23 @@
1313
;; You should have received a copy of the GNU General Public License
1414
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
1515
;;
16-
;; This file is not part of GNU Emacs.
17-
;;
1816
;;; Commentary:
1917
;;
20-
;; lsp-dart custom protocol
18+
;; lsp-dart custom protocol definitions
2119
;;
2220
;;; Code:
2321

2422
(require 'lsp-protocol)
2523

26-
(lsp-interface (OutlineNotification (:uri :outline) nil))
24+
(lsp-interface
25+
(OutlineNotification (:uri :outline) nil)
26+
(Outline (:element :range :codeRange :children) nil)
27+
(Element (:name :range :kind) (:parameters :typeParameters :returnType)))
28+
29+
(lsp-interface
30+
(FlutterOutlineNotification (:uri :outline) nil)
31+
(FlutterOutline (:range :codeRange :children :kind) (:dartElement :label :className :variableName :attributes))
32+
(FlutterOutlineAttribute (:name :label) nil))
2733

2834
(provide 'lsp-dart-protocol)
2935
;;; lsp-dart-protocol.el ends here

0 commit comments

Comments
 (0)