Skip to content

add markdown list folding, #57104 #57899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export default class MarkdownFoldingProvider implements vscode.FoldingRangeProvi
_: vscode.FoldingContext,
_token: vscode.CancellationToken
): Promise<vscode.FoldingRange[]> {
const [regions, sections] = await Promise.all([this.getRegions(document), this.getHeaderFoldingRanges(document)]);
return [...regions, ...sections].slice(0, rangeLimit);
const [regions, sections, lists] = await Promise.all([this.getRegions(document), this.getHeaderFoldingRanges(document), this.getListFoldingRanges(document)]);
return [...regions, ...sections, ...lists].slice(0, rangeLimit);
}

private async getHeaderFoldingRanges(document: vscode.TextDocument) {
Expand All @@ -64,4 +64,18 @@ export default class MarkdownFoldingProvider implements vscode.FoldingRangeProvi
return new vscode.FoldingRange(entry.line, endLine);
});
}

private async getListFoldingRanges(document: vscode.TextDocument): Promise<vscode.FoldingRange[]> {

const isMultiLineListOpen = (token: Token) =>
token.type === 'list_item_open' && token.map[1] > token.map[0];

const tokens = await this.engine.parse(document.uri, document.getText());
const multiLineListItems = tokens.filter(isMultiLineListOpen);
return multiLineListItems.map(listItem => {
const start = listItem.map[0];
const end = listItem.map[1] - 1;
return new vscode.FoldingRange(start, end);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ a`);
assert.strictEqual(second.end, 9);
});

test('Should fold from list to end of document', async () => {
const folds = await getFoldsForDocument(`a
- b
c
d`);
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
});

test('Should fold if list has multiple lines of content', async () => {
const folds = await getFoldsForDocument(`a
- This list item\n spans multiple\n lines.`);
assert.strictEqual(folds.length, 1);
const firstFold = folds[0];
assert.strictEqual(firstFold.start, 1);
assert.strictEqual(firstFold.end, 3);
});
});


Expand Down