Full function generation - CodeWhisperer

CodeWhisperer's features are becoming a part of Amazon Q Developer. Learn more

Full function generation

CodeWhisperer can generate an entire function based on a comment that you've written. As you finish your comment CodeWhisperer will suggest a function signature. If you accept the suggestion, CodeWhisperer automatically advances your cursor to the next part of the function and makes a suggestion. Even if you enter an additional comment or line of code in between suggestions, CodeWhisperer will refactor based on your input.

Lambda

In the following example, using JavaScript and Lambda, the user generates, and then edits, a full function based on a set of comments.

An example of the full-function generation feature.

In the following image, a user has written a function signature for reading a file from Amazon S3. Amazon CodeWhisperer then suggests a full implementation of the read_from_s3 method.

A screenshot that shows a CodeWhisperer recommendation to complete an entire function that reads an object from an Amazon S3 location.
Note

Sometimes, as in the previous example, CodeWhisperer includes import statements as part of its suggestions. As a best practice, manually move these import statements to the top of your file.

As another example, in the following image, a user has written a function signature. CodeWhisperer then suggests a full implementation of the quicksort method.

A screenshot that shows a CodeWhisperer recommendation for an entire function implementation of the quicksort algorithm.

CodeWhisperer considers past code snippets when making suggestions. In the following image, the user in the previous example has accepted the suggested implementation for quicksort above. The user then writes another function signature for a generic sort method. CodeWhisperer then suggests an implementation based on what has already been written.

A screenshot that shows a CodeWhisperer recommendation for a function implementation based on context.

In the following image, a user has written a comment. Based on this comment, CodeWhisperer then suggests a function signature.

A screenshot that shows a CodeWhisperer recommendation for a binary search function signature based on user code comments.

In the following image, the user in the previous example has accepted the suggested function signature. CodeWhisperer can then suggest a complete implementation of the binary_search function.

A screenshot that shows a CodeWhisperer recommendation for a complete implementation of the binary search algorithm.
AWS Cloud9

The following list contains examples of how CodeWhisperer makes suggestions and advances you through the entire process of creating a function.

  1. In the example below, in Java, a user inputs a comment. CodeWhisperer suggests a function signature.

    After the user accepts that suggestion, CodeWhisperer suggests a function body.

    An example of a function generated from a comment.
  2. In the image below, a user inputs a comment in the body of the function prior to accepting a suggestion from CodeWhisperer. On the following line, CodeWhisperer generates a suggestion based on the comment.

    An example of a function generated from a comment inside an existing block of code.
AWS Toolkit for Visual Studio Code

In the following example using C# and VS Code, CodeWhisperer recommends a full function.

Code editor showing CodeWhisperer suggesting a complete C# function implementation.

In the following example, using TypeScript and VS Code, CodeWhisperer generates a function based on the user's docstrings.

An example of the full function completion feature.
AWS Toolkit for JetBrains

CodeWhisperer can generate an entire function based on a comment that you've written. As you finish your comment, CodeWhisperer will suggest a function signature. If you accept the suggestion, CodeWhisperer automatically advances your cursor to the next part of the function and makes a suggestion. Even if you enter an additional comment or line of code in between suggestions, CodeWhisperer will refactor based on your input.

In the following example, using Python in Pycharm, CodeWhisperer generates both a full function and the corresponding unit test.

An example of the full function completion feature.

The following list contains examples of how CodeWhisperer makes suggestions and advances you through the entire process of creating a function.

  1. In the image below, a user has input a comment. The function signature, located below the comment, is a suggestion from CodeWhisperer.

    alt_text
  2. In the image below, the user has accepted the CodeWhisperer suggestion for a function signature. Accepting the suggestion automatically advanced the cursor and CodeWhisperer has made a new suggestion for the function body.

    alt_text
  3. In the image below, a user input a comment in the body of the function prior to accepting a suggestion from CodeWhisperer. On the following line, CodeWhisperer has generated a new suggestion based on the content of the comment.

    alt_text
SageMaker Studio

In this example using Python and SageMaker Studio, CodeWhisperer recommends a full function after the user types part of the signature.

An example of the full function feature.