Skip to content
Snippets Groups Projects
Commit 3e4fe101 authored by George Marchment's avatar George Marchment
Browse files

Update function which turns a single line condition into a multiline condition

parent a0abc48e
No related merge requests found
Pipeline #14459 failed with stage
in 2 minutes and 7 seconds
from .outils import remove_comments, get_parenthese_count, get_curly_count
from .outils import remove_comments, get_parenthese_count, get_curly_count, get_code_until_parenthese_count
from .bioflowinsighterror import BioFlowInsightError
import re
from . import constant
......@@ -32,12 +32,22 @@ class Code:
raise BioFlowInsightError(f"The presence of '{bit_of_code}' is detected{self.get_string_line(bit_of_code)}.", num = 1,origin=self)
#This methods turns a single line condition into a muli line conditions
def turn_single_condition_into_multiline(self, code):
pattern = r"(if *\()(.+)\n"
to_replace = []
for match in re.finditer(r"if *\((.+)\) *([^{\n]+)\n", code):
old = match.group(0)
new = f"if ({match.group(1)}) {{\n{match.group(2)}\n}}\n"
to_replace.append((old, new))
for match in re.finditer(pattern, code):
all = match.group(0)
extarcted = match.group(2).strip()
if(extarcted!="" and extarcted[-1] not in ["{", "}"]):
start, end = match.span(1)
extracted_condition = get_code_until_parenthese_count(code=code[end:], val=-1)
condition = extracted_condition[:-1]
body = extarcted.replace(extracted_condition, "")
new = f"if ({condition}) {{\n{body}\n}}\n"
to_replace.append((all, new))
for r in to_replace:
old, new = r
code = code.replace(old, new)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment