提交 66c24011 编写于 作者: J Joao Moreno

git: commit template

上级 77f0e953
......@@ -453,7 +453,7 @@ export class CommandCenter {
const didCommit = await this.smartCommit(getCommitMessage, opts);
if (message && didCommit) {
scm.inputBox.value = '';
scm.inputBox.value = await this.model.getCommitTemplate();
}
}
......@@ -467,7 +467,7 @@ export class CommandCenter {
const didCommit = await this.smartCommit(async () => scm.inputBox.value);
if (didCommit) {
scm.inputBox.value = '';
scm.inputBox.value = await this.model.getCommitTemplate();
}
}
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { workspace, window, languages, Disposable, Uri, HoverProvider, Hover, TextEditor, Position, TextDocument, Range, TextEditorDecorationType, WorkspaceEdit } from 'vscode';
import { filterEvent } from './util';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
const scmInputUri = Uri.parse('scm:input');
function isSCMInput(uri: Uri) {
return uri.toString() === scmInputUri.toString();
}
interface Diagnostic {
range: Range;
message: string;
}
// TODO@Joao: hover disappears if editor is scrolled
export class CommitController implements HoverProvider {
private visibleTextEditorsDisposable: Disposable;
private editor: TextEditor;
private diagnostics: Diagnostic[] = [];
private decorationType: TextEditorDecorationType;
private disposables: Disposable[] = [];
get message(): string | undefined {
if (!this.editor) {
return;
}
return this.editor.document.getText();
}
set message(message: string | undefined) {
if (!this.editor || message === undefined) {
return;
}
const document = this.editor.document;
const start = document.lineAt(0).range.start;
const end = document.lineAt(document.lineCount - 1).range.end;
const range = new Range(start, end);
const edit = new WorkspaceEdit();
edit.replace(scmInputUri, range, message);
workspace.applyEdit(edit);
}
constructor() {
this.visibleTextEditorsDisposable = window.onDidChangeVisibleTextEditors(this.onVisibleTextEditors, this);
this.onVisibleTextEditors(window.visibleTextEditors);
this.decorationType = window.createTextEditorDecorationType({
isWholeLine: true,
color: 'rgb(228, 157, 43)',
dark: {
color: 'rgb(220, 211, 71)'
}
});
}
private onVisibleTextEditors(editors: TextEditor[]): void {
const [editor] = editors.filter(e => isSCMInput(e.document.uri));
if (!editor) {
return;
}
this.visibleTextEditorsDisposable.dispose();
this.editor = editor;
const onDidChange = filterEvent(workspace.onDidChangeTextDocument, e => e.document && isSCMInput(e.document.uri));
onDidChange(this.update, this, this.disposables);
workspace.onDidChangeConfiguration(this.update, this, this.disposables);
languages.registerHoverProvider({ scheme: 'scm' }, this);
}
private update(): void {
this.diagnostics = [];
const range = this.editor.document.lineAt(0).range;
const length = range.end.character - range.start.character;
if (workspace.getConfiguration('git').get<boolean>('enableLongCommitWarning') && length > 80) {
const message = localize('too long', "You should keep the first line under 50 characters.\n\nYou can use more lines for extra information.");
this.diagnostics.push({ range, message });
}
this.editor.setDecorations(this.decorationType, this.diagnostics.map(d => d.range));
}
provideHover(document: TextDocument, position: Position): Hover | undefined {
const [decoration] = this.diagnostics.filter(d => d.range.contains(position));
if (!decoration) {
return;
}
return new Hover(decoration.message, decoration.range);
}
dispose(): void {
this.disposables.forEach(d => d.dispose());
}
}
......@@ -5,7 +5,7 @@
'use strict';
import { ExtensionContext, workspace, window, Disposable, commands, Uri } from 'vscode';
import { ExtensionContext, workspace, window, Disposable, commands, Uri, scm } from 'vscode';
import { findGit, Git } from './git';
import { Model } from './model';
import { GitSCMProvider } from './scmProvider';
......@@ -14,7 +14,6 @@ import { CheckoutStatusBar, SyncStatusBar } from './statusbar';
import { GitContentProvider } from './contentProvider';
import { AutoFetcher } from './autofetch';
import { MergeDecorator } from './merge';
import { CommitController } from './commit';
import { Askpass } from './askpass';
import * as nls from 'vscode-nls';
......@@ -43,7 +42,6 @@ async function init(disposables: Disposable[]): Promise<void> {
outputChannel.appendLine(localize('using git', "Using git {0} from {1}", info.version, info.path));
git.onOutput(str => outputChannel.append(str), null, disposables);
const commitHandler = new CommitController();
const commandCenter = new CommandCenter(model, outputChannel);
const provider = new GitSCMProvider(model, commandCenter);
const contentProvider = new GitContentProvider(model);
......@@ -53,7 +51,6 @@ async function init(disposables: Disposable[]): Promise<void> {
const mergeDecorator = new MergeDecorator(model);
disposables.push(
commitHandler,
commandCenter,
provider,
contentProvider,
......@@ -72,6 +69,8 @@ async function init(disposables: Disposable[]): Promise<void> {
commands.executeCommand('vscode.open', Uri.parse('https://git-scm.com/'));
}
}
scm.inputBox.value = await model.getCommitTemplate();
}
export function activate(context: ExtensionContext): any {
......
......@@ -184,7 +184,8 @@ export enum Operation {
Sync = 1 << 11,
Init = 1 << 12,
Show = 1 << 13,
Stage = 1 << 13
Stage = 1 << 14,
GetCommitTemplate = 1 << 15
}
export interface Operations {
......@@ -455,6 +456,10 @@ export class Model implements Disposable {
});
}
async getCommitTemplate(): Promise<string> {
return await this.run(Operation.GetCommitTemplate, async () => this.repository.getCommitTemplate());
}
private async run<T>(operation: Operation, runOperation: () => Promise<T> = () => Promise.resolve<any>(null)): Promise<T> {
return window.withScmProgress(async () => {
this._operations = this._operations.start(operation);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册