Skip to content

Commit

Permalink
Merge pull request #36 from emacs-lsp/plists
Browse files Browse the repository at this point in the history
Migrate all hts -> plists
  • Loading branch information
ericdallo authored Jun 14, 2020
2 parents 7703507 + 9baddd4 commit 8a5ca12
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 50 deletions.
4 changes: 2 additions & 2 deletions lsp-dart-dap.el
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ Call CALLBACK when the device is chosen and started successfully."
(lsp-dart-log "No devices found. Try to create a device first via `flutter emulators` command")
(-let* ((chosen-device (dap--completing-read "Select a device to use: "
devices
(-lambda ((&hash "id" "name" "is-device" "platformType" platform))
(lsp-dart-dap--device-label id name is-device platform))
(-lambda ((&FlutterDaemonDevice :id :name :is-device? :platform-type platform))
(lsp-dart-dap--device-label id name is-device? platform))
nil
t)))
(lsp-dart-flutter-daemon-launch chosen-device callback))))))
Expand Down
49 changes: 24 additions & 25 deletions lsp-dart-flutter-daemon.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

(require 'comint)
(require 'dash)
(require 'ht)
(require 'lsp-mode)

(require 'lsp-dart-protocol)
(require 'lsp-dart-utils)

(defconst lsp-dart-flutter-daemon-buffer-name "*LSP Dart - Flutter daemon*")
Expand Down Expand Up @@ -66,10 +66,10 @@
(defun lsp-dart-flutter-daemon--build-command (id method &optional params)
"Build a command from an ID and METHOD.
PARAMS is the optional method params."
(let ((command (ht ("id" id)
("method" method))))
(let ((command (lsp-make-flutter-daemon-command :id id
:method method)))
(when params
(ht-set! command "params" params))
(lsp:set-flutter-daemon-command-params? command params))
(concat "["
(lsp--json-serialize command)
"]\n")))
Expand All @@ -86,22 +86,23 @@ PARAMS is the optional method params."

(defun lsp-dart-flutter-daemon--handle-responses (raw-response)
"Handle Flutter daemon response from RAW-RESPONSE."
(-map (-lambda ((&hash "id" "event" "result" "params" (params &as &hash? "level" "message")))
(if event
(pcase event
(-map (-lambda ((&FlutterDaemonResponse :id :event? :result?
:params? (params &as &FlutterDaemonResponseParams? :level? :message?)))
(if event?
(pcase event?
("device.removed" (lsp-dart-flutter-daemon--device-removed params))

("device.added" (lsp-dart-flutter-daemon--device-added params))

("daemon.logMessage" (lsp-dart-flutter-daemon--log level message)))
("daemon.logMessage" (lsp-dart-flutter-daemon--log level? message?)))
(let* ((command (alist-get id lsp-dart-flutter-daemon-commands))
(callback (gethash "callback" command)))
(callback (plist-get command :callback)))
(when command
(setq lsp-dart-flutter-daemon-commands
(lsp-dart-remove-from-alist id lsp-dart-flutter-daemon-commands)))
(when callback
(when result
(funcall callback result))))))
(when result?
(funcall callback result?))))))
(lsp-dart-flutter-daemon--raw->response raw-response)))

(defun lsp-dart-flutter-daemon--send (method &optional params callback)
Expand All @@ -112,7 +113,7 @@ of this command."
(lsp-dart-flutter-daemon-start))
(let* ((id (lsp-dart-flutter-daemon--generate-command-id))
(command (lsp-dart-flutter-daemon--build-command id method params)))
(add-to-list 'lsp-dart-flutter-daemon-commands (cons id (ht ("callback" callback))))
(add-to-list 'lsp-dart-flutter-daemon-commands (cons id (list :callback callback)))
(comint-send-string (get-buffer-process lsp-dart-flutter-daemon-buffer-name) command)))

(defun lsp-dart-flutter-daemon--device-removed (device)
Expand All @@ -121,18 +122,17 @@ of this command."
(lsp-dart-remove-from-alist it lsp-dart-flutter-daemon-devices)
(setq lsp-dart-flutter-daemon-devices it)))

(defun lsp-dart-flutter-daemon--device-added (device)
(lsp-defun lsp-dart-flutter-daemon--device-added ((device &as &FlutterDaemonDevice :id))
"Add DEVICE to the devices list."
(-let* ((id (gethash "id" device))
(device-to-add (cons id device)))
(ht-set! device "is-device" t)
(-let ((device-to-add (cons id device)))
(lsp:set-flutter-daemon-device-is-device? device t)
(setq lsp-dart-flutter-daemon-devices
(lsp-dart-remove-from-alist id lsp-dart-flutter-daemon-devices))
(add-to-list 'lsp-dart-flutter-daemon-devices device-to-add)
(-when-let (listener (alist-get id lsp-dart-flutter-daemon-device-added-listeners))
(setq lsp-dart-flutter-daemon-device-added-listeners
(lsp-dart-remove-from-alist id lsp-dart-flutter-daemon-device-added-listeners))
(funcall (gethash "callback" listener) device))))
(funcall (plist-get listener :callback) device))))

(defun lsp-dart-flutter-daemon-get-devices (callback)
"Call CALLBACK with the available emulators and devices from Flutter daemon."
Expand All @@ -145,18 +145,17 @@ of this command."
(append emulators)
(funcall callback)))))

(defun lsp-dart-flutter-daemon-launch (emulator callback)
"Launch EMULATOR and wait for connected state and call CALLBACK."
(lsp-defun lsp-dart-flutter-daemon-launch ((device &as &FlutterDaemonDevice :id :is-device?) callback)
"Launch DEVICE and wait for connected state and call CALLBACK."
(if lsp-dart-flutter-daemon-current-device
(funcall callback lsp-dart-flutter-daemon-current-device)
(progn
(setq lsp-dart-flutter-daemon-current-device emulator)
(if (gethash "is-device" emulator)
(funcall callback emulator)
(-let* (((&hash "id") emulator)
(params (ht ("emulatorId" id))))
(setq lsp-dart-flutter-daemon-current-device device)
(if is-device?
(funcall callback device)
(-let* ((params (lsp-make-flutter-daemon-emulator-launch :emulator-id id)))
(add-to-list 'lsp-dart-flutter-daemon-device-added-listeners
(cons id (ht ("callback" callback))))
(cons id (list :callback callback)))
(lsp-dart-flutter-daemon--send "emulator.launch" params callback))))))

(defun lsp-dart-flutter-daemon--reset-current-device (_session)
Expand Down
37 changes: 18 additions & 19 deletions lsp-dart-flutter-widget-guide.el
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
;;; Code:

(require 'dash)
(require 'ht)
(require 'lsp-mode)

(require 'lsp-dart-protocol)
Expand Down Expand Up @@ -54,8 +53,8 @@
(goto-char (point-min))
(forward-line line)
(back-to-indentation)
(ht ("line" line)
("character" (current-column)))))
(lsp-make-position :line line
:character (current-column))))

(defun lsp-dart-flutter-widget-guide--last-col-at (line)
"Return the last col at LINE."
Expand All @@ -79,8 +78,8 @@ Return nil if the widget guilde does not apply."
(when children-start
(let ((start-pos (lsp-dart-flutter-widget-guide--first-non-whitespace-pos parent-line)))
(->> children-start
(--map (ht ("start" start-pos)
("end" (lsp-dart-flutter-widget-guide--first-non-whitespace-pos it))))
(--map (lsp-make-range :start start-pos
:end (lsp-dart-flutter-widget-guide--first-non-whitespace-pos it)))
(-flatten)))))))

(lsp-defun lsp-dart-flutter-widget-guide--outline->guides ((outline &as &FlutterOutline :children))
Expand All @@ -97,14 +96,14 @@ Return nil if the widget guilde does not apply."

(defun lsp-dart-flutter-widget-guide--guides->guides-by-line (guides)
"Convert a widget guide GUIDES to a map by line."
(let ((guides-by-line (ht-create)))
(let ((guides-by-line '()))
(seq-doseq (guide guides)
(-let (((&Range :start (&Position :line start-line)
:end (&Position :line end-line)) guide))
(while (<= start-line end-line)
(if-let ((cur-guides (ht-get guides-by-line start-line)))
(ht-set! guides-by-line start-line (append cur-guides (list guide)))
(ht-set! guides-by-line start-line (list guide)))
(if-let ((cur-guides (plist-get guides-by-line start-line)))
(add-to-list 'guides-by-line (cons start-line (append cur-guides (list guide))))
(add-to-list 'guides-by-line (cons start-line (list guide))))
(setq start-line (1+ start-line)))))
guides-by-line))

Expand Down Expand Up @@ -145,16 +144,16 @@ ANCHOR is the anchor point of the widget guide at LINE."
(remove-overlays (point-min) (point-max) 'category 'lsp-dart-flutter-widget-guide)
(let* ((guides (lsp-dart-flutter-widget-guide--outline->guides outline))
(guides-by-line (lsp-dart-flutter-widget-guide--guides->guides-by-line guides)))
(ht-each (lambda (line guide-lines)
(let* ((first-guide-char (-min (--map (min (-> it lsp:range-start lsp:position-character)
(-> it lsp:range-end lsp:position-character)) guide-lines)))
(last-guide-char (-max (--map (max (-> it lsp:range-start lsp:position-character)
(-> it lsp:range-end lsp:position-character)) guide-lines)))
(last-line-char (lsp-dart-flutter-widget-guide--last-col-at line))
(anchor (max 0 (if (< last-line-char first-guide-char) 0 first-guide-char)))
(chars (lsp-dart-flutter-widget-guide--build-chars line guide-lines last-guide-char last-line-char anchor)))
(--each-indexed chars (lsp-dart-flutter-widget-guide--add-overlay-to buffer line (+ it-index anchor) it))))
guides-by-line)))))
(seq-doseq (line (mapcar 'car guides-by-line))
(let* ((guide-lines (alist-get line guides-by-line))
(first-guide-char (-min (--map (min (-> it lsp:range-start lsp:position-character)
(-> it lsp:range-end lsp:position-character)) guide-lines)))
(last-guide-char (-max (--map (max (-> it lsp:range-start lsp:position-character)
(-> it lsp:range-end lsp:position-character)) guide-lines)))
(last-line-char (lsp-dart-flutter-widget-guide--last-col-at line))
(anchor (max 0 (if (< last-line-char first-guide-char) 0 first-guide-char)))
(chars (lsp-dart-flutter-widget-guide--build-chars line guide-lines last-guide-char last-line-char anchor)))
(--each-indexed chars (lsp-dart-flutter-widget-guide--add-overlay-to buffer line (+ it-index anchor) it))))))))

(provide 'lsp-dart-flutter-widget-guide)
;;; lsp-dart-flutter-widget-guide.el ends here
10 changes: 10 additions & 0 deletions lsp-dart-protocol.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,15 @@
(FlutterOutline (:range :codeRange :children :kind) (:dartElement :label :className :variableName :attributes))
(FlutterOutlineAttribute (:name :label) nil))

(lsp-interface
(FlutterWidgetGuide ()))

(lsp-interface
(FlutterDaemonCommand (:id :method) (:params))
(FlutterDaemonResponse (:id) (:result :event :params))
(FlutterDaemonResponseParams nil (:level :message))
(FlutterDaemonDevice (:id :name :platform :category :platformType :ephemeral :emulator) (:isDevice))
(FlutterDaemonEmulatorLaunch (:emulatorId)))

(provide 'lsp-dart-protocol)
;;; lsp-dart-protocol.el ends here
2 changes: 1 addition & 1 deletion lsp-dart.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;; lsp-dart.el --- Dart support lsp-mode -*- lexical-binding: t; -*-

;; Version: 1.11.9
;; Package-Requires: ((emacs "25.2") (lsp-treemacs "0.1") (lsp-mode "6.0") (dap-mode "0.4") (ht "2.0") (f "0.20.0") (dash "2.14.1") (pkg-info "0.4") (dart-mode "1.0.5"))
;; Package-Requires: ((emacs "25.2") (lsp-treemacs "0.1") (lsp-mode "6.4") (dap-mode "0.4") (ht "2.0") (f "0.20.0") (dash "2.14.1") (pkg-info "0.4") (dart-mode "1.0.5"))
;; Keywords: languages, extensions
;; URL: https://emacs-lsp.github.io/lsp-dart

Expand Down
6 changes: 3 additions & 3 deletions test/lsp-dart-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@
(ert-deftest lsp-dart--handle-analyzer-status--when-analyzing-test ()
(with-mock
(mock (lsp-dart-workspace-status "Analyzing project..." "workspace"))
(lsp-dart--handle-analyzer-status "workspace" (ht ("isAnalyzing" t)))))
(lsp-dart--handle-analyzer-status "workspace" (lsp-make-analyzer-status-notification :is-analyzing t))))

(ert-deftest lsp-dart--handle-analyzer-status--when-analyzing-test ()
(ert-deftest lsp-dart--handle-analyzer-status--when-not-analyzing-test ()
(with-mock
(mock (lsp-dart-workspace-status nil "workspace"))
(lsp-dart--handle-analyzer-status "workspace" (ht ("isAnalyzing" nil)))))
(lsp-dart--handle-analyzer-status "workspace" (lsp-make-analyzer-status-notification :is-analyzing nil))))

(ert-deftest lsp-dart-version--test ()
(let ((pkg-version (lsp-dart-test-package-version "lsp-dart.el")))
Expand Down

0 comments on commit 8a5ca12

Please sign in to comment.