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