               (with-current-buffer buffer
                 (cond ((not err)
                        (dolist (file files)
-                         (when (equal (cdr (assoc 'type file)) "full")
-                           (with-current-buffer (find-file-noselect (concat tern-project-dir (cdr (assoc 'name file))))
+                         (when (equal (cdr (assq 'type file)) "full")
+                           (with-current-buffer (find-file-noselect (concat tern-project-dir (cdr (assq 'name file))))
                              (setf tern-buffer-is-dirty nil))))
                        (funcall f data offset))
                       ((and (eq (cadar err) 'connection-failed) (not retrying))

        (tern-run-request #'tern-do-complete "completions" (point)))))

(defun tern-do-complete (data offset)
  (let ((cs (loop for elt across (cdr (assoc 'completions data)) collect (cdr (assoc 'name elt))))
        (start (+ 1 offset (cdr (assoc 'from data))))
        (end (+ 1 offset (cdr (assoc 'to data)))))
    (setf tern-last-completions (list (buffer-substring-no-properties start end) start end cs))
    (completion-in-region start end cs)))


(defun tern-do-refactor (data _offset)
  (let ((per-file ())
        (orig-buffer (current-buffer)))
    (loop for change across (cdr (assq 'changes data)) do
          (let ((found (assq-string (cdr (assq 'file change)) per-file)))
            (unless found (setf found (list (cdr (assq 'file change)))) (push found per-file))
            (push change (cdr found))))
    (loop for (file . changes) in per-file do
          (setf changes (sort changes (lambda (a b) (> (cdr (assq 'start a)) (cdr (assq 'start b))))))
          (find-file (concat (tern-project-dir) file))
          (loop for change in changes do
                (let ((start (1+ (cdr (assq 'start change))))
                      (end (1+ (cdr (assq 'end change)))))
                (delete-region start end)
                (save-excursion
                  (goto-char start)
                  (insert (cdr (assq 'text change)))))))
    (switch-to-buffer orig-buffer)))

(defun tern-rename-variable (new-name)
