Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 SourceFrame.SourcesTextEditor = class extends TextEditor.CodeMirrorTextEditor { | 7 SourceFrame.SourcesTextEditor = class extends TextEditor.CodeMirrorTextEditor { |
| 8 /** | 8 /** |
| 9 * @param {!SourceFrame.SourcesTextEditorDelegate} delegate | 9 * @param {!SourceFrame.SourcesTextEditorDelegate} delegate |
| 10 */ | 10 */ |
| 11 constructor(delegate) { | 11 constructor(delegate) { |
| 12 super({ | 12 super({ |
| 13 lineNumbers: true, | 13 lineNumbers: true, |
| 14 lineWrapping: false, | 14 lineWrapping: false, |
| 15 bracketMatchingSetting: Common.moduleSetting('textEditorBracketMatching'), | 15 bracketMatchingSetting: Common.moduleSetting('textEditorBracketMatching'), |
| 16 padBottom: true | 16 padBottom: true |
| 17 }); | 17 }); |
| 18 | 18 |
| 19 this.codeMirror().addKeyMap({'Enter': 'smartNewlineAndIndent', 'Esc': 'sourc esDismiss'}); | 19 this.codeMirror().addKeyMap({'Enter': 'smartNewlineAndIndent', 'Esc': 'sourc esDismiss'}); |
| 20 | 20 |
| 21 this._delegate = delegate; | 21 this._delegate = delegate; |
| 22 | 22 |
| 23 this.codeMirror().on('changes', this._fireTextChanged.bind(this)); | |
| 24 this.codeMirror().on('cursorActivity', this._cursorActivity.bind(this)); | 23 this.codeMirror().on('cursorActivity', this._cursorActivity.bind(this)); |
| 25 this.codeMirror().on('gutterClick', this._gutterClick.bind(this)); | 24 this.codeMirror().on('gutterClick', this._gutterClick.bind(this)); |
| 26 this.codeMirror().on('scroll', this._scroll.bind(this)); | 25 this.codeMirror().on('scroll', this._scroll.bind(this)); |
| 27 this.codeMirror().on('focus', this._focus.bind(this)); | 26 this.codeMirror().on('focus', this._focus.bind(this)); |
| 28 this.codeMirror().on('blur', this._blur.bind(this)); | 27 this.codeMirror().on('blur', this._blur.bind(this)); |
| 29 this.codeMirror().on('beforeSelectionChange', this._fireBeforeSelectionChang ed.bind(this)); | 28 this.codeMirror().on('beforeSelectionChange', this._fireBeforeSelectionChang ed.bind(this)); |
| 30 this.element.addEventListener('contextmenu', this._contextMenu.bind(this), f alse); | 29 this.element.addEventListener('contextmenu', this._contextMenu.bind(this), f alse); |
| 31 | 30 |
| 32 this.codeMirror().addKeyMap(SourceFrame.SourcesTextEditor._BlockIndentContro ller); | 31 this.codeMirror().addKeyMap(SourceFrame.SourcesTextEditor._BlockIndentContro ller); |
| 33 this._tokenHighlighter = new SourceFrame.SourcesTextEditor.TokenHighlighter( this, this.codeMirror()); | 32 this._tokenHighlighter = new SourceFrame.SourcesTextEditor.TokenHighlighter( this, this.codeMirror()); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 | 298 |
| 300 /** | 299 /** |
| 301 * @override | 300 * @override |
| 302 * @param {!Common.TextRange} range | 301 * @param {!Common.TextRange} range |
| 303 * @param {string} text | 302 * @param {string} text |
| 304 * @param {string=} origin | 303 * @param {string=} origin |
| 305 * @return {!Common.TextRange} | 304 * @return {!Common.TextRange} |
| 306 */ | 305 */ |
| 307 editRange(range, text, origin) { | 306 editRange(range, text, origin) { |
| 308 var newRange = super.editRange(range, text, origin); | 307 var newRange = super.editRange(range, text, origin); |
| 309 this.dispatchEventToListeners( | 308 this.emit(new UI.TextEditor.TextChangedEvent(range, newRange)); |
|
pfeldman
2017/04/04 20:21:09
You should not emit text editor's events on source
luoe
2017/04/04 21:08:59
Done.
| |
| 310 SourceFrame.SourcesTextEditor.Events.TextChanged, {oldRange: range, newR ange: newRange}); | |
| 311 | 309 |
| 312 if (Common.moduleSetting('textEditorAutoDetectIndent').get()) | 310 if (Common.moduleSetting('textEditorAutoDetectIndent').get()) |
| 313 this._onUpdateEditorIndentation(); | 311 this._onUpdateEditorIndentation(); |
| 314 | 312 |
| 315 return newRange; | 313 return newRange; |
| 316 } | 314 } |
| 317 | 315 |
| 318 _onUpdateEditorIndentation() { | 316 _onUpdateEditorIndentation() { |
| 319 this._setEditorIndentation(TextEditor.CodeMirrorUtils.pullLines( | 317 this._setEditorIndentation(TextEditor.CodeMirrorUtils.pullLines( |
| 320 this.codeMirror(), SourceFrame.SourcesTextEditor.LinesToScanForIndentati onGuessing)); | 318 this.codeMirror(), SourceFrame.SourcesTextEditor.LinesToScanForIndentati onGuessing)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 } | 368 } |
| 371 | 369 |
| 372 this._autoAppendedSpaces = []; | 370 this._autoAppendedSpaces = []; |
| 373 var selections = this.selections(); | 371 var selections = this.selections(); |
| 374 for (var i = 0; i < selections.length; ++i) { | 372 for (var i = 0; i < selections.length; ++i) { |
| 375 var selection = selections[i]; | 373 var selection = selections[i]; |
| 376 this._autoAppendedSpaces.push(this.textEditorPositionHandle(selection.star tLine, selection.startColumn)); | 374 this._autoAppendedSpaces.push(this.textEditorPositionHandle(selection.star tLine, selection.startColumn)); |
| 377 } | 375 } |
| 378 } | 376 } |
| 379 | 377 |
| 380 /** | |
| 381 * @param {!CodeMirror} codeMirror | |
| 382 * @param {!Array.<!CodeMirror.ChangeObject>} changes | |
| 383 */ | |
| 384 _fireTextChanged(codeMirror, changes) { | |
| 385 if (!changes.length || this._muteTextChangedEvent) | |
| 386 return; | |
| 387 var edits = []; | |
| 388 var currentEdit; | |
| 389 | |
| 390 for (var changeIndex = 0; changeIndex < changes.length; ++changeIndex) { | |
| 391 var changeObject = changes[changeIndex]; | |
| 392 var edit = TextEditor.CodeMirrorUtils.changeObjectToEditOperation(changeOb ject); | |
| 393 if (currentEdit && edit.oldRange.equal(currentEdit.newRange)) { | |
| 394 currentEdit.newRange = edit.newRange; | |
| 395 } else { | |
| 396 currentEdit = edit; | |
| 397 edits.push(currentEdit); | |
| 398 } | |
| 399 } | |
| 400 | |
| 401 for (var i = 0; i < edits.length; ++i) | |
| 402 this.dispatchEventToListeners(SourceFrame.SourcesTextEditor.Events.TextCha nged, edits[i]); | |
| 403 } | |
| 404 | |
| 405 _cursorActivity() { | 378 _cursorActivity() { |
| 406 if (!this._isSearchActive()) | 379 if (!this._isSearchActive()) |
| 407 this.codeMirror().operation(this._tokenHighlighter.highlightSelectedTokens .bind(this._tokenHighlighter)); | 380 this.codeMirror().operation(this._tokenHighlighter.highlightSelectedTokens .bind(this._tokenHighlighter)); |
| 408 | 381 |
| 409 var start = this.codeMirror().getCursor('anchor'); | 382 var start = this.codeMirror().getCursor('anchor'); |
| 410 var end = this.codeMirror().getCursor('head'); | 383 var end = this.codeMirror().getCursor('head'); |
| 411 this.dispatchEventToListeners( | 384 this.dispatchEventToListeners( |
| 412 SourceFrame.SourcesTextEditor.Events.SelectionChanged, TextEditor.CodeMi rrorUtils.toRange(start, end)); | 385 SourceFrame.SourcesTextEditor.Events.SelectionChanged, TextEditor.CodeMi rrorUtils.toRange(start, end)); |
| 413 } | 386 } |
| 414 | 387 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 Common.moduleSetting('textEditorIndent').removeChangeListener(this._onUpdate EditorIndentation, this); | 431 Common.moduleSetting('textEditorIndent').removeChangeListener(this._onUpdate EditorIndentation, this); |
| 459 Common.moduleSetting('textEditorAutoDetectIndent').removeChangeListener(this ._onUpdateEditorIndentation, this); | 432 Common.moduleSetting('textEditorAutoDetectIndent').removeChangeListener(this ._onUpdateEditorIndentation, this); |
| 460 Common.moduleSetting('showWhitespacesInEditor').removeChangeListener(this._u pdateWhitespace, this); | 433 Common.moduleSetting('showWhitespacesInEditor').removeChangeListener(this._u pdateWhitespace, this); |
| 461 } | 434 } |
| 462 | 435 |
| 463 /** | 436 /** |
| 464 * @override | 437 * @override |
| 465 * @param {string} text | 438 * @param {string} text |
| 466 */ | 439 */ |
| 467 setText(text) { | 440 setText(text) { |
| 468 this._muteTextChangedEvent = true; | |
| 469 this._setEditorIndentation( | 441 this._setEditorIndentation( |
| 470 text.split('\n').slice(0, SourceFrame.SourcesTextEditor.LinesToScanForIn dentationGuessing)); | 442 text.split('\n').slice(0, SourceFrame.SourcesTextEditor.LinesToScanForIn dentationGuessing)); |
| 471 super.setText(text); | 443 super.setText(text); |
| 472 delete this._muteTextChangedEvent; | |
| 473 } | 444 } |
| 474 | 445 |
| 475 _updateWhitespace() { | 446 _updateWhitespace() { |
| 476 this.setMimeType(this.mimeType()); | 447 this.setMimeType(this.mimeType()); |
| 477 } | 448 } |
| 478 | 449 |
| 479 /** | 450 /** |
| 480 * @override | 451 * @override |
| 481 * @param {string} mimeType | 452 * @param {string} mimeType |
| 482 * @return {string} | 453 * @return {string} |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 575 doc.head.appendChild(style); | 546 doc.head.appendChild(style); |
| 576 } | 547 } |
| 577 }; | 548 }; |
| 578 | 549 |
| 579 /** @typedef {{lineNumber: number, event: !Event}} */ | 550 /** @typedef {{lineNumber: number, event: !Event}} */ |
| 580 SourceFrame.SourcesTextEditor.GutterClickEventData; | 551 SourceFrame.SourcesTextEditor.GutterClickEventData; |
| 581 | 552 |
| 582 /** @enum {symbol} */ | 553 /** @enum {symbol} */ |
| 583 SourceFrame.SourcesTextEditor.Events = { | 554 SourceFrame.SourcesTextEditor.Events = { |
| 584 GutterClick: Symbol('GutterClick'), | 555 GutterClick: Symbol('GutterClick'), |
| 585 TextChanged: Symbol('TextChanged'), | |
| 586 SelectionChanged: Symbol('SelectionChanged'), | 556 SelectionChanged: Symbol('SelectionChanged'), |
| 587 ScrollChanged: Symbol('ScrollChanged'), | 557 ScrollChanged: Symbol('ScrollChanged'), |
| 588 EditorFocused: Symbol('EditorFocused'), | 558 EditorFocused: Symbol('EditorFocused'), |
| 589 EditorBlurred: Symbol('EditorBlurred'), | 559 EditorBlurred: Symbol('EditorBlurred'), |
| 590 JumpHappened: Symbol('JumpHappened') | 560 JumpHappened: Symbol('JumpHappened') |
| 591 }; | 561 }; |
| 592 | 562 |
| 593 /** | 563 /** |
| 594 * @interface | 564 * @interface |
| 595 */ | 565 */ |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 871 */ | 841 */ |
| 872 _setHighlighter(highlighter, selectionStart) { | 842 _setHighlighter(highlighter, selectionStart) { |
| 873 var overlayMode = {token: highlighter}; | 843 var overlayMode = {token: highlighter}; |
| 874 this._codeMirror.addOverlay(overlayMode); | 844 this._codeMirror.addOverlay(overlayMode); |
| 875 this._highlightDescriptor = {overlay: overlayMode, selectionStart: selection Start}; | 845 this._highlightDescriptor = {overlay: overlayMode, selectionStart: selection Start}; |
| 876 } | 846 } |
| 877 }; | 847 }; |
| 878 | 848 |
| 879 SourceFrame.SourcesTextEditor.LinesToScanForIndentationGuessing = 1000; | 849 SourceFrame.SourcesTextEditor.LinesToScanForIndentationGuessing = 1000; |
| 880 SourceFrame.SourcesTextEditor.MaximumNumberOfWhitespacesPerSingleSpan = 16; | 850 SourceFrame.SourcesTextEditor.MaximumNumberOfWhitespacesPerSingleSpan = 16; |
| OLD | NEW |