Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commad): close #11346, use available number to label when create … #11347

Merged
merged 7 commits into from
Jun 28, 2022
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
3 changes: 2 additions & 1 deletion packages/navigator/src/browser/navigator-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// *****************************************************************************

import { injectable, inject, postConstruct } from '@theia/core/shared/inversify';
import { UNTITLED_SCHEME } from '@theia/core/lib/common';
import URI from '@theia/core/lib/common/uri';
import { FileNode, FileTreeModel } from '@theia/filesystem/lib/browser';
import { OpenerService, open, TreeNode, ExpandableTreeNode, CompositeTreeNode, SelectableTreeNode } from '@theia/core/lib/browser';
Expand Down Expand Up @@ -152,7 +153,7 @@ export class FileNavigatorModel extends FileTreeModel {
const workspace = this.workspaceService.workspace;
let name = workspace
? workspace.resource.path.name
: 'untitled';
: UNTITLED_SCHEME;
name += ' (Workspace)';
return WorkspaceNode.createRoot(name);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/common/uri-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { UNTITLED_SCHEME } from '@theia/core/lib/common';
import URI, { UriComponents } from '@theia/core/lib/common/uri';

export { UriComponents };
Expand Down Expand Up @@ -61,7 +62,7 @@ export namespace Schemes {

export const mailto = 'mailto';

export const untitled = 'untitled';
export const untitled = UNTITLED_SCHEME;

export const data = 'data';

Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/plugin/document-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// *****************************************************************************

import * as theia from '@theia/plugin';
import { UNTITLED_SCHEME } from '@theia/core/lib/common';
import { ModelChangedEvent, DocumentsMain } from '../common/plugin-api-rpc';
import { Range as ARange } from '../common/plugin-api-rpc-model';
import { EndOfLine, Position, Range, URI } from './types-impl';
Expand Down Expand Up @@ -80,7 +81,7 @@ export class DocumentDataExt {
this._document = {
get uri(): theia.Uri { return that.uri; },
get fileName(): string { return that.uri.fsPath; },
get isUntitled(): boolean { return that.uri.scheme === 'untitled'; },
get isUntitled(): boolean { return that.uri.scheme === UNTITLED_SCHEME; },
get languageId(): string { return that.languageId; },
get version(): number { return that.versionId; },
get isClosed(): boolean { return that.disposed; },
Expand Down