blob: 89701dd21152366bd6e76f50c906412f3f8286c6 (
plain)
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
|
(ns swank.commands.contrib.swank-c-p-c.internal
(:use (swank util core commands)
(swank.commands completion)
(swank.util string clojure)))
(defn compound-prefix-match?
"Takes a `prefix' and a `target' string and returns whether `prefix'
is a compound-prefix of `target'.
Viewing each of `prefix' and `target' as a series of substrings
split by `split', if each substring of `prefix' is a prefix of the
corresponding substring in `target' then we call `prefix' a
compound-prefix of `target'."
([split #^String prefix #^String target]
(let [prefixes (split prefix)
targets (split target)]
(when (<= (count prefixes) (count targets))
(every? true? (map #(.startsWith #^String %1 %2) targets prefixes))))))
(defn unacronym
"Interposes delimiter between each character of string."
([delimiter #^String string]
(apply str (interpose delimiter string)))
{:tag String})
(defn delimited-compound-prefix-match?
"Uses a delimiter as the `split' for a compound prefix match check.
See also: `compound-prefix-match?'"
([delimiter prefix target]
(compound-prefix-match? #(.split #^String % (str "[" (java.util.regex.Pattern/quote delimiter) "]") -1)
prefix
target)))
(defn delimited-compound-prefix-match-acronym?
([delimiter prefix target]
(or (delimited-compound-prefix-match? delimiter prefix target)
(delimited-compound-prefix-match? delimiter (unacronym (first delimiter) prefix) target))))
(defn camel-compound-prefix-match?
"Uses camel case as a delimiter for a compound prefix match check.
See also: `compound-prefix-match?'"
([#^String prefix #^String target]
(compound-prefix-match? #(re-seq #"(?:^.|[A-Z])[^A-Z]*" %)
prefix
target)))
(defn split-compound-prefix-match? [#^String symbol-string #^String potential]
(if (.startsWith symbol-string ".")
(and (.startsWith potential ".")
(camel-compound-prefix-match? symbol-string potential))
(let [[sym-ns sym-name] (symbol-name-parts symbol-string)
[pot-ns pot-name] (symbol-name-parts potential)]
(and (or (= sym-ns pot-ns)
(and sym-ns pot-ns
(delimited-compound-prefix-match-acronym? "." sym-ns pot-ns)))
(or (delimited-compound-prefix-match-acronym? "-." sym-name pot-name)
(camel-compound-prefix-match? sym-name pot-name))))))
|