forked from rksm/paredit.js
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paredit-bundle.min.js
1 lines (1 loc) · 17.7 KB
/
paredit-bundle.min.js
1
"format global";!function(){var e="undefined"!=typeof module&&module.exports,n=e?module.exports:window.paredit={};e&&(n.reader=require("./lib/reader").reader,n.navigator=require("./lib/navigator").navigator,n.walk=require("./lib/navigator").walk,n.editor=require("./lib/editor").editor,n.specialForms=require("./lib/editor").specialForms),n.parse=function(e,t){t=t||{};var r=!t.hasOwnProperty("addSourceForLeafs")||t.addSourceForLeafs,i=[],o=n.reader.readSeq(e,function(n,t,o,u,l){var a={type:n,start:o.idx,end:u.idx};return"error"===n?(a.error=t.error,t.children&&(a.children=t.children),i.push(a)):r&&"list"!==n&&(a.source=e.slice(a.start,a.end)),"list"===n&&(a.children=t),("list"===n||"string"===n||"error"===n&&l)&&(a.open=l.open,a.close=l.close),a});return{type:"toplevel",start:0,end:o&&o.length&&o[o.length-1].end||0,errors:i,children:o}}}(),function(e){var n="undefined"!=typeof module&&module.exports,t=n?module.exports:window.paredit;!function(e){var n=e.util={merge:function(e){return arguments.length>1?n.merge(Array.prototype.slice.call(arguments)):Array.isArray(e[0])?Array.prototype.concat.apply([],e):e.reduce(function(e,n){for(var t in n)n.hasOwnProperty(t)&&(e[t]=n[t]);return e},{})},mapTree:function(e,t,r){var i=(r(e)||[]).map(function(e){return n.mapTree(e,t,r)});return t(e,i)},flatFilterTree:function(e,t,r){var i=[];return t(e)&&i.push(e),i.concat((r(e)||[]).reduce(function(e,i){return e.concat(n.flatFilterTree(i,t,r))},[]))},last:function(e){return e[e.length-1]},times:function(e,n){return new Array(e+1).join(n)},clone:function(e){if(Array.isArray(e))return Array.prototype.slice.call(e);var n={};for(var t in e)n[t]=e[t];return n}}}(t)}(),function(e){var n="undefined"!=typeof module&&module.exports,t=n?module.exports:window.paredit;!function(e){function n(e){w=e,y=Object.keys(w),S=y.map(function(e){return w[e]}),A=new RegExp("[^\\s"+Object.entries(w).map(function(e){return"\\"+e[0]+"\\"+e[1]}).join("")+',"\\\\`@^#~]')}function t(e,n,t,p,h){var v=n[0];if(!v&&e&&w[e])return{input:n,context:t,pos:p,flag:I};if(!v&&!w[e])return{input:n,context:t,pos:p,flag:I};if(/\s|,/.test(v))return{input:n.slice(1),context:t,pos:m(p,v)};if(C.test(v))return c(n,t,p,h);if(";"===v)return a(n,t,p,h);if('"'===v)return i(n,t,p,h);if("\\"===v)return o(n,t,p,h);if(/[0-9]/.test(v))return l(n,t,p,h);if("-"===v&&(/[0-9]/.test(n[1])||"."==n[1]&&/[0-9]/.test(n[2])))return l(n,t,p,h);if("."===v&&/[0-9]/.test(n[1]))return l(n,t,p,h);if(A.test(v))return u(n,t,p,h);if(S.indexOf(v)>-1){if(!e){var x=s(n,t,p,h);return{input:x.input,context:x.context,pos:x.pos}}return{input:n,context:t,pos:p,flag:b}}if(y.indexOf(v)>-1){var k,O,F=g(p),q=r(v,n.slice(1),Object.freeze([]),m(p,v),h),T=q.input[0],j={open:v,close:w[v]};if(T!==w[v]){var E=g(q.pos),R="Expected '"+w[v]+"'"+(T?" but got '"+T+"'":" but reached end of input"),L=q.context,z=d(R,F,E,L);k=f(h,"error",z,F,E,j),O=T?m(q.pos,T):q.pos}else O=T?m(q.pos,T):q.pos,k=f(h,"list",q.context,F,O,j);t=t.concat([k]);return{input:q.input.slice(T?1:0),context:t,pos:O}}var F=g(p),E=m(p,v),z=d("Unexpected character: "+v,F,E,null);return z=f(h,"error",z,F,E),t=t.concat([z]),{input:n.slice(1),context:t,pos:E}}function r(e,n,r,i,o){for(var u;;){var l=i.row,a=i.column;u=t(e,n,r,i,o),n=u.input,r=u.context,i=u.pos;var c=u.flag===I||u.flag===b&&(e||!n.length);if(!c&&i.row<=l&&i.column<=a)throw new Error("paredit reader cannot go forward at "+v(i)+" with input "+n);if(c)break}return{input:n,context:r,pos:i}}function i(e,n,t,r){var i=!1,o=g(t),u=e[0];return t=m(t,e[0]),e=e.slice(1),p(e,t,function(e){return!(!i&&'"'===e)&&(i?i=!1:"\\"===e&&(i=!0),!0)},function(e,t,i,l){var a;if('"'==t[0])u=u+e+t[0],l=m(l,t[0]),t=t.slice(1),a=f(r,"string",u,o,l,{open:'"',close:'"'});else{var c=d("Expected '\"' but reached end of input",o,l,null);a=f(r,"error",c,i,l)}return n=n.concat([a]),{pos:l,input:t,context:n}})}function o(e,n,t,r){var i=g(t),o=e.slice(0,2),u=m(t,o),l=f(r,"char",o,i,u),a=e.slice(2);return n=n.concat([l]),{pos:u,input:a,context:n}}function u(e,n,t,r){return p(e,t,function(e){return A.test(e)},function(e,t,i,o){var u=f(r,"symbol",e,i,o);return n=n.concat([u]),{pos:o,input:t,context:n}})}function l(e,n,t,r){var i=!0,o=!1;return p(e,t,function(e){return!(!i||(i=!1,"-"!==e))||(o&&"."===e?(o=!1,!0):/[0-9.]/.test(e))},function(e,t,i,o){var u=f(r,"number",Number(e),i,o);return n=n.concat([u]),{pos:o,input:t,context:n}})}function a(e,n,t,r){for(var i=g(t),o="",u=e;u.length&&/^\s*;/.test(u);){var l=x(u);o+=l[0],u=l[1]}var a=m(t,o),c=f(r,"comment",o,i,a);return n=n.concat([c]),{pos:a,input:u,context:n}}function c(e,n,t,r){var i=g(t),o=e.slice(0,1),u=m(t,o),l=f(r,"special",o,i,u),a=e.slice(1);return n=n.concat([l]),{pos:u,input:a,context:n}}function s(e,n,t,r){return p(e,t,function(e){return S.indexOf(e)>-1},function(e,t,i,o){var u=d("Unexpected input: '"+e+"'",i,o,null),l=f(r,"error",u,i,o);return n=n.concat([l]),{pos:o,input:t,context:n}})}function d(e,n,t,r){return{error:e+" at line "+(t.row+1)+" column "+t.column,start:g(n),end:g(t),children:r}}function f(e,n,t,r,i,o){return e?e(n,t,g(r),g(i),o):t}function p(e,n,t,r){for(var i=g(n),o="",u=0;u<e.length&&t(e[u]);u++)o+=e[u];return r(o,e.slice(o.length),i,m(n,o))}function h(){return{idx:0,column:0,row:0}}function g(e){return{idx:e.idx,column:e.column,row:e.row}}function v(e){return JSON.stringify(e)}function x(e){var n=e.indexOf("\n");return n=n>-1?n+1:e.length,[e.slice(0,n),e.slice(n)]}function m(e,n){if(!n)return e;e.idx+=n.length;var t=n.split("\n"),r=t.length;e.row+=r-1;var i=t[r-1].length;return e.column=r>1?i:e.column+i,e}e.reader={setParentheses:function(e){return n(e)},readSeq:function(e,n){return r(null,e,Object.freeze([]),h(),n).context},readSexp:function(e,n){return t(null,e,Object.freeze([]),h(),n).context[0]}};var w,y,S,A,b={},I={},C=/[`@^#~]/;n({"[":"]","(":")","{":"}"})}(t)}(),function(e){var n="undefined"!=typeof module&&module.exports,t=n?module.exports:window.paredit,r=n?require("./util").util:window.paredit.util;!function(e,n){function t(e){return e[e.length-1]}function r(e){return e.children||[]}var i=n.navigator={forwardSexp:function(e,n){var r=t(o.containingSexpsAt(e,n,function(e){return!o.hasChildren(e)}));if(r)return r.end;var i=o.nextSexp(e,n);return i?i.end:n},backwardSexp:function(e,n){var r=t(o.containingSexpsAt(e,n,function(e){return!o.hasChildren(e)}));if(r)return r.start;var i=o.prevSexp(e,n);return i?i.start:n},forwardDownSexp:function(e,n){var t=o.nextSexp(e,n,function(e){return"list"===e.type});return t?t.children&&t.children[0]?t.children[0].start:t.start+1:n},backwardUpSexp:function(e,n){var r=o.containingSexpsAt(e,n,function(e){return"list"===e.type||"string"===e.type||"comment"===e.type});return r&&r.length?t(r).start:n},closeList:function(e,n){var r=o.containingSexpsAt(e,n),i=t(r);if(!i||"toplevel"===i.type)return n;if("string"!==i.type&&"comment"!==i.type){return t(r.filter(o.hasChildren)).end}},sexpRange:function(e,n){return i.sexpRangeExpansion(e,n,n)},sexpRangeExpansion:function(n,i,u){if(i!==u){var l=t(o.sexpsAt(n,i,function(e){return e.start===i})),a=l&&t(o.sexpsAt(n,u,function(e){return e.end===u}));if(l&&a){var c=t(o.sexpsAt(n,i,function(e){return e.start<i&&!o.hasChildren(e)}));if(c)return[c.start,u];var s=t(o.sexpsAt(n,u,function(e){return u<e.end&&!o.hasChildren(e)}));if(s)return[i,s.end]}}var d=t(e.flatFilterTree(n,function(e){return"toplevel"!==e.type&&(i===u?e.start<=i&&u<=e.end:e.start===i?u<e.end:e.end===u?e.start<i:e.start<i&&u<e.end)},r));if(!d)return null;var f=d.start===i||d.end===u;if("list"===d.type||"string"===d.type){if(f&&(i===d.start||u===d.end))return[d.start,d.end];if(d.start+1<i||u<d.end-1)return[d.start+1,d.end-1]}return[d.start,d.end]},rangeForDefun:function(e,n){var t=e.children&&e.children.filter(function(e){return e.start<=n&&n<=e.end})[0];return t?[t.start,t.end]:null}},o=n.walk={hasChildren:function(e){return"list"===e.type||"toplevel"===e.type||"error"===e.type&&e.children},containingSexpsAt:function(n,t,i){return e.flatFilterTree(n,function(e){return("toplevel"===e.type||"error"===e.type&&e.start<t&&t<=e.end||e.start<t&&t<e.end)&&(!i||i(e))},r)},sexpsAt:function(n,t,i){return e.flatFilterTree(n,function(e){return e.start<=t&&t<=e.end&&(!i||i(e))},r)},nextSexp:function(n,i,u){var l=e.flatFilterTree(n,function(e){return e.start<=i&&i<e.end&&o.hasChildren(e)},r);if(!l.length)return null;var a=l.filter(function(e){return e.start===i&&"toplevel"!==e.type})[0];if(a)return a;var c=t(l).children.filter(function(e){return i<=e.start&&(!u||!!u(e))});return c.length?c[0]:null},prevSexp:function(n,i,u){var l=e.flatFilterTree(n,function(e){return e.start<i&&i<=e.end&&o.hasChildren(e)},r);if(!l.length)return null;var a=l.filter(function(e){return e.end===i&&"toplevel"!==e.type})[0];if(a)return a;var c=t(l).children.filter(function(e){return e.end<=i&&(!u||!!u(e))});return c.length?t(c):null},stringify:function(n){return e.mapTree(n,function(t,r){return"list"===t.type||"toplevel"===t.type?"("+r.join(" ")+")":t.source?t.source:e.times(n.end-n.start,"x")},function(e){return e&&e.children||[]})},source:function(e,n){return n.source?n.source:e.slice(n.start,n.end)}}}(r,t)}(),function(e){var n="undefined"!=typeof module&&module.exports,t=n?module.exports:window.paredit,r=n?require("./util").util:window.paredit.util,i=(n?require("./navigator").navigator:window.paredit.navigator,n?require("./navigator").walk:window.paredit.walk);!function(e,n,t,r){function i(e,n){return e.slice(0,n).lastIndexOf("\n")+1}function o(e,n){return n-i(e,n)}function u(e,n,t){if("toplevel"===n.type)return 0;var r=s(n,t);return f(n,e)?o(e,n.start+n.open.length+1):r.length<=1||"("!==n.open?o(e,n.start+n.open.length):o(e,r[1].start)}function l(e){return e[e.length-1]}function a(e,n){return new Array(e+1).join(n)}function c(e,n){return t.mapTree(n,function(n,r){return t.merge(n,{start:n.start+e,end:n.end+e,children:r})},function(e){return e.children})}function s(e,n){return e.children.filter(function(e){return e.end<=n})}function d(e,n){return e.children.filter(function(e){return n<=e.start})}function f(e,t){if(!n.hasChildren(e)||!e.children.length)return!1;var i=e.children[0].source;return!!i&&r.specialForms.some(function(n){return"string"==typeof n?n===i:"function"==typeof n?n(i,e.children[0]):n instanceof RegExp&&n.test(i)})}r.specialForms=["&","monitor-exit",/^case/,"try",/^reify/,"finally",/^(.*-)?loop/,/^do/,/^let/,/^import/,"new",/^deftype/,/^let/,"fn","recur",/^set.*!$/,".","var","quote","catch","throw","monitor-enter","ns","in-ns",/^([^\/]+\/)?def/,/^if/,/^when/,/^unless/,/->/,"while","for",/(^|\/)with/,"testing","while","cond","condp","apply","binding","locking","proxy","reify",/^extend/,"facts"];var p=r.editor={rewrite:function(e,r,i){var o=i.length?l(i).end-r.end:r.start-r.end;return n.containingSexpsAt(e,r.start).reduceRight(function(e,n){var r,i=n.children.indexOf(e.original);return r=i>-1?n.children.slice(0,i).concat(e.nodes).concat(n.children.slice(i+1).map(c.bind(null,o))):n.children,{original:n,nodes:[t.merge(n,{end:n.end+o,children:r})]}},{original:r,nodes:i}).nodes[0]},openList:function(e,t,r,i){i=i||{};var o=(i.count,i.open||"("),u=i.close||")";if(i.freeEdits||e.errors&&e.errors.length)return{changes:[["insert",r,o]],newIndex:r+o.length};var a=n.containingSexpsAt(e,r),c=l(a);if(c&&"comment"===c.type||"string"===c.type)return{changes:[["insert",r,o]],newIndex:r+o.length};if(!i.endIdx)return{changes:[["insert",r,o+u]],newIndex:r+o.length};var s=l(n.containingSexpsAt(e,r,n.hasChildren)),d=l(n.containingSexpsAt(e,i.endIdx,n.hasChildren));if(s!==d)return{changes:[["insert",r,o+u]],newIndex:r+o.length};var f=d.children.filter(function(e){return e.start<r&&r<e.end}),p=d.children.filter(function(e){return e.start<i.endIdx&&i.endIdx<e.end}),h=f[0]&&f[0]!==p[0]&&(p[0]||"symbol"!==f[0].type),g=p[0]&&f[0]!==p[0]&&(f[0]||"symbol"!==p[0].type),v=h?f[0].end:r;return{changes:[["insert",g?p[0].start:i.endIdx,u],["insert",v,o]],newIndex:v+o.length}},spliceSexp:function(e,t,r){var i=n.containingSexpsAt(e,r,n.hasChildren);if(!i.length)return null;var o=i.pop(),u="toplevel"===o.type,l=o.children.filter(function(e){return e.start<r&&r<e.end})[0],a=l&&"string"===l.type,c=[],s=r;return u||c.push(["remove",o.end-1,o.close.length]),a&&(c.push(["remove",l.end-1,l.close.length]),c.push(["remove",l.start,l.open.length]),s-=l.open.length),u||(c.push(["remove",o.start,o.open.length]),s-=o.open.length),{changes:c,newIndex:s}},spliceSexpKill:function(e,t,r,i){i=i||{};var o=(i.count,i.backward),u=n.containingSexpsAt(e,r,n.hasChildren);if(!u.length)return null;if(o)var a=s(l(u),r),c=p.killSexp(e,t,r,{count:a.length,backward:!0});else var f=d(l(u),r),c=p.killSexp(e,t,r,{count:f.length,backward:!1});var h=p.spliceSexp(e,t,r);if(!c)return h;if(!h)return c;var g=Array.prototype.slice.call(h.changes);return 2===g.length?g.splice(1,0,c.changes[0]):4===g.length&&g.splice(2,0,c.changes[0]),{changes:g,newIndex:c.newIndex-(3===g.length?1:2)}},splitSexp:function(e,t,r){var i=n.containingSexpsAt(e,r);if(!i.length)return null;var o=i.pop();return"toplevel"===o.type?null:n.hasChildren(o)||"string"===o.type?{changes:[["insert",r,o.close+" "+o.open]],newIndex:r+o.close.length}:null},killSexp:function(e,t,r,i){i=i||{};var o=i.count||1,u=i.backward,a=n.containingSexpsAt(e,r,n.hasChildren);if(!a.length)return null;var c=a.pop(),f=c.children.filter(function(e){return e.start<r&&r<e.end})[0];if(f){var p=u?f.start:r,h=u?r:f.end;return"string"===f.type&&(p+=u?f.open.length:0,h+=u?0:-f.close.length),{changes:[["remove",p,h-p]],newIndex:p}}if(f&&"string"===f.type){var p=u?f.start+f.open.length:r,h=u?r:f.end-f.close.length;return{changes:[["remove",p,h-p]],newIndex:p}}if(u){var g=s(c,r);if(!g.length)return null;var v=g.slice(-o)[0].start,x=[["remove",v,r-v]],m=v}else{var w=d(c,r);if(!w.length)return null;var m=r,x=[["remove",r,l(w.slice(0,o)).end-r]]}return{changes:x,newIndex:m}},wrapAround:function(e,t,r,i,o,u){var a=u&&u.count||1,c=n.containingSexpsAt(e,r,n.hasChildren);if(!c.length)return null;var s=l(c),d=s.children.filter(function(e){return e.start>=r}).slice(0,a),f=l(d);return{changes:[["insert",r,i],["insert",(f?f.end:r)+i.length,o]],newIndex:r+i.length}},closeAndNewline:function(e,t,r,i){i=i||")";var u=n.containingSexpsAt(e,r,function(e){return n.hasChildren(e)&&e.close===i});if(!u.length)return null;var c=l(u),s=a(o(t,c.start)," "),d="\n"+s;return{changes:[["insert",c.end,d]],newIndex:c.end+d.length}},barfSexp:function(e,t,r,i){var o=i&&i.backward,u=n.containingSexpsAt(e,r,n.hasChildren);if(!u.length)return null;var a=l(u),c=l(n.containingSexpsAt(e,r));if(c===a&&(c=null),o){var f=s(a,r);if(!f.length)return null;var p=[["insert",f[1]?f[1].start:c?c.start:r,a.open],["remove",a.start,a.open.length]]}else{var h=d(a,r);if(!h.length)return null;var p=[["remove",a.end-a.close.length,a.close.length],["insert",h[h.length-2]?h[h.length-2].end:c?c.end:r,a.close]]}return{changes:p,newIndex:r}},slurpSexp:function(e,t,r,i){var o=i&&i.backward,u=i.count||1,a=n.containingSexpsAt(e,r,n.hasChildren);if(a.length<2)return null;var c=a.pop(),f=a.pop();if(o){var p=s(f,r);if(!p.length)return null;var h=[["remove",c.start,c.open.length],["insert",p.slice(-u)[0].start,c.open]]}else{var g=d(f,r);if(!g.length)return null;var h=[["insert",l(g.slice(0,u)).end,c.close],["remove",c.end-c.close.length,c.close.length]]}return{changes:h,newIndex:r}},transpose:function(e,t,r,i){i=i||{};var o=n.containingSexpsAt(e,r,n.hasChildren),u=l(o),a=s(u,r),c=d(u,r),f=u.children.find(function(e){return e.start<r&&r<e.end});if(f&&(c=[f]),!a.length||!c.length)return null;var p=l(a),h=c[0],g=t.slice(p.end,h.start)+n.source(t,p);return{changes:[["insert",h.end,g],["remove",p.start,h.start-p.start]],newIndex:r-(p.end-p.start)+(h.end-h.start)}},delete:function(e,t,r,i){function o(e){return("string"===e.type||"list"===e.type)&&e.end-e.start===e.open.length+e.close.length}function u(e){return{changes:[["remove",e.start,e.end-e.start]],newIndex:e.start}}function a(e){return"symbol"===e.type||"comment"===e.type||"number"===e.type||"special"===e.type}i=i||{};var c=i.count||1,f=!!i.backward,p=i.endIdx;if(i.freeEdits||e.errors&&e.errors.length)return p?{changes:[["remove",r,p-r]],newIndex:r}:{changes:[["remove",f?r-c:r,c]],newIndex:f?r-c:r};var h=n.containingSexpsAt(e,r),g=h.filter(function(e){return n.hasChildren(e)}),v=l(g),x=l(h);if("number"==typeof p){if(v!==l(n.containingSexpsAt(e,p,n.hasChildren)))return null;var m=l(n.sexpsAt(v,r)),w=l(n.sexpsAt(v,p)),y=!a(m)&&m.start===r,S=!a(w)&&w.end===p;if(m===w&&(y&&!S||!y&&S))return null;if((w!==v&&!a(w)&&!S||m!==v&&!a(m)&&!y)&&m!==w)return null;if(-1===v.children.indexOf(m)&&m!==v||-1===v.children.indexOf(w)&&w!==v)return null;var A=Math.min(r,p);return{changes:[["remove",A,Math.max(r,p)-A]],newIndex:A}}var b=v===x,I=b&&s(v,r),C=b&&d(v,r),k={changes:[],newIndex:r},O={changes:[],newIndex:r-1},F={changes:[["remove",f?r-c:r,c]],newIndex:f?r-c:r};if(!b&&"comment"===x.type)return F;if(I&&I.length&&f){var q=l(I);return q.end!==r||a(q)?F:o(q)||"char"===q.type?u(q):1==c?O:k}if(C&&C.length&&!f){var q=C[0];return q.start!==r||a(q)?F:o(q)||"char"===q.type?u(q):k}b||(v=x);var T=r===v.start+(v.open?v.open.length:0),j=r===v.end-(v.close?v.close.length:0);return v.children&&v.children.length||!(T&&f||j&&!f)?T&&f&&(b?v.children.length:v.end-v.start>1)?k:j&&!f&&(b?v.children.length:v.end-v.start>1)?k:F:u(v)},indentRange:function(e,r,o,s){var f=i(r,o),h=r.slice(s).indexOf("\n");return h=h>-1?h+s:r.length,r.slice(f,h).split("\n").reduce(function(e,r){var i=e.idx,o=e.changes,s=e.ast,f=e.src,h=n.containingSexpsAt(s,i,n.hasChildren),g=l(h),v=g&&l(n.sexpsAt(s,i));if(!g)return{idx:i+r.length+1,newIndex:i,changes:o,ast:s,src:f};var x=r.match(/^\s*/)[0],m=v&&"string"===v.type&&i>v.start?0:u(f,g,i)-x.length,w=r.length+m;if(m>0){var y=a(m," ");o.push(["insert",i,y]),f=f.slice(0,i)+y+f.slice(i)}else m<0&&(o.push(["remove",i,-m]),f=f.slice(0,i)+f.slice(i-m));var S=d(g,i)[0];if(S){var A=c(m,S);s=p.rewrite(s,S,[A])}else s=p.rewrite(s,g,[t.merge(g,{end:g.end+m})]);return{idx:i+w+1,newIndex:i+m,changes:o,ast:s,src:f}},{idx:f,changes:[],ast:e,src:r})}}}(0,i,r,t)}();