about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/haskell-mode-20180913.348/haskell-complete-module.el
blob: ce165348f70a1bfc0d21f59d7b8547c6d639eb64 (plain) (blame)
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
;;; haskell-complete-module.el --- A fast way to complete Haskell module names -*- lexical-binding: t -*-

;; Copyright (c) 2014 Chris Done. All rights reserved.

;; 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/>.

;;; Code:

(require 'cl-lib)

(defcustom haskell-complete-module-preferred
  '()
  "Override ordering of module results by specifying preferred modules."
  :group 'haskell
  :type '(repeat string))

(defcustom haskell-complete-module-max-display
  10
  "Maximum items to display in minibuffer."
  :group 'haskell
  :type 'number)

(defun haskell-complete-module-read (prompt candidates)
  "Interactively auto-complete from a list of candidates."
  (let ((stack (list))
        (pattern "")
        (result nil))
    (delete-dups candidates)
    (setq candidates
          (sort candidates
                (lambda (a b)
                  (let ((a-mem (member a haskell-complete-module-preferred))
                        (b-mem (member b haskell-complete-module-preferred)))
                    (cond
                     ((and a-mem (not b-mem))
                      t)
                     ((and b-mem (not a-mem))
                      nil)
                     (t
                      (string< a b)))))))
    (while (not result)
      (let ((key
             (key-description
              (vector
               (read-key
                (concat (propertize prompt 'face 'minibuffer-prompt)
                        (propertize pattern 'face 'font-lock-type-face)
                        "{"
                        (mapconcat #'identity
                                   (let* ((i 0))
                                     (cl-loop for candidate in candidates
                                              while (<= i haskell-complete-module-max-display)
                                              do (cl-incf i)
                                              collect (cond ((> i haskell-complete-module-max-display)
                                                             "...")
                                                            ((= i 1)
                                                             (propertize candidate 'face 'ido-first-match-face))
                                                            (t candidate))))
                                   " | ")
                        "}"))))))
        (cond
         ((string= key "C-g")
          (keyboard-quit))
         ((string= key "DEL")
          (unless (null stack)
            (setq candidates (pop stack)))
          (unless (string= "" pattern)
            (setq pattern (substring pattern 0 -1))))
         ((string= key "RET")
          (setq result (or (car candidates)
                           pattern)))
         ((string= key "<left>")
          (setq candidates
                (append (last candidates)
                        (butlast candidates))))
         ((string= key "<right>")
          (setq candidates
                (append (cdr candidates)
                        (list (car candidates)))))
         (t
          (when (string-match "[A-Za-z0-9_'.]+" key)
            (push candidates stack)
            (setq pattern (concat pattern key))
            (setq candidates (haskell-complete-module pattern candidates)))))))
    result))

(defun haskell-complete-module (pattern candidates)
  "Filter the CANDIDATES using PATTERN."
  (let ((case-fold-search t))
    (cl-loop for candidate in candidates
             when (haskell-complete-module-match pattern candidate)
             collect candidate)))

(defun haskell-complete-module-match (pattern text)
  "Match PATTERN against TEXT."
  (string-match (haskell-complete-module-regexp pattern)
                text))

(defun haskell-complete-module-regexp (pattern)
  "Make a regular expression for the given module pattern. Example:

\"c.m.s\" -> \"^c[^.]*\\.m[^.]*\\.s[^.]*\"

"
  (let ((components (mapcar #'haskell-complete-module-component
                            (split-string pattern "\\." t))))
    (concat "^"
            (mapconcat #'identity
                       components
                       "\\."))))

(defun haskell-complete-module-component (component)
  "Make a regular expression for the given component. Example:

\"co\" -> \"c[^.]*o[^.]*\"

"
  (replace-regexp-in-string "\\(.\\)" "\\1[^.]*" component))

(provide 'haskell-complete-module)