tildefriends/core/codemirror/searchcursor.min.js
2021-12-22 14:27:52 +00:00

1 line
5.1 KiB
JavaScript

!function(t){"object"==typeof exports&&"object"==typeof module?t(require("../../lib/codemirror")):"function"==typeof define&&define.amd?define(["../../lib/codemirror"],t):t(CodeMirror)}(function(i){"use strict";var p,x,L=i.Pos;function d(t,e){for(var n,r,i=null!=(r=(n=t).flags)?r:(n.ignoreCase?"i":"")+(n.global?"g":"")+(n.multiline?"m":""),o=i,l=0;l<e.length;l++)-1==o.indexOf(e.charAt(l))&&(o+=e.charAt(l));return i==o?t:new RegExp(t.source,o)}function v(t){return/\\s|\\n|\n|\\W|\\D|\[\^/.test(t.source)}function m(t,e,n){e=d(e,"g");for(var r=n.line,i=n.ch,o=t.lastLine();r<=o;r++,i=0){e.lastIndex=i;var l=t.getLine(r),h=e.exec(l);if(h)return{from:L(r,h.index),to:L(r,h.index+h[0].length),match:h}}}function C(t,e,n){for(var r,i=0;i<=t.length;){e.lastIndex=i;var o=e.exec(t);if(!o)break;var l=o.index+o[0].length;if(l>t.length-n)break;(!r||l>r.index+r[0].length)&&(r=o),i=o.index+1}return r}function O(t,e,n){e=d(e,"g");for(var r=n.line,i=n.ch,o=t.firstLine();o<=r;r--,i=-1){var l=t.getLine(r),h=C(l,e,i<0?0:l.length-i);if(h)return{from:L(r,h.index),to:L(r,h.index+h[0].length),match:h}}}function b(t,e,n,r){if(t.length==e.length)return n;for(var i=0,o=n+Math.max(0,t.length-e.length);;){if(i==o)return i;var l=i+o>>1,h=r(t.slice(0,l)).length;if(h==n)return l;n<h?o=l:i=1+l}}function r(n,r,t,e){var i;this.atOccurrence=!1,this.doc=n,t=t?n.clipPos(t):L(0,0),this.pos={from:t,to:t},"object"==typeof e?i=e.caseFold:(i=e,e=null),"string"==typeof r?(null==i&&(i=!1),this.matches=function(t,e){return(t?function(t,e,n,r){if(!e.length)return null;var i=r?p:x,o=i(e).split(/\r|\n\r?/);t:for(var l=n.line,h=n.ch,s=t.firstLine()-1+o.length;s<=l;l--,h=-1){var c=t.getLine(l);-1<h&&(c=c.slice(0,h));var f=i(c);if(1==o.length){var u=f.lastIndexOf(o[0]);if(-1==u)continue t;return{from:L(l,b(c,f,u,i)),to:L(l,b(c,f,u+o[0].length,i))}}var g=o[o.length-1];if(f.slice(0,g.length)==g){for(var a=1,n=l-o.length+1;a<o.length-1;a++)if(i(t.getLine(n+a))!=o[a])continue t;var m=t.getLine(l+1-o.length),d=i(m);if(d.slice(d.length-o[0].length)==o[0])return{from:L(l+1-o.length,b(m,d,m.length-o[0].length,i)),to:L(l,b(c,f,g.length,i))}}}}:function(t,e,n,r){if(!e.length)return null;var i=r?p:x,o=i(e).split(/\r|\n\r?/);t:for(var l=n.line,h=n.ch,s=t.lastLine()+1-o.length;l<=s;l++,h=0){var c=t.getLine(l).slice(h),f=i(c);if(1==o.length){var u=f.indexOf(o[0]);if(-1==u)continue t;n=b(c,f,u,i)+h;return{from:L(l,b(c,f,u,i)+h),to:L(l,b(c,f,u+o[0].length,i)+h)}}var g=f.length-o[0].length;if(f.slice(g)==o[0]){for(var a=1;a<o.length-1;a++)if(i(t.getLine(l+a))!=o[a])continue t;var m=t.getLine(l+o.length-1),d=i(m),v=o[o.length-1];if(d.slice(0,v.length)==v)return{from:L(l,b(c,f,g,i)+h),to:L(l+o.length-1,b(m,d,v.length,i))}}}})(n,r,e,i)}):(r=d(r,"gm"),e&&!1===e.multiline?this.matches=function(t,e){return(t?O:m)(n,r,e)}:this.matches=function(t,e){return(t?function(t,e,n){if(!v(e))return O(t,e,n);e=d(e,"gm");for(var r=1,i=t.getLine(n.line).length-n.ch,o=n.line,l=t.firstLine();l<=o;){for(var h=0;h<r&&l<=o;h++)var s=t.getLine(o--),c=null==c?s:s+"\n"+c;r*=2;var f=C(c,e,i);if(f){var u=c.slice(0,f.index).split("\n"),g=f[0].split("\n"),a=o+u.length,m=u[u.length-1].length;return{from:L(a,m),to:L(a+g.length-1,1==g.length?m+g[0].length:g[g.length-1].length),match:f}}}}:function(t,e,n){if(!v(e))return m(t,e,n);e=d(e,"gm");for(var r=1,i=n.line,o=t.lastLine();i<=o;){for(var l=0;l<r&&!(o<i);l++)var h=t.getLine(i++),s=null==s?h:s+"\n"+h;r*=2,e.lastIndex=n.ch;var c=e.exec(s);if(c){var f=s.slice(0,c.index).split("\n"),u=c[0].split("\n"),g=n.line+f.length-1,a=f[f.length-1].length;return{from:L(g,a),to:L(g+u.length-1,1==u.length?a+u[0].length:u[u.length-1].length),match:c}}}})(n,r,e)})}x=String.prototype.normalize?(p=function(t){return t.normalize("NFD").toLowerCase()},function(t){return t.normalize("NFD")}):(p=function(t){return t.toLowerCase()},function(t){return t}),r.prototype={findNext:function(){return this.find(!1)},findPrevious:function(){return this.find(!0)},find:function(t){for(var e=this.matches(t,this.doc.clipPos(t?this.pos.from:this.pos.to));e&&0==i.cmpPos(e.from,e.to);)t?e.from.ch?e.from=L(e.from.line,e.from.ch-1):e=e.from.line==this.doc.firstLine()?null:this.matches(t,this.doc.clipPos(L(e.from.line-1))):e.to.ch<this.doc.getLine(e.to.line).length?e.to=L(e.to.line,e.to.ch+1):e=e.to.line==this.doc.lastLine()?null:this.matches(t,L(e.to.line+1,0));if(e)return this.pos=e,this.atOccurrence=!0,this.pos.match||!0;var n=L(t?this.doc.firstLine():this.doc.lastLine()+1,0);return this.pos={from:n,to:n},this.atOccurrence=!1},from:function(){if(this.atOccurrence)return this.pos.from},to:function(){if(this.atOccurrence)return this.pos.to},replace:function(t,e){var n;this.atOccurrence&&(n=i.splitLines(t),this.doc.replaceRange(n,this.pos.from,this.pos.to,e),this.pos.to=L(this.pos.from.line+n.length-1,n[n.length-1].length+(1==n.length?this.pos.from.ch:0)))}},i.defineExtension("getSearchCursor",function(t,e,n){return new r(this.doc,t,e,n)}),i.defineDocExtension("getSearchCursor",function(t,e,n){return new r(this,t,e,n)}),i.defineExtension("selectMatches",function(t,e){for(var n=[],r=this.getSearchCursor(t,this.getCursor("from"),e);r.findNext()&&!(0<i.cmpPos(r.to(),this.getCursor("to")));)n.push({anchor:r.from(),head:r.to()});n.length&&this.setSelections(n,0)})});