-
Notifications
You must be signed in to change notification settings - Fork 67
/
elm-util.el
165 lines (136 loc) · 5.66 KB
/
elm-util.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
;;; elm-utils.el --- General utility functions used by Elm mode modules.
;; Copyright (C) 2013, 2014 Joseph Collard
;; Copyright (C) 2015 Bogdan Popa
;; Author: Joseph Collard
;; URL: https://github.com/jcollard/elm-mode
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'elm-defuns)
(require 'f)
(require 'json)
(require 'let-alist)
(require 's)
(require 'pulse)
(require 'haskell-decl-scan nil 'noerror)
(require 'inf-haskell nil 'noerror)
(defcustom elm-main-file "Main.elm"
"Allows for a custom main file to be specified."
:type 'string
:group 'elm-util)
(defcustom elm-flash-duration 0.40
"Time in seconds for which declarations will be highlighted when applicable."
:type 'number
:group 'elm-util)
(defcustom elm-package-json
"elm.json"
"The name of the package JSON configuration file."
:type 'string
:group 'elm-util)
(when (require 'project nil t)
(defun elm-project-find-function (dir)
"Find a project for project.el looking upwards from DIR.
This can be added to `project-find-functions' so that
`project-root' will return the directory in which the
`elm-package-json' file is found."
(let ((root-dir (locate-dominating-file dir elm-package-json)))
(when root-dir
(cons 'elm root-dir))))
(cl-defmethod project-root ((project (head elm)))
(cdr project)))
(defun elm--get-module-name ()
"Return the qualified name of the module in the current buffer."
(save-excursion
(goto-char (point-min))
(unless (re-search-forward "module +\\([A-Z][A-Za-z0-9.]*\\)" nil t)
(error "Module declaration not found"))
(buffer-substring-no-properties (match-beginning 1) (match-end 1))))
(defun elm--get-decl ()
"Return the current declaration."
(save-excursion
(goto-char (1+ (point)))
(unless (elm-beginning-of-defun)
(user-error "Not in a declaration"))
(let ((start (point)))
(elm-end-of-defun)
(let* ((end (point))
(raw-decl (s-trim-right (buffer-substring start end)))
(lines (split-string raw-decl "\n"))
(first-line (car lines))
;; Shadow the defcustom pulse-delay variable.
(pulse-delay (/ elm-flash-duration 10.0)))
(pulse-momentary-highlight-region start end)
(if (string-match-p "^[a-z].*:" first-line)
(cdr lines)
lines)))))
(defun elm--build-import-statement ()
"Generate a statement that will import the current module."
(concat "import " (elm--get-module-name) " exposing (..) \n"))
(defun elm--get-buffer-dirname ()
"Return the absolute dirname of the current buffer."
(file-name-as-directory default-directory))
(defun elm--buffer-local-file-name ()
"Return the current file name relative to the dependency file."
(let ((dirname (buffer-file-name))
(deppath (elm--find-dependency-file-path)))
(f-relative dirname deppath)))
(defun elm--find-elm-test-root-directory ()
"Find the directory from which to run \"elm-test\".
This is determined by looking for the closest parent directory
which is not called \"tests\" and which contains a file named as
per the `elm-package-json' variable."
(or (locate-dominating-file
default-directory
(lambda (dir)
(and (not (s-suffix-p "/tests/" dir))
(file-exists-p (expand-file-name elm-package-json dir)))))
(error "No %s found in non-test parent directories" elm-package-json)))
(defun elm--find-dependency-file-path ()
"Recursively search for a directory containing a package JSON file."
(or (locate-dominating-file default-directory elm-package-json)
(file-name-as-directory (f-dirname (buffer-file-name)))))
(defun elm--has-dependency-file ()
"Check if a dependency file exists."
(f-exists? (f-join (elm--find-dependency-file-path) elm-package-json)))
(declare-function elm-create-package "elm-interactive.el" nil)
(defun elm--assert-dependency-file ()
"Report an error unless there is a package file."
(unless (elm--has-dependency-file)
(if (yes-or-no-p "Elm package file not found. Create a new package?")
(call-interactively #'elm-create-package)
(error "Elm package file not found"))))
(defun elm--read-dependency-file ()
"Find and read the JSON dependency file into an object."
(elm--assert-dependency-file)
(let ((dep-file (f-join (elm--find-dependency-file-path) elm-package-json)))
(json-read-file dep-file)))
(defun elm--find-main-file ()
"Find the main elm file."
(let-alist (elm--read-dependency-file)
(let ((source-dir (aref .source-directories 0)))
(if (equal "." source-dir)
elm-main-file
(f-join source-dir elm-main-file)))))
(defun elm--shell-and-command ()
"Determine the appropriate \"and\" command for the current shell.
Currently only special cases the Fish shell, returning \"; and \" when
Fish is used as the default system shell. Returns \" && \" in all other
cases."
;; TODO: Windows?
(let* ((shell (getenv "SHELL"))
(executable (car (reverse (s-split "/" shell)))))
(pcase executable
("fish" "; and ")
(_ " && "))))
(provide 'elm-util)
;;; elm-util.el ends here