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