Debian Codegraph @

resources/codemirror.gotoLine.js

raw
1 const {
2 EditorSelection,
3 EditorView,
4 Prec,
5 StateEffect,
6 StateEffectType,
7 StateField,
8 keymap,
9 showPanel
10 } = require( 'ext.CodeMirror.v6.lib' );
11 const CodeMirrorPanel = require( './codemirror.panel.js' );
12
13 /**
14 * Custom goto line panel for CodeMirror using CSS-only Codex components.
15 *
16 * Using the Alt-g keybinding, this shows a panel asking the user for a line number,
17 * when a valid position is provided, moves the cursor to that line.
18 *
19 * This feature supports line numbers, relative line offsets prefixed with `+` or `-`,
20 * document percentages suffixed with `%`, and an optional column position by adding `:`
21 * and a second number after the line number.
22 *
23 * Based on the CodeMirror implementation (MIT).
24 *
25 * @see https://github.com/codemirror/search/blob/0d8af3e4cc/src/goto-line.ts
26 * @extends CodeMirrorPanel
27 */
28 class CodeMirrorGotoLine extends CodeMirrorPanel {
29 constructor() {
30 super();
31
32 /**
33 * @type {StateEffectType}
34 */
35 this.toggleEffect = StateEffect.define();
36
37 /**
38 * @type {StateField}
39 */
40 this.panelStateField = StateField.define( {
41 create: () => true,
42 update: ( value, transaction ) => {
43 for ( const e of transaction.effects ) {
44 if ( e.is( this.toggleEffect ) ) {
45 value = e.value;
46 }
47 }
48
49 return value;
50 },
51 // eslint-disable-next-line arrow-body-style
52 provide: ( stateField ) => {
53 // eslint-disable-next-line arrow-body-style
54 return showPanel.from( stateField, ( on ) => {
55 return on ? () => this.panel : null;
56 } );
57 }
58 } );
59
60 /**
61 * @type {HTMLInputElement}
62 */
63 this.input = undefined;
64 }
65
66 /**
67 * @inheritDoc
68 */
69 get extension() {
70 // Use Prec.highest to ensure that this keymap is used before the default searchKeymap.
71 return Prec.highest(
72 keymap.of( {
73 key: 'Mod-Alt-g',
74 run: ( view ) => {
75 this.view = view;
76 const effects = [ this.toggleEffect.of( true ) ];
77 if ( !this.view.state.field( this.panelStateField, false ) ) {
78 effects.push( StateEffect.appendConfig.of( [ this.panelStateField ] ) );
79 }
80 this.view.dispatch( { effects } );
81 return true;
82 }
83 } )
84 );
85 }
86
87 /**
88 * @inheritDoc
89 */
90 get panel() {
91 const container = document.createElement( 'div' );
92 container.className = 'cm-mw-goto-line-panel cm-mw-panel cm-mw-panel--row';
93 container.addEventListener( 'keydown', this.onKeydown.bind( this ) );
94
95 // Line input.
96 const [ inputWrapper, input ] = this.getTextInput( 'line', this.line );
97 this.input = input;
98 container.appendChild( inputWrapper );
99
100 // Go button.
101 const button = this.getButton( 'codemirror-goto-line-go' );
102 button.addEventListener( 'click', this.go.bind( this ) );
103 container.appendChild( button );
104
105 return {
106 dom: container,
107 top: true,
108 mount: () => {
109 this.input.value = String(
110 this.view.state.doc.lineAt(
111 this.view.state.selection.main.head
112 ).number
113 );
114 this.input.focus();
115 this.input.select();
116 }
117 };
118 }
119
120 /**
121 * Respond to keydown events.
122 *
123 * @param {KeyboardEvent} event
124 */
125 onKeydown( event ) {
126 if ( event.key === 'Escape' ) {
127 event.preventDefault();
128 this.view.dispatch( { effects: this.toggleEffect.of( false ) } );
129 this.view.focus();
130 } else if ( event.key === 'Enter' ) {
131 event.preventDefault();
132 this.go();
133 }
134 }
135
136 /**
137 * Go to the specified line.
138 */
139 go() {
140 const match = /^([+-])?(\d+)?(:\d+)?(%)?$/.exec( this.input.value );
141 if ( !match ) {
142 return;
143 }
144 const { state } = this.view;
145 const startLine = state.doc.lineAt( state.selection.main.head );
146 const [ , sign, ln, cl, percent ] = match;
147 const col = cl ? +cl.slice( 1 ) : 0;
148 let line = ln ? +ln : startLine.number;
149 if ( ln && percent ) {
150 let pc = line / 100;
151 if ( sign ) {
152 pc = pc * ( sign === '-' ? -1 : 1 ) + ( startLine.number / state.doc.lines );
153 }
154 line = Math.round( state.doc.lines * pc );
155 } else if ( ln && sign ) {
156 line = line * ( sign === '-' ? -1 : 1 ) + startLine.number;
157 }
158 const docLine = state.doc.line( Math.max( 1, Math.min( state.doc.lines, line ) ) );
159 const selection = EditorSelection.cursor(
160 docLine.from + Math.max( 0, Math.min( col, docLine.length ) )
161 );
162 this.view.dispatch( {
163 effects: [
164 this.toggleEffect.of( false ),
165 EditorView.scrollIntoView( selection.from, { y: 'center' } )
166 ],
167 selection
168 } );
169 this.view.focus();
170 }
171 }
172
173 module.exports = CodeMirrorGotoLine;