Skip to content

Commit

Permalink
Add default visitors in RefactorBase that can be reused
Browse files Browse the repository at this point in the history
  • Loading branch information
Myoldmopar committed Jan 30, 2024
1 parent 2f6f3cf commit 8e7c44f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
34 changes: 24 additions & 10 deletions energyplus_refactor_helper/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ def function_calls(self) -> list[str]: # return something better eventually
"""
raise NotImplementedError()

@staticmethod
def default_function_call_visitor(function_call) -> str:
"""
This is a small helper function that simply takes a function call argument, and rewrites it in a functionally
equivalent manner, returning that string.
:param function_call: The FunctionCall instance to operate on
;return str: The function call in a functionally equivalent string
"""
return function_call.rewrite()

@staticmethod
def default_function_group_visitor(function_group) -> str:
individual_call_strings = []
for i, function in enumerate(function_group.function_calls):
args = ', '.join(function.parse_arguments())
full_call = f"{function.function_name}({args});"
include_prefix = len(function_group.function_calls) > 0 and i > 0
including_prefix = (function.preceding_text + full_call) if include_prefix else full_call
individual_call_strings.append(including_prefix)
return '\n'.join(individual_call_strings)

# noinspection PyMethodMayBeStatic
def function_visitor(self, function_call_or_group) -> str:
"""
Expand All @@ -61,8 +83,7 @@ def function_visitor(self, function_call_or_group) -> str:
didn't break anything.
:return: A string representation of the function call or function call group.
"""
args = ', '.join(function_call_or_group.parse_arguments())
return f"{function_call_or_group.function_name}({args});"
return RefactorBase.default_function_call_visitor(function_call_or_group)

# noinspection PyMethodMayBeStatic
def visits_each_group(self) -> bool:
Expand Down Expand Up @@ -149,14 +170,7 @@ def function_visitor(self, function_group) -> str:
:param function_group: The FunctionCallGroup to inspect, and from that, generate a new string representation.
:return: A string representation of the function call group.
"""
individual_call_strings = []
for i, function in enumerate(function_group.function_calls):
args = ', '.join(function.parse_arguments())
full_call = f"{function.function_name}({args});"
include_prefix = len(function_group.function_calls) > 0 and i > 0
including_prefix = (function.preceding_text + full_call) if include_prefix else full_call
individual_call_strings.append(including_prefix)
return '\n'.join(individual_call_strings)
return RefactorBase.default_function_group_visitor(function_group)

# noinspection PyMethodMayBeStatic
def visits_each_group(self) -> bool:
Expand Down
8 changes: 8 additions & 0 deletions energyplus_refactor_helper/function_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,11 @@ def __str__(self):
"""String representation summary of the function call"""
single_line = ''.join(self.as_cleaned_multiline()).strip()
return f"{self.starting_line_number} - {self.ending_line_number} : {single_line[:35]}"

def rewrite(self) -> str:
"""
Rewrite the function call as a string, functionally equivalent to the original form, but modified with
line breaks and such missing.
"""
args = ', '.join(self.parse_arguments())
return f"{self.function_name}({args});"

0 comments on commit 8e7c44f

Please sign in to comment.