Skip to content

Commit

Permalink
Folding mix of Attributes and single-line comments
Browse files Browse the repository at this point in the history
It removes the possibility to have Folding range for the first group of
attributes or first group of single-line comments. This is due to a
limitation of VS Code which doesn't handle several folding ranges
starting on the same line.

resolves #719

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Jun 23, 2023
1 parent 45302b5 commit f95b2fe
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Improvements

- provide folding for list of sibling attributes by @apupier (#719)
- provide folding for mix of attributes and single line comments by @apupier (#719)

### Bug fixes

Expand Down
78 changes: 70 additions & 8 deletions src/features/foldingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
}
}

private static handleSingleLineCommentFoldingRanges (singleLineCommentStartIndexes: any[], foldingRanges: any[], lineIndex: number, lineText: string,
private static handleSingleLineCommentFoldingRanges (
singleLineCommentStartIndexes: any[],
mixOfMultiAttributesAndSingleLineCommentsIndexes: any[],
foldingRanges: any[],
lineIndex: number,
lineText: string,
documentLineCount: number) {
if (lineText.startsWith('//')) {
if (singleLineCommentStartIndexes.length === 0) {
Expand All @@ -102,7 +107,7 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
if (lineIndex >= documentLineCount - 1) {
// comment on last line of the document
const startIndex = singleLineCommentStartIndexes.pop()
if (lineIndex > startIndex) {
if (lineIndex > startIndex && !(lineText.startsWith(':') && mixOfMultiAttributesAndSingleLineCommentsIndexes[0] === startIndex)) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
lineIndex,
Expand All @@ -114,7 +119,7 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
if (singleLineCommentStartIndexes.length !== 0) {
const startIndex = singleLineCommentStartIndexes.pop()
const endIndex = lineIndex - 1
if (endIndex > startIndex) {
if (endIndex > startIndex && !(lineText.startsWith(':') && mixOfMultiAttributesAndSingleLineCommentsIndexes[0] === startIndex)) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
endIndex,
Expand All @@ -124,15 +129,21 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
}
}

private static handleMultiAttributesFoldingRanges (multiAttributesIndexes: any[], foldingRanges: any[], lineIndex: number, lineText: string, documentLineCount: number) {
private static handleMultiAttributesFoldingRanges (
multiAttributesIndexes: any[],
mixOfMultiAttributesAndSingleLineCommentsIndexes: any[],
foldingRanges: any[],
lineIndex: number,
lineText: string,
documentLineCount: number) {
if (lineText.startsWith(':')) {
if (multiAttributesIndexes.length === 0) {
multiAttributesIndexes.push(lineIndex)
}
if (lineIndex >= documentLineCount - 1) {
// Attribute on last line of the document
const startIndex = multiAttributesIndexes.pop()
if (lineIndex > startIndex) {
if (lineIndex > startIndex && !(lineText.startsWith('//') && mixOfMultiAttributesAndSingleLineCommentsIndexes[0] === startIndex)) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
lineIndex)
Expand All @@ -143,11 +154,47 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
if (multiAttributesIndexes.length !== 0) {
const startIndex = multiAttributesIndexes.pop()
const endIndex = lineIndex - 1
if (endIndex > startIndex) {
if (endIndex > startIndex && !(lineText.startsWith('//') && mixOfMultiAttributesAndSingleLineCommentsIndexes[0] === startIndex)) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
endIndex))
}
}
}
}

private static handleMixOfMultiAttributesAndSingleLineCommentsFoldingRanges (
mixOfMultiAttributesAndSingleLineCommentsIndexes: any[],
mixOfMultiAttributesAndSingleLineCommentsCharacters: Set<string>,
foldingRanges: any[],
lineIndex: number,
lineText: string,
documentLineCount: number) {
if (lineText.startsWith(':') || lineText.startsWith('//')) {
if (mixOfMultiAttributesAndSingleLineCommentsIndexes.length === 0) {
mixOfMultiAttributesAndSingleLineCommentsIndexes.push(lineIndex)
}
mixOfMultiAttributesAndSingleLineCommentsCharacters.add(lineText.charAt(0))
if (lineIndex >= documentLineCount - 1) {
// Attribute on last line of the document
const startIndex = mixOfMultiAttributesAndSingleLineCommentsIndexes.pop()
if (lineIndex > startIndex && mixOfMultiAttributesAndSingleLineCommentsCharacters.size === 2) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
lineIndex)
)
}
}
} else {
if (mixOfMultiAttributesAndSingleLineCommentsIndexes.length !== 0) {
const startIndex = mixOfMultiAttributesAndSingleLineCommentsIndexes.pop()
const endIndex = lineIndex - 1
if (endIndex > startIndex && mixOfMultiAttributesAndSingleLineCommentsCharacters.size === 2) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
endIndex))
}
mixOfMultiAttributesAndSingleLineCommentsCharacters.clear()
}
}
}
Expand All @@ -158,14 +205,29 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
const commentBlockIndexes = []
const singleLineCommentStartIndexes = []
const multiAttributesIndexes = []
const mixOfMultiAttributesAndSingleLineCommentsIndexes = []
const mixOfMultiAttributesAndSingleLineCommentsCharacters = new Set<string>()
const documentLineCount = document.lineCount
for (let lineIndex = 0; lineIndex < documentLineCount; lineIndex++) {
const line = document.lineAt(lineIndex)
const lineText = line.text
this.handleOpenBlockFoldingRanges(openBlockIndexes, foldingRanges, lineIndex, lineText, documentLineCount)
this.handleCommentBlockFoldingRanges(commentBlockIndexes, foldingRanges, lineIndex, lineText, documentLineCount)
this.handleSingleLineCommentFoldingRanges(singleLineCommentStartIndexes, foldingRanges, lineIndex, lineText, documentLineCount)
this.handleMultiAttributesFoldingRanges(multiAttributesIndexes, foldingRanges, lineIndex, lineText, documentLineCount)
this.handleSingleLineCommentFoldingRanges(
singleLineCommentStartIndexes,
mixOfMultiAttributesAndSingleLineCommentsIndexes,
foldingRanges,
lineIndex,
lineText,
documentLineCount)
this.handleMultiAttributesFoldingRanges(multiAttributesIndexes, mixOfMultiAttributesAndSingleLineCommentsIndexes, foldingRanges, lineIndex, lineText, documentLineCount)
this.handleMixOfMultiAttributesAndSingleLineCommentsFoldingRanges(
mixOfMultiAttributesAndSingleLineCommentsIndexes,
mixOfMultiAttributesAndSingleLineCommentsCharacters,
foldingRanges,
lineIndex,
lineText,
documentLineCount)
}
return foldingRanges
}
Expand Down
73 changes: 72 additions & 1 deletion src/test/foldingProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InMemoryDocument } from './inMemoryDocument'

