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