3030; ;; Code:
3131
3232(require 'swift-mode-lexer )
33+ (require 'seq )
3334
3435;;;### autoload
3536(defgroup swift-mode:imenu nil
@@ -102,7 +103,8 @@ names."
102103 " Scan declarations from current point.
103104
104105Return found declarations in reverse order."
105- (let (next-token
106+ (let (previous-token
107+ next-token
106108 next-type
107109 next-text
108110 name-token
@@ -178,21 +180,35 @@ Return found declarations in reverse order."
178180 (swift-mode:declaration (intern next-text) name-token nil )
179181 declarations)))
180182
181- ((equal next-text " func" )
183+ ((member next-text '( " func" " init " " subscript " ) )
182184 (setq last-class-token nil )
183- (setq name-token
184- (swift-mode:forward-token-or-list-except-curly-bracket))
185- ; ; TODO parse parameter names?
186- (when (memq (swift-mode:token:type name-token) '(identifier operator))
187- (push
188- (swift-mode:declaration 'func name-token nil )
189- declarations)))
185+ (unless (equal next-text " func" )
186+ (goto-char (swift-mode:token:start next-token)))
187+ (let ((declaration-type (intern next-text))
188+ (names (swift-mode:scan-function-name-and-parameter-names)))
189+ (when names
190+ (setq name-token (car names))
191+ (push
192+ (swift-mode:declaration
193+ declaration-type
194+ (swift-mode:token
195+ (swift-mode:token:type name-token)
196+ (concat (swift-mode:token:text name-token)
197+ " ("
198+ (mapconcat
199+ (lambda (token )
200+ (concat (swift-mode:token:text token) " :" ))
201+ (cdr names)
202+ " " )
203+ " )" )
204+ (swift-mode:token:start name-token)
205+ (swift-mode:token:end name-token))
206+ nil )
207+ declarations))))
190208
191- ((member next-text '( " init " " deinit" " subscript " ) )
209+ ((equal next-text " deinit" )
192210 (setq last-class-token nil )
193- (push
194- (swift-mode:declaration (intern next-text) next-token nil )
195- declarations))
211+ (push (swift-mode:declaration 'deinit next-token nil ) declarations))
196212
197213 ((member next-text '(" let" " var" ))
198214 (setq last-class-token nil )
@@ -208,7 +224,7 @@ Return found declarations in reverse order."
208224 (when (equal (swift-mode:token:text next-token) " operator" )
209225 (setq name-token
210226 (swift-mode:forward-token-or-list-except-curly-bracket))
211- (when (eq (swift-mode:token:type name-token) 'operator )
227+ (when (eq (swift-mode:token:type name-token) 'identifier )
212228 (push
213229 (swift-mode:declaration 'operator name-token nil )
214230 declarations))))
@@ -297,6 +313,51 @@ TYPE is one of `case', `let', or `var'."
297313 (goto-char (swift-mode:token:start next-token)))
298314 items))
299315
316+ (defun swift-mode:scan-function-name-and-parameter-names ()
317+ (let* ((name-token
318+ (swift-mode:forward-token-or-list-except-curly-bracket))
319+ next-token
320+ parameter-end
321+ (parameter-names '())
322+ (is-operator (seq-contains " /=-+!*%<>&|^~?."
323+ (elt (swift-mode:token:text name-token) 0 ))))
324+ (cond
325+ ((eq (swift-mode:token:type name-token) 'identifier )
326+ (while (progn
327+ (setq next-token
328+ (swift-mode:forward-token-or-list-except-curly-bracket))
329+ (not (memq (swift-mode:token:type next-token)
330+ '(\(\) \( { \; implicit-\; outside-of-buffer)))))
331+ (if (eq (swift-mode:token:type next-token) '\(\) )
332+ (progn
333+ (setq parameter-end (swift-mode:token:end next-token))
334+ (goto-char (swift-mode:token:start next-token))
335+ (swift-mode:forward-token)
336+
337+ (while (< (point ) parameter-end)
338+ (setq next-token (swift-mode:forward-token))
339+
340+ (when (eq (swift-mode:token:type next-token) 'identifier )
341+ (when (or is-operator
342+ (and (equal (swift-mode:token:text name-token)
343+ " subscript" )
344+ (eq (swift-mode:token:type
345+ (swift-mode:forward-token-or-list))
346+ ': )))
347+ (setq next-token (swift-mode:token
348+ 'identifier
349+ " _"
350+ (swift-mode:token:start next-token)
351+ (swift-mode:token:end next-token))))
352+ (push next-token parameter-names))
353+
354+ (while (and (< (point ) parameter-end)
355+ (not (eq (swift-mode:token:type next-token) '\, )))
356+ (setq next-token (swift-mode:forward-token-or-list))))
357+ (cons name-token (reverse parameter-names)))
358+ (list name-token)))
359+ (t nil ))))
360+
300361(defun swift-mode:format-for-imenu:flat (declarations )
301362 " Convert list of DECLARATIONS to alist for `imenu--index-alist' .
302363
0 commit comments