Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git-base/src/foldingProvider.ts
4772 views
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
import * as vscode from 'vscode';
7
8
export class GitCommitFoldingProvider implements vscode.FoldingRangeProvider {
9
10
provideFoldingRanges(
11
document: vscode.TextDocument,
12
_context: vscode.FoldingContext,
13
_token: vscode.CancellationToken
14
): vscode.ProviderResult<vscode.FoldingRange[]> {
15
const ranges: vscode.FoldingRange[] = [];
16
17
let commentBlockStart: number | undefined;
18
let currentDiffStart: number | undefined;
19
20
for (let i = 0; i < document.lineCount; i++) {
21
const line = document.lineAt(i);
22
const lineText = line.text;
23
24
// Check for comment lines (lines starting with #)
25
if (lineText.startsWith('#')) {
26
// Close any active diff block when we encounter a comment
27
if (currentDiffStart !== undefined) {
28
// Only create fold if there are at least 2 lines
29
if (i - currentDiffStart > 1) {
30
ranges.push(new vscode.FoldingRange(currentDiffStart, i - 1));
31
}
32
currentDiffStart = undefined;
33
}
34
35
if (commentBlockStart === undefined) {
36
commentBlockStart = i;
37
}
38
} else {
39
// End of comment block
40
if (commentBlockStart !== undefined) {
41
// Only create fold if there are at least 2 lines
42
if (i - commentBlockStart > 1) {
43
ranges.push(new vscode.FoldingRange(
44
commentBlockStart,
45
i - 1,
46
vscode.FoldingRangeKind.Comment
47
));
48
}
49
commentBlockStart = undefined;
50
}
51
}
52
53
// Check for diff sections (lines starting with "diff --git")
54
if (lineText.startsWith('diff --git ')) {
55
// If there's a previous diff block, close it
56
if (currentDiffStart !== undefined) {
57
// Only create fold if there are at least 2 lines
58
if (i - currentDiffStart > 1) {
59
ranges.push(new vscode.FoldingRange(currentDiffStart, i - 1));
60
}
61
}
62
// Start new diff block
63
currentDiffStart = i;
64
}
65
}
66
67
// Handle end-of-document cases
68
69
// If comment block extends to end of document
70
if (commentBlockStart !== undefined) {
71
if (document.lineCount - commentBlockStart > 1) {
72
ranges.push(new vscode.FoldingRange(
73
commentBlockStart,
74
document.lineCount - 1,
75
vscode.FoldingRangeKind.Comment
76
));
77
}
78
}
79
80
// If diff block extends to end of document
81
if (currentDiffStart !== undefined) {
82
if (document.lineCount - currentDiffStart > 1) {
83
ranges.push(new vscode.FoldingRange(
84
currentDiffStart,
85
document.lineCount - 1
86
));
87
}
88
}
89
90
return ranges;
91
}
92
}
93
94