markdown toc should include full span to next header

The folding range should exclude blank lines before the next header level
This commit is contained in:
Matt Bierner
2018-08-24 12:01:06 +02:00
parent 30d0a61a8f
commit 49edd3d038
2 changed files with 7 additions and 5 deletions

View File

@@ -56,6 +56,12 @@ export default class MarkdownFoldingProvider implements vscode.FoldingRangeProvi
private async getHeaderFoldingRanges(document: vscode.TextDocument) {
const tocProvider = new TableOfContentsProvider(this.engine, document);
const toc = await tocProvider.getToc();
return toc.map((entry) => new vscode.FoldingRange(entry.line, entry.location.range.end.line));
return toc.map(entry => {
let endLine = entry.location.range.end.line;
if (document.lineAt(endLine).isEmptyOrWhitespace && endLine >= entry.line + 1) {
endLine = endLine - 1;
}
return new vscode.FoldingRange(entry.line, endLine);
});
}
}

View File

@@ -65,14 +65,10 @@ export class TableOfContentsProvider {
// Get full range of section
return toc.map((entry, startIndex): TocEntry => {
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;
if (document.lineAt(end).isEmptyOrWhitespace && end >= start + 1) {
end = end - 1;
}
break;
}
}