|
| 1 | +import {spawn} from "child_process" |
| 2 | +import * as vscode from "vscode" |
| 3 | +import {format} from "./utils.js" |
| 4 | + |
| 5 | +export function enableGitLineBlame(context: vscode.ExtensionContext) { |
| 6 | + context.subscriptions.push( |
| 7 | + vscode.window.onDidChangeTextEditorSelection((event) => { |
| 8 | + renderGitBlame(event.textEditor) |
| 9 | + }), |
| 10 | + ) |
| 11 | +} |
| 12 | + |
| 13 | +function renderGitBlame(editor: vscode.TextEditor) { |
| 14 | + const line = editor.document.lineAt(editor.selection.active.line) |
| 15 | + if (line.lineNumber >= editor.document.lineCount - 1) { |
| 16 | + decoration.clear(editor) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + const path = editor.document.uri |
| 21 | + const args = ["blame", "-p", path.path, `-L${line.lineNumber + 1},+1`] |
| 22 | + const stream = spawn("git", args, { |
| 23 | + cwd: vscode.workspace.getWorkspaceFolder(path).uri.fsPath, |
| 24 | + }) |
| 25 | + |
| 26 | + stream.stdout.on("data", (data) => renderGitBlameData(editor, line, data)) |
| 27 | + stream.stderr.on("error", () => { |
| 28 | + vscode.window.showWarningMessage( |
| 29 | + `Cannot render Git line blame at: ${path}:${line.lineNumber}`, |
| 30 | + ) |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +function renderGitBlameData( |
| 35 | + editor: vscode.TextEditor, |
| 36 | + line: vscode.TextLine, |
| 37 | + data: any, |
| 38 | +) { |
| 39 | + const blame = new GitLineBlame(data.toString()) |
| 40 | + const message = blame.formatLine() |
| 41 | + const range = new vscode.Range( |
| 42 | + line.lineNumber, |
| 43 | + line.text.length, |
| 44 | + line.lineNumber, |
| 45 | + line.text.length + message.length, |
| 46 | + ) |
| 47 | + decoration.updateOptions(editor, { |
| 48 | + range, |
| 49 | + hoverMessage: blame.formatHover(), |
| 50 | + renderOptions: {after: {contentText: message}}, |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +namespace decoration { |
| 55 | + let currentType = vscode.window.createTextEditorDecorationType({ |
| 56 | + after: { |
| 57 | + textDecoration: "none; opacity: 25%", |
| 58 | + margin: "0 0 0 1rem", |
| 59 | + }, |
| 60 | + }) |
| 61 | + |
| 62 | + let currentOptions: vscode.DecorationOptions[] = [] |
| 63 | + |
| 64 | + export function clear(editor: vscode.TextEditor) { |
| 65 | + currentOptions = [] |
| 66 | + editor.setDecorations(currentType, currentOptions) |
| 67 | + } |
| 68 | + |
| 69 | + export function updateType( |
| 70 | + editor: vscode.TextEditor, |
| 71 | + type: vscode.TextEditorDecorationType, |
| 72 | + ) { |
| 73 | + clear(editor) |
| 74 | + currentType = type |
| 75 | + editor.setDecorations(type, currentOptions) |
| 76 | + } |
| 77 | + |
| 78 | + export function updateOptions( |
| 79 | + editor: vscode.TextEditor, |
| 80 | + option: vscode.DecorationOptions, |
| 81 | + ) { |
| 82 | + clear(editor) |
| 83 | + currentOptions = [option] |
| 84 | + editor.setDecorations(currentType, currentOptions) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class GitLineBlame { |
| 89 | + readonly committed: boolean |
| 90 | + readonly author: string |
| 91 | + readonly authorMail: string |
| 92 | + readonly authorTime: number |
| 93 | + readonly authorTimeZone: string |
| 94 | + readonly summary: string |
| 95 | + |
| 96 | + constructor(raw: string) { |
| 97 | + const map = this.map(raw) |
| 98 | + this.author = map["author"] |
| 99 | + this.authorMail = map["author-mail"] |
| 100 | + this.authorTime = parseInt(map["author-time"]) |
| 101 | + this.authorTimeZone = map["author-tz"] |
| 102 | + this.summary = map["summary"] |
| 103 | + |
| 104 | + this.committed = this.author !== "Not Committed Yet" |
| 105 | + } |
| 106 | + |
| 107 | + private map(raw: string) { |
| 108 | + const lines = raw.split("\n") |
| 109 | + lines[0] = `commit-id ${lines[0]}` |
| 110 | + lines[lines.length - 1] = `content ${lines[lines.length - 1]}` |
| 111 | + |
| 112 | + const handler = {} |
| 113 | + for (const line of lines) { |
| 114 | + const parts = line.split(" ") |
| 115 | + handler[parts[0]] = parts.slice(1).join(" ") |
| 116 | + } |
| 117 | + return handler |
| 118 | + } |
| 119 | + |
| 120 | + formatLine(): string { |
| 121 | + if (!this.committed) return "Not Committed Yet..." |
| 122 | + const duration = format.duration(Date.now() - this.authorTime * 1000) |
| 123 | + return `${this.author}, ${duration} • ${this.summary}` |
| 124 | + } |
| 125 | + |
| 126 | + formatHover(): string { |
| 127 | + if (!this.committed) return "Not Committed Yet..." |
| 128 | + return ( |
| 129 | + `${this.author} <${this.authorMail}>, ` + |
| 130 | + `${format.timestamp(this.authorTime * 1000)} ${this.authorTimeZone}` |
| 131 | + ) |
| 132 | + } |
| 133 | +} |
0 commit comments