forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplpgsqlFoldService.js
More file actions
53 lines (43 loc) · 1.36 KB
/
plpgsqlFoldService.js
File metadata and controls
53 lines (43 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { foldService } from '@codemirror/language';
function findRange(pair, state, startLine) {
let depth = 1;
const from = startLine.to;
for (let i = startLine.number + 1; i <= state.doc.lines; i++) {
const line = state.doc.line(i);
const text = line.text.trim();
if (pair.start.test(text)) {
depth++;
} else if (pair.end.test(text)) {
depth--;
if (depth === 0) {
// Only fold if there is at least one line between the start and end.
if (i <= startLine.number + 1) {
return null;
}
// leaving the closing keyword line visible after folding.
const to = state.doc.line(i - 1).to;
return { from, to };
}
}
}
// No valid closing block found
return null;
}
const plpgsqlFoldService = foldService.of((state, startPos) => {
const startLine = state.doc.lineAt(startPos);
const startText = startLine.text.trim();
const foldPairs = [
// Added 'i' flag for case-insensitivity
{ start: /^BEGIN\b/i, end: /^END\b\s*;$/i },
{ start: /^IF\b/i, end: /^END IF\b\s*;$/i },
{ start: /^FOR\b/i, end: /^END LOOP\b\s*;$/i },
{ start: /^CASE\b/i, end: /^END CASE\b\s*;$/i }
];
for (let pair of foldPairs) {
if (pair.start.test(startText)) {
return findRange(pair, state, startLine);
}
}
return null;
});
export default plpgsqlFoldService;