Skip to content

Commit

Permalink
Provide folding range for sibling attributes
Browse files Browse the repository at this point in the history
part of asciidoctor#719

basic use case covered, other cases potentially to cover:
- include comment lines
- include empty lines

Signed-off-by: Aurélien Pupier <apupier@redhat.com>
  • Loading branch information
apupier committed Jun 16, 2023
1 parent 5051550 commit 828e3ce
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Please note that when exporting to HTML, `stylesdir` and `stylesheet` will be us
**NOTE:** We strongly recommend to use [`.asciidoctorconfig` file](https://intellij-asciidoc-plugin.ahus1.de/docs/users-guide/features/advanced/asciidoctorconfig-file.html) to define common attributes.
This file will be used in the preview and when exporting to HTML and PDF (using `asciidoctor-pdf`).

### Improvements

- provide folding for list of sibling attributes by @apupier (#719)

### Bug fixes

- declare `supports_templates` as attribute otherwise `backendTraits` overrides other values, as a result syntax highlighting wasn't working anymore! (#666)
Expand Down
30 changes: 30 additions & 0 deletions src/features/foldingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,48 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
}
}

private static handleMultiAttributesFoldingRanges (multiAttributesIndexes: 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) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
lineIndex)
)
}
}
} else {
if (multiAttributesIndexes.length !== 0) {
const startIndex = multiAttributesIndexes.pop()
const endIndex = lineIndex - 1
if (endIndex > startIndex) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
endIndex))
}
}
}
}

private static getBlockFoldingRanges (document: vscode.TextDocument) {
const foldingRanges = []
const openBlockIndexes = []
const commentBlockIndexes = []
const singleLineCommentStartIndexes = []
const multiAttributesIndexes = []
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)
}
return foldingRanges
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/foldingProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,24 @@ this is the same paragraph`)
])
})
})

suite('getMultiAttributesFoldingRanges', () => {
test('Should fold on a group of attributes ', () => {
const folds = getFoldsForDocument(
`this is a paragraph
:attribute1: value 1
:attribute2: value 2
:attribute3: value 3
:attribute4: value 4
this is a paragraph`)
assert.strictEqual(folds.length, 1, 'expecting 1 fold')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(2, 5),
])
})
})
})

function getFoldsForDocument (contents: string) {
Expand Down

0 comments on commit 828e3ce

Please sign in to comment.