Skip to content

Commit 4a4d456

Browse files
Improved PL/PGSQL code folding and support nested blocks. #6118
1 parent 3286b4e commit 4a4d456

8 files changed

Lines changed: 88 additions & 10 deletions

File tree

.github/workflows/run-python-tests-epas.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ jobs:
3939
steps:
4040
- uses: actions/checkout@v4
4141

42+
- name: Update python version
43+
uses: actions/setup-python@v5
44+
with:
45+
python-version: '3.10'
46+
4247
- name: Setup the EDB APT repo on Linux
4348
if: ${{ matrix.os == 'ubuntu-22.04' }}
4449
run: |

.github/workflows/run-python-tests-pg.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ jobs:
2828
steps:
2929
- uses: actions/checkout@v4
3030

31+
- name: Update python version
32+
uses: actions/setup-python@v5
33+
with:
34+
python-version: '3.10'
35+
3136
- name: Setup the PGDG APT repo on Linux
3237
if: ${{ matrix.os == 'ubuntu-22.04' }}
3338
run: |

docs/en_US/release_notes_9_5.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ Housekeeping
2828
Bug fixes
2929
*********
3030

31-
| `Issue #8032 <https://github.com/pgadmin-org/pgadmin4/issues/8032>`_ - Fixed an issue where the Schema Diff Tool incorrectly reported differences due to variations in the order of the privileges.
31+
| `Issue #6118 <https://github.com/pgadmin-org/pgadmin4/issues/6118>`_ - Improved PL/pgSQL code folding and support nested blocks.
32+
| `Issue #8032 <https://github.com/pgadmin-org/pgadmin4/issues/8032>`_ - Fixed an issue where the Schema Diff Tool incorrectly reported differences due to variations in the order of the privileges.
33+
| `Issue #8691 <https://github.com/pgadmin-org/pgadmin4/issues/8691>`_ - Fixed an issue in the query tool where using multiple cursors to copy text resulted in only the first line being copied.

web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"@babel/plugin-proposal-class-properties": "^7.10.4",
7474
"@babel/preset-react": "^7.12.13",
7575
"@codemirror/lang-json": "^6.0.1",
76-
"@codemirror/lang-sql": "^6.8.0",
76+
"@codemirror/lang-sql": "^6.9.0",
7777
"@date-io/core": "^3.0.0",
7878
"@date-io/date-fns": "3.x",
7979
"@emotion/sheet": "^1.0.1",

web/pgadmin/static/js/components/ReactCodeMirror/components/Editor.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ import currentQueryHighlighterExtn from '../extensions/currentQueryHighlighter';
5454
import { autoCompleteCompartment, eolCompartment, indentNewLine, eol } from '../extensions/extraStates';
5555
import { OS_EOL } from '../../../../../tools/sqleditor/static/js/components/QueryToolConstants';
5656
import { useTheme } from '@mui/material';
57+
import plpgsqlFoldService from '../extensions/plpgsqlFoldService';
5758

58-
const arrowRightHtml = ReactDOMServer.renderToString(<KeyboardArrowRightRoundedIcon style={{width: '16px'}} />);
59-
const arrowDownHtml = ReactDOMServer.renderToString(<ExpandMoreRoundedIcon style={{width: '16px'}} />);
59+
const arrowRightHtml = ReactDOMServer.renderToString(<KeyboardArrowRightRoundedIcon style={{width: '16px', fill: 'currentcolor'}} />);
60+
const arrowDownHtml = ReactDOMServer.renderToString(<ExpandMoreRoundedIcon style={{width: '16px', fill: 'currentcolor'}} />);
6061

6162
function handleDrop(e, editor) {
6263
let dropDetails = null;
@@ -158,7 +159,9 @@ const defaultExtensions = [
158159
autoCompleteCompartment.of([]),
159160
EditorView.clipboardOutputFilter.of((text, state)=>{
160161
return CustomEditorView.getSelectionFromState(state);
161-
})
162+
}),
163+
// Custom folding service for PL/pgSQL
164+
plpgsqlFoldService,
162165
];
163166

