'use strict';(function(g){"object"==typeof exports&&"object"==typeof module?g(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],g):g(CodeMirror)})(function(g){function p(a,b){this.cm=a;this.options=b;this.widget=null;this.tick=this.debounce=0;this.startPos=this.cm.getCursor("start");this.startLen=this.cm.getLine(this.startPos.line).length-this.cm.getSelection().length;var c=this;a.on("cursorActivity",this.activityFunc=function(){c.cursorActivity()})}
function A(a,b){function c(a,c){var e="string"!=typeof c?function(a){return c(a,b)}:d.hasOwnProperty(c)?d[c]:c;g[a]=e}var d={Up:function(){b.moveFocus(-1)},Down:function(){b.moveFocus(1)},PageUp:function(){b.moveFocus(-b.menuSize()+1,!0)},PageDown:function(){b.moveFocus(b.menuSize()-1,!0)},Home:function(){b.setFocus(0)},End:function(){b.setFocus(b.length-1)},Enter:b.pick,Tab:b.pick,Esc:b.close};/Mac/.test(navigator.platform)&&(d["Ctrl-P"]=function(){b.moveFocus(-1)},d["Ctrl-N"]=function(){b.moveFocus(1)});
var e=a.options.customKeys,g=e?{}:d;if(e)for(var f in e)e.hasOwnProperty(f)&&c(f,e[f]);if(a=a.options.extraKeys)for(f in a)a.hasOwnProperty(f)&&c(f,a[f]);return g}function y(a,b){for(;b&&b!=a;){if("LI"===b.nodeName.toUpperCase()&&b.parentNode==a)return b;b=b.parentNode}}function q(a,b){this.completion=a;this.data=b;this.picked=!1;var c=this,d=a.cm,e=d.getInputField().ownerDocument,n=e.defaultView||e.parentWindow,f=this.hints=e.createElement("ul");f.className="CodeMirror-hints "+a.cm.options.theme;
this.selectedHint=b.selectedHint||0;for(var v=b.list,k=0;k<v.length;++k){var m=f.appendChild(e.createElement("li")),h=v[k],r="CodeMirror-hint"+(k!=this.selectedHint?"":" CodeMirror-hint-active");null!=h.className&&(r=h.className+" "+r);m.className=r;h.render?h.render(m,b,h):m.appendChild(e.createTextNode(h.displayText||("string"==typeof h?h:h.text)));m.hintId=k}k=d.cursorCoords(a.options.alignWithWord?b.from:null);var w=k.left,x=k.bottom,p=!0;f.style.left=w+"px";f.style.top=x+"px";m=n.innerWidth||
Math.max(e.body.offsetWidth,e.documentElement.offsetWidth);var l=n.innerHeight||Math.max(e.body.offsetHeight,e.documentElement.offsetHeight);(a.options.container||e.body).appendChild(f);h=f.getBoundingClientRect();var t=h.bottom-l;r=f.scrollHeight>f.clientHeight+1;var q=d.getScrollInfo();0<t&&(t=h.bottom-h.top,0<k.top-(k.bottom-h.top)-t?(f.style.top=(x=k.top-t)+"px",p=!1):t>l&&(f.style.height=l-5+"px",f.style.top=(x=k.bottom-h.top)+"px",l=d.getCursor(),b.from.ch!=l.ch&&(k=d.cursorCoords(l),f.style.left=
(w=k.left)+"px",h=f.getBoundingClientRect())));l=h.right-m;0<l&&(h.right-h.left>m&&(f.style.width=m-5+"px",l-=h.right-h.left-m),f.style.left=(w=k.left-l)+"px");if(r)for(k=f.firstChild;k;k=k.nextSibling)k.style.paddingRight=d.display.nativeBarWidth+"px";d.addKeyMap(this.keyMap=A(a,{moveFocus:function(a,b){c.changeActive(c.selectedHint+a,b)},setFocus:function(a){c.changeActive(a)},menuSize:function(){return c.screenAmount()},length:v.length,close:function(){a.close()},pick:function(){c.pick()},data:b}));
if(a.options.closeOnUnfocus){var u;d.on("blur",this.onBlur=function(){u=setTimeout(function(){a.close()},100)});d.on("focus",this.onFocus=function(){clearTimeout(u)})}d.on("scroll",this.onScroll=function(){var b=d.getScrollInfo(),c=d.getWrapperElement().getBoundingClientRect(),g=x+q.top-b.top,h=g-(n.pageYOffset||(e.documentElement||e.body).scrollTop);p||(h+=f.offsetHeight);if(h<=c.top||h>=c.bottom)return a.close();f.style.top=g+"px";f.style.left=w+q.left-b.left+"px"});g.on(f,"dblclick",function(a){(a=
y(f,a.target||a.srcElement))&&null!=a.hintId&&(c.changeActive(a.hintId),c.pick())});g.on(f,"click",function(b){(b=y(f,b.target||b.srcElement))&&null!=b.hintId&&(c.changeActive(b.hintId),a.options.completeOnSingleClick&&c.pick())});g.on(f,"mousedown",function(){setTimeout(function(){d.focus()},20)});g.signal(b,"select",v[this.selectedHint],f.childNodes[this.selectedHint]);return!0}function B(a,b){if(!a.somethingSelected())return b;a=[];for(var c=0;c<b.length;c++)b[c].supportsSelection&&a.push(b[c]);
return a}function u(a,b,c,d){a.async?a(b,d,c):(a=a(b,c))&&a.then?a.then(d):d(a)}g.showHint=function(a,b,c){if(!b)return a.showHint(c);c&&c.async&&(b.async=!0);b={hint:b};if(c)for(var d in c)b[d]=c[d];return a.showHint(b)};g.defineExtension("showHint",function(a){var b=this.getCursor("start"),c=this.options.hintOptions,d={},e;for(e in z)d[e]=z[e];if(c)for(e in c)void 0!==c[e]&&(d[e]=c[e]);if(a)for(e in a)void 0!==a[e]&&(d[e]=a[e]);d.hint.resolve&&(d.hint=d.hint.resolve(this,b));a=d;b=this.listSelections();
if(!(1<b.length)){if(this.somethingSelected()){if(!a.hint.supportsSelection)return;for(e=0;e<b.length;e++)if(b[e].head.line!=b[e].anchor.line)return}this.state.completionActive&&this.state.completionActive.close();b=this.state.completionActive=new p(this,a);b.options.hint&&(g.signal(this,"startCompletion",this),b.update(!0))}});g.defineExtension("closeHint",function(){this.state.completionActive&&this.state.completionActive.close()});var C=window.requestAnimationFrame||function(a){return setTimeout(a,
1E3/60)},D=window.cancelAnimationFrame||clearTimeout;p.prototype={close:function(){this.active()&&(this.tick=this.cm.state.completionActive=null,this.cm.off("cursorActivity",this.activityFunc),this.widget&&this.data&&g.signal(this.data,"close"),this.widget&&this.widget.close(),g.signal(this.cm,"endCompletion",this.cm))},active:function(){return this.cm.state.completionActive==this},pick:function(a,b){b=a.list[b];b.hint?b.hint(this.cm,a,b):this.cm.replaceRange("string"==typeof b?b:b.text,b.from||a.from,
b.to||a.to,"complete");g.signal(a,"pick",b);this.close()},cursorActivity:function(){this.debounce&&(D(this.debounce),this.debounce=0);var a=this.cm.getCursor(),b=this.cm.getLine(a.line);if(a.line!=this.startPos.line||b.length-a.ch!=this.startLen-this.startPos.ch||a.ch<this.startPos.ch||this.cm.somethingSelected()||!a.ch||this.options.closeCharacters.test(b.charAt(a.ch-1)))this.close();else{var c=this;this.debounce=C(function(){c.update()});this.widget&&this.widget.disable()}},update:function(a){if(null!=
this.tick){var b=this,c=++this.tick;u(this.options.hint,this.cm,this.options,function(d){b.tick==c&&b.finishUpdate(d,a)})}},finishUpdate:function(a,b){this.data&&g.signal(this.data,"update");b=this.widget&&this.widget.picked||b&&this.options.completeSingle;this.widget&&this.widget.close();(this.data=a)&&a.list.length&&(b&&1==a.list.length?this.pick(a,0):(this.widget=new q(this,a),g.signal(a,"shown")))}};q.prototype={close:function(){if(this.completion.widget==this){this.completion.widget=null;this.hints.parentNode.removeChild(this.hints);
this.completion.cm.removeKeyMap(this.keyMap);var a=this.completion.cm;this.completion.options.closeOnUnfocus&&(a.off("blur",this.onBlur),a.off("focus",this.onFocus));a.off("scroll",this.onScroll)}},disable:function(){this.completion.cm.removeKeyMap(this.keyMap);var a=this;this.keyMap={Enter:function(){a.picked=!0}};this.completion.cm.addKeyMap(this.keyMap)},pick:function(){this.completion.pick(this.data,this.selectedHint)},changeActive:function(a,b){a>=this.data.list.length?a=b?this.data.list.length-
1:0:0>a&&(a=b?0:this.data.list.length-1);if(this.selectedHint!=a){if(b=this.hints.childNodes[this.selectedHint])b.className=b.className.replace(" CodeMirror-hint-active","");b=this.hints.childNodes[this.selectedHint=a];b.className+=" CodeMirror-hint-active";b.offsetTop<this.hints.scrollTop?this.hints.scrollTop=b.offsetTop-3:b.offsetTop+b.offsetHeight>this.hints.scrollTop+this.hints.clientHeight&&(this.hints.scrollTop=b.offsetTop+b.offsetHeight-this.hints.clientHeight+3);g.signal(this.data,"select",
this.data.list[this.selectedHint],b)}},screenAmount:function(){return Math.floor(this.hints.clientHeight/this.hints.firstChild.offsetHeight)||1}};g.registerHelper("hint","auto",{resolve:function(a,b){var c=a.getHelpers(b,"hint"),d;return c.length?(a=function(a,b,d){function e(c){if(c==f.length)return b(null);u(f[c],a,d,function(a){a&&0<a.list.length?b(a):e(c+1)})}var f=B(a,c);e(0)},a.async=!0,a.supportsSelection=!0,a):(d=a.getHelper(a.getCursor(),"hintWords"))?function(a){return g.hint.fromList(a,
{words:d})}:g.hint.anyword?function(a,b){return g.hint.anyword(a,b)}:function(){}}});g.registerHelper("hint","fromList",function(a,b){var c=a.getCursor(),d=a.getTokenAt(c);a=g.Pos(c.line,d.start);d.start<c.ch&&/\w/.test(d.string.charAt(c.ch-d.start-1))?d=d.string.substr(0,c.ch-d.start):(d="",a=c);for(var e=[],n=0;n<b.words.length;n++){var f=b.words[n];f.slice(0,d.length)==d&&e.push(f)}if(e.length)return{list:e,from:a,to:c}});g.commands.autocomplete=g.showHint;var z={hint:g.hint.auto,completeSingle:!0,
alignWithWord:!0,closeCharacters:/[\s()\[\]{};:>,]/,closeOnUnfocus:!0,completeOnSingleClick:!0,container:null,customKeys:null,extraKeys:null};g.defineOption("hintOptions",null)});
