-
Notifications
You must be signed in to change notification settings - Fork 7
/
swift-helpful-regex.el
125 lines (108 loc) · 4.45 KB
/
swift-helpful-regex.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
;;; swift-helpful-regex.el --- A minor mode to show information about Swift things at point. -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Daniel Martín
;; This program 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.
;; 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 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Part of swift-helpful that contains regular expression logic.
;;; Code:
(require 's)
(require 'dash)
(defvar swift-helpful--where-clause-match nil
"Regular expression match of a `where' clause in a Swift generic declaration.")
(defun swift-helpful--generic-replacement (where-str)
"Convert `where' generic syntax in WHERE-STR to a format more appropriate for searching."
(s-join ", "
(-remove-first
(lambda (str)
(when
(string-match "\\(where \\)?\\([A-Z] : [a-z]+\\)"
(s-trim str))
(setq swift-helpful--where-clause-match (match-string 2 str))))
(-map
's-trim
(split-string where-str ",")))))
(defun swift-helpful--adapt-for-generics (signature)
"Replace a generic Swift SIGNATURE so that \"where T : StringProtocol\" becomes <T: StringProtocol>."
(setq swift-helpful--where-clause-match "")
(let ((str (replace-regexp-in-string
"\\(where .*\\)"
(lambda (match)
(save-match-data
(swift-helpful--generic-replacement match)))
signature)))
(if (not (string= "" swift-helpful--where-clause-match))
(s-trim
(replace-regexp-in-string
"<\\([^>]*\\)>"
swift-helpful--where-clause-match
str
t
nil
1))
(s-trim str))))
(defun swift-helpful--prepend-public-keywords (signature)
"Prepend Swift keywords like `public', `func', `var' in SIGNATURE."
(let ((case-fold-search nil))
(replace-regexp-in-string
(format "%s%s\\)"
"\\(__.+\\|mutating \\)?\\("
(regexp-opt
'("func" "let" "var" "class" "struct" "protocol" "extension")))
"public \\1\\2"
signature)))
(defun swift-helpful--prepare-regex-for-sequence-api (signature)
"Replace \"Element\" with the actual type element in SIGNATURE."
(if (string-match
"\\[\\([a-z]\\{2,\\}\\)\\]"
signature)
(let ((type (match-string 1 signature)))
(replace-regexp-in-string
type
"Element"
signature))
signature))
(defun swift-helpful--remove-non-interesting-syntax (signature)
"Remove SIGNATURE parts that are not specially useful when grepping the standard library code."
(s-trim (replace-regexp-in-string
"Self."
""
(replace-regexp-in-string
"{ \\(get\\)?\\(set\\)? }"
""
(replace-regexp-in-string
"KeyedEncodingContainer<K>."
""
(replace-regexp-in-string
"KeyedDecodingContainer<K>."
""
signature))))))
(defun swift-helpful--prepare-type-signature-for-grep (signature)
"Perform a series of transformations on SIGNATURE for grepping the standard library code."
(swift-helpful--adapt-for-generics
(swift-helpful--prepend-public-keywords
(swift-helpful--prepare-regex-for-sequence-api
(swift-helpful--remove-non-interesting-syntax
signature)))))
(defun swift-helpful--regex-new-lines-escape-chars (str escape-chars)
"Build a regex sequence flexibly matching STR and escaping some ESCAPE-CHARS.
Insert a regex between each char that matches a few
non-interesting Swift attributes and new line/whitespace
characters (we need to do this because the signature prototype
may be formatted in multiple lines in the standard library
code)."
(mapconcat (lambda (str)
(if (member str escape-chars)
(format "\\%s" str)
str))
(mapcar #'char-to-string str)
"[\\n \\(__owned\\|@inline(__always)\\|@discardableResult\\|__consuming\\)]*"))
(provide 'swift-helpful-regex)
;;; swift-helpful-regex.el ends here