Skip to content

Commit

Permalink
add property consumeEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
daiyam committed Jun 21, 2021
1 parent 89a7ffe commit c5cdea4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type FoldingConfig = {
whileRegex?: string,
indentation?: boolean,
offSide?: boolean;
consumeEnd?: boolean | boolean[],
foldLastLine?: boolean | boolean[],
foldBOF?: boolean,
foldEOF?: boolean,
Expand Down
19 changes: 14 additions & 5 deletions src/foldingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type FoldingRegex = {
loopRegex?: RegExp,
while?: RegExp,
continuation?: boolean,
consumeEnd?: (...args: string[]) => boolean,
foldLastLine: (...args: string[]) => boolean,
foldBOF: boolean,
foldEOF: boolean,
Expand Down Expand Up @@ -256,6 +257,7 @@ export default class ExplicitFoldingProvider implements FoldingRangeProvider {
begin,
middle,
end,
consumeEnd: typeof configuration.consumeEnd === 'boolean' ? id(configuration.consumeEnd) : id(true),
foldLastLine: typeof configuration.foldLastLine === 'boolean' ? id(configuration.foldLastLine) : id(true),
foldBOF: false,
foldEOF: configuration.foldEOF || false,
Expand All @@ -274,6 +276,13 @@ export default class ExplicitFoldingProvider implements FoldingRangeProvider {
const middleGroupCount = regex.middle ? 1 + this.getCaptureGroupCount(middle!.source) : 0;
const endGroupCount = 1 + this.getCaptureGroupCount(end.source);

if (Array.isArray(configuration.consumeEnd) && configuration.consumeEnd.length === endGroupCount) {
const consumeEnd = configuration.consumeEnd;
const groupIndex = 1 + (nested ? this.groupIndex : 0) + middleGroupCount;

regex.consumeEnd = shouldFoldLastLine(consumeEnd, groupIndex, endGroupCount)
}

if (Array.isArray(configuration.foldLastLine) && configuration.foldLastLine.length === endGroupCount) {
const foldLastLine = configuration.foldLastLine;
const groupIndex = 1 + (nested ? this.groupIndex : 0) + middleGroupCount;
Expand Down Expand Up @@ -665,7 +674,7 @@ export default class ExplicitFoldingProvider implements FoldingRangeProvider {
case Marker.END:
const last = stack.length && stack[stack.length - 1];
if (secondaryLoop && last && last.regex === regex && (!last.expectedEnd || match[0] === last.expectedEnd)) {
const end = line;
const end = regex.consumeEnd!() ? line : line - 1;

while (stack.length > 1) {
const begin = stack[0].line;
Expand All @@ -686,21 +695,21 @@ export default class ExplicitFoldingProvider implements FoldingRangeProvider {
foldingRanges.push(new FoldingRange(begin, end, regex.kind));
}

return { line: line + 1, offset: 0 };
return { line: end + 1, offset: 0 };
} else {
if (end > begin + 1) {
foldingRanges.push(new FoldingRange(begin, end - 1, regex.kind));
}

if (end > begin) {
return { line, offset: 0 };
return { line: end + 1, offset: 0 };
}
else {
return { line, offset: nextOffset };
return { line: end, offset: nextOffset };
}
}
} else if (stack[0] && stack[0].regex === regex && (!stack[0].expectedEnd || match[0] === stack[0].expectedEnd)) {
const end = line;
const end = regex.consumeEnd!() ? line : line - 1;

const begin = stack[0].line;

Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/cpp/notnested.conflict.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(

"(
)"

)
16 changes: 16 additions & 0 deletions test/fixtures/cpp/notnested.conflict.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
config:
- beginRegex: "\"\\("
endRegex: \)"
nested: false
foldLastLine: false
- begin: "("
end: ")"
foldLastLine: false

foldings:
- start: 2
end: 3
kind: 3
- start: 0
end: 5
kind: 3
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/fixtures/jcl/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ config:
kind: comment
beginRegex: "^\\/\\/\\*"
endRegex: "^\\/\\/[^*]"
foldLastLine: false
consumeEnd: false
nested: false
- separatorRegex: "^\\/\\/[^*]\\S* +JOB "
strict: never
Expand Down

0 comments on commit c5cdea4

Please sign in to comment.