Skip to content

Commit 820f4b8

Browse files
committed
Added selected text comment in TRQL editor
1 parent f5f2ad3 commit 820f4b8

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

apps/webapp/app/components/code/TSQLEditor.tsx

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,46 @@ const defaultProps: TSQLEditorDefaultProps = {
6060
schema: [],
6161
};
6262

63-
// Toggle comment on current line with -- comment symbol
63+
// Toggle comment on current line or selected lines with -- comment symbol
6464
const toggleLineComment = (view: EditorView): boolean => {
65-
const { from } = view.state.selection.main;
66-
const line = view.state.doc.lineAt(from);
67-
const lineText = line.text;
68-
const trimmed = lineText.trimStart();
69-
const indent = lineText.length - trimmed.length;
65+
const { from, to } = view.state.selection.main;
66+
const startLine = view.state.doc.lineAt(from);
67+
const endLine = view.state.doc.lineAt(to);
7068

71-
if (trimmed.startsWith("--")) {
72-
// Remove comment: strip "-- " or just "--"
73-
const afterComment = trimmed.slice(2);
74-
const newText = lineText.slice(0, indent) + afterComment.replace(/^\s/, "");
75-
view.dispatch({
76-
changes: { from: line.from, to: line.to, insert: newText },
77-
});
78-
} else {
79-
// Add comment: prepend "-- " to the line content
80-
const newText = lineText.slice(0, indent) + "-- " + trimmed;
81-
view.dispatch({
82-
changes: { from: line.from, to: line.to, insert: newText },
83-
});
69+
// Collect all lines in the selection
70+
const lines: { from: number; to: number; text: string }[] = [];
71+
for (let i = startLine.number; i <= endLine.number; i++) {
72+
const line = view.state.doc.line(i);
73+
lines.push({ from: line.from, to: line.to, text: line.text });
74+
}
75+
76+
// Determine action: if all non-empty lines are commented, uncomment; otherwise comment
77+
const allCommented = lines.every((line) => {
78+
const trimmed = line.text.trimStart();
79+
return trimmed.length === 0 || trimmed.startsWith("--");
80+
});
81+
82+
const changes = lines
83+
.map((line) => {
84+
const trimmed = line.text.trimStart();
85+
if (trimmed.length === 0) return null; // skip empty lines
86+
const indent = line.text.length - trimmed.length;
87+
88+
if (allCommented) {
89+
// Remove comment: strip "-- " or just "--"
90+
const afterComment = trimmed.slice(2);
91+
const newText = line.text.slice(0, indent) + afterComment.replace(/^\s/, "");
92+
return { from: line.from, to: line.to, insert: newText };
93+
} else {
94+
// Add comment: prepend "-- " to the line content
95+
const newText = line.text.slice(0, indent) + "-- " + trimmed;
96+
return { from: line.from, to: line.to, insert: newText };
97+
}
98+
})
99+
.filter((c): c is { from: number; to: number; insert: string } => c !== null);
100+
101+
if (changes.length > 0) {
102+
view.dispatch({ changes });
84103
}
85104

86105
return true;

0 commit comments

Comments
 (0)