ternjs / tern

A JavaScript code analyzer for deep, cross-editor language support
https://ternjs.net/
MIT License
4.25k stars 378 forks source link

Is it possible to get Tern work for non-project files? #701

Closed stardiviner closed 8 years ago

stardiviner commented 8 years ago

I want company-tern work in js-comint (JavaScript inferior buffer) too. But I got error like this:

Debugger entered--Lisp error: (wrong-type-argument arrayp nil)
  substring(nil 0)
  tern-project-relative-file()
  (cons 'name (tern-project-relative-file))
  (list '(type . "full") (cons 'text (buffer-string)) (cons 'name (tern-project-relative-file)))
  (cons (list '(type . "full") (cons 'text (buffer-string)) (cons 'name (tern-project-relative-file))) files)
  (setq files (cons (list '(type . "full") (cons 'text (buffer-string)) (cons 'name (tern-project-relative-file))) files))
  (cond ((not tern-buffer-is-dirty) (setq file-name (tern-project-relative-file))) ((and (not (eq mode :full-file)) (> (buffer-size) 8000)) (setq files (cons (tern-get-partial-file pos) files)) (setq file-name "#0") (setq pos (- pos (cdr (assq 'offset (car files)))))) (t (setq files (cons (list '(type . "full") (cons 'text (buffer-string)) (cons 'name (tern-project-relative-file))) files)) (setq file-name (tern-project-relative-file))))
  (let ((generation (setq tern-command-generation (1+ tern-command-generation))) (doc (list (cons 'query query))) (files (and (eq mode :full-file) (tern-modified-sibling-buffers))) file-name (pos pos)) (cond ((not tern-buffer-is-dirty) (setq file-name (tern-project-relative-file))) ((and (not (eq mode :full-file)) (> (buffer-size) 8000)) (setq files (cons (tern-get-partial-file pos) files)) (setq file-name "#0") (setq pos (- pos (cdr (assq 'offset (car files)))))) (t (setq files (cons (list '(type . "full") (cons 'text (buffer-string)) (cons 'name (tern-project-relative-file))) files)) (setq file-name (tern-project-relative-file)))) (if files (progn (setq doc (cons (cons 'files (apply #'vector files)) doc)))) (let* ((v (cons 'file file-name)) (v (assq 'query doc))) (setcdr v (cons v (cdr v)))) (let* ((v (cons 'end (1- pos))) (v (assq 'query doc))) (setcdr v (cons v (cdr v)))) (tern-run-request #'(lambda (err data) (if (< tern-activity-since-command generation) (progn (cond (... ... ...) (... ...))))) doc))
  tern-run-query
  company--begin-new()
  company--perform()
  company-auto-begin()
  company-idle-begin(#<buffer *js*> #<window 207 on init-my-prog-tools-container-docker.el> 218 218)
  timer-event-handler([t 22131 30701 536542 nil company-idle-begin (#<buffer *js*> #<window 207 on init-my-prog-tools-container-docker.el> 218 218) nil 586000])

Seems Tern need to check file is in a project.

Why Tern can't support general JS source code completion.

puremourning commented 8 years ago

You can put .tern-config in your home directory. It's like a global fallback for .tern-project. Don't know if that helps.

stardiviner commented 8 years ago

I tried .tern-config in my home directory, but it still does not work.

stardiviner commented 8 years ago

I bind run-js to global keybinding, it seems will use current buffer's path, not set to home dir by default. But after I tried to launch run-js in a buffer which is in home directory path, Still does not work. I even tried to start run-js from a JS project path. still does not work. WEIRD.

marijnh commented 8 years ago

The Emacs mode was written with the assumption that the buffer corresponds to a file, in order to know where to spawn the server and such. It could be changed to remove that assumption, if someone spends the time on that, but I'm probably not going to do that myself.

Attached patch fixes the crash, landing you on the intentional error message "Buffer is not associated with a fle" instead

stardiviner commented 8 years ago

I see. Thanks.

stardiviner commented 8 years ago

I found a way to fake current buffer as a file.

Here is an advice for making flycheck work in org babel editing source code.

;; how to correctly enable flycheck in babel source blocks
(defadvice org-edit-src-code (around set-buffer-file-name activate compile)
  (let ((file-name (buffer-file-name)))
    ad-do-it
    (setq buffer-file-name file-name)))

Do you know how to adapt to this case? If it work, I think it should add into tern.