|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import { workspace, window, languages, Disposable, Uri, TextDocumentChangeEvent, HoverProvider, Hover, TextEditor, Position, TextDocument, Range, TextEditorDecorationType, WorkspaceEdit } from 'vscode'; |
| 9 | +import { Model } from './model'; |
| 10 | +import { filterEvent } from './util'; |
| 11 | +import * as nls from 'vscode-nls'; |
| 12 | + |
| 13 | +const localize = nls.loadMessageBundle(); |
| 14 | +const scmInputUri = Uri.parse('scm:input'); |
| 15 | + |
| 16 | +function isSCMInput(uri: Uri) { |
| 17 | + return uri.toString() === scmInputUri.toString(); |
| 18 | +} |
| 19 | + |
| 20 | +interface Diagnostic { |
| 21 | + range: Range; |
| 22 | + message: string; |
| 23 | +} |
| 24 | + |
| 25 | +// TODO@Joao: hover dissapears if editor is scrolled |
| 26 | +export class CommitController implements HoverProvider { |
| 27 | + |
| 28 | + private visibleTextEditorsDisposable: Disposable; |
| 29 | + private editor: TextEditor; |
| 30 | + private diagnostics: Diagnostic[] = []; |
| 31 | + private decorationType: TextEditorDecorationType; |
| 32 | + private disposables: Disposable[] = []; |
| 33 | + |
| 34 | + get message(): string | undefined { |
| 35 | + if (!this.editor) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + return this.editor.document.getText(); |
| 40 | + } |
| 41 | + |
| 42 | + set message(message: string | undefined) { |
| 43 | + if (!this.editor || message === undefined) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const document = this.editor.document; |
| 48 | + const start = document.lineAt(0).range.start; |
| 49 | + const end = document.lineAt(document.lineCount - 1).range.end; |
| 50 | + const range = new Range(start, end); |
| 51 | + const edit = new WorkspaceEdit(); |
| 52 | + edit.replace(scmInputUri, range, message); |
| 53 | + workspace.applyEdit(edit); |
| 54 | + } |
| 55 | + |
| 56 | + constructor(private model: Model) { |
| 57 | + this.visibleTextEditorsDisposable = window.onDidChangeVisibleTextEditors(this.onVisibleTextEditors, this); |
| 58 | + this.onVisibleTextEditors(window.visibleTextEditors); |
| 59 | + |
| 60 | + this.decorationType = window.createTextEditorDecorationType({ |
| 61 | + isWholeLine: true, |
| 62 | + color: 'rgb(228, 157, 43)', |
| 63 | + dark: { |
| 64 | + color: 'rgb(220, 211, 71)' |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + private onVisibleTextEditors(editors: TextEditor[]): void { |
| 70 | + const [editor] = editors.filter(e => isSCMInput(e.document.uri)); |
| 71 | + |
| 72 | + if (!editor) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + this.visibleTextEditorsDisposable.dispose(); |
| 77 | + this.editor = editor; |
| 78 | + |
| 79 | + const onDidChange = filterEvent(workspace.onDidChangeTextDocument, e => e.document && isSCMInput(e.document.uri)); |
| 80 | + onDidChange(this.onSCMInputChange, this, this.disposables); |
| 81 | + |
| 82 | + languages.registerHoverProvider({ scheme: 'scm' }, this); |
| 83 | + } |
| 84 | + |
| 85 | + private onSCMInputChange(e: TextDocumentChangeEvent): void { |
| 86 | + this.diagnostics = []; |
| 87 | + |
| 88 | + const range = e.document.lineAt(0).range; |
| 89 | + const length = range.end.character - range.start.character; |
| 90 | + |
| 91 | + if (length > 80) { |
| 92 | + const message = localize('too long', "You should keep the first line under 50 characters.\n\nYou can use more lines for extra information."); |
| 93 | + this.diagnostics.push({ range, message }); |
| 94 | + } |
| 95 | + |
| 96 | + this.editor.setDecorations(this.decorationType, this.diagnostics.map(d => d.range)); |
| 97 | + } |
| 98 | + |
| 99 | + provideHover(document: TextDocument, position: Position): Hover | undefined { |
| 100 | + const [decoration] = this.diagnostics.filter(d => d.range.contains(position)); |
| 101 | + |
| 102 | + if (!decoration) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + return new Hover(decoration.message, decoration.range); |
| 107 | + } |
| 108 | + |
| 109 | + dispose(): void { |
| 110 | + this.disposables.forEach(d => d.dispose()); |
| 111 | + } |
| 112 | +} |
0 commit comments