forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Create new Python file command #18522
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
577b3d0
Create new Python file command
luabud 1408619
Rename command
luabud 87d9174
Format documents
luabud da11fef
Add a news entry
luabud eff1172
Add telemetry and add experimental setting
luabud 4984416
Add tests
luabud 54f7621
Format document
luabud 0d47caa
Disable eslint class-methods-use-this
luabud a441e77
Attempt to fix test
luabud 02305d8
Add workspace service
luabud eb5cb17
Try to fix test again
luabud 102ac24
Fix tests and add appShell
luabud 7fc6fdb
Add title for command
luabud abb6bcf
Add titles to package.nls.json
luabud 31ddd39
Remove accidental launch.json edit
luabud 49b1599
Apply suggestions from code review
luabud 461fdcd
Add shorttitle to new file command
luabud 8c2dab3
Merge branch 'newfilecontrib' of https://github.com/luabud/vscode-pyt…
luabud 7248625
Apply suggestions from code review
luabud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Implement a "New Python File" command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/client/common/application/commands/createFileCommand.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { injectable, inject } from 'inversify'; | ||
import { IExtensionSingleActivationService } from '../../../activation/types'; | ||
import { Commands } from '../../constants'; | ||
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../types'; | ||
import { sendTelemetryEvent } from '../../../telemetry'; | ||
import { EventName } from '../../../telemetry/constants'; | ||
|
||
@injectable() | ||
export class CreatePythonFileCommandHandler implements IExtensionSingleActivationService { | ||
public readonly supportedWorkspaceTypes = { untrustedWorkspace: true, virtualWorkspace: true }; | ||
|
||
constructor( | ||
@inject(ICommandManager) private readonly commandManager: ICommandManager, | ||
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, | ||
@inject(IApplicationShell) private readonly appShell: IApplicationShell, | ||
) {} | ||
|
||
public async activate(): Promise<void> { | ||
if (!this.workspaceService.getConfiguration('python').get<boolean>('createNewFileEnabled')) { | ||
return; | ||
} | ||
this.commandManager.registerCommand(Commands.CreateNewFile, this.createPythonFile, this); | ||
} | ||
|
||
public async createPythonFile(): Promise<void> { | ||
const newFile = await this.workspaceService.openTextDocument({ language: 'python' }); | ||
this.appShell.showTextDocument(newFile); | ||
sendTelemetryEvent(EventName.CREATE_NEW_FILE_COMMAND); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/common/application/commands/createNewFileCommand.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { anything, deepEqual, instance, mock, verify, when } from 'ts-mockito'; | ||
luabud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { TextDocument } from 'vscode'; | ||
import { Commands } from '../../../../client/common/constants'; | ||
import { CommandManager } from '../../../../client/common/application/commandManager'; | ||
import { CreatePythonFileCommandHandler } from '../../../../client/common/application/commands/createFileCommand'; | ||
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../../../../client/common/application/types'; | ||
import { MockWorkspaceConfiguration } from '../../../mocks/mockWorkspaceConfig'; | ||
import { WorkspaceService } from '../../../../client/common/application/workspace'; | ||
import { ApplicationShell } from '../../../../client/common/application/applicationShell'; | ||
|
||
suite('Create New Python File Commmand', () => { | ||
let createNewFileCommandHandler: CreatePythonFileCommandHandler; | ||
let cmdManager: ICommandManager; | ||
let workspaceService: IWorkspaceService; | ||
let appShell: IApplicationShell; | ||
|
||
setup(async () => { | ||
cmdManager = mock(CommandManager); | ||
workspaceService = mock(WorkspaceService); | ||
appShell = mock(ApplicationShell); | ||
|
||
createNewFileCommandHandler = new CreatePythonFileCommandHandler( | ||
instance(cmdManager), | ||
instance(workspaceService), | ||
instance(appShell), | ||
); | ||
when(workspaceService.getConfiguration('python')).thenReturn( | ||
new MockWorkspaceConfiguration({ | ||
createNewFileEnabled: true, | ||
}), | ||
); | ||
when(workspaceService.openTextDocument(deepEqual({ language: 'python' }))).thenReturn( | ||
Promise.resolve(({} as unknown) as TextDocument), | ||
); | ||
await createNewFileCommandHandler.activate(); | ||
}); | ||
|
||
test('Create Python file command is registered', async () => { | ||
verify(cmdManager.registerCommand(Commands.CreateNewFile, anything(), anything())).once(); | ||
}); | ||
test('Create a Python file if command is executed', async () => { | ||
await createNewFileCommandHandler.createPythonFile(); | ||
verify(workspaceService.openTextDocument(deepEqual({ language: 'python' }))).once(); | ||
verify(appShell.showTextDocument(anything())).once(); | ||
}); | ||
}); | ||
luabud marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.