> ## Documentation Index
> Fetch the complete documentation index at: https://codegeninc-codegen-bot-sdk-docs-2-0.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Comments and Docstrings

Codegen enables reading, modifying, and manipulating comments and docstrings while preserving proper formatting.

This guide describes proper usage of the following classes:

* [Comment](/api-reference/core/Comment) - Represents a single comment.
* [CommentGroup](/api-reference/core/CommentGroup) - Represents a group of comments.

## Accessing with Comments

Comments can be accessed through any symbol or directly from code blocks. Each comment is represented by a `Comment` object that provides access to both the raw source and parsed text:

```python
# Find all comments in a file
file = codebase.get_file("my_file.py")
for comment in file.code_block.comments:
    print(comment.text)

# Access comments associated with a symbol
symbol = file.get_symbol("my_function")
if symbol.comment:
    print(symbol.comment.text)  # Comment text without delimiters
    print(symbol.comment.source)  # Full comment including delimiters

# Access inline comments
if symbol.inline_comment:
    print(symbol.inline_comment.text)

# Accessing all comments in a function
for comment in symbol.code_block.comments:
    print(comment.text)
```

### Editing Comments

Comments can be modified using the `edit_text()` method, which handles formatting and delimiters automatically:

```python
# Edit a regular comment
symbol.comment.edit_text("Updated comment text")

# Edit an inline comment
symbol.set_inline_comment("New inline comment")
```

### Comment Groups

Multiple consecutive comments are automatically grouped into a `CommentGroup`, which can be edited as a single unit:

```python
# Original comments:
# First line
# Second line
# Third line

comment_group = symbol.comment
print(comment_group.text)  # "First line\nSecond line\nThird line"

# Edit the entire group at once
comment_group.edit_text("New first line\nNew second line")
```

## Working with Docstrings

Docstrings are special comments that document functions, classes, and modules. Codegen provides similar APIs for working with docstrings:

```python
function = file.get_symbol("my_function")
if function.docstring:
    print(function.docstring.text)  # Docstring content
    print(function.docstring.source)  # Full docstring with delimiters
```

### Adding Docstrings

You can add docstrings to any symbol that supports them:

```python
# Add a single-line docstring
function.set_docstring("A brief description")

# Add a multi-line docstring
function.set_docstring("""
    A longer description that
    spans multiple lines.

    Args:
        param1: Description of first parameter
""")
```

### Language-Specific Formatting

Codegen automatically handles language-specific docstring formatting:

```python
# Python: Uses triple quotes
def my_function():
    """Docstring is formatted with triple quotes."""
    pass
```

```typescript
// TypeScript: Uses JSDoc style
function myFunction() {
  /** Docstring is formatted as JSDoc */
}
```

### Editing Docstrings

Like comments, docstrings can be modified while preserving formatting:

```python
# Edit a docstring
function.docstring.edit_text("Updated documentation")

# Edit a multi-line docstring
function.docstring.edit_text("""
    Updated multi-line documentation
    that preserves indentation and formatting.
""")
```

## Comment Operations

Codegen provides utilities for working with comments at scale. For example, you can update or remove specific types of comments across your codebase:

```python
# Example: Remove eslint disable comments for a specific rule
for file in codebase.files:
    for comment in file.code_block.comments:
        if "eslint-disable" in comment.source:
            # Check if comment disables specific rule
            if "@typescript-eslint/no-explicit-any" in comment.text:
                comment.remove()
```

<Note>
  When editing multi-line comments or docstrings, Codegen automatically handles
  indentation and maintains the existing comment style.
</Note>

## Special APIs and AI Integration

### Google Style Docstrings

Codegen supports Google-style docstrings and can handle their specific formatting, using the [CommentGroup.to\_google\_docstring(...)](/api-reference/core/CommentGroup#to-google-docstring) method.

```python
# Edit while preserving Google style
symbol_a = file.get_symbol("SymbolA")
func_b = symbol_a.get_method("funcB")
func_b.docstring.to_google_docstring(func_b)
```

### Using AI for Documentation

Codegen integrates with LLMs to help generate and improve documentation. You can use the [Codebase.ai(...)](/api-reference/core/Codebase#ai) method to:

* Generate comprehensive docstrings
* Update existing documentation
* Convert between documentation styles
* Add parameter descriptions

```python
# Generate a docstring using AI
function = codebase.get_function("my_function")

new_docstring = codebase.ai(
    "Generate a comprehensive docstring in Google style",
    target=function
    context={
        # provide additional context to the LLM
        'usages': function.usages,
        'dependencies': function.dependencies
    }
)
function.set_docstring(new_docstring)
```

<Tip>
  Learn more about AI documentation capabilities in our [Documentation
  Guide](/tutorials/creating-documentation) and [LLM Integration
  Guide](/building-with-codegen/calling-out-to-llms).
</Tip>

### Documentation Coverage

You can analyze and improve documentation coverage across your codebase:

```python
# Count documented vs undocumented functions
total = 0
documented = 0
for function in codebase.functions:
    total += 1
    if function.docstring:
        documented += 1

coverage = (documented / total * 100) if total > 0 else 0
print(f"Documentation coverage: {coverage:.1f}%")
```

<Note>
  Check out the [Documentation Guide](/tutorials/creating-documentation) for
  more advanced coverage analysis and bulk documentation generation.
</Note>
