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