Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/common/editor/resourceEditorInput.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Verbosity, EditorInputWithPreferredResource, EditorInputCapabilities, IFileLimitedEditorInputOptions } from '../editor.js';
7
import { EditorInput } from './editorInput.js';
8
import { URI } from '../../../base/common/uri.js';
9
import { ByteSize, IFileReadLimits, IFileService, getLargeFileConfirmationLimit } from '../../../platform/files/common/files.js';
10
import { ILabelService } from '../../../platform/label/common/label.js';
11
import { dirname, isEqual } from '../../../base/common/resources.js';
12
import { IFilesConfigurationService } from '../../services/filesConfiguration/common/filesConfigurationService.js';
13
import { IMarkdownString } from '../../../base/common/htmlContent.js';
14
import { isConfigured } from '../../../platform/configuration/common/configuration.js';
15
import { ITextResourceConfigurationService } from '../../../editor/common/services/textResourceConfiguration.js';
16
import { ICustomEditorLabelService } from '../../services/editor/common/customEditorLabelService.js';
17
18
/**
19
* The base class for all editor inputs that open resources.
20
*/
21
export abstract class AbstractResourceEditorInput extends EditorInput implements EditorInputWithPreferredResource {
22
23
override get capabilities(): EditorInputCapabilities {
24
let capabilities = EditorInputCapabilities.CanSplitInGroup;
25
26
if (this.fileService.hasProvider(this.resource)) {
27
if (this.filesConfigurationService.isReadonly(this.resource)) {
28
capabilities |= EditorInputCapabilities.Readonly;
29
}
30
} else {
31
capabilities |= EditorInputCapabilities.Untitled;
32
}
33
34
if (!(capabilities & EditorInputCapabilities.Readonly)) {
35
capabilities |= EditorInputCapabilities.CanDropIntoEditor;
36
}
37
38
return capabilities;
39
}
40
41
private _preferredResource: URI;
42
get preferredResource(): URI { return this._preferredResource; }
43
44
constructor(
45
readonly resource: URI,
46
preferredResource: URI | undefined,
47
@ILabelService protected readonly labelService: ILabelService,
48
@IFileService protected readonly fileService: IFileService,
49
@IFilesConfigurationService protected readonly filesConfigurationService: IFilesConfigurationService,
50
@ITextResourceConfigurationService protected readonly textResourceConfigurationService: ITextResourceConfigurationService,
51
@ICustomEditorLabelService protected readonly customEditorLabelService: ICustomEditorLabelService
52
) {
53
super();
54
55
this._preferredResource = preferredResource || resource;
56
57
this.registerListeners();
58
}
59
60
private registerListeners(): void {
61
62
// Clear our labels on certain label related events
63
this._register(this.labelService.onDidChangeFormatters(e => this.onLabelEvent(e.scheme)));
64
this._register(this.fileService.onDidChangeFileSystemProviderRegistrations(e => this.onLabelEvent(e.scheme)));
65
this._register(this.fileService.onDidChangeFileSystemProviderCapabilities(e => this.onLabelEvent(e.scheme)));
66
this._register(this.customEditorLabelService.onDidChange(() => this.updateLabel()));
67
this._register(this.filesConfigurationService.onDidChangeReadonly(() => this._onDidChangeCapabilities.fire()));
68
}
69
70
private onLabelEvent(scheme: string): void {
71
if (scheme === this._preferredResource.scheme) {
72
this.updateLabel();
73
}
74
}
75
76
private updateLabel(): void {
77
78
// Clear any cached labels from before
79
this._name = undefined;
80
this._shortDescription = undefined;
81
this._mediumDescription = undefined;
82
this._longDescription = undefined;
83
this._shortTitle = undefined;
84
this._mediumTitle = undefined;
85
this._longTitle = undefined;
86
87
// Trigger recompute of label
88
this._onDidChangeLabel.fire();
89
}
90
91
setPreferredResource(preferredResource: URI): void {
92
if (!isEqual(preferredResource, this._preferredResource)) {
93
this._preferredResource = preferredResource;
94
95
this.updateLabel();
96
}
97
}
98
99
private _name: string | undefined = undefined;
100
override getName(): string {
101
if (typeof this._name !== 'string') {
102
this._name = this.customEditorLabelService.getName(this._preferredResource) ?? this.labelService.getUriBasenameLabel(this._preferredResource);
103
}
104
105
return this._name;
106
}
107
108
override getDescription(verbosity = Verbosity.MEDIUM): string | undefined {
109
switch (verbosity) {
110
case Verbosity.SHORT:
111
return this.shortDescription;
112
case Verbosity.LONG:
113
return this.longDescription;
114
case Verbosity.MEDIUM:
115
default:
116
return this.mediumDescription;
117
}
118
}
119
120
private _shortDescription: string | undefined = undefined;
121
private get shortDescription(): string {
122
if (typeof this._shortDescription !== 'string') {
123
this._shortDescription = this.labelService.getUriBasenameLabel(dirname(this._preferredResource));
124
}
125
126
return this._shortDescription;
127
}
128
129
private _mediumDescription: string | undefined = undefined;
130
private get mediumDescription(): string {
131
if (typeof this._mediumDescription !== 'string') {
132
this._mediumDescription = this.labelService.getUriLabel(dirname(this._preferredResource), { relative: true });
133
}
134
135
return this._mediumDescription;
136
}
137
138
private _longDescription: string | undefined = undefined;
139
private get longDescription(): string {
140
if (typeof this._longDescription !== 'string') {
141
this._longDescription = this.labelService.getUriLabel(dirname(this._preferredResource));
142
}
143
144
return this._longDescription;
145
}
146
147
private _shortTitle: string | undefined = undefined;
148
private get shortTitle(): string {
149
if (typeof this._shortTitle !== 'string') {
150
this._shortTitle = this.getName();
151
}
152
153
return this._shortTitle;
154
}
155
156
private _mediumTitle: string | undefined = undefined;
157
private get mediumTitle(): string {
158
if (typeof this._mediumTitle !== 'string') {
159
this._mediumTitle = this.labelService.getUriLabel(this._preferredResource, { relative: true });
160
}
161
162
return this._mediumTitle;
163
}
164
165
private _longTitle: string | undefined = undefined;
166
private get longTitle(): string {
167
if (typeof this._longTitle !== 'string') {
168
this._longTitle = this.labelService.getUriLabel(this._preferredResource);
169
}
170
171
return this._longTitle;
172
}
173
174
override getTitle(verbosity?: Verbosity): string {
175
switch (verbosity) {
176
case Verbosity.SHORT:
177
return this.shortTitle;
178
case Verbosity.LONG:
179
return this.longTitle;
180
default:
181
case Verbosity.MEDIUM:
182
return this.mediumTitle;
183
}
184
}
185
186
override isReadonly(): boolean | IMarkdownString {
187
return this.filesConfigurationService.isReadonly(this.resource);
188
}
189
190
protected ensureLimits(options?: IFileLimitedEditorInputOptions): IFileReadLimits | undefined {
191
if (options?.limits) {
192
return options.limits; // respect passed in limits if any
193
}
194
195
// We want to determine the large file configuration based on the best defaults
196
// for the resource but also respecting user settings. We only apply user settings
197
// if explicitly configured by the user. Otherwise we pick the best limit for the
198
// resource scheme.
199
200
const defaultSizeLimit = getLargeFileConfirmationLimit(this.resource);
201
let configuredSizeLimit: number | undefined = undefined;
202
203
const configuredSizeLimitMb = this.textResourceConfigurationService.inspect<number>(this.resource, null, 'workbench.editorLargeFileConfirmation');
204
if (isConfigured(configuredSizeLimitMb)) {
205
configuredSizeLimit = configuredSizeLimitMb.value * ByteSize.MB; // normalize to MB
206
}
207
208
return {
209
size: configuredSizeLimit ?? defaultSizeLimit
210
};
211
}
212
}
213
214