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