Skip to content

Commit

Permalink
Initial stab at a flow-type layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jdelStrother committed Oct 7, 2016
1 parent ff95f2a commit c8b1b67
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
13 changes: 13 additions & 0 deletions layers/+lang/flow-type/README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#+TITLE: flow-type layer

* Description
This layer adds some [[https://flowtype.org/][flow]] related functionality to Emacs:
- Show the flow-type of the object under the cursor using Eldoc
- Add flow checking to flycheck (based on the flycheck-flow package)
- company-mode completion using company-flow

* Install
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =flow-type= to the existing =dotspacemacs-configuration-layers= list in this
file.

19 changes: 19 additions & 0 deletions layers/+lang/flow-type/funcs.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(defun flow-type/call-process-on-buffer-to-string (command)
(with-output-to-string
(call-process-region (point-min) (point-max) shell-file-name nil standard-output nil shell-command-switch command)))

(defun flow-type/type-description (info)
(let ((type (alist-get 'type info)))
(if (string-equal type "(unknown)")
(let ((reasons (alist-get 'reasons info)))
(if (> (length reasons) 0) (alist-get 'desc (aref reasons 0))))
type)))

(defun flow-type/type-at-cursor ()
(let ((output (flow-type/call-process-on-buffer-to-string
(format "flow type-at-pos --retry-if-init=false --json %d %d" (line-number-at-pos) (+ (current-column) 1)))))
(unless (string-match "\w*flow is still initializing" output)
(flow-type/type-description (json-read-from-string output)))))

(defun flow-type/init-mode ()
(set (make-local-variable 'eldoc-documentation-function) 'flow-type/type-at-cursor))
55 changes: 55 additions & 0 deletions layers/+lang/flow-type/packages.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
;;; packages.el --- flow-type layer packages file for Spacemacs.
;;
;; Copyright (c) 2012-2016 Sylvain Benner & Contributors
;;
;; Author: Jonathan del Strother <jdelStrother@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3

(defconst flow-type-packages
'(company
(company-flow :toggle (configuration-layer/package-usedp 'company))
(flycheck-flow :toggle (configuration-layer/package-usedp 'flycheck))
js2-mode
web-mode))

(defun flow-type/post-init-js2-mode()
(push 'flow-type/init-mode js2-mode-hook))

(defun flow-type/post-init-web-mode()
(push 'flow-type/init-mode react-mode-hook))

(defun flow-type/post-init-company()
(spacemacs|add-company-hook js2-mode)
(when (configuration-layer/layer-usedp 'react)
(spacemacs|add-company-hook react-mode)))

(defun flow-type/init-company-flow ()
(use-package company-flow
:defer t
:init
(progn
(push 'company-flow company-backends-js2-mode)
(when (configuration-layer/package-usedp 'web-mode)
(push 'company-flow company-backends-react-mode))
)
:config
(when (configuration-layer/package-usedp 'web-mode)
(push 'react-mode company-flow-modes)))
)

(defun flow-type/init-flycheck-flow()
(with-eval-after-load 'flycheck
(use-package flycheck-flow
:config
(progn
;; Don't run flow if there's no @flow pragma
(custom-set-variables '(flycheck-javascript-flow-args (quote ("--respect-pragma"))))
;; Run flow in react-mode files
(flycheck-add-mode 'javascript-flow 'react-mode)
;; After running js-flow, run js-eslint
(flycheck-add-next-checker 'javascript-flow 'javascript-eslint)
))))

0 comments on commit c8b1b67

Please sign in to comment.