                       (push (cons (buffer-file-name) (point)) tern-find-definition-stack)
                       (let ((too-long (nthcdr 20 tern-find-definition-stack)))
                         (when too-long (setf (cdr too-long) nil)))
-                      (tern-go-to-position (concat (tern-project-dir) (cdr (assoc 'file data)))
-                                           (1+ (cdr (assoc 'start data)))))
+                      (tern-go-to-position (concat (tern-project-dir) (cdr (assq 'file data)))
+                                           (1+ (cdr (assq 'start data)))))
                     "definition"
                     (point)
                     :full-file))

        (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)
