about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/company-20180704.701/company-xcode.el
blob: 56da19890b2d5d453d91f3b56a8431c869161431 (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
;;; company-xcode.el --- company-mode completion backend for Xcode projects

;; Copyright (C) 2009-2011, 2014  Free Software Foundation, Inc.

;; Author: Nikolaj Schumacher

;; This file is part of GNU Emacs.

;; GNU Emacs 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 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.


;;; Commentary:
;;

;;; Code:

(require 'company)
(require 'cl-lib)

(defgroup company-xcode nil
  "Completion backend for Xcode projects."
  :group 'company)

(defcustom company-xcode-xcodeindex-executable (executable-find "xcodeindex")
  "Location of xcodeindex executable."
  :type 'file)

(defvar company-xcode-tags nil)

(defun company-xcode-reset ()
  "Reset the cached tags."
  (interactive)
  (setq company-xcode-tags nil))

(defcustom company-xcode-types
  '("Class" "Constant" "Enum" "Macro" "Modeled Class" "Structure"
    "Type" "Union" "Function")
  "The types of symbols offered by `company-xcode'.
No context-enabled completion is available.  Types like methods will be
offered regardless of whether the class supports them.  The defaults should be
valid in most contexts."
  :set (lambda (variable value)
         (set variable value)
         (company-xcode-reset))
  :type '(set (const "Category") (const "Class") (const "Class Method")
              (const "Class Variable") (const "Constant") (const "Enum")
              (const "Field") (const "Instance Method")
              (const "Instance Variable") (const "Macro")
              (const "Modeled Class") (const "Modeled Method")
              (const "Modeled Property") (const "Property") (const "Protocol")
              (const "Structure") (const "Type") (const "Union")
              (const "Variable") (const "Function")))

(defvar-local company-xcode-project 'unknown)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun company-xcode-fetch (project-bundle)
  (setq project-bundle (directory-file-name project-bundle))
  (message "Retrieving dump from %s..." project-bundle)
  (with-temp-buffer
    (let ((default-directory (file-name-directory project-bundle)))
      (call-process company-xcode-xcodeindex-executable nil (current-buffer)
                    nil "dump" "-project"
                    (file-name-nondirectory project-bundle) "-quiet")
      (goto-char (point-min))
      (let ((regexp (concat "^\\([^\t\n]*\\)\t[^\t\n]*\t"
                            (regexp-opt company-xcode-types)
                            "\t[^\t\n]*\t[^\t\n]*"))
            candidates)
        (while (re-search-forward regexp nil t)
          (cl-pushnew (match-string 1) candidates :test #'equal))
        (message "Retrieving dump from %s...done" project-bundle)
        candidates))))

(defun company-xcode-find-project ()
  (let ((dir (if buffer-file-name
                 (file-name-directory buffer-file-name)
               (expand-file-name default-directory)))
        (prev-dir nil)
        file)
    (while (not (or file (equal dir prev-dir)))
      (setq file (car (directory-files dir t ".xcodeproj\\'" t))
            prev-dir dir
            dir (file-name-directory (directory-file-name dir))))
    file))

(defun company-xcode-tags ()
  (when (eq company-xcode-project 'unknown)
    (setq company-xcode-project (company-xcode-find-project)))
  (when company-xcode-project
    (cdr (or (assoc company-xcode-project company-xcode-tags)
             (car (push (cons company-xcode-project
                              (company-xcode-fetch company-xcode-project))
                        company-xcode-tags))))))
;;;###autoload
(defun company-xcode (command &optional arg &rest ignored)
  "`company-mode' completion backend for Xcode projects."
  (interactive (list 'interactive))
  (cl-case command
    (interactive (company-begin-backend 'company-xcode))
    (prefix (and company-xcode-xcodeindex-executable
                 (company-xcode-tags)
                 (not (company-in-string-or-comment))
                 (or (company-grab-symbol) 'stop)))
    (candidates (let ((completion-ignore-case nil))
                  (company-xcode-tags)
                  (all-completions arg (company-xcode-tags))))))


(provide 'company-xcode)
;;; company-xcode.el ends here