Skip to content

Commit daa77e1

Browse files
author
Benjamin Pasero
committed
editors - polish readonly handling
1 parent 4934a6f commit daa77e1

7 files changed

Lines changed: 44 additions & 34 deletions

File tree

src/vs/editor/browser/editorBrowser.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
77
import { IMouseEvent, IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
88
import { IDisposable } from 'vs/base/common/lifecycle';
9-
import { OverviewRulerPosition, ConfigurationChangedEvent, EditorLayoutInfo, IComputedEditorOptions, EditorOption, FindComputedEditorOptionValueById, IEditorOptions } from 'vs/editor/common/config/editorOptions';
9+
import { OverviewRulerPosition, ConfigurationChangedEvent, EditorLayoutInfo, IComputedEditorOptions, EditorOption, FindComputedEditorOptionValueById, IEditorOptions, IDiffEditorOptions } from 'vs/editor/common/config/editorOptions';
1010
import { ICursors } from 'vs/editor/common/controller/cursorCommon';
1111
import { ICursorPositionChangedEvent, ICursorSelectionChangedEvent } from 'vs/editor/common/controller/cursorEvents';
1212
import { IPosition, Position } from 'vs/editor/common/core/position';
@@ -938,6 +938,11 @@ export interface IDiffEditor extends editorCommon.IEditor {
938938
* If the diff computation is not finished or the model is missing, will return null.
939939
*/
940940
getDiffLineInformationForModified(lineNumber: number): IDiffLineInformation | null;
941+
942+
/**
943+
* Update the editor's options after the editor has been created.
944+
*/
945+
updateOptions(newOptions: IDiffEditorOptions): void;
941946
}
942947

943948
/**

src/vs/monaco.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,6 +4233,10 @@ declare namespace monaco.editor {
42334233
* If the diff computation is not finished or the model is missing, will return null.
42344234
*/
42354235
getDiffLineInformationForModified(lineNumber: number): IDiffLineInformation | null;
4236+
/**
4237+
* Update the editor's options after the editor has been created.
4238+
*/
4239+
updateOptions(newOptions: IDiffEditorOptions): void;
42364240
}
42374241