164167
export default function Editor({

web/pgadmin/static/js/components/ReactCodeMirror/extensions/dialect.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SQLDialect, PostgreSQL } from '@codemirror/lang-sql';
2+
import { foldNodeProp } from '@codemirror/language';
23

34
const extraKeywords = 'unsafe';
45
const keywords = PostgreSQL.spec.keywords.replace(/\b\w\b/, '') + ' ' + extraKeywords;
@@ -10,5 +11,14 @@ const PgSQL = SQLDialect.define({
1011
specialVar: '',
1112
keywords: keywords,
1213
types: PostgreSQL.spec.types,
14+
}).configureLanguage({
15+
// Disable default folding behavior as it conflicts with custom folding
16+
props: [
17+
foldNodeProp.add({
18+
Statement() { return null; },
19+
BlockComment() { return null; }
20+
}),
21+
]
1322
});
23+
1424
export default PgSQL;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { foldService } from '@codemirror/language';
2+
3+
function findRange(pair, state, startLine) {
4+
let depth = 1;
5+
const from = startLine.to;
6+
7+
for (let i = startLine.number + 1; i <= state.doc.lines; i++) {
8+
const line = state.doc.line(i);
9+
const text = line.text.trim();
10+
11+
if (pair.start.test(text)) {
12+
depth++;
13+
} else if (pair.end.test(text)) {
14+
depth--;
15+
if (depth === 0) {
16+
// Only fold if there is at least one line between the start and end.
17+
if (i <= startLine.number + 1) {
18+
return null;
19+
}
20+
21+
// leaving the closing keyword line visible after folding.
22+
const to = state.doc.line(i - 1).to;
23+
return { from, to };
24+
}
25+
}
26+
}
27+
// No valid closing block found
28+
return null;
29+
}
30+
31+
32+
const plpgsqlFoldService = foldService.of((state, startPos) => {
33+
const startLine = state.doc.lineAt(startPos);
34+
const startText = startLine.text.trim();
35+
36+
const foldPairs = [
37+
// Added 'i' flag for case-insensitivity
38+
{ start: /^BEGIN\b/i, end: /^END\b\s*;$/i },
39+
{ start: /^IF\b/i, end: /^END IF\b\s*;$/i },
40+
{ start: /^FOR\b/i, end: /^END LOOP\b\s*;$/i },
41+
{ start: /^CASE\b/i, end: /^END CASE\b\s*;$/i }
42+
];
43+
44+
for (let pair of foldPairs) {
45+
if (pair.start.test(startText)) {
46+
return findRange(pair, state, startLine);
47+
}
48+
}
49+
50+
return null;
51+
});
52+
53+
export default plpgsqlFoldService;

web/yarn.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,17 +1497,17 @@ __metadata:
14971497
languageName: node
14981498
linkType: hard
14991499

1500-
"@codemirror/lang-sql@npm:^6.8.0":
1501-
version: 6.8.0
1502-
resolution: "@codemirror/lang-sql@npm:6.8.0"
1500+
"@codemirror/lang-sql@npm:^6.9.0":
1501+
version: 6.9.0
1502+
resolution: "@codemirror/lang-sql@npm:6.9.0"
15031503
dependencies:
15041504
"@codemirror/autocomplete": ^6.0.0
15051505
"@codemirror/language": ^6.0.0
15061506
"@codemirror/state": ^6.0.0
15071507
"@lezer/common": ^1.2.0
15081508
"@lezer/highlight": ^1.0.0
15091509
"@lezer/lr": ^1.0.0
1510-
checksum: 1b5a3c8129b09f24039d8c0906fc4cb8d0f706a424a1d56721057bd1e647797c2b1240bb53eed9bf2bac5806a4e0363e555a3963f04c478efa05829890c537f7
1510+
checksum: aca08c11b519f962e9a59e14dc6f54102deaa7a15a390c2dc50401234d33a1fd0c5d2436e6f1810a0363b6f2649480d28eb16a10a7e3f4221ffa3f130ef0672e
15111511
languageName: node
15121512
linkType: hard
15131513

@@ -13637,7 +13637,7 @@ __metadata:
1363713637
"@babel/preset-react": ^7.12.13
1363813638
"@babel/preset-typescript": ^7.24.7
1363913639
"@codemirror/lang-json": ^6.0.1
13640-
"@codemirror/lang-sql": ^6.8.0
13640+
"@codemirror/lang-sql": ^6.9.0
1364113641
"@date-io/core": ^3.0.0
1364213642
"@date-io/date-fns": 3.x
1364313643
"@emotion/memoize": ^0.9.0

0 commit comments

Comments
 (0)