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

Provide folding range for sibling attributes #723

Merged
merged 1 commit into from
Jun 17, 2023
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
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