Skip to content

Instantly share code, notes, and snippets.

@ManasJayanth
Last active December 3, 2019 02:41
Show Gist options
  • Save ManasJayanth/2371c42ae6c9128c76b6c40593a444f3 to your computer and use it in GitHub Desktop.
Save ManasJayanth/2371c42ae6c9128c76b6c40593a444f3 to your computer and use it in GitHub Desktop.
Esy with lsp-mode 🚧 Experimental
(defun load-esy-env ()
"Loads esy env"
(let* ((env-json-str (shell-command-to-string "esy command-env --json"))
(json-key-type 'string)
(env-pairs (json-read-from-string env-json-str)))
(progn
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(make-local-variable 'process-environment)
(setq process-environment (copy-sequence process-environment))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(make-local-variable 'exec-path)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cond ((listp env-pairs)
;; If X is a symbol, put it on LIST.
(dolist (e env-pairs)
(let ((var (car e)))
(setenv var (cdr e))
(if (equal var "PATH")
(setq exec-path (split-string (cdr e) ":"))) nil)))
((hash-table-p env-pairs)
(maphash (lambda (k v)
(if (equal k "PATH")
(progn
(setq exec-path (split-string v ":"))))) env-pairs))
(t
;; We handle only symbols and lists.
(error
"Invalid argument %s received for env-pairs"
env-pairs)))
;;(make-local-variable 'merlin-command)
(setq merlin-command (executable-find "ocamlmerlin"))
(make-local-variable 'refmt-command)
(setq refmt-command (executable-find "refmt")))))
(defun esy-locally-installed-lsp-or-global ()
;; First find merlin lsp
(let ((ocamlmerlin-lsp-path (executable-find "ocamlmerlin-lsp"))
(rls-path (executable-find "reason-language-server.exe")))
(if rls-path rls-path (if ocamlmerlin-lsp-path ocamlmerlin-lsp-path (concat (getenv "HOME")
"/install/rls/reason-language-server.exe")))))
(defun esy-hook ()
(let* ((json-object-type 'hash-table)
(json-array-type 'list)
(json-key-type 'string)
(json-false 'nil)
(json-str (shell-command-to-string "esy status"))
(json (json-read-from-string json-str))
(is-esy-project-ready (gethash "isProjectReadyForDev" json))
(esy-is-valid-project (gethash "isProject" json)))
(if is-esy-project-ready (progn (load-esy-env)
(make-local-variable 'compile-command)
(setq compile-command "esy")
(message (concat "Loading LSP at "
(esy-locally-installed-lsp-or-global)))
(lsp-register-client (make-lsp-client :new-connection
(lsp-stdio-connection
(esy-locally-installed-lsp-or-global))
:major-modes '(reason-mode
tuareg-mode)
:server-id 'esy-ls))
(lsp))
(if esy-is-valid-project (if (y-or-n-p
"Seems like a valid esy project. Go ahead and install and build all dependencies?")
(progn (let* ((output-buffer (generate-new-buffer "*Running
esy*"))
(proc (progn (async-shell-command "esy"
output-buffer)
(get-buffer-process output-buffer))))
(if (process-live-p proc)
(set-process-sentinel proc (lambda (process signal)
(when (memq
(process-status
process)
'(exit signal))
(message "esy command
exited")
(shell-command-sentinel
process signal))))
(message "callback couldnot be set to esy command"))))
(progn
;; code if user answered no.
()))
(message "Not an esy project")
;; (lsp-register-client (make-lsp-client :new-connection (lsp-stdio-connection (concat (getenv
;; "HOME")
;; "/install/rls/reason-language-server.exe"))
;; :major-modes '(reason-mode tuareg-mode)
;; :server-id 'esy-ls))
(lsp)))))
(defun pesy-hook ()
(make-local-variable 'compile-command)
(setq compile-command "esy pesy && esy"))
;; (add-hook 'reason-mode-hook 'pesy-hook)
;; (add-hook 'reason-mode-hook 'esy-hook)
;; (add-hook 'tuareg-mode-hook 'esy-hook)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment