|
1 | 1 | // The module 'vscode' contains the VS Code extensibility API
|
2 | 2 | // Import the module and reference it with the alias vscode in your code below
|
3 |
| -import * as vscode from 'vscode'; |
| 3 | +import * as vscode from "vscode"; |
4 | 4 |
|
5 | 5 | const maxSubjectLineLength = 72;
|
6 |
| -const maxSubjectLineLengthUrl = vscode.Uri.parse("https://www.gitkraken.com/learn/git/best-practices/git-commit-message"); |
| 6 | +const maxSubjectLineLengthUrl = vscode.Uri.parse( |
| 7 | + "https://www.gitkraken.com/learn/git/best-practices/git-commit-message" |
| 8 | +); |
7 | 9 |
|
8 | 10 | /** Subset of vscode.TextLine, for simplifying test writing. */
|
9 | 11 | export interface TextLineLite {
|
10 |
| - text: string; |
| 12 | + text: string; |
11 | 13 | }
|
12 | 14 |
|
13 | 15 | /** Subset of vscode.TextDocument, for simplifying test writing. */
|
14 | 16 | export interface TextDocumentLite {
|
15 |
| - lineCount: number; |
16 |
| - lineAt(line: number): TextLineLite; |
| 17 | + lineCount: number; |
| 18 | + lineAt(line: number): TextLineLite; |
17 | 19 | }
|
18 | 20 |
|
19 | 21 | let diagnosticCollection: vscode.DiagnosticCollection;
|
20 | 22 |
|
21 | 23 | // This method is called when your extension is activated
|
22 | 24 | // Your extension is activated the very first time the command is executed
|
23 | 25 | export function activate(context: vscode.ExtensionContext) {
|
24 |
| - console.log('Git Commit Message Plus says hello!'); |
25 |
| - |
26 |
| - diagnosticCollection = vscode.languages.createDiagnosticCollection('git-commit-message'); |
27 |
| - context.subscriptions.push(diagnosticCollection); |
28 |
| - |
29 |
| - const documentChangeListener = vscode.workspace.onDidChangeTextDocument(event => { |
30 |
| - const doc = event.document; |
31 |
| - if (doc.languageId !== 'git-commit') { |
32 |
| - return; |
33 |
| - } |
34 |
| - |
35 |
| - diagnosticCollection.set(doc.uri, getDiagnostics(doc)); |
36 |
| - }, null, context.subscriptions); |
37 |
| - context.subscriptions.push(documentChangeListener); |
38 |
| - |
39 |
| - // FIXME: Make sure linting is triggered when we open a commit message, not |
40 |
| - // just when it changes. I tried to react to onDidOpenTextDocument(), but |
41 |
| - // documents seem to default to plain-text when loaded so that didn't work. |
| 26 | + console.log("Git Commit Message Plus says hello!"); |
| 27 | + |
| 28 | + diagnosticCollection = |
| 29 | + vscode.languages.createDiagnosticCollection("git-commit-message"); |
| 30 | + context.subscriptions.push(diagnosticCollection); |
| 31 | + |
| 32 | + const documentChangeListener = vscode.workspace.onDidChangeTextDocument( |
| 33 | + (event) => { |
| 34 | + const doc = event.document; |
| 35 | + if (doc.languageId !== "git-commit") { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + diagnosticCollection.set(doc.uri, getDiagnostics(doc)); |
| 40 | + }, |
| 41 | + null, |
| 42 | + context.subscriptions |
| 43 | + ); |
| 44 | + context.subscriptions.push(documentChangeListener); |
| 45 | + |
| 46 | + // FIXME: Make sure linting is triggered when we open a commit message, not |
| 47 | + // just when it changes. I tried to react to onDidOpenTextDocument(), but |
| 48 | + // documents seem to default to plain-text when loaded so that didn't work. |
42 | 49 | }
|
43 | 50 |
|
44 | 51 | // This method is called when your extension is deactivated
|
45 | 52 | export function deactivate() {
|
46 |
| - console.log('Git Commit Message Plus says good bye!'); |
| 53 | + console.log("Git Commit Message Plus says good bye!"); |
47 | 54 | }
|
48 | 55 |
|
49 | 56 | function getDiagnostics(doc: TextDocumentLite): vscode.Diagnostic[] {
|
50 |
| - if (doc.lineCount < 1) { |
51 |
| - return []; |
52 |
| - } |
53 |
| - |
54 |
| - let returnMe: vscode.Diagnostic[] = []; |
55 |
| - |
56 |
| - const firstLine = doc.lineAt(0).text; |
57 |
| - if (firstLine.length > maxSubjectLineLength) { |
58 |
| - const range = new vscode.Range(new vscode.Position(0, 72), new vscode.Position(0, firstLine.length)); |
59 |
| - |
60 |
| - let diagnostic = new vscode.Diagnostic( |
61 |
| - range, |
62 |
| - `Keep the subject line to at most ${maxSubjectLineLength} characters`, |
63 |
| - vscode.DiagnosticSeverity.Warning |
64 |
| - ); |
65 |
| - diagnostic.code = { |
66 |
| - "target": maxSubjectLineLengthUrl, |
67 |
| - "value": "Git Commit Message Structure" |
68 |
| - }; |
69 |
| - |
70 |
| - returnMe.push(diagnostic); |
71 |
| - } |
72 |
| - |
73 |
| - return returnMe; |
| 57 | + if (doc.lineCount < 1) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + const returnMe: vscode.Diagnostic[] = []; |
| 62 | + |
| 63 | + const firstLine = doc.lineAt(0).text; |
| 64 | + if (firstLine.length > maxSubjectLineLength) { |
| 65 | + const range = new vscode.Range( |
| 66 | + new vscode.Position(0, 72), |
| 67 | + new vscode.Position(0, firstLine.length) |
| 68 | + ); |
| 69 | + |
| 70 | + const diagnostic = new vscode.Diagnostic( |
| 71 | + range, |
| 72 | + `Keep the subject line to at most ${maxSubjectLineLength} characters`, |
| 73 | + vscode.DiagnosticSeverity.Warning |
| 74 | + ); |
| 75 | + diagnostic.code = { |
| 76 | + target: maxSubjectLineLengthUrl, |
| 77 | + value: "Git Commit Message Structure", |
| 78 | + }; |
| 79 | + |
| 80 | + returnMe.push(diagnostic); |
| 81 | + } |
| 82 | + |
| 83 | + return returnMe; |
74 | 84 | }
|
75 | 85 |
|
76 | 86 | // Exports for testing
|
77 | 87 | //
|
78 | 88 | // Ref: https://stackoverflow.com/a/65422568/473672
|
79 | 89 | export const _private = {
|
80 |
| - getDiagnostics, |
81 |
| - maxSubjectLineLengthUrl |
| 90 | + getDiagnostics, |
| 91 | + maxSubjectLineLengthUrl, |
82 | 92 | };
|
0 commit comments