Skip to content

Commit

Permalink
[sourcegen] Fix indentation levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 14, 2024
1 parent f41e007 commit 081947a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
5 changes: 5 additions & 0 deletions interfaces/sourcegen/sourcegen/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def with_unpack_iter(cls: type) -> type:
return cls


def hanging_text(text: str, spaces: int) -> str:
ret = ("\n" + " "*spaces).join(text.split("\n"))
return "\n".join([_.rstrip() for _ in ret.split("\n")])


def normalize_indent(code: str) -> str:
code = textwrap.dedent(code).strip()

Expand Down
35 changes: 18 additions & 17 deletions interfaces/sourcegen/sourcegen/csharp/_CSharpSourceGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from pathlib import Path
from typing import List, Dict
import re
import textwrap

from ._dataclasses import CsFunc
from ._Config import Config
from .._helpers import normalize_indent
from .._helpers import normalize_indent, hanging_text
from .._dataclasses import Func, Param, HeaderFile
from .._SourceGenerator import SourceGenerator

Expand Down Expand Up @@ -200,32 +201,32 @@ def _write_file(self, filename: str, contents: str):
def _scaffold_interop(self, header_file_path: Path, cs_funcs: List[CsFunc]):
functions_text = "\n\n".join(map(self._get_interop_func_text, cs_funcs))

interop_text = normalize_indent(f"""
{normalize_indent(self._config.preamble)}
interop_text = textwrap.dedent(f"""
{hanging_text(self._config.preamble, 12)}
using System.Runtime.InteropServices;
namespace Cantera.Interop;
static partial class LibCantera
{{
{normalize_indent(functions_text)}
{hanging_text(functions_text, 16)}
}}
""")
""").lstrip()

self._write_file("Interop.LibCantera." + header_file_path.name + ".g.cs",
interop_text)

def _scaffold_handles(self, header_file_path: Path, handles: Dict[str, str]):
handles_text = "\n\n".join(starmap(self._get_base_handle_text, handles.items()))

handles_text = normalize_indent(f"""
{normalize_indent(self._config.preamble)}
handles_text = textwrap.dedent(f"""
{hanging_text(self._config.preamble, 12)}
namespace Cantera.Interop;
{normalize_indent(handles_text)}
""")
{hanging_text(handles_text, 12)}
""").lstrip()

self._write_file("Interop.Handles." + header_file_path.name + ".g.cs",
handles_text)
Expand All @@ -234,13 +235,13 @@ def _scaffold_derived_handles(self):
derived_handles = "\n\n".join(starmap(self._get_derived_handle_text,
self._config.derived_handles.items()))

derived_handles_text = normalize_indent(f"""
{normalize_indent(self._config.preamble)}
derived_handles_text = textwrap.dedent(f"""
{hanging_text(self._config.preamble, 12)}
namespace Cantera.Interop;
{derived_handles}
""")
{hanging_text(derived_handles, 12)}
""").lstrip()

self._write_file("Interop.Handles.g.cs", derived_handles_text)

Expand All @@ -253,8 +254,8 @@ def _scaffold_wrapper_class(self, clib_area: str, props: Dict[str, str],
self._get_property_text(clib_area, c_name, cs_name, known_funcs)
for (c_name, cs_name) in props.items())

wrapper_class_text = normalize_indent(f"""
{normalize_indent(self._config.preamble)}
wrapper_class_text = textwrap.dedent(f"""
{hanging_text(self._config.preamble, 12)}
using Cantera.Interop;
Expand All @@ -266,7 +267,7 @@ def _scaffold_wrapper_class(self, clib_area: str, props: Dict[str, str],
#pragma warning disable CS1591
{normalize_indent(properties_text)}
{hanging_text(properties_text, 16)}
#pragma warning restore CS1591
Expand All @@ -277,7 +278,7 @@ def _scaffold_wrapper_class(self, clib_area: str, props: Dict[str, str],
public void Dispose() =>
_handle.Dispose();
}}
""")
""").lstrip()

self._write_file(wrapper_class_name + ".g.cs", wrapper_class_text)

Expand Down

0 comments on commit 081947a

Please sign in to comment.