-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-objc-ide.el
404 lines (341 loc) · 15.9 KB
/
pjb-objc-ide.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
;;;; -*- mode:emacs-lisp; coding:utf-8; lexical-binding:t -*-
;;;;**************************************************************************
;;;;FILE: pjb-objc-ide.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Some Objective-C refactoring tools.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal Bourguignon <pbourguignon@dxo.com>
;;;;MODIFICATIONS
;;;; 2013-01-22 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; AGPL3
;;;;
;;;; Copyright Pascal Bourguignon 2013 - 2013
;;;;
;;;; This program is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Affero General Public License as published by
;;;; the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program 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 Affero General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Affero General Public License
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************
(require 'cl)
(require 'cedet)
(require 'pjb-strings)
(require 'pjb-emacs)
(require 'pjb-objc-parser)
(defvar *pjb-objc-ide--nslog-function* "NSLog"
"The Objective-C function to us to log messages.
It should have the same signature as NSLog.")
(defvar *pjb-objc-ide--entry-log-tag* "DEBUG"
"A tag that will be put in comment on the lines inserted by
`pjb-objc-ide--add-method-entry-log'.")
(defun pjb-objc-ide--ns-class-type-p (type)
;; TODO: It would be better to collect the exact list of NS classes…
(and (listp type)
(eq (second type) '*)
(null (cddr type))
(symbolp (first type))
(string= "NS" (substring (symbol-name (first type)) 0 2))))
(defun pjb-objc-ide--rect-formatter-arguments (argument)
(mapcar (lambda (fmt) (intern (format fmt argument)))
'("%s.origin.x" "%s.origin.y" "%s.size.width" "%s.size.height")))
(defun pjb-objc-ide--point-formatter-arguments (argument)
(mapcar (lambda (fmt) (intern (format fmt argument)))
'("%s.x" "%s.y")))
(defun pjb-objc-ide--size-formatter-arguments (argument)
(mapcar (lambda (fmt) (intern (format fmt argument)))
'("%s.width" "%s.height")))
(defun pjb-objc-ide--range-formatter-arguments (argument)
(mapcar (lambda (fmt) (intern (format fmt argument)))
'("%s.location" "%s.length")))
(defparameter *pjb-objc-ide--type-formatter-map*
'(((NSUInteger) . "%d")
((NSInteger) . "%d")
((BOOL) . "%d")
((CGFloat) . "%f")
((int) . "%d")
((unsigned) . "%u")
((unsigned int) . "%u")
((char) . "%c")
((char *) . "%s")
((const char *) . "%s")
((id) . "%@")
((NSRange) . ("{l=%d,c=%d}" pjb-objc-ide--range-formatter-arguments))
((NSPoint) . ("{x=%f,y=%f}" pjb-objc-ide--point-formatter-arguments))
((NSSize) . ("{w=%f,h=%f}" pjb-objc-ide--size-formatter-arguments))
((NSRect) . ("{{x=%f,y=%f},{w=%f,h=%f}}" pjb-objc-ide--rect-formatter-arguments))
(pjb-objc-ide--ns-class-type-p . "%@"))
"A list of conses.
The car slots are lists representing Objective-C types,
or a predicate name to be called to match the type.
The cdr slots are NSLog formatters: either a string to be inserted
into the formatter string of NSLog, or a list containing a formatter
string, and a function to build a list of arguments to NSLog for this
formatter string, from the expression to be formatted.
")
(defun pjb-objc-ide--formatter-for-type (type)
(loop
for (type-match . formatter) in *pjb-objc-ide--type-formatter-map*
do (when (if (and (atom type-match)
(fboundp type-match))
(funcall type-match type)
(equal type-match type))
(return formatter))
finally (return (cond
((and (atom type) (char= ?* (last-char (ensure-string type))))
(pjb-objc-ide--formatter-for-type (list (intern (butlast-char (ensure-string type))) '*)))
((and (listp type) (null (rest type)) (atom (first type))
(char= ?* (last-char (ensure-string (first type)))))
(pjb-objc-ide--formatter-for-type (list (intern (butlast-char (ensure-string (first type)))) '*)))
(t
nil)))))
(assert (equal (mapcar 'pjb-objc-ide--formatter-for-type
'((NSInteger) (NSUInteter) (BOOL) (char) (char *) (const char *) (id)
(NSString *) (NSMutableString *) (NSDictionary *) (NSMutableArray *)
(something *) (something) NSArray* (NSArray*)))
'("%d" nil "%d" "%c" "%s" "%s" "%@" "%@" "%@" "%@" "%@" nil nil "%@" "%@")))
(defun pjb-objc-ide--princ-to-string (x)
(let ((print-circle nil))
(format "%s" x)))
(defun pjb-objc-ide--process-method (processor &optional count)
"Process method implementations.
If `count' is negative then processes all the implementation methods
from the point down to the end of the buffer, otherwise process the
given number of methods (default 1).
Calls the function `processor' with as arguments, the method
signature, the start point and end point of the body region (including
braces), and with the current point before the opening brace (=
start-pt).
The excursion is saved while running `processor'.
"
(loop
with count = (or count 1)
for i from 0
while (or (minusp count) (< i count))
do
(incf i)
(c-forward-comments)
(if (re-search-forward "\\(\\s-\\|\n\\)*[-+]\\(\\s-\\|\n\\)*(" nil t)
(progn
(goto-char (match-beginning 0))
(let ((signature (pjb-objc-parser--parse-method-signature)))
(when (looking-at "\\(\\s-\\|\n\\)*{")
(forward-sexp)
(let ((end-pt (point)))
(backward-sexp)
(save-excursion
(funcall processor signature (point) end-pt))))))
(return))))
(defun pjb-objc-ide--insert-tag ()
"Insert a tag comment on a newline."
(insert (format " /*** %s ***/ " *pjb-objc-ide--entry-log-tag*)))
(defun pjb-objc-ide--insert-method-entry-log (&optional count)
"Insert a logging statement at the entry of the objective-c method dumping the parameters.
If `count' is negative then processes all the implementation methods
from the point down to the end of the buffer, otherwise process the
given number of methods (default 1).
The excursion is saved.
"
(interactive "p")
(let ((nil-types '()))
(pjb-objc-ide--process-method
(lambda (signature start-pt end-pt)
(down-list)
(insert "\n")
(pjb-objc-ide--insert-tag)
(insert (format "%s(@\"%%s" *pjb-objc-ide--nslog-function*))
(let ((formatter-parameters '()))
(dolist (param (cons (pjb-objc-parameter :name 'self :type '(id))
(pjb-objc-method-signature-parameters signature)))
(let* ((type (pjb-objc-parameter-type param))
(formatter (pjb-objc-ide--formatter-for-type type)))
(if formatter
(let ((args (if (listp formatter)
(funcall (second formatter) (pjb-objc-parameter-name param))
(list (pjb-objc-parameter-name param))))
(formatter (if (listp formatter)
(first formatter)
formatter)))
(push (cons (pjb-objc-parameter-name param) args) formatter-parameters)
(insert (format ", %%@ = %s" formatter)))
(pushnew type nil-types :test (function equal)))))
(insert "\", __PRETTY_FUNCTION__")
(dolist (param (reverse formatter-parameters))
(insert (format ", @\"%s\", %s"
(car param)
(mapconcat (function pjb-objc-ide--princ-to-string) (cdr param) ", ")))))
(insert ");"))
count)
(when nil-types
(message "Objective-C types without a formatter (see `*pjb-objc-ide--type-formatter-map*'): %S"
nil-types))))
(defun pjb-objc-ide--insert-method-return-log (&optional count)
"Insert a logging statement before each return statement in the objective-c method dumping the returned result.
If `count' is negative then processes all the implementation methods
from the point down to the end of the buffer, otherwise process the
given number of methods (default 1).
The excursion is saved.
"
(interactive "p")
(let ((nil-types '()))
(pjb-objc-ide--process-method
(lambda (signature start-pt end-pt)
;; We assume return statements are never left alone, but are
;; always in a block. Therefore we can insert before them as
;; many statements we want, including another return statement.
;;
;; {
;; return a+b;
;; }
;; -->
;; {
;; /*** TAG ***/ ResultTypeFromMethodSignature result=a+b;
;; /*** TAG ***/ NSLog(@"%p returns %d", __PRETTY_FUNCTION__, result);
;; /*** TAG ***/ return result;
;; return a+b;
;; }
(with-marker (end-pt end-pt)
(loop
while (re-search-forward "^\\s-*return \\([^;]*\\);" end-pt t)
do
(with-marker (mend (match-end 0))
(let ((result-type (pjb-objc-method-signature-result-type signature))
(result-variable (gensym "result_"))
(result-expression (match-string 1)))
(goto-char (match-beginning 0))
(pjb-objc-ide--insert-tag) (insert (format "%s %s=%s;\n"
(mapconcat (function pjb-objc-ide--princ-to-string) result-type " ")
result-variable
result-expression))
(pjb-objc-ide--insert-tag) (insert (format "%s(@\"%%s returns "
*pjb-objc-ide--nslog-function*))
(let ((formatter (pjb-objc-ide--formatter-for-type result-type)))
(if formatter
(let ((results (if (listp formatter)
(funcall (second formatter) result-variable)
(list result-variable)))
(formatter (if (listp formatter)
(first formatter)
formatter)))
(insert (format "%s\", __PRETTY_FUNCTION__, %s"
formatter
(mapconcat (function pjb-objc-ide--princ-to-string) results ", "))))
(progn
(insert (format "<%s>\", __PRETTY_FUNCTION__"
result-type))
(pushnew result-type nil-types :test (function equal)))))
(insert ");\n")
(pjb-objc-ide--insert-tag) (insert (format "return %s;\n" result-variable)))
(goto-char mend)))))
count)
(when nil-types
(message "Objective-C types without a formatter (see `*pjb-objc-ide--type-formatter-map*'): %S"
nil-types))))
(defun pjb-objc-ide-synthesize-properties ()
"Replace all @property <type> <identifier>; lines by @synthesize <identifier>; lines."
;; TODO: The right thing would be to search all properties in class
;; headers and superclasses headers, and to synthesize those
;; that need to be synthesized, depending of the @property
;; declaration and the presence of methods in the source
;; files...
(interactive)
(goto-char (point-min))
(while (re-search-forward "^[ \t]*@property\\>.*\\(\\<[_A-Za-z][_A-Za-z0-9]*\\)[ \t\n]*;" (point-max) t)
(let ((name (match-string 1)))
(delete-region (match-beginning 0) (match-end 0))
(insert (format "@synthesize %s;" name)))))
(defun pjb-objc-ide-beginning-of-class ()
(interactive)
(when (re-search-backward "@\\(\\(interface\\|implementation\\|prototype\\) +\\([A-Za-z0-9_]+\\)\\|end\\)" nil t)
(goto-char (match-beginning 0))))
(defun pjb-objc-ide-current-class ()
(save-excursion
(when (re-search-backward "@\\(\\(interface\\|implementation\\|prototype\\) +\\([A-Za-z0-9_]+\\)\\|end\\)" nil t)
(let ((token (match-string 2))
(name (match-string 3)))
(and token name)))))
(defun pjb-objc-ide-current-superclass ()
(save-window-excursion
(save-excursion
(when (re-search-backward "@\\(\\(\\(interface\\|implementation\\|prototype\\) +\\([A-Za-z0-9_]+\\)\\( *: *\\([A-Za-z0-9_]+\\)\\)?\\)\\|end\\)" nil t)
(let ((token (match-string 3))
(name (match-string 4))
(super (match-string 6)))
(when token
(or super
;; TODO: search all @interface/@implementation/@prototype
;; in the current file, in pjb-thi-other-file, and
;; in all the files in the same directory.
(progn
;; Quick and dirty:
(toggle-header/implementation)
(goto-char (point-min))
(when (re-search-forward (format "@interface +%s *: *\\([A-Za-z0-9_]+\\)" name) nil t)
(match-string 1))))))))))
(defun pjb-objc-ide-find-superclass-file ()
(interactive)
(let ((superclass (pjb-objc-ide-current-superclass)))
(when superclass
(message "superclass = %s" superclass)
(sources-find-file-named (format "%s.h" superclass))
(goto-char (point-min))
(re-search-forward (format "@interface +%s" superclass) nil t))))
(defun insert-braces (&optional arg)
"Enclose following ARG sexps in braces.
Leave point after open-paren.
A negative ARG encloses the preceding ARG sexps instead.
No argument is equivalent to zero: just insert `{}' and leave point between.
If `parens-require-spaces' is non-nil, this command also inserts a space
before and after, depending on the surrounding characters.
If region is active, insert enclosing characters at region boundaries.
This command assumes point is not in a string or comment."
(interactive "P")
(insert-pair arg ?\{ ?\}))
(defun pjb-ide-beginning-of-line-to-point ()
(buffer-substring (save-excursion
(beginning-of-line)
(point))
(point)))
(defun pjb-ide-tag-comment (tag)
(case tag
((1) "/*** PJB-DEBUG ***/")
((-1) "//*** PJB-DISABLED ***//")
(otherwise "/*** PJB ***/")))
(defun pjb-ide-insert-tag-comment (&optional tag)
(interactive "p")
(let ((tag (or tag 0)))
(unless (string= "" (cl:string-trim " \t" (pjb-ide-beginning-of-line-to-point)))
(insert "\n"))
(if (region-active-p)
(let ((start (region-beginning))
(end (region-end)))
(message "tag=%S start=%S end=%S" tag start end)
(goto-char start)
(with-marker (end end)
(while (< (point) end)
(insert (pjb-ide-tag-comment tag))
(beginning-of-line 2))))
(insert (pjb-ide-tag-comment tag)))))
(defun pjb-ide-insert-documentation-comment ()
(interactive)
(insert "
/**
*
*
*
*/
"))
(provide 'pjb-objc-ide)