Skip to content

Commit

Permalink
fix: don't fold before first line if end is on the same line
Browse files Browse the repository at this point in the history
ref: #95
  • Loading branch information
daiyam committed Oct 31, 2024
1 parent fd40ebc commit d3906cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/folding-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ export class FoldingProvider implements FoldingRangeProvider {
break;
case Marker.MIDDLE:
if(stack.length > 0 && stack[0].rule === rule) {
const begin = rule.foldBeforeFirstLine && stack[0].line > 0 ? stack[0].line - 1 : stack[0].line;
const begin = rule.foldBeforeFirstLine && stack[0].line > 0 && stack[0].line !== line ? stack[0].line - 1 : stack[0].line;
const end = line;

if(end > begin + 1) {
Expand All @@ -851,7 +851,7 @@ export class FoldingProvider implements FoldingRangeProvider {
return { line, offset };
}

const begin = rule.foldBeforeFirstLine && last.line > 0 ? last.line - 1 : last.line;
const begin = rule.foldBeforeFirstLine && last.line > 0 && last.line !== line ? last.line - 1 : last.line;
const end = rule.consumeEnd!() ? line : Math.max(line - 1, begin);

while(stack.length > 1) {
Expand Down Expand Up @@ -880,7 +880,7 @@ export class FoldingProvider implements FoldingRangeProvider {
}

if(stack.length > 0 && stack[0].rule === rule) {
const begin = rule.foldBeforeFirstLine && stack[0].line > 0 ? stack[0].line - 1 : stack[0].line;
const begin = rule.foldBeforeFirstLine && stack[0].line > 0 && stack[0].line !== line ? stack[0].line - 1 : stack[0].line;
const end = rule.consumeEnd!() ? line : Math.max(line - 1, begin);

if(rule.foldLastLine(matchOffset, ...match)) {
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/cpp/before.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>


int main()
{
std::cout << "Hello!\n";
}
13 changes: 13 additions & 0 deletions test/fixtures/cpp/before.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
config:
- begin: "{"
end: "}"
foldBeforeFirstLine: true
foldLastLine: true
- begin: "("
end: ")"
foldBeforeFirstLine: true
foldLastLine: true
foldings:
- start: 3
end: 6
kind: Region

0 comments on commit d3906cc

Please sign in to comment.