Skip to content

Commit 22d8581

Browse files
author
Benjamin Pasero
committed
resources - add a helper to create local resources
1 parent 8d0ce97 commit 22d8581

4 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/vs/base/common/resources.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ export namespace DataUri {
284284
}
285285
}
286286

287-
288287
export class ResourceGlobMatcher {
289288

290289
private readonly globalExpression: ParsedExpression;
@@ -311,3 +310,14 @@ export class ResourceGlobMatcher {
311310
return !!this.globalExpression(resource.path);
312311
}
313312
}
313+
314+
export function toLocalResource(resource: URI, remoteAuthority: string | undefined): URI {
315+
let scheme: string;
316+
if (remoteAuthority) {
317+
scheme = Schemas.vscodeRemote;
318+
} else {
319+
scheme = Schemas.file;
320+
}
321+
322+
return resource.with({ scheme });
323+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import { Schemas } from 'vs/base/common/network';
5151
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
5252
import { OpenWorkspaceButtonContribution } from 'vs/workbench/browser/parts/editor/editorWidgets';
5353
import { ZoomStatusbarItem } from 'vs/workbench/browser/parts/editor/resourceViewer';
54+
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
55+
import { toLocalResource } from 'vs/base/common/resources';
5456

5557
// Register String Editor
5658
Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
@@ -111,7 +113,8 @@ interface ISerializedUntitledEditorInput {
111113
class UntitledEditorInputFactory implements IEditorInputFactory {
112114

113115
constructor(
114-
@ITextFileService private readonly textFileService: ITextFileService
116+
@ITextFileService private readonly textFileService: ITextFileService,
117+
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService
115118
) { }
116119

117120
serialize(editorInput: EditorInput): string | undefined {
@@ -123,7 +126,7 @@ class UntitledEditorInputFactory implements IEditorInputFactory {
123126

124127
let resource = untitledEditorInput.getResource();
125128
if (untitledEditorInput.hasAssociatedFilePath) {
126-
resource = resource.with({ scheme: Schemas.file }); // untitled with associated file path use the file schema
129+
resource = toLocalResource(resource, this.environmentService.configuration.remoteAuthority); // untitled with associated file path use the local schema
127130
}
128131

129132
const serialized: ISerializedUntitledEditorInput = {
@@ -140,7 +143,7 @@ class UntitledEditorInputFactory implements IEditorInputFactory {
140143
return instantiationService.invokeFunction<UntitledEditorInput>(accessor => {
141144
const deserialized: ISerializedUntitledEditorInput = JSON.parse(serializedEditorInput);
142145
const resource = !!deserialized.resourceJSON ? URI.revive(deserialized.resourceJSON) : URI.parse(deserialized.resource);
143-
const filePath = resource.scheme === Schemas.file ? resource.fsPath : undefined;
146+
const filePath = resource.scheme === Schemas.untitled ? undefined : resource.scheme === Schemas.file ? resource.fsPath : resource.path;
144147
const language = deserialized.modeId;
145148
const encoding = deserialized.encoding;
146149

src/vs/workbench/contrib/files/browser/fileCommands.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/
3838
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
3939
import { ILabelService } from 'vs/platform/label/common/label';
4040
import { onUnexpectedError } from 'vs/base/common/errors';
41-
import { basename } from 'vs/base/common/resources';
41+
import { basename, toLocalResource } from 'vs/base/common/resources';
4242
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
4343
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
4444

@@ -138,11 +138,7 @@ function save(
138138
if (!isSaveAs && resource.scheme === Schemas.untitled && untitledEditorService.hasAssociatedFilePath(resource)) {
139139
savePromise = textFileService.save(resource, options).then(result => {
140140
if (result) {
141-
if (environmentService.configuration.remoteAuthority) {
142-
return resource.with({ scheme: Schemas.vscodeRemote });
143-
}
144-
145-
return resource.with({ scheme: Schemas.file });
141+
return toLocalResource(resource, environmentService.configuration.remoteAuthority);
146142
}
147143

148144
return undefined;

src/vs/workbench/contrib/files/common/files.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsSe
2424
import { ExplorerItem } from 'vs/workbench/contrib/files/common/explorerModel';
2525
import { once } from 'vs/base/common/functional';
2626
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
27+
import { toLocalResource } from 'vs/base/common/resources';
2728

2829
/**
2930
* Explorer viewlet id.
@@ -145,7 +146,7 @@ export class FileOnDiskContentProvider implements ITextModelContentProvider {
145146
}
146147

147148
provideTextContent(resource: URI): Promise<ITextModel> {
148-
const savedFileResource = this.toSavedFileResource(resource);
149+
const savedFileResource = toLocalResource(resource, this.environmentService.configuration.remoteAuthority);
149150

150151
// Make sure our file from disk is resolved up to date
151152
return this.resolveEditorModel(resource).then(codeEditorModel => {
@@ -173,7 +174,7 @@ export class FileOnDiskContentProvider implements ITextModelContentProvider {
173174
private resolveEditorModel(resource: URI, createAsNeeded?: true): Promise<ITextModel>;
174175
private resolveEditorModel(resource: URI, createAsNeeded?: boolean): Promise<ITextModel | null>;
175176
private resolveEditorModel(resource: URI, createAsNeeded: boolean = true): Promise<ITextModel | null> {
176-
const savedFileResource = this.toSavedFileResource(resource);
177+
const savedFileResource = toLocalResource(resource, this.environmentService.configuration.remoteAuthority);
177178

178179
return this.textFileService.resolveTextContent(savedFileResource).then(content => {
179180
let codeEditorModel = this.modelService.getModel(resource);
@@ -196,14 +197,6 @@ export class FileOnDiskContentProvider implements ITextModelContentProvider {
196197
});
197198
}
198199

199-
private toSavedFileResource(resource: URI): URI {
200-
if (this.environmentService.configuration.remoteAuthority) {
201-
return resource.with({ scheme: Schemas.vscodeRemote }); // assume file on disk is remote
202-
}
203-
204-
return resource.with({ scheme: Schemas.file });
205-
}
206-
207200
dispose(): void {
208201
dispose(this.fileWatcherDisposable);
209202
this.fileWatcherDisposable = undefined;

0 commit comments

Comments
 (0)