42384242
export class FontInfo extends BareFontInfo {

src/vs/workbench/browser/parts/editor/baseEditor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ export abstract class BaseEditor extends Panel implements IEditor {
7878
* The provided cancellation token should be used to test if the operation
7979
* was cancelled.
8080
*/
81-
setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
81+
async setInput(input: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
8282
this._input = input;
8383
this._options = options;
84-
85-
return Promise.resolve();
8684
}
8785

8886
/**

src/vs/workbench/browser/parts/editor/textDiffEditor.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
123123
});
124124
this.diffNavigatorDisposables.add(this.diffNavigator);
125125

126-
// Readonly flag
127-
diffEditor.updateOptions({ readOnly: resolvedDiffEditorModel.isReadonly() });
126+
// Since the resolved model provides information about being readonly
127+
// or not, we apply it here to the editor even though the editor input
128+
// was already asked for being readonly or not. The rationale is that
129+
// a resolved model might have more specific information about being
130+
// readonly or not that the input did not have.
131+
diffEditor.updateOptions({
132+
readOnly: resolvedDiffEditorModel.modifiedModel?.isReadonly(),
133+
originalEditable: !resolvedDiffEditorModel.originalModel?.isReadonly()
134+
});
128135
} catch (error) {
129136

130137
// In case we tried to open a file and the response indicates that this is not a text file, fallback to binary diff.
@@ -136,14 +143,6 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
136143
}
137144
}
138145

139-
setOptions(options: EditorOptions | undefined): void {
140-
const textOptions = <TextEditorOptions>options;
141-
if (textOptions && isFunction(textOptions.apply)) {
142-
const diffEditor = assertIsDefined(this.getControl());
143-
textOptions.apply(diffEditor, ScrollType.Smooth);
144-
}
145-
}
146-
147146
private restoreTextDiffEditorViewState(editor: EditorInput, control: IDiffEditor): boolean {
148147
if (editor instanceof DiffEditorInput) {
149148
const resource = this.toDiffEditorViewStateResource(editor);
@@ -210,6 +209,7 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditor {
210209
protected getConfigurationOverrides(): ICodeEditorOptions {
211210
const options: IDiffEditorOptions = super.getConfigurationOverrides();
212211

212+
options.readOnly = this.input instanceof DiffEditorInput && this.input.modifiedInput.isReadonly();
213213
options.originalEditable = this.input instanceof DiffEditorInput && !this.input.originalInput.isReadonly();
214214
options.lineDecorationsWidth = '2ch';
215215

src/vs/workbench/browser/parts/editor/textEditor.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import { localize } from 'vs/nls';
77
import { URI } from 'vs/base/common/uri';
88
import { distinct, deepClone, assign } from 'vs/base/common/objects';
9-
import { isObject, assertIsDefined, withNullAsUndefined } from 'vs/base/common/types';
9+
import { isObject, assertIsDefined, withNullAsUndefined, isFunction } from 'vs/base/common/types';
1010
import { Dimension } from 'vs/base/browser/dom';
1111
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
12-
import { EditorInput, EditorOptions, IEditorMemento, ITextEditor, SaveReason } from 'vs/workbench/common/editor';
12+
import { EditorInput, EditorOptions, IEditorMemento, ITextEditor, SaveReason, TextEditorOptions } from 'vs/workbench/common/editor';
1313
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
14-
import { IEditorViewState, IEditor } from 'vs/editor/common/editorCommon';
14+
import { IEditorViewState, IEditor, ScrollType } from 'vs/editor/common/editorCommon';
1515
import { IStorageService } from 'vs/platform/storage/common/storage';
1616
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1717
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
@@ -202,6 +202,14 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditor {
202202
editorContainer.setAttribute('aria-label', this.computeAriaLabel());
203203
}
204204

205+
setOptions(options: EditorOptions | undefined): void {
206+
const textOptions = options as TextEditorOptions;
207+
if (textOptions && isFunction(textOptions.apply)) {
208+
const textEditor = assertIsDefined(this.getControl());
209+
textOptions.apply(textEditor, ScrollType.Smooth);
210+
}
211+
}
212+
205213
protected setEditorVisible(visible: boolean, group: IEditorGroup | undefined): void {
206214

207215
// Pass on to Editor

src/vs/workbench/browser/parts/editor/textResourceEditor.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
9090
if (!optionsGotApplied) {
9191
this.restoreTextResourceEditorViewState(input, textEditor);
9292
}
93+
94+
// Since the resolved model provides information about being readonly
95+
// or not, we apply it here to the editor even though the editor input
96+
// was already asked for being readonly or not. The rationale is that
97+
// a resolved model might have more specific information about being
98+
// readonly or not that the input did not have.
99+
textEditor.updateOptions({ readOnly: resolvedModel.isReadonly() });
93100
}
94101

95102
private restoreTextResourceEditorViewState(editor: EditorInput, control: IEditor) {
@@ -101,14 +108,6 @@ export class AbstractTextResourceEditor extends BaseTextEditor {
101108
}
102109
}
103110

104-
setOptions(options: EditorOptions | undefined): void {
105-
const textOptions = <TextEditorOptions>options;
106-
if (textOptions && isFunction(textOptions.apply)) {
107-
const textEditor = assertIsDefined(this.getControl());
108-
textOptions.apply(textEditor, ScrollType.Smooth);
109-
}
110-
}
111-
112111
protected getAriaLabel(): string {
113112
let ariaLabel: string;
114113

src/vs/workbench/contrib/files/browser/editors/textFileEditor.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ export class TextFileEditor extends BaseTextEditor {
117117
}
118118
}
119119

120-
setOptions(options: EditorOptions | undefined): void {
121-
const textOptions = options as TextEditorOptions;
122-
if (textOptions && isFunction(textOptions.apply)) {
123-
const textEditor = assertIsDefined(this.getControl());
124-
textOptions.apply(textEditor, ScrollType.Smooth);
125-
}
126-
}
127-
128120
async setInput(input: FileEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
129121

130122
// Update/clear view settings if input changes
@@ -164,7 +156,11 @@ export class TextFileEditor extends BaseTextEditor {
164156
(<TextEditorOptions>options).apply(textEditor, ScrollType.Immediate);
165157
}
166158

167-
// Readonly flag
159+
// Since the resolved model provides information about being readonly
160+
// or not, we apply it here to the editor even though the editor input
161+
// was already asked for being readonly or not. The rationale is that
162+
// a resolved model might have more specific information about being
163+
// readonly or not that the input did not have.
168164
textEditor.updateOptions({ readOnly: textFileModel.isReadonly() });
169165
} catch (error) {
170166
this.handleSetInputError(error, input, options);

0 commit comments

Comments
 (0)