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

Add code folding on single line comment #565

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
32 changes: 32 additions & 0 deletions src/features/foldingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,48 @@ export default class AsciidocFoldingRangeProvider implements vscode.FoldingRange
}
}

private static handleSingleLineCommentFoldingRanges (singleLineCommentStartIndexes: any[], foldingRanges: any[], lineIndex: number, lineText: string,
documentLineCount: number) {
if (lineText.startsWith('//')) {
if (singleLineCommentStartIndexes.length === 0 && lineIndex < documentLineCount - 1) {
singleLineCommentStartIndexes.push(lineIndex)
} else {
// comment on last line of the document
const startIndex = singleLineCommentStartIndexes.pop()
if (lineIndex > startIndex) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
lineIndex,
FoldingRangeKind.Region)
)
}
}
} else {
if (singleLineCommentStartIndexes.length !== 0) {
const startIndex = singleLineCommentStartIndexes.pop()
const endIndex = lineIndex - 1
if (endIndex > startIndex) {
foldingRanges.push(new vscode.FoldingRange(
startIndex,
endIndex,
FoldingRangeKind.Region))
}
}
}
}

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

suite('getSingleLineCommentFoldingRanges', () => {
test('Should fold on a group of single line comments ', () => {
const folds = getFoldsForDocument(
`this is a paragraph

// A single-line comment.
// Another single-line comment.

this is a paragraph`)
assert.strictEqual(folds.length, 1, 'expecting 1 fold')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(2, 3, vscode.FoldingRangeKind.Region),
])
})

test('Should not fold single line comment if not contiguous ', () => {
const folds = getFoldsForDocument(
`// A single-line comment.

// Another single-line comment.

// This is a comment too.

This is a paragraph.

// This is another comment.`)
assert.strictEqual(folds.length, 0, 'expecting 0 fold')
})

test('Should not fold single lines comments if slashes are part of literal text ', () => {
const folds = getFoldsForDocument(
`this is a paragraph
// This is literal text
// This is a single line comment
this is the same paragraph`)
assert.strictEqual(folds.length, 0, 'expecting 0 fold')
})

test('Should fold if last single line comment is on the last line of the document', () => {
const folds = getFoldsForDocument(
`this is a paragraph
// This is a comment.
// The last line of the document is also a comment!`)
assert.strictEqual(folds.length, 1, 'expecting 1 fold')
assert.deepStrictEqual(folds, [
new vscode.FoldingRange(1, 2, vscode.FoldingRangeKind.Region),
])
})
})
})

function getFoldsForDocument (contents: string) {
Expand Down