!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 n(e){var n=e.search(l);return-1==n?0:n}function t(e,n){var t=e.getMode();return!1!==t.useInnerComments&&t.innerMode?e.getModeAt(n):t}var i={},l=/[^\s\u00a0]/,o=e.Pos;e.commands.toggleComment=function(e){e.toggleComment()},e.defineExtension("toggleComment",function(e){e||(e=i);for(var n=1/0,t=this.listSelections(),l=null,r=t.length-1;r>=0;r--){var a=t[r].from(),m=t[r].to();a.line>=n||(m.line>=n&&(m=o(n,0)),n=a.line,null==l?this.uncomment(a,m,e)?l="un":(this.lineComment(a,m,e),l="line"):"un"==l?this.uncomment(a,m,e):this.lineComment(a,m,e))}}),e.defineExtension("lineComment",function(e,r,a){a||(a=i);var m=this,c=t(m,e),f=m.getLine(e.line);if(null!=f&&!function(e,n,t){return/\bstring\b/.test(e.getTokenTypeAt(o(n.line,0)))&&!/^[\'\"\`]/.test(t)}(m,e,f)){var g=a.lineComment||c.lineComment;if(g){var s=Math.min(0!=r.ch||r.line==e.line?r.line+1:r.line,m.lastLine()+1),d=null==a.padding?" ":a.padding,u=a.commentBlankLines||e.line==r.line;m.operation(function(){if(a.indent){for(var t=null,i=e.line;i<s;++i){var r=(c=m.getLine(i)).slice(0,n(c));(null==t||t.length>r.length)&&(t=r)}for(i=e.line;i<s;++i){var c=m.getLine(i),f=t.length;(u||l.test(c))&&(c.slice(0,f)!=t&&(f=n(c)),m.replaceRange(t+g+d,o(i,0),o(i,f)))}}else for(i=e.line;i<s;++i)(u||l.test(m.getLine(i)))&&m.replaceRange(g+d,o(i,0))})}else(a.blockCommentStart||c.blockCommentStart)&&(a.fullLines=!0,m.blockComment(e,r,a))}}),e.defineExtension("blockComment",function(e,n,r){r||(r=i);var a=this,m=t(a,e),c=r.blockCommentStart||m.blockCommentStart,f=r.blockCommentEnd||m.blockCommentEnd;if(c&&f){if(!/\bcomment\b/.test(a.getTokenTypeAt(o(e.line,0)))){var g=Math.min(n.line,a.lastLine());g!=e.line&&0==n.ch&&l.test(a.getLine(g))&&--g;var s=null==r.padding?" ":r.padding;e.line>g||a.operation(function(){if(0!=r.fullLines){var t=l.test(a.getLine(g));a.replaceRange(s+f,o(g)),a.replaceRange(c+s,o(e.line,0));var i=r.blockCommentLead||m.blockCommentLead;if(null!=i)for(var d=e.line+1;d<=g;++d)(d!=g||t)&&a.replaceRange(i+s,o(d,0))}else a.replaceRange(f,n),a.replaceRange(c,e)})}}else(r.lineComment||m.lineComment)&&0!=r.fullLines&&a.lineComment(e,n,r)}),e.defineExtension("uncomment",function(e,n,r){r||(r=i);var a,m=this,c=t(m,e),f=Math.min(0!=n.ch||n.line==e.line?n.line:n.line-1,m.lastLine()),g=Math.min(e.line,f),s=r.lineComment||c.lineComment,d=[],u=null==r.padding?" ":r.padding;e:if(s){for(var h=g;h<=f;++h){var p=m.getLine(h),v=p.indexOf(s);if(v>-1&&!/comment/.test(m.getTokenTypeAt(o(h,v+1)))&&(v=-1),-1==v&&l.test(p))break e;if(v>-1&&l.test(p.slice(0,v)))break e;d.push(p)}if(m.operation(function(){for(var e=g;e<=f;++e){var n=d[e-g],t=n.indexOf(s),i=t+s.length;t<0||(n.slice(i,i+u.length)==u&&(i+=u.length),a=!0,m.replaceRange("",o(e,t),o(e,i)))}}),a)return!0}var C=r.blockCommentStart||c.blockCommentStart,b=r.blockCommentEnd||c.blockCommentEnd;if(!C||!b)return!1;var k=r.blockCommentLead||c.blockCommentLead,L=m.getLine(g),x=L.indexOf(C);if(-1==x)return!1;var R=f==g?L:m.getLine(f),O=R.indexOf(b,f==g?x+C.length:0),T=o(g,x+1),y=o(f,O+1);if(-1==O||!/comment/.test(m.getTokenTypeAt(T))||!/comment/.test(m.getTokenTypeAt(y))||m.getRange(T,y,"\n").indexOf(b)>-1)return!1;var E=L.lastIndexOf(C,e.ch),M=-1==E?-1:L.slice(0,e.ch).indexOf(b,E+C.length);if(-1!=E&&-1!=M&&M+b.length!=e.ch)return!1;M=R.indexOf(b,n.ch);var S=R.slice(n.ch).lastIndexOf(C,M-n.ch);return E=-1==M||-1==S?-1:n.ch+S,(-1==M||-1==E||E==n.ch)&&(m.operation(function(){m.replaceRange("",o(f,O-(u&&R.slice(O-u.length,O)==u?u.length:0)),o(f,O+b.length));var e=x+C.length;if(u&&L.slice(e,e+u.length)==u&&(e+=u.length),m.replaceRange("",o(g,x),o(g,e)),k)for(var n=g+1;n<=f;++n){var t=m.getLine(n),i=t.indexOf(k);if(-1!=i&&!l.test(t.slice(0,i))){var r=i+k.length;u&&t.slice(r,r+u.length)==u&&(r+=u.length),m.replaceRange("",o(n,i),o(n,r))}}}),!0)})});