Skip to content
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

Folding mix of Attributes and single-line comments #733

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
71 changes: 71 additions & 0 deletions src/test/foldingProvider.test.ts
Original file line number Diff line number Diff line change
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