Skip to content

Commit

Permalink
Improve URI creation for untitled resources (#11347)
Browse files Browse the repository at this point in the history
Signed-off-by: yiliang114 <1204183885@qq.com>
  • Loading branch information
yiliang114 authored Jun 28, 2022
1 parent 8d46c9b commit 61f4103
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/browser/common-frontend-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import { DecorationStyle } from './decoration-style';
import { isPinned, Title, togglePinned, Widget } from './widgets';
import { SaveResourceService } from './save-resource-service';
import { UserWorkingDirectoryProvider } from './user-working-directory-provider';
import { createUntitledURI } from '../common';
import { UntitledResourceResolver } from '../common';
import { LanguageQuickPickService } from './i18n/language-quick-pick-service';

export namespace CommonMenus {
Expand Down Expand Up @@ -404,6 +404,9 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
@inject(LanguageQuickPickService)
protected readonly languageQuickPickService: LanguageQuickPickService;

@inject(UntitledResourceResolver)
protected readonly untitledResourceResolver: UntitledResourceResolver;

protected pinnedKey: ContextKey<boolean>;

async configure(app: FrontendApplication): Promise<void> {
Expand Down Expand Up @@ -961,7 +964,11 @@ export class CommonFrontendContribution implements FrontendApplicationContributi
execute: () => this.configureDisplayLanguage()
});
commandRegistry.registerCommand(CommonCommands.NEW_FILE, {
execute: async () => open(this.openerService, createUntitledURI('', await this.workingDirProvider.getUserWorkingDir()))
execute: async () => {
const untitledUri = this.untitledResourceResolver.createUntitledURI('', await this.workingDirProvider.getUserWorkingDir());
this.untitledResourceResolver.resolve(untitledUri);
return open(this.openerService, untitledUri);
}
});
}

Expand Down
31 changes: 29 additions & 2 deletions packages/core/src/common/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ export class UntitledResourceResolver implements ResourceResolver {

protected readonly resources = new Map<string, UntitledResource>();

has(uri: URI): boolean {
if (uri.scheme !== UNTITLED_SCHEME) {
throw new Error('The given uri is not untitled file uri: ' + uri);
} else {
return this.resources.has(uri.toString());
}
}

async resolve(uri: URI): Promise<UntitledResource> {
if (uri.scheme !== UNTITLED_SCHEME) {
throw new Error('The given uri is not untitled file uri: ' + uri);
Expand All @@ -341,8 +349,24 @@ export class UntitledResourceResolver implements ResourceResolver {
}

async createUntitledResource(content?: string, extension?: string, uri?: URI): Promise<UntitledResource> {
return new UntitledResource(this.resources, uri ? uri : new URI().withScheme(UNTITLED_SCHEME).withPath(`/Untitled-${untitledResourceSequenceIndex++}${extension ?? ''}`),
content);
if (!uri) {
uri = this.createUntitledURI(extension);
}
return new UntitledResource(this.resources, uri, content);
}

createUntitledURI(extension?: string, parent?: URI): URI {
let counter = 1; // vscode starts at 1
let untitledUri;
do {
const name = `Untitled-${counter}${extension ?? ''}`;
if (parent) {
untitledUri = parent.resolve(name).withScheme(UNTITLED_SCHEME);
}
untitledUri = new URI().resolve(name).withScheme(UNTITLED_SCHEME);
counter++;
} while (this.has(untitledUri));
return untitledUri;
}
}

Expand Down Expand Up @@ -389,6 +413,9 @@ export class UntitledResource implements Resource {
}
}

/**
* @deprecated Since 1.27.0. Please use `UntitledResourceResolver.createUntitledURI` instead.
*/
export function createUntitledURI(extension?: string, parent?: URI): URI {
const name = `Untitled-${untitledResourceSequenceIndex++}${extension ?? ''}`;
if (parent) {
Expand Down

0 comments on commit 61f4103

Please sign in to comment.