2 lines
26 KiB
JavaScript
2 lines
26 KiB
JavaScript
import{Annotation as Me,Facet as be,EditorSelection as h,Text as Ee,findClusterBreak as O,countColumn as X,combineConfig as qt,StateField as Kt,Transaction as Y,ChangeSet as $t,ChangeDesc as _t,StateEffect as Qt,CharCategory as L}from"@codemirror/state";import{EditorView as M,Direction as Xt}from"@codemirror/view";import{IndentContext as Te,getIndentation as Oe,indentString as U,matchBrackets as b,syntaxTree as Z,getIndentUnit as j,indentUnit as Yt}from"@codemirror/language";import{NodeProp as ee}from"@lezer/common";const Ie=e=>{let{state:t}=e,r=t.doc.lineAt(t.selection.main.from),n=te(e.state,r.from);return n.line?Re(e):n.block?Ue(e):!1};function D(e,t){return({state:r,dispatch:n})=>{if(r.readOnly)return!1;let l=e(t,r);return l?(n(r.update(l)),!0):!1}}const Re=D(ne,0),Zt=D(ne,1),jt=D(ne,2),ve=D(N,0),en=D(N,1),tn=D(N,2),Ue=D((e,t)=>N(e,t,rn(t)),0);function te(e,t){let r=e.languageDataAt("commentTokens",t);return r.length?r[0]:{}}const I=50;function nn(e,{open:t,close:r},n,l){let o=e.sliceDoc(n-I,n),c=e.sliceDoc(l,l+I),s=/\s*$/.exec(o)[0].length,i=/^\s*/.exec(c)[0].length,f=o.length-s;if(o.slice(f-t.length,f)==t&&c.slice(i,i+r.length)==r)return{open:{pos:n-s,margin:s&&1},close:{pos:l+i,margin:i&&1}};let u,a;l-n<=2*I?u=a=e.sliceDoc(n,l):(u=e.sliceDoc(n,n+I),a=e.sliceDoc(l-I,l));let d=/^\s*/.exec(u)[0].length,B=/\s*$/.exec(a)[0].length,g=a.length-B-r.length;return u.slice(d,d+t.length)==t&&a.slice(g,g+r.length)==r?{open:{pos:n+d+t.length,margin:/\s/.test(u.charAt(d+t.length))?1:0},close:{pos:l-B-r.length,margin:/\s/.test(a.charAt(g-1))?1:0}}:null}function rn(e){let t=[];for(let r of e.selection.ranges){let n=e.doc.lineAt(r.from),l=r.to<=n.to?n:e.doc.lineAt(r.to),o=t.length-1;o>=0&&t[o].to>n.from?t[o].to=l.to:t.push({from:n.from+/^\s*/.exec(n.text)[0].length,to:l.to})}return t}function N(e,t,r=t.selection.ranges){let n=r.map(o=>te(t,o.from).block);if(!n.every(o=>o))return null;let l=r.map((o,c)=>nn(t,n[c],o.from,o.to));if(e!=2&&!l.every(o=>o))return{changes:t.changes(r.map((o,c)=>l[c]?[]:[{from:o.from,insert:n[c].open+" "},{from:o.to,insert:" "+n[c].close}]))};if(e!=1&&l.some(o=>o)){let o=[];for(let c=0,s;c<l.length;c++)if(s=l[c]){let i=n[c],{open:f,close:u}=s;o.push({from:f.pos-i.open.length,to:f.pos+f.margin},{from:u.pos-u.margin,to:u.pos+i.close.length})}return{changes:o}}return null}function ne(e,t,r=t.selection.ranges){let n=[],l=-1;for(let{from:o,to:c}of r){let s=n.length,i=1e9,f=te(t,o).line;if(f){for(let u=o;u<=c;){let a=t.doc.lineAt(u);if(a.from>l&&(o==c||c>a.from)){l=a.from;let d=/^\s*/.exec(a.text)[0].length,B=d==a.length,g=a.text.slice(d,d+f.length)==f?d:-1;d<a.text.length&&d<i&&(i=d),n.push({line:a,comment:g,token:f,indent:d,empty:B,single:!1})}u=a.to+1}if(i<1e9)for(let u=s;u<n.length;u++)n[u].indent<n[u].line.text.length&&(n[u].indent=i);n.length==s+1&&(n[s].single=!0)}}if(e!=2&&n.some(o=>o.comment<0&&(!o.empty||o.single))){let o=[];for(let{line:s,token:i,indent:f,empty:u,single:a}of n)(a||!u)&&o.push({from:s.from+f,insert:i+" "});let c=t.changes(o);return{changes:c,selection:t.selection.map(c,1)}}else if(e!=1&&n.some(o=>o.comment>=0)){let o=[];for(let{line:c,comment:s,token:i}of n)if(s>=0){let f=c.from+s,u=f+i.length;c.text[u-c.from]==" "&&u++,o.push({from:f,to:u})}return{changes:o}}return null}const re=Me.define(),Ne=Me.define(),Ve=be.define(),Fe=be.define({combine(e){return qt(e,{minDepth:100,newGroupDelay:500,joinToEvent:(t,r)=>r},{minDepth:Math.max,newGroupDelay:Math.min,joinToEvent:(t,r)=>(n,l)=>t(n,l)||r(n,l)})}}),V=Kt.define({create(){return C.empty},update(e,t){let r=t.state.facet(Fe),n=t.annotation(re);if(n){let i=p.fromTransaction(t,n.selection),f=n.side,u=f==0?e.undone:e.done;return i?u=P(u,u.length,r.minDepth,i):u=ze(u,t.startState.selection),new C(f==0?n.rest:u,f==0?u:n.rest)}let l=t.annotation(Ne);if((l=="full"||l=="before")&&(e=e.isolate()),t.annotation(Y.addToHistory)===!1)return t.changes.empty?e:e.addMapping(t.changes.desc);let o=p.fromTransaction(t),c=t.annotation(Y.time),s=t.annotation(Y.userEvent);return o?e=e.addChanges(o,c,s,r,t):t.selection&&(e=e.addSelection(t.startState.selection,c,s,r.newGroupDelay)),(l=="full"||l=="after")&&(e=e.isolate()),e},toJSON(e){return{done:e.done.map(t=>t.toJSON()),undone:e.undone.map(t=>t.toJSON())}},fromJSON(e){return new C(e.done.map(p.fromJSON),e.undone.map(p.fromJSON))}});function on(e={}){return[V,Fe.of(e),M.domEventHandlers({beforeinput(t,r){let n=t.inputType=="historyUndo"?oe:t.inputType=="historyRedo"?G:null;return n?(t.preventDefault(),n(r)):!1}})]}const ln=V;function F(e,t){return function({state:r,dispatch:n}){if(!t&&r.readOnly)return!1;let l=r.field(V,!1);if(!l)return!1;let o=l.pop(e,r,t);return o?(n(o),!0):!1}}const oe=F(0,!1),G=F(1,!1),Ge=F(0,!0),Pe=F(1,!0);function Je(e){return function(t){let r=t.field(V,!1);if(!r)return 0;let n=e==0?r.done:r.undone;return n.length-(n.length&&!n[0].changes?1:0)}}const cn=Je(0),sn=Je(1);class p{constructor(t,r,n,l,o){this.changes=t,this.effects=r,this.mapped=n,this.startSelection=l,this.selectionsAfter=o}setSelAfter(t){return new p(this.changes,this.effects,this.mapped,this.startSelection,t)}toJSON(){var t,r,n;return{changes:(t=this.changes)===null||t===void 0?void 0:t.toJSON(),mapped:(r=this.mapped)===null||r===void 0?void 0:r.toJSON(),startSelection:(n=this.startSelection)===null||n===void 0?void 0:n.toJSON(),selectionsAfter:this.selectionsAfter.map(l=>l.toJSON())}}static fromJSON(t){return new p(t.changes&&$t.fromJSON(t.changes),[],t.mapped&&_t.fromJSON(t.mapped),t.startSelection&&h.fromJSON(t.startSelection),t.selectionsAfter.map(h.fromJSON))}static fromTransaction(t,r){let n=k;for(let l of t.startState.facet(Ve)){let o=l(t);o.length&&(n=n.concat(o))}return!n.length&&t.changes.empty?null:new p(t.changes.invert(t.startState.doc),n,void 0,r||t.startState.selection,k)}static selection(t){return new p(void 0,k,void 0,void 0,t)}}function P(e,t,r,n){let l=t+1>r+20?t-r-1:0,o=e.slice(l,t);return o.push(n),o}function un(e,t){let r=[],n=!1;return e.iterChangedRanges((l,o)=>r.push(l,o)),t.iterChangedRanges((l,o,c,s)=>{for(let i=0;i<r.length;){let f=r[i++],u=r[i++];s>=f&&c<=u&&(n=!0)}}),n}function fn(e,t){return e.ranges.length==t.ranges.length&&e.ranges.filter((r,n)=>r.empty!=t.ranges[n].empty).length===0}function we(e,t){return e.length?t.length?e.concat(t):e:t}const k=[],an=200;function ze(e,t){if(e.length){let r=e[e.length-1],n=r.selectionsAfter.slice(Math.max(0,r.selectionsAfter.length-an));return n.length&&n[n.length-1].eq(t)?e:(n.push(t),P(e,e.length-1,1e9,r.setSelAfter(n)))}else return[p.selection([t])]}function hn(e){let t=e[e.length-1],r=e.slice();return r[e.length-1]=t.setSelAfter(t.selectionsAfter.slice(0,t.selectionsAfter.length-1)),r}function le(e,t){if(!e.length)return e;let r=e.length,n=k;for(;r;){let l=dn(e[r-1],t,n);if(l.changes&&!l.changes.empty||l.effects.length){let o=e.slice(0,r);return o[r-1]=l,o}else t=l.mapped,r--,n=l.selectionsAfter}return n.length?[p.selection(n)]:k}function dn(e,t,r){let n=we(e.selectionsAfter.length?e.selectionsAfter.map(s=>s.map(t)):k,r);if(!e.changes)return p.selection(n);let l=e.changes.map(t),o=t.mapDesc(e.changes,!0),c=e.mapped?e.mapped.composeDesc(o):o;return new p(l,Qt.mapEffects(e.effects,t),c,e.startSelection.map(o),n)}const mn=/^(input\.type|delete)($|\.)/;class C{constructor(t,r,n=0,l=void 0){this.done=t,this.undone=r,this.prevTime=n,this.prevUserEvent=l}isolate(){return this.prevTime?new C(this.done,this.undone):this}addChanges(t,r,n,l,o){let c=this.done,s=c[c.length-1];return s&&s.changes&&!s.changes.empty&&t.changes&&(!n||mn.test(n))&&(!s.selectionsAfter.length&&r-this.prevTime<l.newGroupDelay&&l.joinToEvent(o,un(s.changes,t.changes))||n=="input.type.compose")?c=P(c,c.length-1,l.minDepth,new p(t.changes.compose(s.changes),we(t.effects,s.effects),s.mapped,s.startSelection,k)):c=P(c,c.length,l.minDepth,t),new C(c,k,r,n)}addSelection(t,r,n,l){let o=this.done.length?this.done[this.done.length-1].selectionsAfter:k;return o.length>0&&r-this.prevTime<l&&n==this.prevUserEvent&&n&&/^select($|\.)/.test(n)&&fn(o[o.length-1],t)?this:new C(ze(this.done,t),this.undone,r,n)}addMapping(t){return new C(le(this.done,t),le(this.undone,t),this.prevTime,this.prevUserEvent)}pop(t,r,n){let l=t==0?this.done:this.undone;if(l.length==0)return null;let o=l[l.length-1],c=o.selectionsAfter[0]||r.selection;if(n&&o.selectionsAfter.length)return r.update({selection:o.selectionsAfter[o.selectionsAfter.length-1],annotations:re.of({side:t,rest:hn(l),selection:c}),userEvent:t==0?"select.undo":"select.redo",scrollIntoView:!0});if(o.changes){let s=l.length==1?k:l.slice(0,l.length-1);return o.mapped&&(s=le(s,o.mapped)),r.update({changes:o.changes,selection:o.startSelection,effects:o.effects,annotations:re.of({side:t,rest:s,selection:c}),filter:!1,userEvent:t==0?"undo":"redo",scrollIntoView:!0})}else return null}}C.empty=new C(k,k);const pn=[{key:"Mod-z",run:oe,preventDefault:!0},{key:"Mod-y",mac:"Mod-Shift-z",run:G,preventDefault:!0},{linux:"Ctrl-Shift-z",run:G,preventDefault:!0},{key:"Mod-u",run:Ge,preventDefault:!0},{key:"Alt-u",mac:"Mod-Shift-u",run:Pe,preventDefault:!0}];function E(e,t){return h.create(e.ranges.map(t),e.mainIndex)}function S(e,t){return e.update({selection:t,scrollIntoView:!0,userEvent:"select"})}function A({state:e,dispatch:t},r){let n=E(e.selection,r);return n.eq(e.selection,!0)?!1:(t(S(e,n)),!0)}function R(e,t){return h.cursor(t?e.to:e.from)}function J(e,t){return A(e,r=>r.empty?e.moveByChar(r,t):R(r,t))}function m(e){return e.textDirectionAt(e.state.selection.main.head)==Xt.LTR}const ce=e=>J(e,!m(e)),se=e=>J(e,m(e)),gn=e=>J(e,!0),yn=e=>J(e,!1);function w(e,t){return A(e,r=>r.empty?e.moveByGroup(r,t):R(r,t))}const We=e=>w(e,!m(e)),He=e=>w(e,m(e)),kn=e=>w(e,!0),An=e=>w(e,!1),qe=typeof Intl<"u"&&Intl.Segmenter?new Intl.Segmenter(void 0,{granularity:"word"}):null;function Ke(e,t,r){let n=e.state.charCategorizer(t.from),l=L.Space,o=t.from,c=0,s=!1,i=!1,f=!1,u=d=>{if(s)return!1;o+=r?d.length:-d.length;let B=n(d),g;if(B==L.Word&&d.charCodeAt(0)<128&&/[\W_]/.test(d)&&(B=-1),l==L.Space&&(l=B),l!=B)return!1;if(l==L.Word)if(d.toLowerCase()==d){if(!r&&i)return!1;f=!0}else if(f){if(r)return!1;s=!0}else{if(i&&r&&n(g=e.state.sliceDoc(o,o+1))==L.Word&&g.toLowerCase()==g)return!1;i=!0}return c++,!0},a=e.moveByChar(t,r,d=>(u(d),u));if(qe&&l==L.Word&&a.from==t.from+c*(r?1:-1)){let d=Math.min(t.head,a.head),B=Math.max(t.head,a.head),g=e.state.sliceDoc(d,B);if(g.length>1&&/[\u4E00-\uffff]/.test(g)){let v=Array.from(qe.segment(g));if(v.length>1)return r?h.cursor(t.head+v[1].index,-1):h.cursor(a.head+v[v.length-1].index,1)}}return a}function $e(e,t){return A(e,r=>r.empty?Ke(e,r,t):R(r,t))}const Bn=e=>$e(e,!0),Sn=e=>$e(e,!1);function Cn(e,t,r){if(t.type.prop(r))return!0;let n=t.to-t.from;return n&&(n>2||/[^\s,.;:]/.test(e.sliceDoc(t.from,t.to)))||t.firstChild}function z(e,t,r){let n=Z(e).resolveInner(t.head),l=r?ee.closedBy:ee.openedBy;for(let i=t.head;;){let f=r?n.childAfter(i):n.childBefore(i);if(!f)break;Cn(e,f,l)?n=f:i=r?f.to:f.from}let o=n.type.prop(l),c,s;return o&&(c=r?b(e,n.from,1):b(e,n.to,-1))&&c.matched?s=r?c.end.to:c.end.from:s=r?n.to:n.from,h.cursor(s,r?-1:1)}const _e=e=>A(e,t=>z(e.state,t,!m(e))),Qe=e=>A(e,t=>z(e.state,t,m(e)));function Xe(e,t){return A(e,r=>{if(!r.empty)return R(r,t);let n=e.moveVertically(r,t);return n.head!=r.head?n:e.moveToLineBoundary(r,t)})}const ie=e=>Xe(e,!1),ue=e=>Xe(e,!0);function Ye(e){let t=e.scrollDOM.clientHeight<e.scrollDOM.scrollHeight-2,r=0,n=0,l;if(t){for(let o of e.state.facet(M.scrollMargins)){let c=o(e);c?.top&&(r=Math.max(c?.top,r)),c?.bottom&&(n=Math.max(c?.bottom,n))}l=e.scrollDOM.clientHeight-r-n}else l=(e.dom.ownerDocument.defaultView||window).innerHeight;return{marginTop:r,marginBottom:n,selfScroll:t,height:Math.max(e.defaultLineHeight,l-5)}}function Ze(e,t){let r=Ye(e),{state:n}=e,l=E(n.selection,c=>c.empty?e.moveVertically(c,t,r.height):R(c,t));if(l.eq(n.selection))return!1;let o;if(r.selfScroll){let c=e.coordsAtPos(n.selection.main.head),s=e.scrollDOM.getBoundingClientRect(),i=s.top+r.marginTop,f=s.bottom-r.marginBottom;c&&c.top>i&&c.bottom<f&&(o=M.scrollIntoView(l.main.head,{y:"start",yMargin:c.top-i}))}return e.dispatch(S(n,l),{effects:o}),!0}const fe=e=>Ze(e,!1),W=e=>Ze(e,!0);function x(e,t,r){let n=e.lineBlockAt(t.head),l=e.moveToLineBoundary(t,r);if(l.head==t.head&&l.head!=(r?n.to:n.from)&&(l=e.moveToLineBoundary(t,r,!1)),!r&&l.head==n.from&&n.length){let o=/^\s*/.exec(e.state.sliceDoc(n.from,Math.min(n.from+100,n.to)))[0].length;o&&t.head!=n.from+o&&(l=h.cursor(n.from+o))}return l}const je=e=>A(e,t=>x(e,t,!0)),et=e=>A(e,t=>x(e,t,!1)),tt=e=>A(e,t=>x(e,t,!m(e))),nt=e=>A(e,t=>x(e,t,m(e))),rt=e=>A(e,t=>h.cursor(e.lineBlockAt(t.head).from,1)),ot=e=>A(e,t=>h.cursor(e.lineBlockAt(t.head).to,-1));function lt(e,t,r){let n=!1,l=E(e.selection,o=>{let c=b(e,o.head,-1)||b(e,o.head,1)||o.head>0&&b(e,o.head-1,1)||o.head<e.doc.length&&b(e,o.head+1,-1);if(!c||!c.end)return o;n=!0;let s=c.start.from==o.head?c.end.to:c.end.from;return r?h.range(o.anchor,s):h.cursor(s)});return n?(t(S(e,l)),!0):!1}const ct=({state:e,dispatch:t})=>lt(e,t,!1),xn=({state:e,dispatch:t})=>lt(e,t,!0);function y(e,t){let r=E(e.state.selection,n=>{let l=t(n);return h.range(n.anchor,l.head,l.goalColumn,l.bidiLevel||void 0)});return r.eq(e.state.selection)?!1:(e.dispatch(S(e.state,r)),!0)}function H(e,t){return y(e,r=>e.moveByChar(r,t))}const ae=e=>H(e,!m(e)),he=e=>H(e,m(e)),Dn=e=>H(e,!0),Ln=e=>H(e,!1);function q(e,t){return y(e,r=>e.moveByGroup(r,t))}const st=e=>q(e,!m(e)),it=e=>q(e,m(e)),Mn=e=>q(e,!0),bn=e=>q(e,!1);function ut(e,t){return y(e,r=>Ke(e,r,t))}const En=e=>ut(e,!0),Tn=e=>ut(e,!1),ft=e=>y(e,t=>z(e.state,t,!m(e))),at=e=>y(e,t=>z(e.state,t,m(e)));function ht(e,t){return y(e,r=>e.moveVertically(r,t))}const de=e=>ht(e,!1),me=e=>ht(e,!0);function dt(e,t){return y(e,r=>e.moveVertically(r,t,Ye(e).height))}const pe=e=>dt(e,!1),ge=e=>dt(e,!0),mt=e=>y(e,t=>x(e,t,!0)),pt=e=>y(e,t=>x(e,t,!1)),gt=e=>y(e,t=>x(e,t,!m(e))),yt=e=>y(e,t=>x(e,t,m(e))),kt=e=>y(e,t=>h.cursor(e.lineBlockAt(t.head).from)),At=e=>y(e,t=>h.cursor(e.lineBlockAt(t.head).to)),ye=({state:e,dispatch:t})=>(t(S(e,{anchor:0})),!0),ke=({state:e,dispatch:t})=>(t(S(e,{anchor:e.doc.length})),!0),Ae=({state:e,dispatch:t})=>(t(S(e,{anchor:e.selection.main.anchor,head:0})),!0),Be=({state:e,dispatch:t})=>(t(S(e,{anchor:e.selection.main.anchor,head:e.doc.length})),!0),Bt=({state:e,dispatch:t})=>(t(e.update({selection:{anchor:0,head:e.doc.length},userEvent:"select"})),!0),St=({state:e,dispatch:t})=>{let r=_(e).map(({from:n,to:l})=>h.range(n,Math.min(l+1,e.doc.length)));return t(e.update({selection:h.create(r),userEvent:"select"})),!0},Ct=({state:e,dispatch:t})=>{let r=E(e.selection,n=>{var l;let o=Z(e).resolveStack(n.from,1);for(let c=o;c;c=c.next){let{node:s}=c;if((s.from<n.from&&s.to>=n.to||s.to>n.to&&s.from<=n.from)&&(!((l=s.parent)===null||l===void 0)&&l.parent))return h.range(s.to,s.from)}return n});return t(S(e,r)),!0},xt=({state:e,dispatch:t})=>{let r=e.selection,n=null;return r.ranges.length>1?n=h.create([r.main]):r.main.empty||(n=h.create([h.cursor(r.main.head)])),n?(t(S(e,n)),!0):!1};function T(e,t){if(e.state.readOnly)return!1;let r="delete.selection",{state:n}=e,l=n.changeByRange(o=>{let{from:c,to:s}=o;if(c==s){let i=t(o);i<c?(r="delete.backward",i=K(e,i,!1)):i>c&&(r="delete.forward",i=K(e,i,!0)),c=Math.min(c,i),s=Math.max(s,i)}else c=K(e,c,!1),s=K(e,s,!0);return c==s?{range:o}:{changes:{from:c,to:s},range:h.cursor(c,c<o.head?-1:1)}});return l.changes.empty?!1:(e.dispatch(n.update(l,{scrollIntoView:!0,userEvent:r,effects:r=="delete.selection"?M.announce.of(n.phrase("Selection deleted")):void 0})),!0)}function K(e,t,r){if(e instanceof M)for(let n of e.state.facet(M.atomicRanges).map(l=>l(e)))n.between(t,t,(l,o)=>{l<t&&o>t&&(t=r?o:l)});return t}const Se=(e,t,r)=>T(e,n=>{let l=n.from,{state:o}=e,c=o.doc.lineAt(l),s,i;if(r&&!t&&l>c.from&&l<c.from+200&&!/[^ \t]/.test(s=c.text.slice(0,l-c.from))){if(s[s.length-1]==" ")return l-1;let f=X(s,o.tabSize),u=f%j(o)||j(o);for(let a=0;a<u&&s[s.length-1-a]==" ";a++)l--;i=l}else i=O(c.text,l-c.from,t,t)+c.from,i==l&&c.number!=(t?o.doc.lines:1)?i+=t?1:-1:!t&&/[\ufe00-\ufe0f]/.test(c.text.slice(i-c.from,l-c.from))&&(i=O(c.text,i-c.from,!1,!1)+c.from);return i}),$=e=>Se(e,!1,!0),On=e=>Se(e,!1,!1),Ce=e=>Se(e,!0,!1),Dt=(e,t)=>T(e,r=>{let n=r.head,{state:l}=e,o=l.doc.lineAt(n),c=l.charCategorizer(n);for(let s=null;;){if(n==(t?o.to:o.from)){n==r.head&&o.number!=(t?l.doc.lines:1)&&(n+=t?1:-1);break}let i=O(o.text,n-o.from,t)+o.from,f=o.text.slice(Math.min(n,i)-o.from,Math.max(n,i)-o.from),u=c(f);if(s!=null&&u!=s)break;(f!=" "||n!=r.head)&&(s=u),n=i}return n}),xe=e=>Dt(e,!1),Lt=e=>Dt(e,!0),Mt=e=>T(e,t=>{let r=e.lineBlockAt(t.head).to;return t.head<r?r:Math.min(e.state.doc.length,t.head+1)}),In=e=>T(e,t=>{let r=e.lineBlockAt(t.head).from;return t.head>r?r:Math.max(0,t.head-1)}),bt=e=>T(e,t=>{let r=e.moveToLineBoundary(t,!1).head;return t.head>r?r:Math.max(0,t.head-1)}),Et=e=>T(e,t=>{let r=e.moveToLineBoundary(t,!0).head;return t.head<r?r:Math.min(e.state.doc.length,t.head+1)}),Rn=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let r=[];for(let n=0,l="",o=e.doc.iter();;){if(o.next(),o.lineBreak||o.done){let c=l.search(/\s+$/);if(c>-1&&r.push({from:n-(l.length-c),to:n}),o.done)break;l=""}else l=o.value;n+=o.value.length}return r.length?(t(e.update({changes:r,userEvent:"delete"})),!0):!1},Tt=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let r=e.changeByRange(n=>({changes:{from:n.from,to:n.to,insert:Ee.of(["",""])},range:h.cursor(n.from)}));return t(e.update(r,{scrollIntoView:!0,userEvent:"input"})),!0},Ot=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let r=e.changeByRange(n=>{if(!n.empty||n.from==0||n.from==e.doc.length)return{range:n};let l=n.from,o=e.doc.lineAt(l),c=l==o.from?l-1:O(o.text,l-o.from,!1)+o.from,s=l==o.to?l+1:O(o.text,l-o.from,!0)+o.from;return{changes:{from:c,to:s,insert:e.doc.slice(l,s).append(e.doc.slice(c,l))},range:h.cursor(s)}});return r.changes.empty?!1:(t(e.update(r,{scrollIntoView:!0,userEvent:"move.character"})),!0)};function _(e){let t=[],r=-1;for(let n of e.selection.ranges){let l=e.doc.lineAt(n.from),o=e.doc.lineAt(n.to);if(!n.empty&&n.to==o.from&&(o=e.doc.lineAt(n.to-1)),r>=l.number){let c=t[t.length-1];c.to=o.to,c.ranges.push(n)}else t.push({from:l.from,to:o.to,ranges:[n]});r=o.number+1}return t}function It(e,t,r){if(e.readOnly)return!1;let n=[],l=[];for(let o of _(e)){if(r?o.to==e.doc.length:o.from==0)continue;let c=e.doc.lineAt(r?o.to+1:o.from-1),s=c.length+1;if(r){n.push({from:o.to,to:c.to},{from:o.from,insert:c.text+e.lineBreak});for(let i of o.ranges)l.push(h.range(Math.min(e.doc.length,i.anchor+s),Math.min(e.doc.length,i.head+s)))}else{n.push({from:c.from,to:o.from},{from:o.to,insert:e.lineBreak+c.text});for(let i of o.ranges)l.push(h.range(i.anchor-s,i.head-s))}}return n.length?(t(e.update({changes:n,scrollIntoView:!0,selection:h.create(l,e.selection.mainIndex),userEvent:"move.line"})),!0):!1}const Rt=({state:e,dispatch:t})=>It(e,t,!1),vt=({state:e,dispatch:t})=>It(e,t,!0);function Ut(e,t,r){if(e.readOnly)return!1;let n=[];for(let l of _(e))r?n.push({from:l.from,insert:e.doc.slice(l.from,l.to)+e.lineBreak}):n.push({from:l.to,insert:e.lineBreak+e.doc.slice(l.from,l.to)});return t(e.update({changes:n,scrollIntoView:!0,userEvent:"input.copyline"})),!0}const Nt=({state:e,dispatch:t})=>Ut(e,t,!1),Vt=({state:e,dispatch:t})=>Ut(e,t,!0),Ft=e=>{if(e.state.readOnly)return!1;let{state:t}=e,r=t.changes(_(t).map(({from:l,to:o})=>(l>0?l--:o<t.doc.length&&o++,{from:l,to:o}))),n=E(t.selection,l=>{let o;if(e.lineWrapping){let c=e.lineBlockAt(l.head),s=e.coordsAtPos(l.head,l.assoc||1);s&&(o=c.bottom+e.documentTop-s.bottom+e.defaultLineHeight/2)}return e.moveVertically(l,!0,o)}).map(r);return e.dispatch({changes:r,selection:n,scrollIntoView:!0,userEvent:"delete.line"}),!0},vn=({state:e,dispatch:t})=>(t(e.update(e.replaceSelection(e.lineBreak),{scrollIntoView:!0,userEvent:"input"})),!0),Un=({state:e,dispatch:t})=>(t(e.update(e.changeByRange(r=>{let n=/^\s*/.exec(e.doc.lineAt(r.from).text)[0];return{changes:{from:r.from,to:r.to,insert:e.lineBreak+n},range:h.cursor(r.from+n.length+1)}}),{scrollIntoView:!0,userEvent:"input"})),!0);function Nn(e,t){if(/\(\)|\[\]|\{\}/.test(e.sliceDoc(t-1,t+1)))return{from:t,to:t};let r=Z(e).resolveInner(t),n=r.childBefore(t),l=r.childAfter(t),o;return n&&l&&n.to<=t&&l.from>=t&&(o=n.type.prop(ee.closedBy))&&o.indexOf(l.name)>-1&&e.doc.lineAt(n.to).from==e.doc.lineAt(l.from).from&&!/\S/.test(e.sliceDoc(n.to,l.from))?{from:n.to,to:l.from}:null}const Gt=Jt(!1),Pt=Jt(!0);function Jt(e){return({state:t,dispatch:r})=>{if(t.readOnly)return!1;let n=t.changeByRange(l=>{let{from:o,to:c}=l,s=t.doc.lineAt(o),i=!e&&o==c&&Nn(t,o);e&&(o=c=(c<=s.to?s:t.doc.lineAt(c)).to);let f=new Te(t,{simulateBreak:o,simulateDoubleBreak:!!i}),u=Oe(f,o);for(u==null&&(u=X(/^\s*/.exec(t.doc.lineAt(o).text)[0],t.tabSize));c<s.to&&/\s/.test(s.text[c-s.from]);)c++;i?{from:o,to:c}=i:o>s.from&&o<s.from+100&&!/\S/.test(s.text.slice(0,o))&&(o=s.from);let a=["",U(t,u)];return i&&a.push(U(t,f.lineIndent(s.from,-1))),{changes:{from:o,to:c,insert:Ee.of(a)},range:h.cursor(o+1+a[1].length)}});return r(t.update(n,{scrollIntoView:!0,userEvent:"input"})),!0}}function De(e,t){let r=-1;return e.changeByRange(n=>{let l=[];for(let c=n.from;c<=n.to;){let s=e.doc.lineAt(c);s.number>r&&(n.empty||n.to>s.from)&&(t(s,l,n),r=s.number),c=s.to+1}let o=e.changes(l);return{changes:l,range:h.range(o.mapPos(n.anchor,1),o.mapPos(n.head,1))}})}const wt=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let r=Object.create(null),n=new Te(e,{overrideIndentation:o=>{let c=r[o];return c??-1}}),l=De(e,(o,c,s)=>{let i=Oe(n,o.from);if(i==null)return;/\S/.test(o.text)||(i=0);let f=/^\s*/.exec(o.text)[0],u=U(e,i);(f!=u||s.from<o.from+f.length)&&(r[o.from]=i,c.push({from:o.from,to:o.from+f.length,insert:u}))});return l.changes.empty||t(e.update(l,{userEvent:"indent"})),!0},Q=({state:e,dispatch:t})=>e.readOnly?!1:(t(e.update(De(e,(r,n)=>{n.push({from:r.from,insert:e.facet(Yt)})}),{userEvent:"input.indent"})),!0),Le=({state:e,dispatch:t})=>e.readOnly?!1:(t(e.update(De(e,(r,n)=>{let l=/^\s*/.exec(r.text)[0];if(!l)return;let o=X(l,e.tabSize),c=0,s=U(e,Math.max(0,o-j(e)));for(;c<l.length&&c<s.length&&l.charCodeAt(c)==s.charCodeAt(c);)c++;n.push({from:r.from+c,to:r.from+l.length,insert:s.slice(c)})}),{userEvent:"delete.dedent"})),!0),zt=e=>(e.setTabFocusMode(),!0),Vn=e=>(e.setTabFocusMode(2e3),!0),Fn=({state:e,dispatch:t})=>e.selection.ranges.some(r=>!r.empty)?Q({state:e,dispatch:t}):(t(e.update(e.replaceSelection(" "),{scrollIntoView:!0,userEvent:"input"})),!0),Wt=[{key:"Ctrl-b",run:ce,shift:ae,preventDefault:!0},{key:"Ctrl-f",run:se,shift:he},{key:"Ctrl-p",run:ie,shift:de},{key:"Ctrl-n",run:ue,shift:me},{key:"Ctrl-a",run:rt,shift:kt},{key:"Ctrl-e",run:ot,shift:At},{key:"Ctrl-d",run:Ce},{key:"Ctrl-h",run:$},{key:"Ctrl-k",run:Mt},{key:"Ctrl-Alt-h",run:xe},{key:"Ctrl-o",run:Tt},{key:"Ctrl-t",run:Ot},{key:"Ctrl-v",run:W}],Ht=[{key:"ArrowLeft",run:ce,shift:ae,preventDefault:!0},{key:"Mod-ArrowLeft",mac:"Alt-ArrowLeft",run:We,shift:st,preventDefault:!0},{mac:"Cmd-ArrowLeft",run:tt,shift:gt,preventDefault:!0},{key:"ArrowRight",run:se,shift:he,preventDefault:!0},{key:"Mod-ArrowRight",mac:"Alt-ArrowRight",run:He,shift:it,preventDefault:!0},{mac:"Cmd-ArrowRight",run:nt,shift:yt,preventDefault:!0},{key:"ArrowUp",run:ie,shift:de,preventDefault:!0},{mac:"Cmd-ArrowUp",run:ye,shift:Ae},{mac:"Ctrl-ArrowUp",run:fe,shift:pe},{key:"ArrowDown",run:ue,shift:me,preventDefault:!0},{mac:"Cmd-ArrowDown",run:ke,shift:Be},{mac:"Ctrl-ArrowDown",run:W,shift:ge},{key:"PageUp",run:fe,shift:pe},{key:"PageDown",run:W,shift:ge},{key:"Home",run:et,shift:pt,preventDefault:!0},{key:"Mod-Home",run:ye,shift:Ae},{key:"End",run:je,shift:mt,preventDefault:!0},{key:"Mod-End",run:ke,shift:Be},{key:"Enter",run:Gt},{key:"Mod-a",run:Bt},{key:"Backspace",run:$,shift:$},{key:"Delete",run:Ce},{key:"Mod-Backspace",mac:"Alt-Backspace",run:xe},{key:"Mod-Delete",mac:"Alt-Delete",run:Lt},{mac:"Mod-Backspace",run:bt},{mac:"Mod-Delete",run:Et}].concat(Wt.map(e=>({mac:e.key,run:e.run,shift:e.shift}))),Gn=[{key:"Alt-ArrowLeft",mac:"Ctrl-ArrowLeft",run:_e,shift:ft},{key:"Alt-ArrowRight",mac:"Ctrl-ArrowRight",run:Qe,shift:at},{key:"Alt-ArrowUp",run:Rt},{key:"Shift-Alt-ArrowUp",run:Nt},{key:"Alt-ArrowDown",run:vt},{key:"Shift-Alt-ArrowDown",run:Vt},{key:"Escape",run:xt},{key:"Mod-Enter",run:Pt},{key:"Alt-l",mac:"Ctrl-l",run:St},{key:"Mod-i",run:Ct,preventDefault:!0},{key:"Mod-[",run:Le},{key:"Mod-]",run:Q},{key:"Mod-Alt-\\",run:wt},{key:"Shift-Mod-k",run:Ft},{key:"Shift-Mod-\\",run:ct},{key:"Mod-/",run:Ie},{key:"Alt-A",run:ve},{key:"Ctrl-m",mac:"Shift-Alt-m",run:zt}].concat(Ht),Pn={key:"Tab",run:Q,shift:Le};export{en as blockComment,tn as blockUncomment,Vt as copyLineDown,Nt as copyLineUp,yn as cursorCharBackward,gn as cursorCharForward,ce as cursorCharLeft,se as cursorCharRight,ke as cursorDocEnd,ye as cursorDocStart,An as cursorGroupBackward,kn as cursorGroupForward,We as cursorGroupLeft,He as cursorGroupRight,et as cursorLineBoundaryBackward,je as cursorLineBoundaryForward,tt as cursorLineBoundaryLeft,nt as cursorLineBoundaryRight,ue as cursorLineDown,ot as cursorLineEnd,rt as cursorLineStart,ie as cursorLineUp,ct as cursorMatchingBracket,W as cursorPageDown,fe as cursorPageUp,Sn as cursorSubwordBackward,Bn as cursorSubwordForward,_e as cursorSyntaxLeft,Qe as cursorSyntaxRight,Gn as defaultKeymap,$ as deleteCharBackward,On as deleteCharBackwardStrict,Ce as deleteCharForward,xe as deleteGroupBackward,Lt as deleteGroupForward,Ft as deleteLine,bt as deleteLineBoundaryBackward,Et as deleteLineBoundaryForward,Mt as deleteToLineEnd,In as deleteToLineStart,Rn as deleteTrailingWhitespace,Wt as emacsStyleKeymap,on as history,ln as historyField,pn as historyKeymap,Le as indentLess,Q as indentMore,wt as indentSelection,Pn as indentWithTab,Pt as insertBlankLine,vn as insertNewline,Gt as insertNewlineAndIndent,Un as insertNewlineKeepIndent,Fn as insertTab,Ve as invertedEffects,Ne as isolateHistory,Zt as lineComment,jt as lineUncomment,vt as moveLineDown,Rt as moveLineUp,G as redo,sn as redoDepth,Pe as redoSelection,Bt as selectAll,Ln as selectCharBackward,Dn as selectCharForward,ae as selectCharLeft,he as selectCharRight,Be as selectDocEnd,Ae as selectDocStart,bn as selectGroupBackward,Mn as selectGroupForward,st as selectGroupLeft,it as selectGroupRight,St as selectLine,pt as selectLineBoundaryBackward,mt as selectLineBoundaryForward,gt as selectLineBoundaryLeft,yt as selectLineBoundaryRight,me as selectLineDown,At as selectLineEnd,kt as selectLineStart,de as selectLineUp,xn as selectMatchingBracket,ge as selectPageDown,pe as selectPageUp,Ct as selectParentSyntax,Tn as selectSubwordBackward,En as selectSubwordForward,ft as selectSyntaxLeft,at as selectSyntaxRight,xt as simplifySelection,Tt as splitLine,Ht as standardKeymap,Vn as temporarilySetTabFocusMode,ve as toggleBlockComment,Ue as toggleBlockCommentByLine,Ie as toggleComment,Re as toggleLineComment,zt as toggleTabFocusMode,Ot as transposeChars,oe as undo,cn as undoDepth,Ge as undoSelection};
|