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 DirectedCodegen to make codegen simpler #1167

Merged
merged 4 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
* {@link SmithyIntegration}.
*
* @param <S> The settings object used to configure the generator.
* @param <W> The type of {@link SymbolWriter} used by the generator.
*/
public interface CodegenContext<S> {
public interface CodegenContext<S, W extends SymbolWriter<W, ?>> {
/**
* @return Gets the model being code generated.
*/
Expand All @@ -44,4 +45,16 @@ public interface CodegenContext<S> {
* @return Gets the FileManifest being written to for code generation.
*/
FileManifest fileManifest();

/**
* Get the WriterDelegator used for generating code.
*
* <p>Generates might need other delegators for specific purposes, and it's fine to
* add more methods for those specific purposes. If an implementation uses a specific
* subclass of a WriterDelegator, implementations can override this method to return
* a more specific WriterDelegator type.
*
* @return Returns the writer delegator used by the generator.
*/
WriterDelegator<W> writerDelegator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Set;
import java.util.logging.Logger;

final class IntegrationTopologicalSort<S, I extends SmithyIntegration<S, ?, ?>> {
final class IntegrationTopologicalSort<I extends SmithyIntegration<?, ?, ?>> {

private static final Logger LOGGER = Logger.getLogger(IntegrationTopologicalSort.class.getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @param <C> The CodegenContext value used by the generator.
*/
@SmithyUnstableApi
public interface SmithyIntegration<S, W extends AbstractCodeWriter<W>, C extends CodegenContext<S>> {
public interface SmithyIntegration<S, W extends SymbolWriter<W, ?>, C extends CodegenContext<S, W>> {
/**
* Gets the name of the integration.
*
Expand Down Expand Up @@ -162,14 +162,12 @@ default void customize(C codegenContext) {
* runBefore, and runAfter, and integration names.
*
* @param integrations Integrations to sort.
* @param <S> The type of settings being sorted.
* @param <I> The type of integration to sort.
* @return Returns the sorted integrations.
* @throws IllegalArgumentException If a cycle is found between integrations.
* @throws IllegalArgumentException If multiple integrations share the same name.
*/
static <S, W extends AbstractCodeWriter<W>, C extends CodegenContext<S>, I extends SmithyIntegration<S, W, C>>
List<I> sort(Iterable<I> integrations) {
static <I extends SmithyIntegration<?, ?, ?>> List<I> sort(Iterable<I> integrations) {
return new IntegrationTopologicalSort<>(integrations).sort();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.logging.Logger;
import software.amazon.smithy.utils.AbstractCodeWriter;
import software.amazon.smithy.utils.SmithyUnstableApi;
Expand Down Expand Up @@ -319,37 +318,4 @@ final void addImportReferences(Symbol symbol, SymbolReference.ContextOption... o
}
}
}

/**
* Writes documentation comments.
*
* <p>This method is responsible for setting up the writer to begin
* writing documentation comments. This includes writing any necessary
* opening tokens (e.g., "/*"), adding tokens to the beginning of lines
* (e.g., "*"), sanitizing documentation strings, and writing any
* tokens necessary to close documentation comments (e.g., "*\/").
*
* <p>This method <em>does not</em> automatically escape the expression
* start character ("$" by default). Write calls made by the Runnable
* should either use {@link AbstractCodeWriter#writeWithNoFormatting} or escape
* the expression start character manually.
*
* <p>This method may be overridden as needed.
*
* @param consumer Consumer that accepts the writer and writes documentation.
* @return Returns the writer.
*/
public abstract W writeDocs(Consumer<W> consumer);

/**
* Writes documentation comments from a string.
*
* @param docs Documentation to write.
* @return Returns the writer.
*/
@SuppressWarnings("unchecked")
public final W writeDocs(String docs) {
writeDocs(w -> w.writeWithNoFormatting(docs));
return (W) this;
}
}
Loading