const testFileName = vscode.Uri.file('test.adoc')

suite('asciidoc.FoldingProvider', () => {
suite.only('asciidoc.FoldingProvider', () => {
suite('getHeaderFoldingRanges', () => {
test('Should not return anything for empty document', () => {
const folds = getFoldsForDocument('')
Expand Down Expand Up @@ -413,6 +413,77 @@ this is a paragraph`)
])
})
})

suite('getMultiAttributeAndSingleLineCommentsFoldingRanges', () => {
test('Should fold on a group of mix of attributes and single line comments ', () => {
const folds = getFoldsForDocument(
`this is a paragraph
// A single-line comment.
:an-attribute: value of attribute
// A second single-line comment.
:a-second-attribute: value of second attribute
this is a paragraph`)
assert.strictEqual(folds.length, 1, 'expecting 1 fold')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(2, 5),
])
})
test('Should fold on a group of mix of attributes and single line comments starting with 2 attributes', () => {
const folds = getFoldsForDocument(
`this is a paragraph
:an-attribute1: value of attribute
:an-attribute2: value of attribute
// A single-line comment.
:an-attribute: value of attribute
// A second single-line comment.
:a-second-attribute: value of second attribute
this is a paragraph`)
assert.strictEqual(folds.length, 1, 'expecting 1 fold')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(2, 7),
])
})
test('Should fold on a group of mix of attributes and single line comments starting with 2 line comments', () => {
const folds = getFoldsForDocument(
`this is a paragraph
// A single-line comment.
// A second single-line comment.
:an-attribute: value of attribute
// A third line comment
:a-second-attribute: value of second attribute
this is a paragraph`)
assert.strictEqual(folds.length, 1, 'expecting 1 fold')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(2, 6),
])
})
test('Should fold on a group of mix of attributes and single line comments and include next groups', () => {
const folds = getFoldsForDocument(
`this is a paragraph
// A single-line comment.
// A second single-line comment.
:an-attribute: value of attribute
:another-attribute:
// A third line comment
// as a potential folded group
:a-third-attribute: value of third attribute
this is a paragraph`)
assert.strictEqual(folds.length, 3, 'expecting 3 folds')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(4, 5),
new vscode.FoldingRange(6, 7, vscode.FoldingRangeKind.Comment),
new vscode.FoldingRange(2, 8),
])
})
})
})

function getFoldsForDocument (contents: string) {
Expand Down

0 comments on commit f95b2fe

Please sign in to comment.