-
Notifications
You must be signed in to change notification settings - Fork 29.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add markdown header based folding (#44795)
Fixes #3347 Adds folding based on markdown heading level. Headers fold to the next header of <= level
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
|
||
import { MarkdownEngine } from '../markdownEngine'; | ||
import { TableOfContentsProvider } from '../tableOfContentsProvider'; | ||
|
||
export default class MarkdownFoldingProvider implements vscode.FoldingProvider { | ||
|
||
constructor( | ||
private readonly engine: MarkdownEngine | ||
) { } | ||
|
||
public async provideFoldingRanges( | ||
document: vscode.TextDocument, | ||
_token: vscode.CancellationToken | ||
): Promise<vscode.FoldingRangeList> { | ||
const tocProvider = new TableOfContentsProvider(this.engine, document); | ||
const toc = await tocProvider.getToc(); | ||
|
||
const foldingRanges = toc.map((entry, startIndex) => { | ||
const start = entry.line; | ||
let end: number | undefined = undefined; | ||
for (let i = startIndex + 1; i < toc.length; ++i) { | ||
if (toc[i].level <= entry.level) { | ||
end = toc[i].line - 1; | ||
break; | ||
} | ||
} | ||
return new vscode.FoldingRange( | ||
start, | ||
typeof end === 'number' ? end : document.lineCount - 1); | ||
}); | ||
|
||
return new vscode.FoldingRangeList(foldingRanges); | ||
} | ||
} |