Skip to content

Commit 9d489ac

Browse files
srguiwizmarijnh
authored andcommitted
[xml-hint addon] Optionally match in middle
For example, "happy" to match "very-happy", "not-so-happy", "happy".
1 parent da99fdf commit 9d489ac

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

addon/hint/xml-hint.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313

1414
var Pos = CodeMirror.Pos;
1515

16+
function matches(hint, typed, matchInMiddle) {
17+
if (matchInMiddle) return hint.indexOf(typed) >= 0;
18+
else return hint.lastIndexOf(typed, 0) == 0;
19+
}
20+
1621
function getHints(cm, options) {
1722
var tags = options && options.schemaInfo;
1823
var quote = (options && options.quoteChar) || '"';
24+
var matchInMiddle = options && options.matchInMiddle;
1925
if (!tags) return;
2026
var cur = cm.getCursor(), token = cm.getTokenAt(cur);
2127
if (token.end > cur.ch) {
@@ -45,14 +51,14 @@
4551
var cx = inner.state.context, curTag = cx && tags[cx.tagName];
4652
var childList = cx ? curTag && curTag.children : tags["!top"];
4753
if (childList && tagType != "close") {
48-
for (var i = 0; i < childList.length; ++i) if (!prefix || childList[i].lastIndexOf(prefix, 0) == 0)
54+
for (var i = 0; i < childList.length; ++i) if (!prefix || matches(childList[i], prefix, matchInMiddle))
4955
result.push("<" + childList[i]);
5056
} else if (tagType != "close") {
5157
for (var name in tags)
52-
if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || name.lastIndexOf(prefix, 0) == 0))
58+
if (tags.hasOwnProperty(name) && name != "!top" && name != "!attrs" && (!prefix || matches(name, prefix, matchInMiddle)))
5359
result.push("<" + name);
5460
}
55-
if (cx && (!prefix || tagType == "close" && cx.tagName.lastIndexOf(prefix, 0) == 0))
61+
if (cx && (!prefix || tagType == "close" && matches(cx.tagName, prefix, matchInMiddle)))
5662
result.push("</" + cx.tagName + ">");
5763
} else {
5864
// Attribute completion
@@ -88,14 +94,14 @@
8894
}
8995
replaceToken = true;
9096
}
91-
for (var i = 0; i < atValues.length; ++i) if (!prefix || atValues[i].lastIndexOf(prefix, 0) == 0)
97+
for (var i = 0; i < atValues.length; ++i) if (!prefix || matches(atValues[i], prefix, matchInMiddle))
9298
result.push(quote + atValues[i] + quote);
9399
} else { // An attribute name
94100
if (token.type == "attribute") {
95101
prefix = token.string;
96102
replaceToken = true;
97103
}
98-
for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || attr.lastIndexOf(prefix, 0) == 0))
104+
for (var attr in attrs) if (attrs.hasOwnProperty(attr) && (!prefix || matches(attr, prefix, matchInMiddle)))
99105
result.push(attr);
100106
}
101107
}

doc/manual.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,8 +2812,13 @@ <h2 id="addons">Addons</h2>
28122812
and <code>attrs</code> (an object mapping attribute names
28132813
to <code>null</code> for free-form attributes, and an array of
28142814
valid values for restricted
2815-
attributes). <a href="../demo/xmlcomplete.html">Demo
2816-
here.</a></dd>
2815+
attributes).<br>The hint options accept an additional property:
2816+
<dl>
2817+
<dt><code><strong>matchInMiddle</strong>: boolean</code></dt>
2818+
<dd>Determines whether typed characters are matched anywhere in
2819+
completions, not just at the beginning. Defaults to false.</dd>
2820+
</dl>
2821+
<a href="../demo/xmlcomplete.html">Demo here</a>.</dd>
28172822

28182823
<dt id="addon_html-hint"><a href="../addon/hint/html-hint.js"><code>hint/html-hint.js</code></a></dt>
28192824
<dd>Provides schema info to

0 commit comments

Comments
 (0)