Skip to content

Commit ec5ffa6

Browse files
committed
adjust end position for comment block folding range
1 parent 7eb1f4d commit ec5ffa6

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/language/folding/ContextMapperDslFoldingRageProvider.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DefaultFoldingRangeProvider } from 'langium/lsp'
22
import { CstNode, LangiumDocument } from 'langium'
3-
import { FoldingRange } from 'vscode-languageserver-types'
3+
import { FoldingRange, FoldingRangeKind } from 'vscode-languageserver-types'
44

55
export class ContextMapperDslFoldingRangeProvider extends DefaultFoldingRangeProvider {
66
// modified version of DefaultFoldingRangeProvider#toFoldingRange
@@ -14,11 +14,25 @@ export class ContextMapperDslFoldingRangeProvider extends DefaultFoldingRangePro
1414
return undefined
1515
}
1616

17-
if (!this.includeLastFoldingLine(node, kind)) { // checks if block is parenthesis/bracket/brace block
17+
if (!this.includeLastFoldingLine(node, kind)) { // checks if block is parenthesis/bracket/brace block or comment
1818
// To prevent something like '...}' in the editor when a code block is collapsed, we want to still show the first line of a code block
19-
start = document.textDocument.positionAt(document.textDocument.offsetAt({ line: start.line + 1, character: 0 }) - 1)
20-
// As we don't want to hide the end token like 'if { ... --> } <--', we simply select the end of the previous line as the end position
21-
end = document.textDocument.positionAt(document.textDocument.offsetAt({ line: end.line, character: 0 }) - 1)
19+
start = document.textDocument.positionAt(document.textDocument.offsetAt({
20+
line: start.line + 1,
21+
character: 0
22+
}) - 1)
23+
24+
let offsetFromEnd
25+
if (kind === FoldingRangeKind.Comment) {
26+
// So that comments are collapsed like '/*...*/' the end character needs to be at the start of the last line
27+
offsetFromEnd = 0
28+
} else {
29+
// As we don't want to hide the end token like 'if { ... --> } <--', we simply select the end of the previous line as the end position
30+
offsetFromEnd = 1
31+
}
32+
end = document.textDocument.positionAt(document.textDocument.offsetAt({
33+
line: end.line,
34+
character: 0
35+
}) - offsetFromEnd)
2236
}
2337
return FoldingRange.create(start.line, end.line, start.character, end.character, kind)
2438
}

0 commit comments

Comments
 (0)