Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CodeWriter newlines and indentation #529

Merged
merged 2 commits into from
Aug 17, 2020
Merged

Conversation

mtdowling
Copy link
Member

This commit simplifies and improves how the CodeWriter handles the
automatic injection of newlines and indentation. Now, the only difference
between write() and writeInline() is that write() appends a newline
(there was previously a lot of special-cased code for writeInline).

  • writeInline() now properly handles indentation when necessary (e.g., if
    a previous call injected a newline). This includes a few edge cases
    like if the first call to the CodeWriter is to writeInline() and uses
    indentation.
  • The character used for newlines is now tracked by the current state rather
    than globally.
  • Trailing newlines are now always inserted to the given text if enabled
    There was a bug previously where they were not appended if the last call
    on the CodeWriter was a writeInline(). This could potentially affect
    usage of CodeWriters that only wrote with inline calls.
  • The automatic appending of a newline by methods like openBlock(),
    closeBlock(), and write() can now enabled or disabled using
    enableNewlines() and disableNewlines().

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

This commit simplifies and improves how the CodeWriter handles the
automatic injection of newlines and indentation. Now, the *only* difference
between write() and writeInline() is that write() appends a newline
(there was previously a lot of special-cased code for writeInline).

* writeInline() now properly handles indentation when necessary (e.g., if
  a previous call injected a newline). This includes a few edge cases
  like if the first call to the CodeWriter is to writeInline() and uses
  indentation.
* The character used for newlines is now tracked by the current state rather
  than globally.
* Trailing newlines are now *always* inserted to the given text if enabled
  There was a bug previously where they were not appended if the last call
  on the CodeWriter was a writeInline(). This could potentially affect
  usage of CodeWriters that only wrote with inline calls.
* The automatic appending of a newline by methods like openBlock(),
  closeBlock(), and write() can now enabled or disabled using
  enableNewlines() and disableNewlines().
@jasdel
Copy link
Contributor

jasdel commented Aug 17, 2020

The automatic appending of a newline by methods like openBlock(), closeBlock(), and write() can now enabled or disabled using enableNewlines() and disableNewlines().

Would this allow additional helpers like openBlockInline to be defined, so I could have an inline block, but not add the trailing newline?

@mtdowling
Copy link
Member Author

I'm not 100% sure what openInlineBlock would do, but it should be possible with this change. There's a test case that does something like that: https://github.com/awslabs/smithy/pull/529/files#diff-07be89dbdaaf4a400e8e226ec79bac02R606

CodeWriter can now configure the character used to start expressions in
the current state, making it easier to generate code for language that
give syntactic meaning to CodeWriter's default expression character,
'$'.
@mtdowling mtdowling merged commit ed2bd63 into master Aug 17, 2020
@kggilmer
Copy link
Contributor

This could potentially affect usage of CodeWriters that only wrote with inline calls.

Anything in particular we need to look out for w/ existing usage of CodeWriter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants