Skip to content

Commit e086d4e

Browse files
committed
some refactorings in reference search, prep for microsoft#5893
1 parent 75ec70c commit e086d4e

3 files changed

Lines changed: 152 additions & 182 deletions

File tree

src/vs/editor/contrib/referenceSearch/browser/referenceSearch.ts

Lines changed: 77 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,20 @@ import {ICodeEditor} from 'vs/editor/browser/editorBrowser';
3131
import {EditorBrowserRegistry} from 'vs/editor/browser/editorBrowserExtensions';
3232
import {IPeekViewService, getOuterEditor} from 'vs/editor/contrib/zoneWidget/browser/peekViewWidget';
3333
import {findReferences} from '../common/referenceSearch';
34-
import {EventType, ReferencesModel} from './referenceSearchModel';
34+
import {ReferencesModel, OneReference} from './referenceSearchModel';
3535
import {ReferenceWidget, LayoutData} from './referenceSearchWidget';
3636
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
3737

3838
export class FindReferencesController implements editorCommon.IEditorContribution {
3939

4040
public static ID = 'editor.contrib.findReferencesController';
4141

42-
private _editor:ICodeEditor;
43-
private _widget:ReferenceWidget;
44-
private _requestIdPool: number;
45-
private _callOnClear:Function[];
46-
private _model:ReferencesModel;
47-
private _modelRevealing:boolean;
48-
49-
private editorService: IEditorService;
50-
private telemetryService: ITelemetryService;
51-
private messageService: IMessageService;
52-
private instantiationService: IInstantiationService;
53-
private contextService: IWorkspaceContextService;
54-
private peekViewService: IPeekViewService;
42+
private _editor: ICodeEditor;
43+
private _widget: ReferenceWidget;
44+
private _model: ReferencesModel;
45+
private _requestIdPool = 0;
46+
private _callOnClear: Function[] = [];
47+
private _ignoreModelChangeEvent = false;
5548

5649
private _startTime: number = -1;
5750
private _referenceSearchVisible: IKeybindingContextKey<boolean>;
@@ -62,24 +55,15 @@ export class FindReferencesController implements editorCommon.IEditorContributio
6255

6356
public constructor(
6457
editor: ICodeEditor,
65-
@IEditorService editorService: IEditorService,
66-
@ITelemetryService telemetryService: ITelemetryService,
67-
@IMessageService messageService: IMessageService,
68-
@IInstantiationService instantiationService: IInstantiationService,
6958
@IKeybindingService keybindingService: IKeybindingService,
70-
@IWorkspaceContextService contextService: IWorkspaceContextService,
59+
@IEditorService private _editorService: IEditorService,
60+
@ITelemetryService private _telemetryService: ITelemetryService,
61+
@IMessageService private _messageService: IMessageService,
62+
@IInstantiationService private _instantiationService: IInstantiationService,
63+
@IWorkspaceContextService private _contextService: IWorkspaceContextService,
7164
@IStorageService private _storageService: IStorageService,
72-
@optional(IPeekViewService) peekViewService: IPeekViewService
65+
@optional(IPeekViewService) private _peekViewService: IPeekViewService
7366
) {
74-
this._requestIdPool = 0;
75-
this._callOnClear = [];
76-
this.editorService = editorService;
77-
this.telemetryService = telemetryService;
78-
this.messageService = messageService;
79-
this.instantiationService = instantiationService;
80-
this.peekViewService = peekViewService;
81-
this.contextService = contextService;
82-
this._modelRevealing = false;
8367
this._editor = editor;
8468
this._referenceSearchVisible = keybindingService.createKey(CONTEXT_REFERENCE_SEARCH_VISIBLE, false);
8569
}
@@ -97,7 +81,7 @@ export class FindReferencesController implements editorCommon.IEditorContributio
9781
}
9882

9983
public isInPeekView() : boolean {
100-
return this.peekViewService && this.peekViewService.isActive;
84+
return this._peekViewService && this._peekViewService.isActive;
10185
}
10286

