Skip to content

Commit b05821b

Browse files
committed
1 parent f8eb740 commit b05821b

1 file changed

Lines changed: 130 additions & 88 deletions

File tree

src/vs/workbench/parts/preferences/browser/preferencesEditor.ts

Lines changed: 130 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,14 @@ class PreferencesRenderers extends Disposable {
316316
}
317317

318318
public filterPreferences(filter: string): number {
319-
const defaultPreferencesFilterResult = filter ? (<ISettingsEditorModel>this._defaultPreferencesRenderer.preferencesModel).filterSettings(filter) : null;
320-
const editablePreferencesFilterResult = filter ? (<ISettingsEditorModel>this._editablePreferencesRenderer.preferencesModel).filterSettings(filter) : null;
321-
const consolidatedSettings = this._consolidateSettings(editablePreferencesFilterResult ? editablePreferencesFilterResult.filteredGroups : (<ISettingsEditorModel>this._editablePreferencesRenderer.preferencesModel).settingsGroups,
322-
defaultPreferencesFilterResult ? defaultPreferencesFilterResult.filteredGroups : (<ISettingsEditorModel>this._defaultPreferencesRenderer.preferencesModel).settingsGroups);
319+
const defaultPreferencesFilterResult = this._filterPreferences(filter, this._defaultPreferencesRenderer);
320+
const editablePreferencesFilterResult = this._filterPreferences(filter, this._editablePreferencesRenderer);
321+
322+
const defaultPreferencesFilteredGroups = defaultPreferencesFilterResult ? defaultPreferencesFilterResult.filteredGroups : this._getAllPreferences(this._defaultPreferencesRenderer);
323+
const editablePreferencesFilteredGroups = editablePreferencesFilterResult ? editablePreferencesFilterResult.filteredGroups : this._getAllPreferences(this._editablePreferencesRenderer);
324+
const consolidatedSettings = this._consolidateSettings(editablePreferencesFilteredGroups, defaultPreferencesFilteredGroups);
323325
this._settingsNavigator = new SettingsNavigator(filter ? consolidatedSettings : []);
324-
this._filterPreferences(defaultPreferencesFilterResult, this._defaultPreferencesRenderer);
325-
this._filterPreferences(editablePreferencesFilterResult, this._editablePreferencesRenderer);
326+
326327
return consolidatedSettings.length;
327328
}
328329

@@ -332,10 +333,17 @@ class PreferencesRenderers extends Disposable {
332333
this._focusPreference(setting, this._editablePreferencesRenderer);
333334
}
334335

