Skip to content

Conversation

@yhakbar
Copy link
Collaborator

@yhakbar yhakbar commented Mar 8, 2025

Closes #16

Summary by CodeRabbit

  • New Features
    • The language server now offers automatic document formatting. This update allows users to format text documents seamlessly, enhancing code readability and consistency.
    • Formatting requests are processed more efficiently, ensuring a smoother and more reliable editing experience.

@coderabbitai
Copy link

coderabbitai bot commented Mar 8, 2025

Walkthrough

The changes integrate document formatting support into the LSP server. The server’s initialization now advertises formatting capabilities. New types have been introduced to handle formatting requests and responses, and a new method is added to process these requests. Additionally, some helper functions and function renames standardize responses. Finally, the message handler in the main file has been extended to parse and dispatch formatting requests.

Changes

File(s) Change Summary
internal/lsp/initialize.go, internal/lsp/textdocument_format.go Updated the server’s capabilities by adding the DocumentFormattingProvider flag and introduced new types (FormatRequest and FormatResponse) for handling document formatting requests and responses.
internal/tg/state.go Renamed empty response functions (e.g., to newEmptyHoverResponse) and added a new method TextDocumentFormatting along with the helper function getEndOfDocument to process formatting requests.
main.go Extended the message handler to include a new case for parsing and handling document formatting requests using the new formatting logic.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Main
    participant State
    participant Formatter

    Client->>Main: Send textDocument/formatting request (FormatRequest)
    Main->>State: Call TextDocumentFormatting(logger, id, docURI)
    State->>State: Retrieve configuration for the document
    alt Configuration is valid
        State->>Formatter: Format document (using hclwrite.Format)
    else Configuration is nil
        State->>State: Return an empty FormatResponse
    end
    State-->>Main: Return FormatResponse (with TextEdit result)
    Main->>Client: Write response back
Loading

Poem

In our code, a new formatting light,
Functions renamed to shine a bit more bright.
New types and helpers join the show,
Document formatting ready to flow.
Code and logs dance in harmony—hooray!
🎉 Formatting features here to stay!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c464b6 and 532cf82.

📒 Files selected for processing (4)
  • internal/lsp/initialize.go (1 hunks)
  • internal/lsp/textdocument_format.go (1 hunks)
  • internal/tg/state.go (6 hunks)
  • main.go (1 hunks)
🔇 Additional comments (9)
internal/lsp/initialize.go (1)

32-32: Nice addition of the formatting capability!

Adding DocumentFormattingProvider: true to the server capabilities is exactly what's needed to let clients know that this server can handle document formatting requests. This is a clean implementation that follows the LSP specification.

internal/lsp/textdocument_format.go (2)

5-8: LGTM - Clean request structure definition

The FormatRequest structure follows the same pattern as other requests in the codebase, properly embedding the base Request type and including the necessary formatting parameters.


10-13: LGTM - Well-defined response structure

The FormatResponse structure is well-designed, consistent with other response types, and correctly uses protocol.TextEdit for returning formatting changes.

main.go (1)

208-225: Formatting handler implementation looks good

Your implementation follows the same pattern as the other request handlers, which makes the code consistent and easy to understand. The formatting request is properly parsed, logged, and processed through the state object.

internal/tg/state.go (5)

98-98: Great function rename for better consistency

Changing from buildEmptyHoverResponse to newEmptyHoverResponse makes the naming more consistent with Go conventions. The "new" prefix is typically used for constructor-like functions in Go, which is exactly what this is.

Also applies to: 105-105, 109-109, 113-113, 137-137, 140-147


171-171: Consistent naming improvement

Similar to the hover response, renaming from buildEmptyDefinitionResponse to newEmptyDefinitionResponse improves naming consistency throughout the codebase.

Also applies to: 183-183, 228-228, 231-245


261-293: Formatting implementation looks good

The TextDocumentFormatting method follows the same pattern as other methods in this file. It correctly checks for a valid config before attempting to format, and uses the built-in hclwrite.Format function to do the actual formatting work.


295-303: LGTM - Consistent empty response handling

Following the pattern established with hover and definition responses, this empty format response function provides a clean way to handle the case when formatting can't be performed.


305-312: Consider edge case with empty last line

The function looks good overall, but there's a potential edge case. If the document ends with a newline character, the last line in the lines array will be empty. This might not cause issues in practice since you're just getting its length, but it's worth keeping in mind.

A more robust approach might be to iterate backward through the lines array to find the last non-empty line, but the current implementation should work fine for most real-world documents.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@yhakbar yhakbar enabled auto-merge March 8, 2025 16:54
@yhakbar yhakbar disabled auto-merge March 8, 2025 16:58
@yhakbar yhakbar merged commit c74854d into main Mar 8, 2025
5 checks passed
@yhakbar yhakbar deleted the 16/feat/add-support-for-formatting branch March 8, 2025 16:58
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.

Add support for formatting

2 participants