10387
public closeReferenceSearch(): void {
@@ -120,13 +104,13 @@ export class FindReferencesController implements editorCommon.IEditorContributio
120104
// close the widget on model/mode changes
121105
this._callOnClear.push(this._editor.addListener(editorCommon.EventType.ModelModeChanged, () => { this.clear(); }));
122106
this._callOnClear.push(this._editor.addListener(editorCommon.EventType.ModelChanged, () => {
123-
if(!this._modelRevealing) {
107+
if(!this._ignoreModelChangeEvent) {
124108
this.clear();
125109
}
126110
}));
127111
const storageKey = 'peekViewLayout';
128112
const data = <LayoutData> JSON.parse(this._storageService.get(storageKey, undefined, '{}'));
129-
this._widget = new ReferenceWidget(this._editor, data, this.editorService, this.contextService, this.instantiationService);
113+
this._widget = new ReferenceWidget(this._editor, data, this._editorService, this._contextService, this._instantiationService);
130114
this._widget.setTitle(nls.localize('labelLoading', "Loading..."));
131115
this._widget.show(range);
132116
this._callOnClear.push(this._widget.onDidClose(() => {
@@ -137,31 +121,27 @@ export class FindReferencesController implements editorCommon.IEditorContributio
137121
this.clear();
138122
}).dispose);
139123

140-
this._callOnClear.push(this._widget.onDidDoubleClick(event => {
141-
142-
if(!event.reference) {
143-
return;
144-
}
145-
146-
// open editor
147-
this.editorService.openEditor({
148-
resource: event.reference,
149-
options: { selection: event.range }
150-
}, event.originalEvent.ctrlKey || event.originalEvent.metaKey).done(null, onUnexpectedError);
151-
152-
// close zone
153-
if (!(event.originalEvent.ctrlKey || event.originalEvent.metaKey)) {
154-
this.clear();
124+
this._callOnClear.push(this._widget.onDidSelectReference(event => {
125+
let {element, kind} = event;
126+
switch (kind) {
127+
case 'side':
128+
case 'open':
129+
this._openReference(element, kind === 'side');
130+
break;
131+
case 'goto':
132+
this._gotoReference(element);
133+
break;
155134
}
156135
}).dispose);
136+
157137
var requestId = ++this._requestIdPool,
158138
editorModel = this._editor.getModel();
159139

160-
var timer = this.telemetryService.timedPublicLog('findReferences', {
140+
var timer = this._telemetryService.timedPublicLog('findReferences', {
161141
mode: editorModel.getMode().getId()
162142
});
163143

164-
referencesPromise.then((references:IReference[]) => {
144+
referencesPromise.then(references => {
165145

166146
// still current request? widget still open?
167147
if(requestId !== this._requestIdPool || !this._widget) {
@@ -177,38 +157,7 @@ export class FindReferencesController implements editorCommon.IEditorContributio
177157
}
178158

179159
// create result model
180-
this._model = new ReferencesModel(references, this.editorService);
181-
this._model.currentReference = this._model.findReference(editorModel.getAssociatedResource(), range.getStartPosition());
182-
183-
var unbind = this._model.addListener(EventType.CurrentReferenceChanged, () => {
184-
185-
this._modelRevealing = true;
186-
187-
this.editorService.openEditor({
188-
resource: this._model.currentReference.resource,
189-
options: { selection: this._model.currentReference.range }
190-
}).done((openedEditor) => {
191-
if(!openedEditor || openedEditor.getControl() !== this._editor) {
192-
// TODO@Alex TODO@Joh
193-
// when opening the current reference we might end up
194-
// in a different editor instance. that means we also have
195-
// a different instance of this reference search controller
196-
// and cannot hold onto the widget (which likely doesn't
197-
// exist). Instead of bailing out we should find the
198-
// 'sister' action and pass our current model on to it.
199-
this.clear();
200-
return;
201-
}
202-
this._modelRevealing = false;
203-
this._widget.show(this._model.currentReference.range);
204-
this._widget.focus();
205-
}, (err) => {
206-
this._modelRevealing = false;
207-
onUnexpectedError(err);
208-
});
209-
});
210-
211-
this._callOnClear.push(unbind);
160+
this._model = new ReferencesModel(references, this._editorService);
212161

213162
// show widget
214163
this._startTime = Date.now();
@@ -219,25 +168,25 @@ export class FindReferencesController implements editorCommon.IEditorContributio
219168
timer.stop();
220169

221170
}, (error:any) => {
222-
this.messageService.show(Severity.Error, error);
171+
this._messageService.show(Severity.Error, error);
223172
timer.stop();
224173
});
225174

226175
return this._widget;
227176
}
228177

229-
private clear():boolean {
178+
private clear(): boolean {
230179

231180
if (this._startTime !== -1) {
232-
this.telemetryService.publicLog('zoneWidgetShown', {
181+
this._telemetryService.publicLog('zoneWidgetShown', {
233182
mode: 'reference search',
234183
elapsedTime: Date.now() - this._startTime
235184
});
236185
this._startTime = -1;
237186
}
238187

239188
var result = false;
240-
if(this._widget) {
189+
if (this._widget) {
241190
this._widget.dispose();
242191
this._widget = null;
243192
result = true;
@@ -254,8 +203,50 @@ export class FindReferencesController implements editorCommon.IEditorContributio
254203
return result;
255204
}
256205

257-
}
206+
private _gotoReference(ref: OneReference): void {
207+
this._ignoreModelChangeEvent = true;
208+
const {resource, range} = ref;
209+
210+
this._editorService.openEditor({
211+
resource,
212+
options: { selection: range }
213+
}).done(openedEditor => {
214+
this._ignoreModelChangeEvent = false;
215+
216+
if (!openedEditor || openedEditor.getControl() !== this._editor) {
217+
// TODO@Alex TODO@Joh
218+
// when opening the current reference we might end up
219+
// in a different editor instance. that means we also have
220+
// a different instance of this reference search controller
221+
// and cannot hold onto the widget (which likely doesn't
222+
// exist). Instead of bailing out we should find the
223+
// 'sister' action and pass our current model on to it.
224+
this.clear();
225+
return;
226+
}
258227

228+
this._widget.show(range);
229+
this._widget.focus();
230+
231+
}, (err) => {
232+
this._ignoreModelChangeEvent = false;
233+
onUnexpectedError(err);
234+
});
235+
}
236+
237+
private _openReference(ref: OneReference, sideBySide: boolean): void {
238+
const {resource, range} = ref;
239+
this._editorService.openEditor({
240+
resource,
241+
options: { selection: range }
242+
}, sideBySide);
243+
244+
// clear stage
245+
if (!sideBySide) {
246+
this.clear();
247+
}
248+
}
249+
}
259250

260251
export class ReferenceAction extends EditorAction {
261252

src/vs/editor/contrib/referenceSearch/browser/referenceSearchModel.ts

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import * as collections from 'vs/base/common/collections';
88
import {EventEmitter} from 'vs/base/common/eventEmitter';
9+
import Event, {fromEventEmitter} from 'vs/base/common/event';
910
import {basename, dirname} from 'vs/base/common/paths';
1011
import * as strings from 'vs/base/common/strings';
1112
import URI from 'vs/base/common/uri';
@@ -16,19 +17,16 @@ import {Range} from 'vs/editor/common/core/range';
1617
import {IModel, IPosition, IRange} from 'vs/editor/common/editorCommon';
1718
import {IReference} from 'vs/editor/common/modes';
1819

19-
export namespace EventType {
20-
export var OnReferenceRangeChanged = 'refrence.rangeChanged';
21-
export var CurrentReferenceChanged = 'reference.currentChanged';
22-
}
23-
2420
export class OneReference {
2521

2622
private _id: string;
27-
private _range: IRange;
2823

29-
constructor(private _parent: FileReferences, reference: IReference) {
24+
constructor(
25+
private _parent: FileReferences,
26+
private _range: IRange,
27+
private _eventBus: EventEmitter
28+
) {
3029
this._id = generateUuid();
31-
this._range = reference.range;
3230
}
3331

3432
public get id(): string {
@@ -61,7 +59,7 @@ export class OneReference {
6159

6260
public set range(value: IRange) {
6361
this._range = value;
64-
this.parent.parent.emit(EventType.OnReferenceRangeChanged, this);
62+
this._eventBus.emit('ref/changed', this);
6563
}
6664
}
6765

@@ -145,16 +143,14 @@ export class FileReferences {
145143
}
146144
}
147145

148-
export class ReferencesModel extends EventEmitter {
146+
export class ReferencesModel {
149147

150148
private _references: FileReferences[];
151-
private _currentReference: OneReference;
149+
private _eventBus = new EventEmitter();
150+
151+
onDidChangeReferenceRange: Event<OneReference> = fromEventEmitter<OneReference>(this._eventBus, 'ref/changed');
152152

153153
constructor(references: IReference[], editorService: IEditorService) {
154-
super([
155-
EventType.CurrentReferenceChanged,
156-
EventType.OnReferenceRangeChanged
157-
]);
158154

159155
let referencesByFile: { [n: string]: FileReferences } = Object.create(null);
160156
let seen: { [n: string]: boolean } = Object.create(null);
@@ -169,7 +165,7 @@ export class ReferencesModel extends EventEmitter {
169165
let fileReferences = new FileReferences(this, resource, editorService);
170166

171167
fileReferences = collections.lookupOrInsert(referencesByFile, fileReferences.id, fileReferences);
172-
fileReferences.children.push(new OneReference(fileReferences, reference));
168+
fileReferences.children.push(new OneReference(fileReferences, reference.range, this._eventBus));
173169
}
174170
});
175171

@@ -181,15 +177,6 @@ export class ReferencesModel extends EventEmitter {
181177
return this._references;
182178
}
183179

184-
public get currentReference(): OneReference {
185-
return this._currentReference;
186-
}
187-
188-
public set currentReference(reference: OneReference) {
189-
this._currentReference = reference;
190-
this.emit(EventType.CurrentReferenceChanged, this);
191-
}
192-
193180
public nextReference(reference: OneReference): OneReference {
194181

195182
var idx = reference.parent.children.indexOf(reference),

0 commit comments

Comments
 (0)