335-
private _filterPreferences(filterResult: IFilterResult, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
336+
private _getAllPreferences(preferencesRenderer: IPreferencesRenderer<ISetting>): ISettingsGroup[] {
337+
return preferencesRenderer ? (<ISettingsEditorModel>preferencesRenderer.preferencesModel).settingsGroups : [];
338+
}
339+
340+
private _filterPreferences(filter: string, preferencesRenderer: IPreferencesRenderer<ISetting>): IFilterResult {
341+
let filterResult = null;
336342
if (preferencesRenderer) {
343+
filterResult = filter ? (<ISettingsEditorModel>preferencesRenderer.preferencesModel).filterSettings(filter) : null;
337344
preferencesRenderer.filterPreferences(filterResult);
338345
}
346+
return filterResult;
339347
}
340348

341349
private _focusPreference(preference: ISetting, preferencesRenderer: IPreferencesRenderer<ISetting>): void {
@@ -421,14 +429,9 @@ class SideBySidePreferencesWidget extends Widget {
421429
return this.getOrCreateEditablePreferencesEditor(editablePreferencesEditorInput)
422430
.then(() => {
423431
this.dolayout(this.sash.getVerticalSashLeft());
424-
return TPromise.join([this.defaultPreferencesEditor.updateInput(defaultPreferencesEditorInput, options, toResource(editablePreferencesEditorInput)),
425-
this.editablePreferencesEditor.setInput(editablePreferencesEditorInput, options)])
426-
.then(() => {
427-
return {
428-
defaultPreferencesRenderer: (<CodeEditor>this.defaultPreferencesEditor.getControl()).getContribution<DefaultSettingsEditorContribution>(DefaultSettingsEditorContribution.ID).getPreferencesRenderer(),
429-
editablePreferencesRenderer: (<CodeEditor>this.editablePreferencesEditor.getControl()).getContribution<SettingsEditorContribution>(SettingsEditorContribution.ID).getPreferencesRenderer()
430-
};
431-
});
432+
return TPromise.join([this.updateInput(this.defaultPreferencesEditor, defaultPreferencesEditorInput, DefaultSettingsEditorContribution.ID, toResource(editablePreferencesEditorInput), options),
433+
this.updateInput(this.editablePreferencesEditor, editablePreferencesEditorInput, SettingsEditorContribution.ID, defaultPreferencesEditorInput.getResource(), options)])
434+
.then(([defaultPreferencesRenderer, editablePreferencesRenderer]) => ({ defaultPreferencesRenderer, editablePreferencesRenderer }));
432435
});
433436
}
434437

@@ -465,6 +468,11 @@ class SideBySidePreferencesWidget extends Widget {
465468
}
466469
}
467470

471+
private updateInput(editor: BaseEditor, input: EditorInput, editorContributionId: string, associatedPreferencesModelUri: URI, options: EditorOptions, ): TPromise<IPreferencesRenderer<ISetting>> {
472+
return editor.setInput(input, options)
473+
.then(() => (<CodeEditor>editor.getControl()).getContribution<ISettingsEditorContribution>(editorContributionId).createPreferencesRenderer(associatedPreferencesModelUri));
474+
}
475+
468476
private getOrCreateEditablePreferencesEditor(editorInput: EditorInput): TPromise<BaseEditor> {
469477
if (this.editablePreferencesEditor) {
470478
return TPromise.as(this.editablePreferencesEditor);
@@ -564,34 +572,24 @@ export class DefaultPreferencesEditor extends BaseTextEditor {
564572
return options;
565573
}
566574

567-
updateInput(input: DefaultPreferencesEditorInput, options: EditorOptions, editablePreferencesUri: URI): TPromise<void> {
568-
return this.setInput(input, options)
575+
setInput(input: DefaultPreferencesEditorInput, options: EditorOptions): TPromise<void> {
576+
return super.setInput(input, options)
569577
.then(() => this.input.resolve()
570-
.then(editorModel => TPromise.join<any>([
571-
editorModel.load(),
572-
this.preferencesService.createPreferencesEditorModel(editablePreferencesUri)
573-
]))
574-
.then(([editorModel, preferencesModel]) => (<DefaultPreferencesCodeEditor>this.getControl()).setModels((<ResourceEditorModel>editorModel).textEditorModel, <SettingsEditorModel>preferencesModel)));
578+
.then(editorModel => editorModel.load())
579+
.then(editorModel => this.getControl().setModel((<ResourceEditorModel>editorModel).textEditorModel)));
575580
}
576581

577582
public layout(dimension: Dimension) {
578583
this.getControl().layout(dimension);
579584
}
580585

581-
public clearInput(): void {
582-
(<DefaultPreferencesCodeEditor>this.getControl()).clearModels();
583-
super.clearInput();
584-
}
585-
586586
protected getAriaLabel(): string {
587587
return nls.localize('preferencesAriaLabel', "Default preferences. Readonly text editor.");
588588
}
589589
}
590590

591591
class DefaultPreferencesCodeEditor extends CodeEditor {
592592

593-
public settingsModel: IPreferencesEditorModel<ISetting>;
594-
595593
protected _getContributions(): IEditorContributionCtor[] {
596594
let contributions = super._getContributions();
597595
let skipContributions = [FoldingController.prototype, SelectionHighlighter.prototype, FindController.prototype];
@@ -600,100 +598,122 @@ class DefaultPreferencesCodeEditor extends CodeEditor {
600598
return contributions;
601599
}
602600

603-
setModels(model: editorCommon.IModel, settingsModel: SettingsEditorModel): void {
604-
this.settingsModel = settingsModel;
605-
super.setModel(model);
606-
const renderer = this.getContribution<DefaultSettingsEditorContribution>(DefaultSettingsEditorContribution.ID).getPreferencesRenderer();
607-
if (renderer) {
608-
renderer.associatedPreferencesModel = this.settingsModel;
609-
}
610-
}
601+
}
602+
603+
interface ISettingsEditorContribution extends editorCommon.IEditorContribution {
604+
605+
createPreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>>;
611606

612-
clearModels(): void {
613-
if (this.settingsModel) {
614-
this.settingsModel.dispose();
615-
this.settingsModel = null;
616-
}
617-
super.setModel(null);
618-
}
619607
}
620608

621-
export abstract class PreferencesEditorContribution<T> extends Disposable implements editorCommon.IEditorContribution {
609+
class DefaultSettingsEditorContribution extends Disposable implements ISettingsEditorContribution {
622610

623-
private preferencesRenderer: IPreferencesRenderer<T>;
611+
static ID: string = 'editor.contrib.defaultsettings';
624612

625-
constructor(protected editor: ICodeEditor,
626-
@IInstantiationService protected instantiationService: IInstantiationService,
627-
@IPreferencesService protected preferencesService: IPreferencesService
613+
private preferencesRenderer: TPromise<IPreferencesRenderer<ISetting>>;
614+
615+
constructor(private editor: ICodeEditor,
616+
@IInstantiationService private instantiationService: IInstantiationService,
617+
@IPreferencesService private preferencesService: IPreferencesService
628618
) {
629619
super();
630-
this._register(editor.onDidChangeModel(() => this.onModelChanged()));
631620
}
632621

633-
private onModelChanged(): void {
634-
const model = this.editor.getModel();
635-
this.disposePreferencesRenderer();
636-
if (model) {
637-
this.createPreferencesRenderer()
638-
.then(preferencesRenderer => {
639-
this.preferencesRenderer = preferencesRenderer;
640-
if (this.preferencesRenderer) {
641-
this.preferencesRenderer.render();
642-
}
643-
});
644-
}
622+
getId(): string {
623+
return DefaultSettingsEditorContribution.ID;
645624
}
646625

647-
getPreferencesRenderer(): IPreferencesRenderer<T> {
648-
return this.preferencesRenderer;
626+
createPreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>> {
627+
return this._hasAssociatedPreferencesModelChanged(associatedPreferencesModelUri)
628+
.then(changed => {
629+
if (changed) {
630+
return this.preferencesService.createPreferencesEditorModel<ISetting>(associatedPreferencesModelUri)
631+
.then(associatedPreferencesEditorModel => {
632+
if (this.preferencesRenderer) {
633+
return this._updatePreferencesRenderer(associatedPreferencesEditorModel);
634+
} else {
635+
return this._createPreferencesRenderer(associatedPreferencesEditorModel);
636+
}
637+
});
638+
}
639+
return this.preferencesRenderer;
640+
});
649641
}
650642

651-
protected abstract createPreferencesRenderer(): TPromise<IPreferencesRenderer<T>>
652-
abstract getId(): string;
653-
654-
private disposePreferencesRenderer() {
643+
_hasAssociatedPreferencesModelChanged(associatedPreferencesModelUri: URI): TPromise<boolean> {
655644
if (this.preferencesRenderer) {
656-
this.preferencesRenderer.dispose();
657-
this.preferencesRenderer = null;
645+
return this.preferencesRenderer.then(preferencesRenderer => {
646+
return !(preferencesRenderer && preferencesRenderer.associatedPreferencesModel && preferencesRenderer.associatedPreferencesModel.uri.fsPath === associatedPreferencesModelUri.fsPath);
647+
});
658648
}
649+
return TPromise.as(true);
659650
}
660651

661-
public dispose() {
662-
this.disposePreferencesRenderer();
663-
super.dispose();
664-
}
665-
}
666-
667-
export class DefaultSettingsEditorContribution extends PreferencesEditorContribution<ISetting> implements editorCommon.IEditorContribution {
668-
669-
static ID: string = 'editor.contrib.defaultsettings';
670-
671-
protected createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
672-
return this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)
652+
_createPreferencesRenderer(associatedPreferencesEditorModel: IPreferencesEditorModel<ISetting>): TPromise<IPreferencesRenderer<ISetting>> {
653+
this.preferencesRenderer = this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)
673654
.then(editorModel => {
674655
if (editorModel instanceof DefaultSettingsEditorModel) {
675-
return this.instantiationService.createInstance(DefaultSettingsRenderer, this.editor, editorModel, (<DefaultPreferencesCodeEditor>this.editor).settingsModel);
656+
return this.instantiationService.createInstance(DefaultSettingsRenderer, this.editor, editorModel, associatedPreferencesEditorModel);
676657
}
677658
return null;
659+
})
660+
.then(preferencesRenderer => {
661+
if (preferencesRenderer) {
662+
preferencesRenderer.render();
663+
}
664+
return preferencesRenderer;
678665
});
666+
return this.preferencesRenderer;
679667
}
680668

681-
getId(): string {
682-
return DefaultSettingsEditorContribution.ID;
669+
_updatePreferencesRenderer(associatedPreferencesEditorModel: IPreferencesEditorModel<ISetting>): TPromise<IPreferencesRenderer<ISetting>> {
670+
return this.preferencesRenderer.then(preferencesRenderer => {
671+
if (preferencesRenderer) {
672+
if (preferencesRenderer.associatedPreferencesModel) {
673+
preferencesRenderer.associatedPreferencesModel.dispose();
674+
}
675+
preferencesRenderer.associatedPreferencesModel = associatedPreferencesEditorModel;
676+
}
677+
return preferencesRenderer;
678+
});
679+
}
680+
681+
dispose() {
682+
if (this.preferencesRenderer) {
683+
this.preferencesRenderer.then(preferencesRenderer => {
684+
if (preferencesRenderer) {
685+
if (preferencesRenderer.associatedPreferencesModel) {
686+
preferencesRenderer.associatedPreferencesModel.dispose();
687+
}
688+
preferencesRenderer.dispose();
689+
}
690+
});
691+
}
692+
super.dispose();
683693
}
684694
}
685695

686696
@editorContribution
687-
export class SettingsEditorContribution extends PreferencesEditorContribution<ISetting> implements editorCommon.IEditorContribution {
697+
class SettingsEditorContribution extends Disposable implements ISettingsEditorContribution {
688698

689699
static ID: string = 'editor.contrib.settings';
690700

701+
private preferencesRenderer: TPromise<IPreferencesRenderer<ISetting>>;
702+
703+
constructor(private editor: ICodeEditor,
704+
@IInstantiationService private instantiationService: IInstantiationService,
705+
@IPreferencesService private preferencesService: IPreferencesService
706+
) {
707+
super();
708+
}
709+
691710
getId(): string {
692711
return SettingsEditorContribution.ID;
693712
}
694713

695-
protected createPreferencesRenderer(): TPromise<IPreferencesRenderer<ISetting>> {
696-
return TPromise.join<any>([this.preferencesService.createPreferencesEditorModel(this.preferencesService.defaultSettingsResource), this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)])
714+
createPreferencesRenderer(associatedPreferencesModelUri: URI): TPromise<IPreferencesRenderer<ISetting>> {
715+
this.disposePreferencesRenderer();
716+
this.preferencesRenderer = TPromise.join<any>([this.preferencesService.createPreferencesEditorModel(this.preferencesService.defaultSettingsResource), this.preferencesService.createPreferencesEditorModel(this.editor.getModel().uri)])
697717
.then(([defaultSettingsModel, settingsModel]) => {
698718
if (settingsModel instanceof SettingsEditorModel) {
699719
if (ConfigurationTarget.USER === settingsModel.configurationTarget) {
@@ -702,7 +722,29 @@ export class SettingsEditorContribution extends PreferencesEditorContribution<IS
702722
return this.instantiationService.createInstance(WorkspaceSettingsRenderer, this.editor, settingsModel, defaultSettingsModel);
703723
}
704724
return null;
725+
})
726+
.then(preferencesRenderer => {
727+
if (preferencesRenderer) {
728+
preferencesRenderer.render();
729+
}
730+
return preferencesRenderer;
731+
});
732+
return this.preferencesRenderer;
733+
}
734+
735+
private disposePreferencesRenderer(): void {
736+
if (this.preferencesRenderer) {
737+
this.preferencesRenderer.then(preferencesRenderer => {
738+
if (preferencesRenderer) {
739+
preferencesRenderer.dispose();
740+
}
705741
});
742+
}
743+
}
744+
745+
dispose() {
746+
this.disposePreferencesRenderer();
747+
super.dispose();
706748
}
707749
}
708750

0 commit comments

Comments
 (0)