Skip to content

Commit

Permalink
feat(editor): ✨ add option to allow keeping editor after saving files
Browse files Browse the repository at this point in the history
If the option set to true and needs to auto-commit,
the workflow will also be doing after the windows close.

Closes: #128
  • Loading branch information
yi-Xu-0100 committed Apr 4, 2021
1 parent 56f709e commit 7d773c4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 42 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ You can access VSCode Conventional Commits in two ways:

### Extension Configuration

| name | description | default |
| :--------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-----: |
| `conventionalCommits.autoCommit` | Controls whether the extension should automatically commit files.<br>When `#git.enableSmartCommit#` enabled and `#git.smartCommitChanges#` was set to `all`, It allows to commit all changes when there are no staged changes.<br>And set `#git.postCommitCommand#` to `sync` to run `git.sync` after commit. | true |
| `conventionalCommits.emojiFormat` | Specifies which format will be shown in the `gitmoji`. | code |
| `conventionalCommits.gitmoji` | Controls whether the extension should prompt for a `gitmoji`. | true |
| `conventionalCommits.lineBreak` | Specifies which word will be treat as line breaks in the `body`.<br>Blank means no line breaks. | "" |
| `conventionalCommits.promptBody` | Controls whether the extension should prompt for the `body` section. | true |
| `conventionalCommits.promptFooter` | Controls whether the extension should prompt for the `footer` section. | true |
| `conventionalCommits.promptScopes` | Controls whether the extension should prompt for the `scope` section. | true |
| `conventionalCommits.scopes` | Specifies available selections in the `scope` section. | [] |
| `conventionalCommits.showEditor` | Show the commit message as a text document in a separate tab. | false |
| name | description | default |
| :---------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :-----: |
| `conventionalCommits.autoCommit` | Controls whether the extension should automatically commit files.<br>When `#git.enableSmartCommit#` enabled and `#git.smartCommitChanges#` was set to `all`, It allows to commit all changes when there are no staged changes.<br>And set `#git.postCommitCommand#` to `sync` to run `git.sync` after commit. | true |
| `conventionalCommits.emojiFormat` | Specifies which format will be shown in the `gitmoji`. | code |
| `conventionalCommits.gitmoji` | Controls whether the extension should prompt for a `gitmoji`. | true |
| `conventionalCommits.lineBreak` | Specifies which word will be treat as line breaks in the `body`.<br>Blank means no line breaks. | "" |
| `conventionalCommits.promptBody` | Controls whether the extension should prompt for the `body` section. | true |
| `conventionalCommits.promptFooter` | Controls whether the extension should prompt for the `footer` section. | true |
| `conventionalCommits.promptScopes` | Controls whether the extension should prompt for the `scope` section. | true |
| `conventionalCommits.scopes` | Specifies available selections in the `scope` section. | [] |
| `conventionalCommits.showEditor` | Controls whether the extension should show the commit message as a text document in a separate tab. | false |
| `conventionalCommits.keepAfterSave` | Controls whether the extension should allow keeping edit status after saving the commit message. | false |

