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

Add stack trace comment support to code writer #1198

Merged
merged 1 commit into from
Apr 22, 2022
Merged

Conversation

mtdowling
Copy link
Member

It's sometimes useful to know where in a code generator a line of code generated text came from. Enabling stack trace comments will output the last relevant stack trace information caused text to appear in the code writer's output.

MyCustomWriter writer = new MyCustomWriter();
writer.enableStackTraceComments(true); // <- this is new
writer.writeWithNoFormatting("Hello 1");
writer.write("Hello 2");
writer.indent().write("Hello 3. ");
writer.writeInline("Hello 4. ");
writer.writeInline("Hello 5. ");
System.out.println(writer.toString());

Outputs:

/* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1367) */ Hello 1
/* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1368) */ Hello 2
    /* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1369) */ Hello 3. 
    /* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1370) */ Hello 4. /* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1371) */ Hello 5. 

The public API of this feature is just this method:

writer.enableStackTraceComments(true);

This tells the writer to turn on emitting stack trace comments. By default, the writer will emit C/Java-style block comments inline before calls to write, writeInline, writeWithNoFormatting, and writeInlineWithNoFormatting.

But not every language supports inline comments or C/Java-style block comments. So for example, in Python, you could do:

private static final class MyCustomWriter extends AbstractCodeWriter<MyCustomWriter> {
    @Override
    protected String formatWithStackTraceElement(String content, StackTraceElement element, boolean inline) {
        if (inline) {
            return content
        } else {
            return "# " + element + getNewline() + content;
        }
    }
}

The class will, by default, filter out any methods of AbstractCodeWriter and of the class that's extending it. Most implementations will not need to modify how frames are filtered, but this method is protected if you do:

@Override
protected boolean isStackTraceRelevant(StackTraceElement e) {
    return super.isStackTraceRelevant(e) && !e.getClassName().equals("Xyz");
}

It's sometimes useful to know where in a code generator a line of code
generated text came from. Enabling stack trace comments will output the
last relevant stack trace information caused text to appear in the code
writer's output.

```
MyCustomWriter writer = new MyCustomWriter();
writer.enableStackTraceComments(true); // <- this is new
writer.writeWithNoFormatting("Hello 1");
writer.write("Hello 2");
writer.indent().write("Hello 3. ");
writer.writeInline("Hello 4. ");
writer.writeInline("Hello 5. ");
System.out.println(writer.toString());
```

Outputs:

```
/* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1367) */ Hello 1
/* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1368) */ Hello 2
    /* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1369) */ Hello 3.
    /* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1370) */ Hello 4. /* software.amazon.smithy.utils.CodeWriterTest.writesDebugInfoOnEachLine(CodeWriterTest.java:1371) */ Hello 5.
```
@mtdowling mtdowling requested a review from a team as a code owner April 22, 2022 20:25
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.

2 participants