Path: blob/main/extensions/git-base/src/foldingProvider.ts
4772 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';67export class GitCommitFoldingProvider implements vscode.FoldingRangeProvider {89provideFoldingRanges(10document: vscode.TextDocument,11_context: vscode.FoldingContext,12_token: vscode.CancellationToken13): vscode.ProviderResult<vscode.FoldingRange[]> {14const ranges: vscode.FoldingRange[] = [];1516let commentBlockStart: number | undefined;17let currentDiffStart: number | undefined;1819for (let i = 0; i < document.lineCount; i++) {20const line = document.lineAt(i);21const lineText = line.text;2223// Check for comment lines (lines starting with #)24if (lineText.startsWith('#')) {25// Close any active diff block when we encounter a comment26if (currentDiffStart !== undefined) {27// Only create fold if there are at least 2 lines28if (i - currentDiffStart > 1) {29ranges.push(new vscode.FoldingRange(currentDiffStart, i - 1));30}31currentDiffStart = undefined;32}3334if (commentBlockStart === undefined) {35commentBlockStart = i;36}37} else {38// End of comment block39if (commentBlockStart !== undefined) {40// Only create fold if there are at least 2 lines41if (i - commentBlockStart > 1) {42ranges.push(new vscode.FoldingRange(43commentBlockStart,44i - 1,45vscode.FoldingRangeKind.Comment46));47}48commentBlockStart = undefined;49}50}5152// Check for diff sections (lines starting with "diff --git")53if (lineText.startsWith('diff --git ')) {54// If there's a previous diff block, close it55if (currentDiffStart !== undefined) {56// Only create fold if there are at least 2 lines57if (i - currentDiffStart > 1) {58ranges.push(new vscode.FoldingRange(currentDiffStart, i - 1));59}60}61// Start new diff block62currentDiffStart = i;63}64}6566// Handle end-of-document cases6768// If comment block extends to end of document69if (commentBlockStart !== undefined) {70if (document.lineCount - commentBlockStart > 1) {71ranges.push(new vscode.FoldingRange(72commentBlockStart,73document.lineCount - 1,74vscode.FoldingRangeKind.Comment75));76}77}7879// If diff block extends to end of document80if (currentDiffStart !== undefined) {81if (document.lineCount - currentDiffStart > 1) {82ranges.push(new vscode.FoldingRange(83currentDiffStart,84document.lineCount - 185));86}87}8889return ranges;90}91}929394