## Commit Workflow

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
"type": "boolean",
"default": false,
"markdownDescription": "%extension.configuration.properties.conventionalCommits.showEditor.markdownDescription%"
},
"conventionalCommits.editor.keepAfterSave": {
"type": "boolean",
"default": false,
"markdownDescription": "%extension.configuration.properties.conventionalCommits.editor.keepAfterSave.markdownDescription%"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"extension.configuration.properties.conventionalCommits.promptFooter.markdownDescription": "Controls whether the extension should prompt for the `footer` section.",
"extension.configuration.properties.conventionalCommits.promptScopes.markdownDescription": "Controls whether the extension should prompt for the `scope` section.",
"extension.configuration.properties.conventionalCommits.scopes.markdownDescription": "Specifies available selections in the `scope` section.",
"extension.configuration.properties.conventionalCommits.showEditor.markdownDescription": "Show the commit message as a text document in a separate tab.",
"extension.configuration.properties.conventionalCommits.showEditor.markdownDescription": "Controls whether the extension should show the commit message as a text document in a separate tab.",
"extension.configuration.properties.conventionalCommits.editor.keepAfterSave.markdownDescription": "Controls whether the extension should allow keeping edit status after saving the commit message.",
"extension.sources.repositoryNotFoundInPath": "Repository not found in path: ",
"extension.sources.repositoriesEmpty": "Please open a repository.",
"extension.sources.promptRepositoryPlaceholder": "Choose a repository",
Expand Down
3 changes: 2 additions & 1 deletion package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"extension.configuration.properties.conventionalCommits.promptFooter.markdownDescription": "是否需要填写 `footer`。",
"extension.configuration.properties.conventionalCommits.promptScopes.markdownDescription": "是否需要填写 `scope`。",
"extension.configuration.properties.conventionalCommits.scopes.markdownDescription": "指定 `scope` 中的可选项。",
"extension.configuration.properties.conventionalCommits.showEditor.markdownDescription": "在新标签页用文本编辑器展示提交信息",
"extension.configuration.properties.conventionalCommits.showEditor.markdownDescription": "是否需要在新标签页用文本编辑器展示提交信息",
"extension.configuration.properties.conventionalCommits.editor.keepAfterSave.markdownDescription": "是否需要在保存提交消息后保持编辑状态。",
"extension.sources.repositoryNotFoundInPath": "以下路径中未找到仓库:",
"extension.sources.repositoriesEmpty": "请打开一个仓库",
"extension.sources.promptRepositoryPlaceholder": "请选择一个仓库",
Expand Down
1 change: 1 addition & 0 deletions src/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Configuration = {
promptScopes: boolean;
promptBody: boolean;
promptFooter: boolean;
'editor.keepAfterSave': boolean;
};

export function getConfiguration() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/conventional-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export default function createConventionalCommits() {
const showEditor = configuration.get<boolean>('showEditor');
if (showEditor) {
repository.inputBox.value = message;
openMessageInTab(repository);
output.info('Show full commit message in a separate tab.');
await openMessageInTab(repository);
output.info('Show full commit message in a separate tab successfully.');
} else {
vscode.commands.executeCommand('workbench.view.scm');
repository.inputBox.value = message;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import * as VSCodeGit from '../../vendors/git';
import { state } from './provider';

/**
* Opens a new empty file and adds the commit message. The language mode is set to "Git Commit Message" and
* Opens a new empty file and adds the commit message.
* The language mode is set to "Git Commit Message" and
* the FileSystemProvider is set to CommitProvider.
* @param repository The project repository.
*/
Expand Down
74 changes: 48 additions & 26 deletions src/lib/editor/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,35 @@ export const state: State = {
};

/**
* Simplified provider for handling Git commit messages.
* On save, the contents are instead autocommited or used to update repository object (depending on settings).
* Simplified file provider for handling Git commit messages.
*/
const CommitProvider = new (class implements vscode.FileSystemProvider {
private _emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> = this
._emitter.event;

watch(_resource: vscode.Uri): vscode.Disposable {
return new vscode.Disposable(() => {});
return new vscode.Disposable(async () => {
const autoCommit = configuration.get<boolean>('autoCommit');
const keepAfterSave = configuration.get<boolean>('editor.keepAfterSave');
output.info('The commit message tab has been disposed.');
if (state.repository) {
output.info(`inputBox.value:\n${state.repository.inputBox.value}`);
if (keepAfterSave) {
await vscode.commands.executeCommand('workbench.view.scm');
if (autoCommit) {
await vscode.commands.executeCommand(
'git.commit',
state.repository,
);
output.info('Auto commit finished successfully.');
}
}
} else {
output.warning('provider: state.repository not found!');
output.info(`state: ${state}`);
}
});
}

stat(): vscode.FileStat {
Expand All @@ -46,34 +65,37 @@ const CommitProvider = new (class implements vscode.FileSystemProvider {
createDirectory(): void {}

writeFile(_uri: vscode.Uri, content: Uint8Array): Thenable<void> {
return new Promise(async (resolve) => {
try {
const value = new TextDecoder().decode(content);

if (state.repository) {
return new Promise(
async (resolve): Promise<void> => {
try {
const autoCommit = configuration.get<boolean>('autoCommit');
state.repository.inputBox.value = value;
vscode.commands.executeCommand(
'workbench.action.revertAndCloseActiveEditor',
const keepAfterSave = configuration.get<boolean>(
'editor.keepAfterSave',
);

if (autoCommit) {
await vscode.commands.executeCommand(
'git.commit',
state.repository,
);
resolve();
} else {
await vscode.commands.executeCommand('workbench.view.scm');
const value = new TextDecoder().decode(content);
if (state.repository) {
state.repository.inputBox.value = value;
output.info('Sync commit message successfully.');
if (!keepAfterSave) {
vscode.commands.executeCommand(
'workbench.action.revertAndCloseActiveEditor',
);
if (autoCommit) {
await vscode.commands.executeCommand('workbench.view.scm');
await vscode.commands.executeCommand(
'git.commit',
state.repository,
);
output.info('Auto commit finished successfully.');
}
}
resolve();
}

output.info('Write file successfully.');
} catch (e) {
output.error('writeFile', e);
}
} catch (e) {
output.error('writeFile', e);
}
});
},
);
}

rename(): void {}
Expand Down

0 comments on commit 7d773c4

Please sign in to comment.