Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 95x 95x 95x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 12x 12x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 5011x 5011x 5011x 5011x 5011x 52x 52x 4959x 4959x 4959x 4959x 109x 109x 109x 4959x 4850x 11x 11x 11x 11x 11x 11x | /** * @license Apache-2.0 * * Copyright (c) 2024 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* eslint-disable no-underscore-dangle, no-restricted-syntax, no-invalid-this */ 'use strict'; // MODULES // var readline = require( 'readline' ); var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var ltrimN = require( '@stdlib/string/left-trim-n' ); var contains = require( '@stdlib/array/base/assert/contains' ).factory; var uppercase = require( '@stdlib/string/base/uppercase' ); var lowercase = require( '@stdlib/string/base/lowercase' ); var capitalize = require( '@stdlib/string/base/capitalize' ); var parseKey = require( './parse_key.js' ); // VARIABLES // var RE_NON_WHITESPACE = /\S+/; var EDITOR_ACTIONS = [ 'moveRight', 'moveLeft', 'moveWordRight', 'moveWordLeft', 'moveBeginning', 'moveEnd', 'tab', 'indentLineRight', 'indentLineLeft', 'deleteLeft', 'deleteRight', 'deleteWordLeft', 'deleteWordRight', 'deleteLineLeft', 'deleteLineRight', 'yankKilled', 'yankPop', 'undo', 'redo', 'transposeAboutCursor', 'uppercaseNextWord', 'capitalizeNextWord', 'lowercaseNextWord', 'clearScreen' ]; var isEditorAction = contains( EDITOR_ACTIONS ); // MAIN // /** * Constructor for creating an editor actions prototype. * * @private * @constructor * @param {REPL} repl - REPL instance * @param {Function} ttyWrite - function to trigger default behavior of a keypress * @returns {EditorActions} editor actions instance */ function EditorActions( repl, ttyWrite ) { if ( !( this instanceof EditorActions ) ) { return new EditorActions( repl, ttyWrite ); } // Cache a reference to the readline interface: this._rli = repl._rli; // Cache a reference to the output writable stream: this._ostream = repl._ostream; // Cache a reference to the private readline interface `ttyWrite` to allow calling the method when wanting default behavior: this._ttyWrite = ttyWrite; // Cache a reference to the REPL keybindings: this._keybindings = repl._keybindings; return this; } /** * Moves cursor to the right by specified offset. * * @private * @name _moveCursorX * @memberof EditorActions.prototype * @type {Function} * @param {number} offset - x offset * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, '_moveCursorX', function moveCursorX( offset ) { readline.moveCursor( this._ostream, offset ); this._rli.cursor += offset; }); /** * Replaces current line with the given line. * * @private * @name _replaceLine * @memberof EditorActions.prototype * @type {Function} * @param {string} line - line * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, '_replaceLine', function replaceLine( line ) { readline.moveCursor( this._ostream, -1 * this._rli.cursor ); readline.clearLine( this._ostream, 1 ); this._ostream.write( line ); this._rli.line = line; this._rli.cursor = line.length; }); /** * Modifies the next word in line. * * @private * @name _modifyNextWord * @memberof EditorActions.prototype * @type {Function} * @param {Function} modifier - modifier function * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, '_modifyNextWord', function modifyNextWord( modifier ) { var updatedLine; var substring; var match; var start; var end; // Use regex to find the first non-whitespace character and the end of the word after the cursor: match = this._rli.line.slice( this._rli.cursor ).match( RE_NON_WHITESPACE ); if ( match ) { start = this._rli.cursor + match.index; end = start + match[ 0 ].length; } else { start = this._rli.cursor; end = this._rli.line.length; } // Extract the word, apply the modifier, and reconstruct the line: substring = this._rli.line.slice( start, end ); updatedLine = this._rli.line.slice( 0, start ) + modifier( substring ) + this._rli.line.slice( end ); // eslint-disable-line max-len this._replaceLine( updatedLine ); this._moveCursorX( end - this._rli.line.length ); }); /** * Moves cursor a character right. * * @name moveRight * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'moveRight', function moveRight() { this._ttyWrite.call( this._rli, null, { 'name': 'right' }); }); /** * Moves cursor a character left. * * @name moveLeft * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'moveLeft', function moveLeft() { this._ttyWrite.call( this._rli, null, { 'name': 'left' }); }); /** * Moves cursor a word to the right. * * @name moveWordRight * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'moveWordRight', function moveWordRight() { this._ttyWrite.call( this._rli, null, { 'name': 'f', 'meta': true }); }); /** * Moves cursor a word to the left. * * @name moveWordLeft * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'moveWordLeft', function moveWordLeft() { this._ttyWrite.call( this._rli, null, { 'name': 'b', 'meta': true }); }); /** * Moves cursor to the beginning of line. * * @name moveBeginning * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'moveBeginning', function moveBeginning() { this._ttyWrite.call( this._rli, null, { 'name': 'home' }); }); /** * Moves cursor to the end of line. * * @name moveEnd * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'moveEnd', function moveEnd() { this._ttyWrite.call( this._rli, null, { 'name': 'end' }); }); /** * Inserts TAB indentation at cursor. * * @name tab * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'tab', function tab() { this._rli.write( '\t' ); }); /** * Indents line to the right. * * @name indentLineRight * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'indentLineRight', function indentLineRight() { var indentedLine = '\t' + this._rli.line; var cursorPos = this._rli.cursor + 1; this._replaceLine( indentedLine ); this._moveCursorX( cursorPos - this._rli.line.length ); // maintain cursor position }); /** * Indents line to the left. * * @name indentLineLeft * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'indentLineLeft', function indentLineLeft() { var indentedLine; var cursorPos; var offset; indentedLine = ltrimN( this._rli.line, 1, [ '\t' ] ); offset = this._rli.line.length - indentedLine.length; cursorPos = this._rli.cursor - offset; // NOTE: `readline.moveCursor()` doesn't know tabs, it treats `\t` as 8 whitespace characters instead of a single character. Hence manually move the cursor when indenting to the left... if ( offset ) { readline.moveCursor( this._ostream, ( -1 * this._rli.tabSize ) + 1 ); } this._replaceLine( indentedLine ); this._moveCursorX( cursorPos - this._rli.line.length ); // maintain cursor position }); /** * Deletes character left to the cursor. * * @name deleteLeft * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'deleteLeft', function deleteLeft() { this._ttyWrite.call( this._rli, null, { 'name': 'backspace' }); }); /** * Deletes character right to the cursor. * * @name deleteRight * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'deleteRight', function deleteRight() { this._ttyWrite.call( this._rli, null, { 'name': 'delete' }); }); /** * Deletes a word left to the cursor. * * @name deleteWordLeft * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'deleteWordLeft', function deleteWordLeft() { this._ttyWrite.call( this._rli, null, { 'name': 'backspace', 'ctrl': true }); }); /** * Deletes a word right to the cursor. * * @name deleteWordRight * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'deleteWordRight', function deleteWordRight() { this._ttyWrite.call( this._rli, null, { 'name': 'delete', 'ctrl': true }); }); /** * Deletes line to the left of the cursor. * * @name deleteLineLeft * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'deleteLineLeft', function deleteLineLeft() { this._ttyWrite.call( this._rli, null, { 'name': 'backspace', 'ctrl': true, 'shift': true }); }); /** * Deletes line to the right of the cursor. * * @name deleteLineRight * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'deleteLineRight', function deleteLineRight() { this._ttyWrite.call( this._rli, null, { 'name': 'delete', 'ctrl': true, 'shift': true }); }); /** * Yank string from the "killed" buffer. * * @name yankKilled * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'yankKilled', function yankKilled() { this._ttyWrite.call( this._rli, null, { 'name': 'y', 'ctrl': true }); }); /** * Yank-pop the next string from the "killed" buffer. * * @name yankPop * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'yankPop', function yankPop() { this._ttyWrite.call( this._rli, null, { 'name': 'y', 'meta': true }); }); /** * Yank-pop the next string from the undo stack. * * @name undo * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'undo', function undo() { this._ttyWrite.call( this._rli, null, { 'sequence': '\u001F' }); }); /** * Yank-pop the next string from the redo stack. * * @name redo * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'redo', function redo() { this._ttyWrite.call( this._rli, null, { 'sequence': '\u001E' }); }); /** * Transposes the characters about cursor. * * @name transposeAboutCursor * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'transposeAboutCursor', function transposeAboutCursor() { var c = this._rli.line[ this._rli.cursor - 1 ]; this.deleteLeft(); this.moveRight(); this._rli.write( c ); }); /** * Changes the next word to uppercase. * * @name uppercaseNextWord * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'uppercaseNextWord', function uppercaseNextWord() { this._modifyNextWord( uppercase ); }); /** * Changes the next word to titlecase. * * @name capitalizeNextWord * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'capitalizeNextWord', function capitalizeNextWord() { this._modifyNextWord( capitalize ); }); /** * Changes the next word to lowercase. * * @name lowercaseNextWord * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'lowercaseNextWord', function lowercaseNextWord() { this._modifyNextWord( lowercase ); }); /** * Clears the entire REPL screen and scrollback history. * * @name clearScreen * @memberof EditorActions.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EditorActions.prototype, 'clearScreen', function clearScreen() { this._ttyWrite.call( this._rli, null, { 'name': 'l', 'ctrl': true }); }); /** * Callback which should be invoked **before** a "keypress" event is processed by a readline interface. * * @name beforeKeypress * @memberof EditorActions.prototype * @type {Function} * @param {string} data - input data * @param {(Object|void)} key - key object * @returns {boolean} boolean indicating whether an editor action was triggered */ setNonEnumerableReadOnly( EditorActions.prototype, 'beforeKeypress', function beforeKeypress( data, key ) { var inputKeys; var action; var i; if ( !key ) { return false; } inputKeys = parseKey( key ); for ( i = 0; i < inputKeys.length; i++ ) { action = this._keybindings.getAction( inputKeys[ i ] ); if ( action && isEditorAction( action ) ) { this[ action ](); return true; } } return false; }); // EXPORTS // module.exports = EditorActions; |