66
77import * as editorCommon from 'vs/editor/common/editorCommon' ;
88import { Position } from 'vs/editor/common/core/position' ;
9- import { ICoordinatesConverter , ViewEventsCollector } from 'vs/editor/common/viewModel/viewModel' ;
9+ import { ICoordinatesConverter , ViewEventsCollector , IViewModel } from 'vs/editor/common/viewModel/viewModel' ;
1010import { Selection } from 'vs/editor/common/core/selection' ;
1111import * as viewEvents from 'vs/editor/common/view/viewEvents' ;
12- import { ICursorRevealRangeEvent } from 'vs/editor/common/controller/cursorEvents' ;
12+ import { ICursorRevealRangeEvent , CursorEventType , CursorScrollRequest } from 'vs/editor/common/controller/cursorEvents' ;
13+ import { Cursor } from "vs/editor/common/controller/cursor" ;
14+ import { ViewEventEmitter } from "vs/editor/common/viewModel/viewModelImpl" ;
15+ import { EmitterEvent } from "vs/base/common/eventEmitter" ;
1316
1417export interface ICursorPositionChangedEvent {
1518 readonly position : Position ;
@@ -26,19 +29,88 @@ export interface ICursorSelectionChangedEvent {
2629 readonly secondaryViewSelections : Selection [ ] ;
2730}
2831
29- export class ViewModelCursors {
32+ function containsLineMappingChanged ( events : viewEvents . ViewEvent [ ] ) : boolean {
33+ for ( let i = 0 , len = events . length ; i < len ; i ++ ) {
34+ if ( events [ i ] . type === viewEvents . ViewEventType . ViewLineMappingChanged ) {
35+ return true ;
36+ }
37+ }
38+ return false ;
39+ }
40+
41+ export class ViewModelCursors extends ViewEventEmitter {
3042
3143 private readonly configuration : editorCommon . IConfiguration ;
44+ private readonly viewModel : IViewModel ;
45+ private readonly cursor : Cursor ;
3246 private readonly coordinatesConverter : ICoordinatesConverter ;
3347
3448 private lastCursorPositionChangedEvent : ICursorPositionChangedEvent ;
3549 private lastCursorSelectionChangedEvent : ICursorSelectionChangedEvent ;
3650
37- constructor ( configuration : editorCommon . IConfiguration , coordinatesConverter : ICoordinatesConverter ) {
51+ constructor ( configuration : editorCommon . IConfiguration , viewModel : IViewModel , cursor : Cursor ) {
52+ super ( ) ;
3853 this . configuration = configuration ;
39- this . coordinatesConverter = coordinatesConverter ;
54+ this . viewModel = viewModel ;
55+ this . cursor = cursor ;
56+ this . coordinatesConverter = viewModel . coordinatesConverter ;
4057 this . lastCursorPositionChangedEvent = null ;
4158 this . lastCursorSelectionChangedEvent = null ;
59+
60+ this . _register ( cursor . addBulkListener ( ( events : EmitterEvent [ ] ) => {
61+ const eventsCollector = new ViewEventsCollector ( ) ;
62+ this . _onCursorEvents ( eventsCollector , events ) ;
63+ this . _emit ( eventsCollector . finalize ( ) ) ;
64+ } ) ) ;
65+
66+ this . _register ( viewModel . addEventListener ( ( events : viewEvents . ViewEvent [ ] ) => {
67+ if ( ! containsLineMappingChanged ( events ) ) {
68+ return ;
69+ }
70+ const eventsCollector = new ViewEventsCollector ( ) ;
71+ this . onLineMappingChanged ( eventsCollector ) ;
72+ this . _emit ( eventsCollector . finalize ( ) ) ;
73+ } ) ) ;
74+ }
75+
76+ private _onCursorEvents ( eventsCollector : ViewEventsCollector , events : EmitterEvent [ ] ) : void {
77+ for ( let i = 0 , len = events . length ; i < len ; i ++ ) {
78+ const _e = events [ i ] ;
79+ const type = _e . type ;
80+ const data = _e . data ;
81+
82+ switch ( type ) {
83+ case CursorEventType . CursorPositionChanged : {
84+ const e = < ICursorPositionChangedEvent > data ;
85+ this . onCursorPositionChanged ( eventsCollector , e ) ;
86+ break ;
87+ }
88+ case CursorEventType . CursorSelectionChanged : {
89+ const e = < ICursorSelectionChangedEvent > data ;
90+ this . onCursorSelectionChanged ( eventsCollector , e ) ;
91+ break ;
92+ }
93+ case CursorEventType . CursorRevealRange : {
94+ const e = < ICursorRevealRangeEvent > data ;
95+ this . onCursorRevealRange ( eventsCollector , e ) ;
96+ break ;
97+ }
98+ case CursorEventType . CursorScrollRequest : {
99+ const e = < CursorScrollRequest > data ;
100+ this . viewModel . viewLayout . setScrollPosition ( {
101+ scrollTop : e . desiredScrollTop
102+ } ) ;
103+ break ;
104+ }
105+ default :
106+ console . info ( 'View received unknown event: ' ) ;
107+ console . info ( type , data ) ;
108+ }
109+ }
110+ }
111+
112+ public dispose ( ) : void {
113+ super . dispose ( ) ;
42114 }
43115
44116 /**
@@ -52,7 +124,7 @@ export class ViewModelCursors {
52124 return position ;
53125 }
54126
55- public onCursorPositionChanged ( eventsCollector : ViewEventsCollector , e : ICursorPositionChangedEvent ) : void {
127+ private onCursorPositionChanged ( eventsCollector : ViewEventsCollector , e : ICursorPositionChangedEvent ) : void {
56128 this . lastCursorPositionChangedEvent = e ;
57129
58130 const stopRenderingLineAfter = this . configuration . editor . viewInfo . stopRenderingLineAfter ;
@@ -66,13 +138,13 @@ export class ViewModelCursors {
66138 eventsCollector . emit ( new viewEvents . ViewCursorPositionChangedEvent ( position , secondaryPositions , e . isInEditableRange ) ) ;
67139 }
68140
69- public onCursorSelectionChanged ( eventsCollector : ViewEventsCollector , e : ICursorSelectionChangedEvent ) : void {
141+ private onCursorSelectionChanged ( eventsCollector : ViewEventsCollector , e : ICursorSelectionChangedEvent ) : void {
70142 this . lastCursorSelectionChangedEvent = e ;
71143
72144 eventsCollector . emit ( new viewEvents . ViewCursorSelectionChangedEvent ( e . viewSelection , e . secondaryViewSelections ) ) ;
73145 }
74146
75- public onCursorRevealRange ( eventsCollector : ViewEventsCollector , e : ICursorRevealRangeEvent ) : void {
147+ private onCursorRevealRange ( eventsCollector : ViewEventsCollector , e : ICursorRevealRangeEvent ) : void {
76148 // Ensure event has viewRange
77149 const viewRange = (
78150 e . viewRange
@@ -86,7 +158,7 @@ export class ViewModelCursors {
86158 ) ) ;
87159 }
88160
89- public onLineMappingChanged ( eventsCollector : ViewEventsCollector ) : void {
161+ private onLineMappingChanged ( eventsCollector : ViewEventsCollector ) : void {
90162 if ( this . lastCursorPositionChangedEvent ) {
91163 const toViewPos = ( pos : Position ) => this . coordinatesConverter . convertModelPositionToViewPosition ( pos ) ;
92164 let e : ICursorPositionChangedEvent = {
0 commit comments