Skip to content

Commit

Permalink
Merge pull request #20 from kevinkehl/unresolved-fix
Browse files Browse the repository at this point in the history
Add handling for multiple unresolved imports
  • Loading branch information
kevinkehl authored Dec 20, 2016
2 parents 4818ece + 6fe09eb commit 15d3951
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions plugin/import-js.el
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
;; Author: Kevin Kehl <[email protected]>
;; URL: http://github.com/Galooshi/emacs-import-js/
;; Package-Requires: ((grizzl "0.1.0") (emacs "24"))
;; Version: 0.7.0
;; Version: 1.0.0
;; Keywords: javascript

;; This file is not part of GNU Emacs.
Expand Down Expand Up @@ -77,51 +77,51 @@
(write-region file-content nil buffer-file-name))
(revert-buffer t t t))

(defun import-js-add (word file)
(defun import-js-add (add-alist)
"Resolves an import with multiple matches"
(let ((json-data (json-encode-alist (list (cons word file)))))
(let ((json-data (json-encode-alist add-alist)))
(let ((import-data (json-read-from-string
(import-js-send-input "add" json-data))))
(import-js-handle-imports import-data word))))
(import-js-handle-imports import-data))))

(defun import-js-handle-unresolved (unresolved word)
(let ((paths (mapcar
(lambda (car)
(cdr (assoc 'importPath car)))
(cdr (assoc-string word unresolved)))))
(let ((file (minibuffer-with-setup-hook
(lambda () (make-sparse-keymap))
(grizzl-completing-read (format "Unresolved import (%s)" word)
(grizzl-make-index
paths
'files
import-js-current-project-root
nil)))))
(import-js-add word file))))

(defun import-js-handle-imports (import-data word)
(minibuffer-with-setup-hook
(lambda () (make-sparse-keymap))
(grizzl-completing-read (format "Unresolved import (%s)" word)
(grizzl-make-index
paths
'files
import-js-current-project-root
nil)))))

(defun import-js-handle-imports (import-data)
"Check to see if import is unresolved. If resolved, write file. Else, prompt the user to select"
(let ((unresolved (cdr (assoc 'unresolvedImports import-data))))
(if unresolved
(import-js-handle-unresolved unresolved word)
(import-js-add (mapcar
(lambda (word)
(let ((key (car word)))
(cons key (import-js-handle-unresolved unresolved key))))
unresolved))
(import-js-write-content import-data))))

;;;###autoload
(defun import-js-import ()
(interactive)
(save-some-buffers)
(let ((word (import-js-word-at-point))
(import-data (json-read-from-string
(import-js-send-input "word" (import-js-word-at-point)))))
(import-js-handle-imports import-data word)))
(import-js-handle-imports (json-read-from-string
(import-js-send-input "word" (import-js-word-at-point)))))

;;;###autoload
(defun import-js-fix ()
(interactive)
(save-some-buffers)
(let ((import-data (json-read-from-string
(import-js-send-input "fix"))))
(import-js-write-content import-data)))
(import-js-handle-imports (json-read-from-string
(import-js-send-input "fix"))))

;;;###autoload
(defun import-js-goto ()
Expand Down

0 comments on commit 15d3951

Please sign in to comment.