B: Fix boolean envelopement

This commit is contained in:
Ivan 2025-05-14 10:47:15 +03:00
parent a99734ac0e
commit 9524727391
2 changed files with 33 additions and 5 deletions

View File

@ -17,8 +17,6 @@ export function TabBlockChildren() {
const children_operations = useWatch({ control, name: 'children_operations' });
const exclude = parent ? [-parent, ...manager.oss.hierarchy.expandAllInputs([-parent]).filter(id => id < 0)] : [];
console.log(exclude);
const value = [...children_blocks.map(id => -id), ...children_operations];
function handleChangeSelected(newValue: number[]) {

View File

@ -1,6 +1,6 @@
// Formatted text editing helpers
import { type ReactCodeMirrorRef } from '@uiw/react-codemirror';
import { type ReactCodeMirrorRef, type SelectionRange } from '@uiw/react-codemirror';
import { CodeMirrorWrapper } from '@/utils/codemirror';
@ -145,8 +145,7 @@ export class RSTextWrapper extends CodeMirrorWrapper {
return true;
}
case TokenID.BOOLEAN: {
const selStart = selection.from;
if (hasSelection && this.ref.view.state.sliceDoc(selStart, selStart + 1) === '') {
if (hasSelection && this.startsWithBoolean(selection)) {
this.envelopeWith('', '');
} else {
this.envelopeWith('(', ')');
@ -285,4 +284,35 @@ export class RSTextWrapper extends CodeMirrorWrapper {
}
return false;
}
private startsWithBoolean(range: SelectionRange): boolean {
const text = this.ref.view.state.sliceDoc(range.from, range.to);
if (!text.startsWith('') || !text.endsWith(')')) {
return false;
}
const openParenIndex = text.indexOf('(', 1);
if (openParenIndex === -1) {
return false;
}
for (const char of text.slice(0, openParenIndex)) {
if (char !== '') {
return false;
}
}
const bracketsContent = text.slice(openParenIndex + 1, text.length - 1);
return this.isValidBracketSequence(bracketsContent);
}
private isValidBracketSequence(text: string): boolean {
let depth = 0;
for (const char of text) {
if (char === '(') {
depth++;
} else if (char === ')') {
depth--;
if (depth < 0) return false;
}
}
return depth === 0;
}
}