1 line
31 KiB
JavaScript
1 line
31 KiB
JavaScript
import{Annotation,Facet,EditorSelection,Text,findClusterBreak,countColumn,combineConfig,StateField,Transaction,ChangeSet,ChangeDesc,StateEffect,CharCategory}from"@codemirror/state";import{EditorView,Direction}from"@codemirror/view";import{IndentContext,getIndentation,indentString,matchBrackets,syntaxTree,getIndentUnit,indentUnit}from"@codemirror/language";import{NodeProp}from"@lezer/common";const toggleComment=e=>{let{state:t}=e,n=t.doc.lineAt(t.selection.main.from),r=getConfig(e.state,n.from);return r.line?toggleLineComment(e):!!r.block&&toggleBlockCommentByLine(e)};function command(e,t){return({state:n,dispatch:r})=>{if(n.readOnly)return!1;let o=e(t,n);return!!o&&(r(n.update(o)),!0)}}const toggleLineComment=command(changeLineComment,0),lineComment=command(changeLineComment,1),lineUncomment=command(changeLineComment,2),toggleBlockComment=command(changeBlockComment,0),blockComment=command(changeBlockComment,1),blockUncomment=command(changeBlockComment,2),toggleBlockCommentByLine=command(((e,t)=>changeBlockComment(e,t,selectedLineRanges(t))),0);function getConfig(e,t){let n=e.languageDataAt("commentTokens",t);return n.length?n[0]:{}}const SearchMargin=50;function findBlockComment(e,{open:t,close:n},r,o){let i,l,a=e.sliceDoc(r-SearchMargin,r),s=e.sliceDoc(o,o+SearchMargin),c=/\s*$/.exec(a)[0].length,d=/^\s*/.exec(s)[0].length,u=a.length-c;if(a.slice(u-t.length,u)==t&&s.slice(d,d+n.length)==n)return{open:{pos:r-c,margin:c&&1},close:{pos:o+d,margin:d&&1}};o-r<=2*SearchMargin?i=l=e.sliceDoc(r,o):(i=e.sliceDoc(r,r+SearchMargin),l=e.sliceDoc(o-SearchMargin,o));let h=/^\s*/.exec(i)[0].length,m=/\s*$/.exec(l)[0].length,f=l.length-m-n.length;return i.slice(h,h+t.length)==t&&l.slice(f,f+n.length)==n?{open:{pos:r+h+t.length,margin:/\s/.test(i.charAt(h+t.length))?1:0},close:{pos:o-m-n.length,margin:/\s/.test(l.charAt(f-1))?1:0}}:null}function selectedLineRanges(e){let t=[];for(let n of e.selection.ranges){let r=e.doc.lineAt(n.from),o=n.to<=r.to?r:e.doc.lineAt(n.to),i=t.length-1;i>=0&&t[i].to>r.from?t[i].to=o.to:t.push({from:r.from+/^\s*/.exec(r.text)[0].length,to:o.to})}return t}function changeBlockComment(e,t,n=t.selection.ranges){let r=n.map((e=>getConfig(t,e.from).block));if(!r.every((e=>e)))return null;let o=n.map(((e,n)=>findBlockComment(t,r[n],e.from,e.to)));if(2!=e&&!o.every((e=>e)))return{changes:t.changes(n.map(((e,t)=>o[t]?[]:[{from:e.from,insert:r[t].open+" "},{from:e.to,insert:" "+r[t].close}])))};if(1!=e&&o.some((e=>e))){let e=[];for(let t,n=0;n<o.length;n++)if(t=o[n]){let o=r[n],{open:i,close:l}=t;e.push({from:i.pos-o.open.length,to:i.pos+i.margin},{from:l.pos-l.margin,to:l.pos+o.close.length})}return{changes:e}}return null}function changeLineComment(e,t,n=t.selection.ranges){let r=[],o=-1;for(let{from:e,to:i}of n){let n=r.length,l=1e9,a=getConfig(t,e).line;if(a){for(let n=e;n<=i;){let s=t.doc.lineAt(n);if(s.from>o&&(e==i||i>s.from)){o=s.from;let e=/^\s*/.exec(s.text)[0].length,t=e==s.length,n=s.text.slice(e,e+a.length)==a?e:-1;e<s.text.length&&e<l&&(l=e),r.push({line:s,comment:n,token:a,indent:e,empty:t,single:!1})}n=s.to+1}if(l<1e9)for(let e=n;e<r.length;e++)r[e].indent<r[e].line.text.length&&(r[e].indent=l);r.length==n+1&&(r[n].single=!0)}}if(2!=e&&r.some((e=>e.comment<0&&(!e.empty||e.single)))){let e=[];for(let{line:t,token:n,indent:o,empty:i,single:l}of r)!l&&i||e.push({from:t.from+o,insert:n+" "});let n=t.changes(e);return{changes:n,selection:t.selection.map(n,1)}}if(1!=e&&r.some((e=>e.comment>=0))){let e=[];for(let{line:t,comment:n,token:o}of r)if(n>=0){let r=t.from+n,i=r+o.length;" "==t.text[i-t.from]&&i++,e.push({from:r,to:i})}return{changes:e}}return null}const fromHistory=Annotation.define(),isolateHistory=Annotation.define(),invertedEffects=Facet.define(),historyConfig=Facet.define({combine:e=>combineConfig(e,{minDepth:100,newGroupDelay:500,joinToEvent:(e,t)=>t},{minDepth:Math.max,newGroupDelay:Math.min,joinToEvent:(e,t)=>(n,r)=>e(n,r)||t(n,r)})});function changeEnd(e){let t=0;return e.iterChangedRanges(((e,n)=>t=n)),t}const historyField_=StateField.define({create:()=>HistoryState.empty,update(e,t){let n=t.state.facet(historyConfig),r=t.annotation(fromHistory);if(r){let o=t.docChanged?EditorSelection.single(changeEnd(t.changes)):void 0,i=HistEvent.fromTransaction(t,o),l=r.side,a=0==l?e.undone:e.done;return a=i?updateBranch(a,a.length,n.minDepth,i):addSelection(a,t.startState.selection),new HistoryState(0==l?r.rest:a,0==l?a:r.rest)}let o=t.annotation(isolateHistory);if("full"!=o&&"before"!=o||(e=e.isolate()),!1===t.annotation(Transaction.addToHistory))return t.changes.empty?e:e.addMapping(t.changes.desc);let i=HistEvent.fromTransaction(t),l=t.annotation(Transaction.time),a=t.annotation(Transaction.userEvent);return i?e=e.addChanges(i,l,a,n,t):t.selection&&(e=e.addSelection(t.startState.selection,l,a,n.newGroupDelay)),"full"!=o&&"after"!=o||(e=e.isolate()),e},toJSON:e=>({done:e.done.map((e=>e.toJSON())),undone:e.undone.map((e=>e.toJSON()))}),fromJSON:e=>new HistoryState(e.done.map(HistEvent.fromJSON),e.undone.map(HistEvent.fromJSON))});function history(e={}){return[historyField_,historyConfig.of(e),EditorView.domEventHandlers({beforeinput(e,t){let n="historyUndo"==e.inputType?undo:"historyRedo"==e.inputType?redo:null;return!!n&&(e.preventDefault(),n(t))}})]}const historyField=historyField_;function cmd(e,t){return function({state:n,dispatch:r}){if(!t&&n.readOnly)return!1;let o=n.field(historyField_,!1);if(!o)return!1;let i=o.pop(e,n,t);return!!i&&(r(i),!0)}}const undo=cmd(0,!1),redo=cmd(1,!1),undoSelection=cmd(0,!0),redoSelection=cmd(1,!0);function depth(e){return function(t){let n=t.field(historyField_,!1);if(!n)return 0;let r=0==e?n.done:n.undone;return r.length-(r.length&&!r[0].changes?1:0)}}const undoDepth=depth(0),redoDepth=depth(1);class HistEvent{constructor(e,t,n,r,o){this.changes=e,this.effects=t,this.mapped=n,this.startSelection=r,this.selectionsAfter=o}setSelAfter(e){return new HistEvent(this.changes,this.effects,this.mapped,this.startSelection,e)}toJSON(){var e,t,n;return{changes:null===(e=this.changes)||void 0===e?void 0:e.toJSON(),mapped:null===(t=this.mapped)||void 0===t?void 0:t.toJSON(),startSelection:null===(n=this.startSelection)||void 0===n?void 0:n.toJSON(),selectionsAfter:this.selectionsAfter.map((e=>e.toJSON()))}}static fromJSON(e){return new HistEvent(e.changes&&ChangeSet.fromJSON(e.changes),[],e.mapped&&ChangeDesc.fromJSON(e.mapped),e.startSelection&&EditorSelection.fromJSON(e.startSelection),e.selectionsAfter.map(EditorSelection.fromJSON))}static fromTransaction(e,t){let n=none;for(let t of e.startState.facet(invertedEffects)){let r=t(e);r.length&&(n=n.concat(r))}return!n.length&&e.changes.empty?null:new HistEvent(e.changes.invert(e.startState.doc),n,void 0,t||e.startState.selection,none)}static selection(e){return new HistEvent(void 0,none,void 0,void 0,e)}}function updateBranch(e,t,n,r){let o=t+1>n+20?t-n-1:0,i=e.slice(o,t);return i.push(r),i}function isAdjacent(e,t){let n=[],r=!1;return e.iterChangedRanges(((e,t)=>n.push(e,t))),t.iterChangedRanges(((e,t,o,i)=>{for(let e=0;e<n.length;){let t=n[e++],l=n[e++];i>=t&&o<=l&&(r=!0)}})),r}function eqSelectionShape(e,t){return e.ranges.length==t.ranges.length&&0===e.ranges.filter(((e,n)=>e.empty!=t.ranges[n].empty)).length}function conc(e,t){return e.length?t.length?e.concat(t):e:t}const none=[],MaxSelectionsPerEvent=200;function addSelection(e,t){if(e.length){let n=e[e.length-1],r=n.selectionsAfter.slice(Math.max(0,n.selectionsAfter.length-MaxSelectionsPerEvent));return r.length&&r[r.length-1].eq(t)?e:(r.push(t),updateBranch(e,e.length-1,1e9,n.setSelAfter(r)))}return[HistEvent.selection([t])]}function popSelection(e){let t=e[e.length-1],n=e.slice();return n[e.length-1]=t.setSelAfter(t.selectionsAfter.slice(0,t.selectionsAfter.length-1)),n}function addMappingToBranch(e,t){if(!e.length)return e;let n=e.length,r=none;for(;n;){let o=mapEvent(e[n-1],t,r);if(o.changes&&!o.changes.empty||o.effects.length){let t=e.slice(0,n);return t[n-1]=o,t}t=o.mapped,n--,r=o.selectionsAfter}return r.length?[HistEvent.selection(r)]:none}function mapEvent(e,t,n){let r=conc(e.selectionsAfter.length?e.selectionsAfter.map((e=>e.map(t))):none,n);if(!e.changes)return HistEvent.selection(r);let o=e.changes.map(t),i=t.mapDesc(e.changes,!0),l=e.mapped?e.mapped.composeDesc(i):i;return new HistEvent(o,StateEffect.mapEffects(e.effects,t),l,e.startSelection.map(i),r)}const joinableUserEvent=/^(input\.type|delete)($|\.)/;class HistoryState{constructor(e,t,n=0,r=void 0){this.done=e,this.undone=t,this.prevTime=n,this.prevUserEvent=r}isolate(){return this.prevTime?new HistoryState(this.done,this.undone):this}addChanges(e,t,n,r,o){let i=this.done,l=i[i.length-1];return i=l&&l.changes&&!l.changes.empty&&e.changes&&(!n||joinableUserEvent.test(n))&&(!l.selectionsAfter.length&&t-this.prevTime<r.newGroupDelay&&r.joinToEvent(o,isAdjacent(l.changes,e.changes))||"input.type.compose"==n)?updateBranch(i,i.length-1,r.minDepth,new HistEvent(e.changes.compose(l.changes),conc(e.effects,l.effects),l.mapped,l.startSelection,none)):updateBranch(i,i.length,r.minDepth,e),new HistoryState(i,none,t,n)}addSelection(e,t,n,r){let o=this.done.length?this.done[this.done.length-1].selectionsAfter:none;return o.length>0&&t-this.prevTime<r&&n==this.prevUserEvent&&n&&/^select($|\.)/.test(n)&&eqSelectionShape(o[o.length-1],e)?this:new HistoryState(addSelection(this.done,e),this.undone,t,n)}addMapping(e){return new HistoryState(addMappingToBranch(this.done,e),addMappingToBranch(this.undone,e),this.prevTime,this.prevUserEvent)}pop(e,t,n){let r=0==e?this.done:this.undone;if(0==r.length)return null;let o=r[r.length-1];if(n&&o.selectionsAfter.length)return t.update({selection:o.selectionsAfter[o.selectionsAfter.length-1],annotations:fromHistory.of({side:e,rest:popSelection(r)}),userEvent:0==e?"select.undo":"select.redo",scrollIntoView:!0});if(o.changes){let n=1==r.length?none:r.slice(0,r.length-1);return o.mapped&&(n=addMappingToBranch(n,o.mapped)),t.update({changes:o.changes,selection:o.startSelection,effects:o.effects,annotations:fromHistory.of({side:e,rest:n}),filter:!1,userEvent:0==e?"undo":"redo",scrollIntoView:!0})}return null}}HistoryState.empty=new HistoryState(none,none);const historyKeymap=[{key:"Mod-z",run:undo,preventDefault:!0},{key:"Mod-y",mac:"Mod-Shift-z",run:redo,preventDefault:!0},{linux:"Ctrl-Shift-z",run:redo,preventDefault:!0},{key:"Mod-u",run:undoSelection,preventDefault:!0},{key:"Alt-u",mac:"Mod-Shift-u",run:redoSelection,preventDefault:!0}];function updateSel(e,t){return EditorSelection.create(e.ranges.map(t),e.mainIndex)}function setSel(e,t){return e.update({selection:t,scrollIntoView:!0,userEvent:"select"})}function moveSel({state:e,dispatch:t},n){let r=updateSel(e.selection,n);return!r.eq(e.selection)&&(t(setSel(e,r)),!0)}function rangeEnd(e,t){return EditorSelection.cursor(t?e.to:e.from)}function cursorByChar(e,t){return moveSel(e,(n=>n.empty?e.moveByChar(n,t):rangeEnd(n,t)))}function ltrAtCursor(e){return e.textDirectionAt(e.state.selection.main.head)==Direction.LTR}const cursorCharLeft=e=>cursorByChar(e,!ltrAtCursor(e)),cursorCharRight=e=>cursorByChar(e,ltrAtCursor(e)),cursorCharForward=e=>cursorByChar(e,!0),cursorCharBackward=e=>cursorByChar(e,!1);function cursorByGroup(e,t){return moveSel(e,(n=>n.empty?e.moveByGroup(n,t):rangeEnd(n,t)))}const cursorGroupLeft=e=>cursorByGroup(e,!ltrAtCursor(e)),cursorGroupRight=e=>cursorByGroup(e,ltrAtCursor(e)),cursorGroupForward=e=>cursorByGroup(e,!0),cursorGroupBackward=e=>cursorByGroup(e,!1),segmenter="undefined"!=typeof Intl&&Intl.Segmenter?new Intl.Segmenter(void 0,{granularity:"word"}):null;function moveBySubword(e,t,n){let r=e.state.charCategorizer(t.from),o=CharCategory.Space,i=t.from,l=0,a=!1,s=!1,c=!1,d=t=>{if(a)return!1;i+=n?t.length:-t.length;let d,u=r(t);if(u==CharCategory.Word&&t.charCodeAt(0)<128&&/[\W_]/.test(t)&&(u=-1),o==CharCategory.Space&&(o=u),o!=u)return!1;if(o==CharCategory.Word)if(t.toLowerCase()==t){if(!n&&s)return!1;c=!0}else if(c){if(n)return!1;a=!0}else{if(s&&n&&r(d=e.state.sliceDoc(i,i+1))==CharCategory.Word&&d.toLowerCase()==d)return!1;s=!0}return l++,!0},u=e.moveByChar(t,n,(e=>(d(e),d)));if(segmenter&&o==CharCategory.Word&&u.from==t.from+l*(n?1:-1)){let r=Math.min(t.head,u.head),o=Math.max(t.head,u.head),i=e.state.sliceDoc(r,o);if(i.length>1&&/[\u4E00-\uffff]/.test(i)){let e=Array.from(segmenter.segment(i));if(e.length>1)return n?EditorSelection.cursor(t.head+e[1].index,-1):EditorSelection.cursor(u.head+e[e.length-1].index,1)}}return u}function cursorBySubword(e,t){return moveSel(e,(n=>n.empty?moveBySubword(e,n,t):rangeEnd(n,t)))}const cursorSubwordForward=e=>cursorBySubword(e,!0),cursorSubwordBackward=e=>cursorBySubword(e,!1);function interestingNode(e,t,n){if(t.type.prop(n))return!0;let r=t.to-t.from;return r&&(r>2||/[^\s,.;:]/.test(e.sliceDoc(t.from,t.to)))||t.firstChild}function moveBySyntax(e,t,n){let r,o,i=syntaxTree(e).resolveInner(t.head),l=n?NodeProp.closedBy:NodeProp.openedBy;for(let r=t.head;;){let t=n?i.childAfter(r):i.childBefore(r);if(!t)break;interestingNode(e,t,l)?i=t:r=n?t.to:t.from}return o=i.type.prop(l)&&(r=n?matchBrackets(e,i.from,1):matchBrackets(e,i.to,-1))&&r.matched?n?r.end.to:r.end.from:n?i.to:i.from,EditorSelection.cursor(o,n?-1:1)}const cursorSyntaxLeft=e=>moveSel(e,(t=>moveBySyntax(e.state,t,!ltrAtCursor(e)))),cursorSyntaxRight=e=>moveSel(e,(t=>moveBySyntax(e.state,t,ltrAtCursor(e))));function cursorByLine(e,t){return moveSel(e,(n=>{if(!n.empty)return rangeEnd(n,t);let r=e.moveVertically(n,t);return r.head!=n.head?r:e.moveToLineBoundary(n,t)}))}const cursorLineUp=e=>cursorByLine(e,!1),cursorLineDown=e=>cursorByLine(e,!0);function pageInfo(e){let t,n=e.scrollDOM.clientHeight<e.scrollDOM.scrollHeight-2,r=0,o=0;if(n){for(let t of e.state.facet(EditorView.scrollMargins)){let n=t(e);(null==n?void 0:n.top)&&(r=Math.max(null==n?void 0:n.top,r)),(null==n?void 0:n.bottom)&&(o=Math.max(null==n?void 0:n.bottom,o))}t=e.scrollDOM.clientHeight-r-o}else t=(e.dom.ownerDocument.defaultView||window).innerHeight;return{marginTop:r,marginBottom:o,selfScroll:n,height:Math.max(e.defaultLineHeight,t-5)}}function cursorByPage(e,t){let n,r=pageInfo(e),{state:o}=e,i=updateSel(o.selection,(n=>n.empty?e.moveVertically(n,t,r.height):rangeEnd(n,t)));if(i.eq(o.selection))return!1;if(r.selfScroll){let t=e.coordsAtPos(o.selection.main.head),l=e.scrollDOM.getBoundingClientRect(),a=l.top+r.marginTop,s=l.bottom-r.marginBottom;t&&t.top>a&&t.bottom<s&&(n=EditorView.scrollIntoView(i.main.head,{y:"start",yMargin:t.top-a}))}return e.dispatch(setSel(o,i),{effects:n}),!0}const cursorPageUp=e=>cursorByPage(e,!1),cursorPageDown=e=>cursorByPage(e,!0);function moveByLineBoundary(e,t,n){let r=e.lineBlockAt(t.head),o=e.moveToLineBoundary(t,n);if(o.head==t.head&&o.head!=(n?r.to:r.from)&&(o=e.moveToLineBoundary(t,n,!1)),!n&&o.head==r.from&&r.length){let n=/^\s*/.exec(e.state.sliceDoc(r.from,Math.min(r.from+100,r.to)))[0].length;n&&t.head!=r.from+n&&(o=EditorSelection.cursor(r.from+n))}return o}const cursorLineBoundaryForward=e=>moveSel(e,(t=>moveByLineBoundary(e,t,!0))),cursorLineBoundaryBackward=e=>moveSel(e,(t=>moveByLineBoundary(e,t,!1))),cursorLineBoundaryLeft=e=>moveSel(e,(t=>moveByLineBoundary(e,t,!ltrAtCursor(e)))),cursorLineBoundaryRight=e=>moveSel(e,(t=>moveByLineBoundary(e,t,ltrAtCursor(e)))),cursorLineStart=e=>moveSel(e,(t=>EditorSelection.cursor(e.lineBlockAt(t.head).from,1))),cursorLineEnd=e=>moveSel(e,(t=>EditorSelection.cursor(e.lineBlockAt(t.head).to,-1)));function toMatchingBracket(e,t,n){let r=!1,o=updateSel(e.selection,(t=>{let o=matchBrackets(e,t.head,-1)||matchBrackets(e,t.head,1)||t.head>0&&matchBrackets(e,t.head-1,1)||t.head<e.doc.length&&matchBrackets(e,t.head+1,-1);if(!o||!o.end)return t;r=!0;let i=o.start.from==t.head?o.end.to:o.end.from;return n?EditorSelection.range(t.anchor,i):EditorSelection.cursor(i)}));return!!r&&(t(setSel(e,o)),!0)}const cursorMatchingBracket=({state:e,dispatch:t})=>toMatchingBracket(e,t,!1),selectMatchingBracket=({state:e,dispatch:t})=>toMatchingBracket(e,t,!0);function extendSel(e,t){let n=updateSel(e.state.selection,(e=>{let n=t(e);return EditorSelection.range(e.anchor,n.head,n.goalColumn,n.bidiLevel||void 0)}));return!n.eq(e.state.selection)&&(e.dispatch(setSel(e.state,n)),!0)}function selectByChar(e,t){return extendSel(e,(n=>e.moveByChar(n,t)))}const selectCharLeft=e=>selectByChar(e,!ltrAtCursor(e)),selectCharRight=e=>selectByChar(e,ltrAtCursor(e)),selectCharForward=e=>selectByChar(e,!0),selectCharBackward=e=>selectByChar(e,!1);function selectByGroup(e,t){return extendSel(e,(n=>e.moveByGroup(n,t)))}const selectGroupLeft=e=>selectByGroup(e,!ltrAtCursor(e)),selectGroupRight=e=>selectByGroup(e,ltrAtCursor(e)),selectGroupForward=e=>selectByGroup(e,!0),selectGroupBackward=e=>selectByGroup(e,!1);function selectBySubword(e,t){return extendSel(e,(n=>moveBySubword(e,n,t)))}const selectSubwordForward=e=>selectBySubword(e,!0),selectSubwordBackward=e=>selectBySubword(e,!1),selectSyntaxLeft=e=>extendSel(e,(t=>moveBySyntax(e.state,t,!ltrAtCursor(e)))),selectSyntaxRight=e=>extendSel(e,(t=>moveBySyntax(e.state,t,ltrAtCursor(e))));function selectByLine(e,t){return extendSel(e,(n=>e.moveVertically(n,t)))}const selectLineUp=e=>selectByLine(e,!1),selectLineDown=e=>selectByLine(e,!0);function selectByPage(e,t){return extendSel(e,(n=>e.moveVertically(n,t,pageInfo(e).height)))}const selectPageUp=e=>selectByPage(e,!1),selectPageDown=e=>selectByPage(e,!0),selectLineBoundaryForward=e=>extendSel(e,(t=>moveByLineBoundary(e,t,!0))),selectLineBoundaryBackward=e=>extendSel(e,(t=>moveByLineBoundary(e,t,!1))),selectLineBoundaryLeft=e=>extendSel(e,(t=>moveByLineBoundary(e,t,!ltrAtCursor(e)))),selectLineBoundaryRight=e=>extendSel(e,(t=>moveByLineBoundary(e,t,ltrAtCursor(e)))),selectLineStart=e=>extendSel(e,(t=>EditorSelection.cursor(e.lineBlockAt(t.head).from))),selectLineEnd=e=>extendSel(e,(t=>EditorSelection.cursor(e.lineBlockAt(t.head).to))),cursorDocStart=({state:e,dispatch:t})=>(t(setSel(e,{anchor:0})),!0),cursorDocEnd=({state:e,dispatch:t})=>(t(setSel(e,{anchor:e.doc.length})),!0),selectDocStart=({state:e,dispatch:t})=>(t(setSel(e,{anchor:e.selection.main.anchor,head:0})),!0),selectDocEnd=({state:e,dispatch:t})=>(t(setSel(e,{anchor:e.selection.main.anchor,head:e.doc.length})),!0),selectAll=({state:e,dispatch:t})=>(t(e.update({selection:{anchor:0,head:e.doc.length},userEvent:"select"})),!0),selectLine=({state:e,dispatch:t})=>{let n=selectedLineBlocks(e).map((({from:t,to:n})=>EditorSelection.range(t,Math.min(n+1,e.doc.length))));return t(e.update({selection:EditorSelection.create(n),userEvent:"select"})),!0},selectParentSyntax=({state:e,dispatch:t})=>{let n=updateSel(e.selection,(t=>{var n;let r=syntaxTree(e).resolveInner(t.head,1);for(;!(r.from<t.from&&r.to>=t.to||r.to>t.to&&r.from<=t.from)&&(null===(n=r.parent)||void 0===n?void 0:n.parent);)r=r.parent;return EditorSelection.range(r.to,r.from)}));return t(setSel(e,n)),!0},simplifySelection=({state:e,dispatch:t})=>{let n=e.selection,r=null;return n.ranges.length>1?r=EditorSelection.create([n.main]):n.main.empty||(r=EditorSelection.create([EditorSelection.cursor(n.main.head)])),!!r&&(t(setSel(e,r)),!0)};function deleteBy(e,t){if(e.state.readOnly)return!1;let n="delete.selection",{state:r}=e,o=r.changeByRange((r=>{let{from:o,to:i}=r;if(o==i){let r=t(o);r<o?(n="delete.backward",r=skipAtomic(e,r,!1)):r>o&&(n="delete.forward",r=skipAtomic(e,r,!0)),o=Math.min(o,r),i=Math.max(i,r)}else o=skipAtomic(e,o,!1),i=skipAtomic(e,i,!0);return o==i?{range:r}:{changes:{from:o,to:i},range:EditorSelection.cursor(o)}}));return!o.changes.empty&&(e.dispatch(r.update(o,{scrollIntoView:!0,userEvent:n,effects:"delete.selection"==n?EditorView.announce.of(r.phrase("Selection deleted")):void 0})),!0)}function skipAtomic(e,t,n){if(e instanceof EditorView)for(let r of e.state.facet(EditorView.atomicRanges).map((t=>t(e))))r.between(t,t,((e,r)=>{e<t&&r>t&&(t=n?r:e)}));return t}const deleteByChar=(e,t)=>deleteBy(e,(n=>{let r,o,{state:i}=e,l=i.doc.lineAt(n);if(!t&&n>l.from&&n<l.from+200&&!/[^ \t]/.test(r=l.text.slice(0,n-l.from))){if("\t"==r[r.length-1])return n-1;let e=countColumn(r,i.tabSize)%getIndentUnit(i)||getIndentUnit(i);for(let t=0;t<e&&" "==r[r.length-1-t];t++)n--;o=n}else o=findClusterBreak(l.text,n-l.from,t,t)+l.from,o==n&&l.number!=(t?i.doc.lines:1)&&(o+=t?1:-1);return o})),deleteCharBackward=e=>deleteByChar(e,!1),deleteCharForward=e=>deleteByChar(e,!0),deleteByGroup=(e,t)=>deleteBy(e,(n=>{let r=n,{state:o}=e,i=o.doc.lineAt(r),l=o.charCategorizer(r);for(let e=null;;){if(r==(t?i.to:i.from)){r==n&&i.number!=(t?o.doc.lines:1)&&(r+=t?1:-1);break}let a=findClusterBreak(i.text,r-i.from,t)+i.from,s=i.text.slice(Math.min(r,a)-i.from,Math.max(r,a)-i.from),c=l(s);if(null!=e&&c!=e)break;" "==s&&r==n||(e=c),r=a}return r})),deleteGroupBackward=e=>deleteByGroup(e,!1),deleteGroupForward=e=>deleteByGroup(e,!0),deleteToLineEnd=e=>deleteBy(e,(t=>{let n=e.lineBlockAt(t).to;return t<n?n:Math.min(e.state.doc.length,t+1)})),deleteToLineStart=e=>deleteBy(e,(t=>{let n=e.lineBlockAt(t).from;return t>n?n:Math.max(0,t-1)})),deleteTrailingWhitespace=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let n=[];for(let t=0,r="",o=e.doc.iter();;){if(o.next(),o.lineBreak||o.done){let e=r.search(/\s+$/);if(e>-1&&n.push({from:t-(r.length-e),to:t}),o.done)break;r=""}else r=o.value;t+=o.value.length}return!!n.length&&(t(e.update({changes:n,userEvent:"delete"})),!0)},splitLine=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let n=e.changeByRange((e=>({changes:{from:e.from,to:e.to,insert:Text.of(["",""])},range:EditorSelection.cursor(e.from)})));return t(e.update(n,{scrollIntoView:!0,userEvent:"input"})),!0},transposeChars=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let n=e.changeByRange((t=>{if(!t.empty||0==t.from||t.from==e.doc.length)return{range:t};let n=t.from,r=e.doc.lineAt(n),o=n==r.from?n-1:findClusterBreak(r.text,n-r.from,!1)+r.from,i=n==r.to?n+1:findClusterBreak(r.text,n-r.from,!0)+r.from;return{changes:{from:o,to:i,insert:e.doc.slice(n,i).append(e.doc.slice(o,n))},range:EditorSelection.cursor(i)}}));return!n.changes.empty&&(t(e.update(n,{scrollIntoView:!0,userEvent:"move.character"})),!0)};function selectedLineBlocks(e){let t=[],n=-1;for(let r of e.selection.ranges){let o=e.doc.lineAt(r.from),i=e.doc.lineAt(r.to);if(r.empty||r.to!=i.from||(i=e.doc.lineAt(r.to-1)),n>=o.number){let e=t[t.length-1];e.to=i.to,e.ranges.push(r)}else t.push({from:o.from,to:i.to,ranges:[r]});n=i.number+1}return t}function moveLine(e,t,n){if(e.readOnly)return!1;let r=[],o=[];for(let t of selectedLineBlocks(e)){if(n?t.to==e.doc.length:0==t.from)continue;let i=e.doc.lineAt(n?t.to+1:t.from-1),l=i.length+1;if(n){r.push({from:t.to,to:i.to},{from:t.from,insert:i.text+e.lineBreak});for(let n of t.ranges)o.push(EditorSelection.range(Math.min(e.doc.length,n.anchor+l),Math.min(e.doc.length,n.head+l)))}else{r.push({from:i.from,to:t.from},{from:t.to,insert:e.lineBreak+i.text});for(let e of t.ranges)o.push(EditorSelection.range(e.anchor-l,e.head-l))}}return!!r.length&&(t(e.update({changes:r,scrollIntoView:!0,selection:EditorSelection.create(o,e.selection.mainIndex),userEvent:"move.line"})),!0)}const moveLineUp=({state:e,dispatch:t})=>moveLine(e,t,!1),moveLineDown=({state:e,dispatch:t})=>moveLine(e,t,!0);function copyLine(e,t,n){if(e.readOnly)return!1;let r=[];for(let t of selectedLineBlocks(e))n?r.push({from:t.from,insert:e.doc.slice(t.from,t.to)+e.lineBreak}):r.push({from:t.to,insert:e.lineBreak+e.doc.slice(t.from,t.to)});return t(e.update({changes:r,scrollIntoView:!0,userEvent:"input.copyline"})),!0}const copyLineUp=({state:e,dispatch:t})=>copyLine(e,t,!1),copyLineDown=({state:e,dispatch:t})=>copyLine(e,t,!0),deleteLine=e=>{if(e.state.readOnly)return!1;let{state:t}=e,n=t.changes(selectedLineBlocks(t).map((({from:e,to:n})=>(e>0?e--:n<t.doc.length&&n++,{from:e,to:n})))),r=updateSel(t.selection,(t=>e.moveVertically(t,!0))).map(n);return e.dispatch({changes:n,selection:r,scrollIntoView:!0,userEvent:"delete.line"}),!0},insertNewline=({state:e,dispatch:t})=>(t(e.update(e.replaceSelection(e.lineBreak),{scrollIntoView:!0,userEvent:"input"})),!0);function isBetweenBrackets(e,t){if(/\(\)|\[\]|\{\}/.test(e.sliceDoc(t-1,t+1)))return{from:t,to:t};let n,r=syntaxTree(e).resolveInner(t),o=r.childBefore(t),i=r.childAfter(t);return o&&i&&o.to<=t&&i.from>=t&&(n=o.type.prop(NodeProp.closedBy))&&n.indexOf(i.name)>-1&&e.doc.lineAt(o.to).from==e.doc.lineAt(i.from).from&&!/\S/.test(e.sliceDoc(o.to,i.from))?{from:o.to,to:i.from}:null}const insertNewlineAndIndent=newlineAndIndent(!1),insertBlankLine=newlineAndIndent(!0);function newlineAndIndent(e){return({state:t,dispatch:n})=>{if(t.readOnly)return!1;let r=t.changeByRange((n=>{let{from:r,to:o}=n,i=t.doc.lineAt(r),l=!e&&r==o&&isBetweenBrackets(t,r);e&&(r=o=(o<=i.to?i:t.doc.lineAt(o)).to);let a=new IndentContext(t,{simulateBreak:r,simulateDoubleBreak:!!l}),s=getIndentation(a,r);for(null==s&&(s=countColumn(/^\s*/.exec(t.doc.lineAt(r).text)[0],t.tabSize));o<i.to&&/\s/.test(i.text[o-i.from]);)o++;l?({from:r,to:o}=l):r>i.from&&r<i.from+100&&!/\S/.test(i.text.slice(0,r))&&(r=i.from);let c=["",indentString(t,s)];return l&&c.push(indentString(t,a.lineIndent(i.from,-1))),{changes:{from:r,to:o,insert:Text.of(c)},range:EditorSelection.cursor(r+1+c[1].length)}}));return n(t.update(r,{scrollIntoView:!0,userEvent:"input"})),!0}}function changeBySelectedLine(e,t){let n=-1;return e.changeByRange((r=>{let o=[];for(let i=r.from;i<=r.to;){let l=e.doc.lineAt(i);l.number>n&&(r.empty||r.to>l.from)&&(t(l,o,r),n=l.number),i=l.to+1}let i=e.changes(o);return{changes:o,range:EditorSelection.range(i.mapPos(r.anchor,1),i.mapPos(r.head,1))}}))}const indentSelection=({state:e,dispatch:t})=>{if(e.readOnly)return!1;let n=Object.create(null),r=new IndentContext(e,{overrideIndentation:e=>{let t=n[e];return null==t?-1:t}}),o=changeBySelectedLine(e,((t,o,i)=>{let l=getIndentation(r,t.from);if(null==l)return;/\S/.test(t.text)||(l=0);let a=/^\s*/.exec(t.text)[0],s=indentString(e,l);(a!=s||i.from<t.from+a.length)&&(n[t.from]=l,o.push({from:t.from,to:t.from+a.length,insert:s}))}));return o.changes.empty||t(e.update(o,{userEvent:"indent"})),!0},indentMore=({state:e,dispatch:t})=>!e.readOnly&&(t(e.update(changeBySelectedLine(e,((t,n)=>{n.push({from:t.from,insert:e.facet(indentUnit)})})),{userEvent:"input.indent"})),!0),indentLess=({state:e,dispatch:t})=>!e.readOnly&&(t(e.update(changeBySelectedLine(e,((t,n)=>{let r=/^\s*/.exec(t.text)[0];if(!r)return;let o=countColumn(r,e.tabSize),i=0,l=indentString(e,Math.max(0,o-getIndentUnit(e)));for(;i<r.length&&i<l.length&&r.charCodeAt(i)==l.charCodeAt(i);)i++;n.push({from:t.from+i,to:t.from+r.length,insert:l.slice(i)})})),{userEvent:"delete.dedent"})),!0),insertTab=({state:e,dispatch:t})=>e.selection.ranges.some((e=>!e.empty))?indentMore({state:e,dispatch:t}):(t(e.update(e.replaceSelection("\t"),{scrollIntoView:!0,userEvent:"input"})),!0),emacsStyleKeymap=[{key:"Ctrl-b",run:cursorCharLeft,shift:selectCharLeft,preventDefault:!0},{key:"Ctrl-f",run:cursorCharRight,shift:selectCharRight},{key:"Ctrl-p",run:cursorLineUp,shift:selectLineUp},{key:"Ctrl-n",run:cursorLineDown,shift:selectLineDown},{key:"Ctrl-a",run:cursorLineStart,shift:selectLineStart},{key:"Ctrl-e",run:cursorLineEnd,shift:selectLineEnd},{key:"Ctrl-d",run:deleteCharForward},{key:"Ctrl-h",run:deleteCharBackward},{key:"Ctrl-k",run:deleteToLineEnd},{key:"Ctrl-Alt-h",run:deleteGroupBackward},{key:"Ctrl-o",run:splitLine},{key:"Ctrl-t",run:transposeChars},{key:"Ctrl-v",run:cursorPageDown}],standardKeymap=[{key:"ArrowLeft",run:cursorCharLeft,shift:selectCharLeft,preventDefault:!0},{key:"Mod-ArrowLeft",mac:"Alt-ArrowLeft",run:cursorGroupLeft,shift:selectGroupLeft,preventDefault:!0},{mac:"Cmd-ArrowLeft",run:cursorLineBoundaryLeft,shift:selectLineBoundaryLeft,preventDefault:!0},{key:"ArrowRight",run:cursorCharRight,shift:selectCharRight,preventDefault:!0},{key:"Mod-ArrowRight",mac:"Alt-ArrowRight",run:cursorGroupRight,shift:selectGroupRight,preventDefault:!0},{mac:"Cmd-ArrowRight",run:cursorLineBoundaryRight,shift:selectLineBoundaryRight,preventDefault:!0},{key:"ArrowUp",run:cursorLineUp,shift:selectLineUp,preventDefault:!0},{mac:"Cmd-ArrowUp",run:cursorDocStart,shift:selectDocStart},{mac:"Ctrl-ArrowUp",run:cursorPageUp,shift:selectPageUp},{key:"ArrowDown",run:cursorLineDown,shift:selectLineDown,preventDefault:!0},{mac:"Cmd-ArrowDown",run:cursorDocEnd,shift:selectDocEnd},{mac:"Ctrl-ArrowDown",run:cursorPageDown,shift:selectPageDown},{key:"PageUp",run:cursorPageUp,shift:selectPageUp},{key:"PageDown",run:cursorPageDown,shift:selectPageDown},{key:"Home",run:cursorLineBoundaryBackward,shift:selectLineBoundaryBackward,preventDefault:!0},{key:"Mod-Home",run:cursorDocStart,shift:selectDocStart},{key:"End",run:cursorLineBoundaryForward,shift:selectLineBoundaryForward,preventDefault:!0},{key:"Mod-End",run:cursorDocEnd,shift:selectDocEnd},{key:"Enter",run:insertNewlineAndIndent},{key:"Mod-a",run:selectAll},{key:"Backspace",run:deleteCharBackward,shift:deleteCharBackward},{key:"Delete",run:deleteCharForward},{key:"Mod-Backspace",mac:"Alt-Backspace",run:deleteGroupBackward},{key:"Mod-Delete",mac:"Alt-Delete",run:deleteGroupForward},{mac:"Mod-Backspace",run:deleteToLineStart},{mac:"Mod-Delete",run:deleteToLineEnd}].concat(emacsStyleKeymap.map((e=>({mac:e.key,run:e.run,shift:e.shift})))),defaultKeymap=[{key:"Alt-ArrowLeft",mac:"Ctrl-ArrowLeft",run:cursorSyntaxLeft,shift:selectSyntaxLeft},{key:"Alt-ArrowRight",mac:"Ctrl-ArrowRight",run:cursorSyntaxRight,shift:selectSyntaxRight},{key:"Alt-ArrowUp",run:moveLineUp},{key:"Shift-Alt-ArrowUp",run:copyLineUp},{key:"Alt-ArrowDown",run:moveLineDown},{key:"Shift-Alt-ArrowDown",run:copyLineDown},{key:"Escape",run:simplifySelection},{key:"Mod-Enter",run:insertBlankLine},{key:"Alt-l",mac:"Ctrl-l",run:selectLine},{key:"Mod-i",run:selectParentSyntax,preventDefault:!0},{key:"Mod-[",run:indentLess},{key:"Mod-]",run:indentMore},{key:"Mod-Alt-\\",run:indentSelection},{key:"Shift-Mod-k",run:deleteLine},{key:"Shift-Mod-\\",run:cursorMatchingBracket},{key:"Mod-/",run:toggleComment},{key:"Alt-A",run:toggleBlockComment}].concat(standardKeymap),indentWithTab={key:"Tab",run:indentMore,shift:indentLess};export{blockComment,blockUncomment,copyLineDown,copyLineUp,cursorCharBackward,cursorCharForward,cursorCharLeft,cursorCharRight,cursorDocEnd,cursorDocStart,cursorGroupBackward,cursorGroupForward,cursorGroupLeft,cursorGroupRight,cursorLineBoundaryBackward,cursorLineBoundaryForward,cursorLineBoundaryLeft,cursorLineBoundaryRight,cursorLineDown,cursorLineEnd,cursorLineStart,cursorLineUp,cursorMatchingBracket,cursorPageDown,cursorPageUp,cursorSubwordBackward,cursorSubwordForward,cursorSyntaxLeft,cursorSyntaxRight,defaultKeymap,deleteCharBackward,deleteCharForward,deleteGroupBackward,deleteGroupForward,deleteLine,deleteToLineEnd,deleteToLineStart,deleteTrailingWhitespace,emacsStyleKeymap,history,historyField,historyKeymap,indentLess,indentMore,indentSelection,indentWithTab,insertBlankLine,insertNewline,insertNewlineAndIndent,insertTab,invertedEffects,isolateHistory,lineComment,lineUncomment,moveLineDown,moveLineUp,redo,redoDepth,redoSelection,selectAll,selectCharBackward,selectCharForward,selectCharLeft,selectCharRight,selectDocEnd,selectDocStart,selectGroupBackward,selectGroupForward,selectGroupLeft,selectGroupRight,selectLine,selectLineBoundaryBackward,selectLineBoundaryForward,selectLineBoundaryLeft,selectLineBoundaryRight,selectLineDown,selectLineEnd,selectLineStart,selectLineUp,selectMatchingBracket,selectPageDown,selectPageUp,selectParentSyntax,selectSubwordBackward,selectSubwordForward,selectSyntaxLeft,selectSyntaxRight,simplifySelection,splitLine,standardKeymap,toggleBlockComment,toggleBlockCommentByLine,toggleComment,toggleLineComment,transposeChars,undo,undoDepth,undoSelection}; |