-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code folding based on heading level
- Loading branch information
1 parent
1c96606
commit a4e16a4
Showing
8 changed files
with
136 additions
and
9 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
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
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,37 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode' | ||
|
||
import { AsciidocEngine } from '../asciidocEngine' | ||
import { TableOfContentsProvider } from '../tableOfContentsProvider' | ||
|
||
export default class AsciidocFoldingRangeProvider implements vscode.FoldingRangeProvider { | ||
constructor ( | ||
private readonly engine: AsciidocEngine | ||
) { | ||
} | ||
|
||
public async provideFoldingRanges ( | ||
document: vscode.TextDocument, | ||
_token: vscode.CancellationToken | ||
): Promise<vscode.FoldingRange[]> { | ||
const tableOfContentsProvider = new TableOfContentsProvider(this.engine, document) | ||
const tableOfContents = await tableOfContentsProvider.getToc() | ||
|
||
return tableOfContents.map((entry, startIndex) => { | ||
const start = entry.line | ||
let end: number | undefined | ||
for (let i = startIndex + 1; i < tableOfContents.length; ++i) { | ||
if (tableOfContents[i].level <= entry.level) { | ||
end = tableOfContents[i].line - 1 | ||
break | ||
} | ||
} | ||
return new vscode.FoldingRange( | ||
start, | ||
typeof end === 'number' ? end : document.lineCount - 1) | ||
}) | ||
} | ||
} |
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
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,87 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as assert from 'assert' | ||
import 'mocha' | ||
import * as vscode from 'vscode' | ||
import AsciidocFoldingProvider from '../features/foldingProvider' | ||
import { createNewAsciidocEngine } from './engine' | ||
import { InMemoryDocument } from './inMemoryDocument' | ||
|
||
const testFileName = vscode.Uri.file('test.adoc') | ||
|
||
suite('asciidoc.FoldingProvider', () => { | ||
test('Should not return anything for empty document', async () => { | ||
const folds = await getFoldsForDocument('') | ||
assert.strictEqual(folds.length, 0) | ||
}) | ||
|
||
test('Should not return anything for document without headers', async () => { | ||
const folds = await getFoldsForDocument(`a | ||
*b* afas | ||
a=b | ||
a`) | ||
assert.strictEqual(folds.length, 0) | ||
}) | ||
|
||
test('Should fold from header to end of document', async () => { | ||
const folds = await getFoldsForDocument(`= a | ||
== b | ||
c | ||
d`) | ||
assert.strictEqual(folds.length, 2) | ||
const firstFold = folds[0] | ||
assert.strictEqual(firstFold.start, 0) | ||
assert.strictEqual(firstFold.end, 5) | ||
}) | ||
|
||
test('Should leave single newline before next header', async () => { | ||
const folds = await getFoldsForDocument(` | ||
== a | ||
x | ||
== b | ||
y`) | ||
assert.strictEqual(folds.length, 2) | ||
const firstFold = folds[0] | ||
assert.strictEqual(firstFold.start, 1) | ||
assert.strictEqual(firstFold.end, 3) | ||
}) | ||
|
||
test('Should collapse multiple newlines to single newline before next header', async () => { | ||
const folds = await getFoldsForDocument(` | ||
= a | ||
x | ||
= b | ||
y`) | ||
assert.strictEqual(folds.length, 2) | ||
const firstFold = folds[0] | ||
assert.strictEqual(firstFold.start, 1) | ||
assert.strictEqual(firstFold.end, 5) | ||
}) | ||
|
||
test('Should not collapse if there is no newline before next header', async () => { | ||
const folds = await getFoldsForDocument(` | ||
= a | ||
x | ||
== b | ||
y`) | ||
assert.strictEqual(folds.length, 1) | ||
const firstFold = folds[0] | ||
assert.strictEqual(firstFold.start, 1) | ||
assert.strictEqual(firstFold.end, 4) | ||
}) | ||
}) | ||
|
||
async function getFoldsForDocument (contents: string) { | ||
const doc = new InMemoryDocument(testFileName, contents) | ||
const provider = new AsciidocFoldingProvider(createNewAsciidocEngine()) | ||
return await provider.provideFoldingRanges(doc, new vscode.CancellationTokenSource().token) | ||
} |