'use strict';(function(h){"object"==typeof exports&&"object"==typeof module?h(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],h):h(CodeMirror)})(function(h){function C(a,c,b){var d=a.docs[c];d?b(z(a,d)):a.options.getFile?a.options.getFile(c,b):b(null)}function w(a,c,b){for(var d in a.docs){var e=a.docs[d];if(e.doc==c)return e}if(!b)for(b=0;;++b)if(d="[doc"+(b||"")+"]",!a.docs[d]){b=d;break}return a.addDoc(b,c)}function D(a,c){if("string"==typeof c)return a.docs[c];
c instanceof h&&(c=c.getDoc());if(c instanceof h.Doc)return w(a,c)}function N(a,c,b){var d=w(a,c),e=a.cachedArgHints;e&&e.doc==c&&0<=A(e.start,b.to)&&(a.cachedArgHints=null);e=d.changed;null==e&&(d.changed=e={from:b.from.line,to:b.from.line});var f=b.from.line+(b.text.length-1);b.from.line<e.to&&(e.to-=b.to.line-f);f>=e.to&&(e.to=f+1);e.from>b.from.line&&(e.from=b.from.line);c.lineCount()>E&&100<b.to-e.from&&setTimeout(function(){d.changed&&100<d.changed.to-d.changed.from&&F(a,d)},200)}function F(a,
c){a.server.request({files:[{type:"full",name:c.name,text:z(a,c)}]},function(a){a?window.console.error(a):c.changed=null})}function O(a,c,b){a.request(c,{type:"completions",types:!0,docs:!0,urls:!0},function(d,e){if(d)return v(a,c,d);d=[];var f="",g=e.start,p=e.end;'["'==c.getRange(r(g.line,g.ch-2),g)&&'"]'!=c.getRange(p,r(p.line,p.ch+2))&&(f='"]');for(var k=0;k<e.completions.length;++k){var l=e.completions[k],u=P(l.type);e.guess&&(u+=" "+n+"guess");d.push({text:l.name+f,displayText:l.displayName||
l.name,className:u,data:l})}e={from:g,to:p,list:d};var m=null;h.on(e,"close",function(){x(m)});h.on(e,"update",function(){x(m)});h.on(e,"select",function(c,b){x(m);if(c=a.options.completionTip?a.options.completionTip(c.data):c.data.doc)m=B(b.parentNode.getBoundingClientRect().right+window.pageXOffset,b.getBoundingClientRect().top+window.pageYOffset,c),m.className+=" "+n+"hint-doc"});b(e)})}function P(a){a="?"==a?"unknown":"number"==a||"string"==a||"bool"==a?a:/^fn\(/.test(a)?"fn":/^\[/.test(a)?"array":
"object";return n+"completion "+n+"completion-"+a}function G(a,c,b,d,e){a.request(c,d,function(b,d){if(b)return v(a,c,b);if(a.options.typeTip)b=a.options.typeTip(d);else if(b=t("span",null,t("strong",null,d.type||"not found")),d.doc&&b.appendChild(document.createTextNode(" \u2014 "+d.doc)),d.url){b.appendChild(document.createTextNode(" "));var g=b.appendChild(t("a",null,"[docs]"));g.href=d.url;g.target="_blank"}H(c,b,a);e&&e()},b)}function Q(a,c){y(a);if(!c.somethingSelected()){var b=c.getTokenAt(c.getCursor()).state;
b=h.innerMode(c.getMode(),b);if("javascript"==b.mode.name&&(b=b.state.lexical,"call"==b.info)){for(var d,e=b.pos||0,f=c.getOption("tabSize"),g=c.getCursor().line,p=Math.max(0,g-9),k=!1;g>=p;--g){for(var l=c.getLine(g),u=d=0;;){u=l.indexOf("\t",u);if(-1==u)break;d+=f-(u+d)%f-1;u+=1}d=b.column-d;if("("==l.charAt(d)){k=!0;break}}if(k){var m=r(g,d);if((b=a.cachedArgHints)&&b.doc==c.getDoc()&&0==A(m,b.start))return I(a,c,e);a.request(c,{type:"type",preferFunction:!0,end:m},function(b,d){!b&&d.type&&/^fn\(/.test(d.type)&&
(a.cachedArgHints={start:m,type:R(d.type),name:d.exprName||d.name||"fn",guess:d.guess,doc:c.getDoc()},I(a,c,e))})}}}}function I(a,c,b){y(a);var d=a.cachedArgHints,e=d.type;d=t("span",d.guess?n+"fhint-guess":null,t("span",n+"fname",d.name),"(");for(var f=0;f<e.args.length;++f){f&&d.appendChild(document.createTextNode(", "));var g=e.args[f];d.appendChild(t("span",n+"farg"+(f==b?" "+n+"farg-current":""),g.name||"?"));"?"!=g.type&&(d.appendChild(document.createTextNode(":\u00a0")),d.appendChild(t("span",
n+"type",g.type)))}d.appendChild(document.createTextNode(e.rettype?") ->\u00a0":")"));e.rettype&&d.appendChild(t("span",n+"type",e.rettype));b=c.cursorCoords(null,"page");var p=a.activeArgHints=B(b.right+1,b.bottom,d);setTimeout(function(){p.clear=J(c,function(){a.activeArgHints==p&&y(a)})},20)}function R(a){function c(c){for(var b=0,e=d;;){var f=a.charAt(d);if(c.test(f)&&!b)return a.slice(e,d);/[{\[\(]/.test(f)?++b:/[}\]\)]/.test(f)&&--b;++d}}var b=[],d=3;if(")"!=a.charAt(d))for(;;){var e=a.slice(d).match(/^([^, \(\[\{]+): /);
e&&(d+=e[0].length,e=e[1]);b.push({name:e,type:c(/[\),]/)});if(")"==a.charAt(d))break;d+=2}e=a.slice(d).match(/^\) -> (.*)$/);return{args:b,rettype:e&&e[1]}}function S(a,c){function b(b){b={type:"definition",variable:b||null};var d=w(a,c.getDoc());a.server.request(K(a,d,b),function(b,e){if(b)return v(a,c,b);if(!e.file&&e.url)window.open(e.url);else{if(e.file){b=a.docs[e.file];var f;if(f=b){var g=b.doc;var l=e.context.slice(0,e.contextOffset).split("\n");var h=e.start.line-(l.length-1);f=r(h,(1==l.length?
e.start.ch:g.getLine(h).length)-l[0].length);var m=g.getLine(h).slice(f.ch);for(h+=1;h<g.lineCount()&&m.length<e.context.length;++h)m+="\n"+g.getLine(h);if(m.slice(0,e.context.length)==e.context)var q=e;else{g=g.getSearchCursor(e.context,0,!1);for(m=Infinity;g.findNext();){h=g.from();var n=1E4*Math.abs(h.line-f.line);n||(n=Math.abs(h.ch-f.ch));n<m&&(q=h,m=n)}q?(1==l.length?q.ch+=l[0].length:q=r(q.line+(l.length-1),l[l.length-1].length),e=e.start.line==e.end.line?r(q.line,q.ch+(e.end.ch-e.start.ch)):
r(q.line+(e.end.line-e.start.line),e.end.ch),q={start:q,end:e}):q=null}f=l=q}if(f){a.jumpStack.push({file:d.name,start:c.getCursor("from"),end:c.getCursor("to")});L(a,d,b,l.start,l.end);return}}v(a,c,"Could not find a definition.")}})}T(c)?b():M(c,"Jump to variable",function(a){a&&b(a)})}function L(a,c,b,d,e){b.doc.setSelection(d,e);c!=b&&a.options.switchToDoc&&(y(a),a.options.switchToDoc(b.name,b.doc))}function T(a){var c=a.getCursor("end"),b=a.getTokenAt(c);return b.start<c.ch&&"comment"==b.type?
!1:/[\w)\]]/.test(a.getLine(c.line).slice(Math.max(c.ch-1,0),c.ch+1))}function U(a,c){var b=c.getTokenAt(c.getCursor());if(!/\w/.test(b.string))return v(a,c,"Not at a variable");M(c,"New name for "+b.string,function(b){a.request(c,{type:"rename",newName:b,fullDocs:!0},function(b,d){if(b)return v(a,c,b);V(a,d.changes)})})}function W(a,c){var b=w(a,c.doc).name;a.request(c,{type:"refs"},function(d,e){if(d)return v(a,c,d);d=[];for(var f=0,g=c.getCursor(),h=0;h<e.refs.length;h++){var k=e.refs[h];k.file==
b&&(d.push({anchor:k.start,head:k.end}),0<=A(g,k.start)&&0>=A(g,k.end)&&(f=d.length-1))}c.setSelections(d,f)})}function V(a,c){for(var b=Object.create(null),d=0;d<c.length;++d){var e=c[d];(b[e.file]||(b[e.file]=[])).push(e)}for(var f in b){c=a.docs[f];var g=b[f];if(c){g.sort(function(a,b){return A(b.start,a.start)});var h="*rename"+ ++X;for(d=0;d<g.length;++d)e=g[d],c.doc.replaceRange(e.text,e.start,e.end,h)}}}function K(a,c,b,d){var e=[],f;(f=!b.fullDocs)||delete b.fullDocs;"string"==typeof b&&(b=
{type:b});b.lineCharPositions=!0;null==b.end&&(b.end=d||c.doc.getCursor("end"),c.doc.somethingSelected()&&(b.start=c.doc.getCursor("start")));d=b.start||b.end;c.changed?c.doc.lineCount()>E&&!1!==f&&100>c.changed.to-c.changed.from&&c.changed.from<=d.line&&c.changed.to>b.end.line?(e.push(Y(c,d,b.end)),b.file="#0",f=e[0].offsetLines,null!=b.start&&(b.start=r(b.start.line- -f,b.start.ch)),b.end=r(b.end.line-f,b.end.ch)):(e.push({type:"full",name:c.name,text:z(a,c)}),b.file=c.name,c.changed=null):b.file=
c.name;for(var g in a.docs)f=a.docs[g],f.changed&&f!=c&&(e.push({type:"full",name:f.name,text:z(a,f)}),f.changed=null);return{query:b,files:e}}function Y(a,c,b){for(var d=a.doc,e=null,f=null,g=c.line-1,p=Math.max(0,g-50);g>=p;--g){var k=d.getLine(g);0>k.search(/\bfunction\b/)||(k=h.countColumn(k,null,4),null!=e&&e<=k||(e=k,f=g))}null==f&&(f=p);g=Math.min(d.lastLine(),b.line+20);if(null==e||e==h.countColumn(d.getLine(c.line),null,4))c=g;else for(c=b.line+1;c<g&&!(k=h.countColumn(d.getLine(c),null,
4),k<=e);++c);e=r(f,0);return{type:"part",name:a.name,offsetLines:e.line,text:d.getRange(e,r(c,b.line==c?null:0))}}function t(a,c){var b=document.createElement(a);c&&(b.className=c);for(var d=2;d<arguments.length;++d){var e=arguments[d];"string"==typeof e&&(e=document.createTextNode(e));b.appendChild(e)}return b}function M(a,c,b){a.openDialog?a.openDialog(c+": <input type=text>",b):b(prompt(c,""))}function H(a,c,b){function d(){a.state.ternTooltip=null;f.parentNode&&Z(f);k()}a.state.ternTooltip&&
x(a.state.ternTooltip);var e=a.cursorCoords(),f=a.state.ternTooltip=B(e.right+1,e.bottom,c),g=!1,p=!1;h.on(f,"mousemove",function(){g=!0});h.on(f,"mouseout",function(a){(a=a.relatedTarget||a.toElement)&&h.contains(f,a)||(p?d():g=!1)});setTimeout(function(){p=!0;g||d()},b.options.hintDelay?b.options.hintDelay:1700);var k=J(a,d)}function J(a,c){a.on("cursorActivity",c);a.on("blur",c);a.on("scroll",c);a.on("setDoc",c);return function(){a.off("cursorActivity",c);a.off("blur",c);a.off("scroll",c);a.off("setDoc",
c)}}function B(a,c,b){b=t("div",n+"tooltip",b);b.style.left=a+"px";b.style.top=c+"px";document.body.appendChild(b);return b}function x(a){var c=a&&a.parentNode;c&&c.removeChild(a)}function Z(a){a.style.opacity="0";setTimeout(function(){x(a)},1100)}function v(a,c,b){a.options.showError?a.options.showError(c,b):H(c,String(b),a)}function y(a){a.activeArgHints&&(a.activeArgHints.clear&&a.activeArgHints.clear(),x(a.activeArgHints),a.activeArgHints=null)}function z(a,c){var b=c.doc.getValue();a.options.fileFilter&&
(b=a.options.fileFilter(b,c.name,c.doc));return b}function aa(a){function c(a,c){c&&(a.id=++d,e[d]=c);b.postMessage(a)}var b=a.worker=new Worker(a.options.workerScript);b.postMessage({type:"init",defs:a.options.defs,plugins:a.options.plugins,scripts:a.options.workerDeps});var d=0,e={};b.onmessage=function(b){var d=b.data;"getFile"==d.type?C(a,d.name,function(a,b){c({type:"getFile",err:String(a),text:b,id:d.id})}):"debug"==d.type?window.console.log(d.message):d.id&&e[d.id]&&(e[d.id](d.err,d.body),
delete e[d.id])};b.onerror=function(a){for(var b in e)e[b](a);e={}};this.addFile=function(a,b){c({type:"add",name:a,text:b})};this.delFile=function(a){c({type:"del",name:a})};this.request=function(a,b){c({type:"req",body:a},b)}}h.TernServer=function(a){var c=this;this.options=a||{};a=this.options.plugins||(this.options.plugins={});a.doc_comment||(a.doc_comment=!0);this.docs=Object.create(null);this.server=this.options.useWorker?new aa(this):new tern.Server({getFile:function(a,d){return C(c,a,d)},
async:!0,defs:this.options.defs||[],plugins:a});this.trackChange=function(a,d){N(c,a,d)};this.activeArgHints=this.cachedArgHints=null;this.jumpStack=[];this.getHint=function(a,d){return O(c,a,d)};this.getHint.async=!0};h.TernServer.prototype={addDoc:function(a,c){var b={doc:c,name:a,changed:null};this.server.addFile(a,z(this,b));h.on(c,"change",this.trackChange);return this.docs[a]=b},delDoc:function(a){if(a=D(this,a))h.off(a.doc,"change",this.trackChange),delete this.docs[a.name],this.server.delFile(a.name)},
hideDoc:function(a){y(this);(a=D(this,a))&&a.changed&&F(this,a)},complete:function(a){a.showHint({hint:this.getHint})},showType:function(a,c,b){G(this,a,c,"type",b)},showDocs:function(a,c,b){G(this,a,c,"documentation",b)},updateArgHints:function(a){Q(this,a)},jumpToDef:function(a){S(this,a)},jumpBack:function(a){var c=this.jumpStack.pop(),b=c&&this.docs[c.file];b&&L(this,w(this,a.getDoc()),b,c.start,c.end)},rename:function(a){U(this,a)},selectName:function(a){W(this,a)},request:function(a,c,b,d){var e=
this,f=w(this,a.getDoc()),g=K(this,f,c,d);if(a=g.query&&this.options.queryOptions&&this.options.queryOptions[g.query.type])for(var h in a)g.query[h]=a[h];this.server.request(g,function(a,d){!a&&e.options.responseFilter&&(d=e.options.responseFilter(f,c,g,a,d));b(a,d)})},destroy:function(){y(this);this.worker&&(this.worker.terminate(),this.worker=null)}};var r=h.Pos,n="CodeMirror-Tern-",E=250,X=0,A=h.cmpPos});
