]> code.delx.au - gnu-emacs-elpa/blob - company-xcode.el
Tag company-dabbrev-ignore-buffers with package-version
[gnu-emacs-elpa] / company-xcode.el
1 ;;; company-xcode.el --- company-mode completion backend for Xcode projects
2
3 ;; Copyright (C) 2009-2011, 2014 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'cl-lib)
30
31 (defgroup company-xcode nil
32 "Completion backend for Xcode projects."
33 :group 'company)
34
35 (defcustom company-xcode-xcodeindex-executable (executable-find "xcodeindex")
36 "Location of xcodeindex executable."
37 :type 'file)
38
39 (defvar company-xcode-tags nil)
40
41 (defun company-xcode-reset ()
42 "Reset the cached tags."
43 (interactive)
44 (setq company-xcode-tags nil))
45
46 (defcustom company-xcode-types
47 '("Class" "Constant" "Enum" "Macro" "Modeled Class" "Structure"
48 "Type" "Union" "Function")
49 "The types of symbols offered by `company-xcode'.
50 No context-enabled completion is available. Types like methods will be
51 offered regardless of whether the class supports them. The defaults should be
52 valid in most contexts."
53 :set (lambda (variable value)
54 (set variable value)
55 (company-xcode-reset))
56 :type '(set (const "Category") (const "Class") (const "Class Method")
57 (const "Class Variable") (const "Constant") (const "Enum")
58 (const "Field") (const "Instance Method")
59 (const "Instance Variable") (const "Macro")
60 (const "Modeled Class") (const "Modeled Method")
61 (const "Modeled Property") (const "Property") (const "Protocol")
62 (const "Structure") (const "Type") (const "Union")
63 (const "Variable") (const "Function")))
64
65 (defvar-local company-xcode-project 'unknown)
66
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68
69 (defun company-xcode-fetch (project-bundle)
70 (setq project-bundle (directory-file-name project-bundle))
71 (message "Retrieving dump from %s..." project-bundle)
72 (with-temp-buffer
73 (let ((default-directory (file-name-directory project-bundle)))
74 (call-process company-xcode-xcodeindex-executable nil (current-buffer)
75 nil "dump" "-project"
76 (file-name-nondirectory project-bundle) "-quiet")
77 (goto-char (point-min))
78 (let ((regexp (concat "^\\([^\t\n]*\\)\t[^\t\n]*\t"
79 (regexp-opt company-xcode-types)
80 "\t[^\t\n]*\t[^\t\n]*"))
81 candidates)
82 (while (re-search-forward regexp nil t)
83 (cl-pushnew (match-string 1) candidates :test #'equal))
84 (message "Retrieving dump from %s...done" project-bundle)
85 candidates))))
86
87 (defun company-xcode-find-project ()
88 (let ((dir (if buffer-file-name
89 (file-name-directory buffer-file-name)
90 (expand-file-name default-directory)))
91 (prev-dir nil)
92 file)
93 (while (not (or file (equal dir prev-dir)))
94 (setq file (car (directory-files dir t ".xcodeproj\\'" t))
95 prev-dir dir
96 dir (file-name-directory (directory-file-name dir))))
97 file))
98
99 (defun company-xcode-tags ()
100 (when (eq company-xcode-project 'unknown)
101 (setq company-xcode-project (company-xcode-find-project)))
102 (when company-xcode-project
103 (cdr (or (assoc company-xcode-project company-xcode-tags)
104 (car (push (cons company-xcode-project
105 (company-xcode-fetch company-xcode-project))
106 company-xcode-tags))))))
107 ;;;###autoload
108 (defun company-xcode (command &optional arg &rest ignored)
109 "`company-mode' completion backend for Xcode projects."
110 (interactive (list 'interactive))
111 (cl-case command
112 (interactive (company-begin-backend 'company-xcode))
113 (prefix (and company-xcode-xcodeindex-executable
114 (company-xcode-tags)
115 (not (company-in-string-or-comment))
116 (or (company-grab-symbol) 'stop)))
117 (candidates (let ((completion-ignore-case nil))
118 (company-xcode-tags)
119 (all-completions arg (company-xcode-tags))))))
120
121
122 (provide 'company-xcode)
123 ;;; company-xcode.el ends here