Skip to content

refactor(mpp-idea): reuse CodeReviewViewModel from mpp-ui#9

Merged
phodal merged 1 commit intomasterfrom
refactor/reuse-code-review-viewmodel
Nov 30, 2025
Merged

refactor(mpp-idea): reuse CodeReviewViewModel from mpp-ui#9
phodal merged 1 commit intomasterfrom
refactor/reuse-code-review-viewmodel

Conversation

@phodal
Copy link
Member

@phodal phodal commented Nov 30, 2025

Summary

This PR refactors IdeaCodeReviewViewModel to extend the common CodeReviewViewModel from mpp-ui, eliminating code duplication and ensuring feature parity.

Changes

IdeaCodeReviewViewModel.kt

  • Simplified from ~670 lines to ~60 lines
  • Now extends CodeReviewViewModel from mpp-ui
  • Creates a Workspace adapter from IntelliJ's Project
  • All features inherited from base class

IdeaCodeReviewModels.kt

  • Deleted - Now using shared models from mpp-ui:
    • CodeReviewState instead of IdeaCodeReviewState
    • CommitInfo instead of IdeaCommitInfo
    • DiffFileInfo instead of IdeaDiffFileInfo
    • AIAnalysisProgress instead of IdeaAIAnalysisProgress
    • AnalysisStage instead of IdeaAnalysisStage

IdeaCodeReviewContent.kt

  • Updated to use shared model types from mpp-ui

Build Configuration

  • Fixed dependency configuration in build.gradle.kts
  • Fixed dependency substitution rules in settings.gradle.kts

Features Now Available (inherited from CodeReviewViewModel)

  • loadCommitHistory() / loadMoreCommits() - Load git commits with infinite scroll
  • selectCommit() - Select commits and load their diffs
  • startAnalysis() - Run AI code review analysis
  • generateModificationPlan() - Generate AI modification plan
  • generateFixes() / proceedToGenerateFixes() - Generate AI fixes
  • analyzeModifiedCode() - Analyze modified code ranges
  • runLint() - Run linting on modified files
  • findRelatedTests() - Find related test files
  • openFile() / closeFileViewer() - File viewer support
  • loadIssueForCommit() - Load issue info from trackers
  • refresh() - Refresh data

Testing

  • Build passes: ./gradlew :mpp-idea:build --no-configuration-cache

Pull Request opened by Augment Code with guidance from the PR author

Summary by CodeRabbit

  • Refactor
    • Restructured code review module to use shared base architecture and unified data types for improved code maintainability.
    • Simplified ViewModel implementation through inheritance of base functionality.
    • Updated Kotlin Multiplatform Project dependency management and repository configuration for better modularity.

✏️ Tip: You can customize this high-level summary in your review settings.

- Simplify IdeaCodeReviewViewModel by extending CodeReviewViewModel from mpp-ui
- Remove duplicate IdeaCodeReviewModels.kt, use shared models from mpp-ui
- Update IdeaCodeReviewContent.kt to use shared model types
- Fix dependency configuration in build.gradle.kts and settings.gradle.kts
- All CodeReview features now inherited from base class:
  - Plan generation (generateModificationPlan)
  - Fix generation (generateFixes, proceedToGenerateFixes)
  - Lint analysis (runLint, analyzeModifiedCode)
  - Issue tracking (loadIssueForCommit)
  - Test discovery (findRelatedTests)
  - File viewer support (openFile, closeFileViewer)
@coderabbitai
Copy link

coderabbitai bot commented Nov 30, 2025

Walkthrough

This PR refactors the mpp-idea module to use centralized Maven artifacts parameterized by mppVersion, removes IDE-specific data models in favor of shared cross-module types, and consolidates the CodeReviewViewModel to extend a shared base class instead of implementing its own state management logic.

Changes

Cohort / File(s) Summary
Gradle and Dependency Configuration
mpp-idea/build.gradle.kts, mpp-idea/settings.gradle.kts
Added mavenLocal() repository and updated dependency coordinates: cc.unitmesh.devins:mpp-ui-jvmAutoDev-Intellij:mpp-ui:$mppVersion, cc.unitmesh.devins:mpp-core-jvmcc.unitmesh:mpp-core:$mppVersion. Updated settings.gradle substitutions to use new coordinates and added substitutions for mpp-codegraph and mpp-viewer modules.
Code Review UI Refactoring
mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewContent.kt
Refactored UI composables to use shared types (CommitInfo, DiffFileInfo, AnalysisStage, AIAnalysisProgress) instead of Idea-prefixed variants. Updated progress state handling, status text logic, and loading indicator branching. Renamed viewModel method from selectCommits(setOf(index)) to selectCommit(index).
Code Review Models Removal
mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewModels.kt
Deleted entire file containing Idea-specific data model definitions: IdeaCodeReviewState, IdeaCommitInfo, IdeaDiffFileInfo, IdeaAIAnalysisProgress, and IdeaAnalysisStage enum.
Code Review ViewModel Refactoring
mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewViewModel.kt
Rewrote class to extend CodeReviewViewModel base class, removing internal state management and Git/diff loading logic. Added JewelRenderer and Logger properties. Introduced companion object with createWorkspaceFromProject() helper. Lifecycle and data loading now delegated to base class.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • IdeaCodeReviewViewModel.kt: Verify that createWorkspaceFromProject() correctly initializes the workspace and that all previous state management functionality is properly delegated to the base CodeReviewViewModel class. Check disposal and lifecycle handling.
  • IdeaCodeReviewContent.kt: Confirm all Idea-prefixed types are correctly mapped to their shared equivalents and that UI behavior remains unchanged, particularly the progress state handling and method call signature change (selectCommitsselectCommit).
  • Gradle/Settings configuration: Validate Maven coordinates and substitution rules are correct and that the version variable is properly resolved across modules.

Possibly related PRs

Poem

🐰 Hops through the gradle files with glee,
Models removed, now shared they'll be!
ViewModel extends, no more lonely code,
Dependencies centralized down the road! 🎯

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: refactoring IdeaCodeReviewViewModel to extend and reuse the shared CodeReviewViewModel from mpp-ui module.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/reuse-code-review-viewmodel

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 1 suggestions posted.

Comment augment review to trigger a new review at any time.

}
}

/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this ViewModel implements Disposable, consider ensuring any coroutines/resources owned by the base CodeReviewViewModel are cancelled or cleared on dispose (or pass a scope tied to the project/component lifecycle) to avoid leaks after the toolwindow closes. (Guideline: no_memory_leaks)

🤖 Was this useful? React with 👍 or 👎

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewViewModel.kt (2)

4-6: Double‑check lifecycle wiring between IntelliJ, CoroutineScope, and CodeReviewViewModel

The class now delegates almost everything to CodeReviewViewModel and only passes workspace = createWorkspaceFromProject(project) into the base constructor. The IntelliJ‑provided coroutineScope is stored but not fed into the base class or otherwise used here.

If CodeReviewViewModel supports injecting an external scope (or other lifecycle hooks), you probably want it to run on the IntelliJ/plugin scope rather than on an internally created default scope, so its jobs are tied to the plugin lifecycle. If the base class already manages its own scoped lifecycle independently, consider dropping the coroutineScope parameter from this class to avoid confusion about which scope actually controls cleanup.

Also, the new jewelRenderer and logger look appropriate for IntelliJ‑specific adaptation.

Also applies to: 24-29, 31-31, 33-34


52-58: Consider explicitly tying coroutine scope cancellation to dispose() (or clarify that base VM handles it)

dispose() currently only logs and the comment says “The parent class cleanup will happen when the scope is cancelled”, but this class doesn’t cancel any scope and doesn’t call into the base ViewModel in dispose().

If there is an IntelliJ‑managed CoroutineScope that should bound the lifetime of CodeReviewViewModel work, consider either:

  • Passing that scope into CodeReviewViewModel (if supported) and cancelling it from the IntelliJ lifecycle (e.g., here in dispose()), or
  • Explicitly delegating to a base‑class cleanup method if one exists.

If the base class already binds its scope to an appropriate lifecycle (e.g., internal viewModelScope that is closed elsewhere), it might be clearer to update the comment and/or remove the unused coroutineScope parameter so dispose() doesn’t suggest extra cleanup that never happens here.

Also applies to: 24-26

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c30107e and c01f9dd.

📒 Files selected for processing (5)
  • mpp-idea/build.gradle.kts (3 hunks)
  • mpp-idea/settings.gradle.kts (1 hunks)
  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewContent.kt (9 hunks)
  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewModels.kt (0 hunks)
  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewViewModel.kt (1 hunks)
💤 Files with no reviewable changes (1)
  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewModels.kt
🧰 Additional context used
📓 Path-based instructions (1)
**/*.kt

📄 CodeRabbit inference engine (AGENTS.md)

Use expect/actual for platform-specific code in KMP projects (e.g., file I/O on JVM/JS/Wasm)

Files:

  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewContent.kt
  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewViewModel.kt
🧠 Learnings (6)
📚 Learning: 2025-11-30T02:30:49.805Z
Learnt from: CR
Repo: phodal/auto-dev-sketch PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-30T02:30:49.805Z
Learning: For IntelliJ IDEA Plugin build, use `cd mpp-idea && ../gradlew compileKotlin` instead of `./gradlew :mpp-idea:compileKotlin` from root

Applied to files:

  • mpp-idea/build.gradle.kts
  • mpp-idea/settings.gradle.kts
📚 Learning: 2025-11-30T02:30:49.805Z
Learnt from: CR
Repo: phodal/auto-dev-sketch PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-30T02:30:49.805Z
Learning: Applies to {mpp-core,mpp-ui}/**/*.kt : Check export first in mpp-core and mpp-ui; if some functions are not working well with CLI (TypeScript), verify JsExport usage

Applied to files:

  • mpp-idea/build.gradle.kts
  • mpp-idea/settings.gradle.kts
📚 Learning: 2025-11-30T02:30:49.805Z
Learnt from: CR
Repo: phodal/auto-dev-sketch PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-30T02:30:49.805Z
Learning: Applies to **/src/{androidMain,desktopMain}/**/*.kt : For Compose (Desktop/Android), use `AutoDevColors` from `cc.unitmesh.devins.ui.compose.theme` or `MaterialTheme.colorScheme`

Applied to files:

  • mpp-idea/build.gradle.kts
  • mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewContent.kt
📚 Learning: 2025-11-30T02:30:49.805Z
Learnt from: CR
Repo: phodal/auto-dev-sketch PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-30T02:30:49.805Z
Learning: Use `cd mpp-idea && ../gradlew test --tests "cc.unitmesh.devins.idea.renderer.JewelRendererTest"` for running standalone JUnit 5 tests that do not require IntelliJ Platform

Applied to files:

  • mpp-idea/build.gradle.kts
  • mpp-idea/settings.gradle.kts
📚 Learning: 2025-11-30T02:30:49.805Z
Learnt from: CR
Repo: phodal/auto-dev-sketch PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-30T02:30:49.805Z
Learning: Use `./gradlew :mpp-ui:generateI18n4kFiles` for internationalization

Applied to files:

  • mpp-idea/settings.gradle.kts
📚 Learning: 2025-11-30T02:30:49.805Z
Learnt from: CR
Repo: phodal/auto-dev-sketch PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-11-30T02:30:49.805Z
Learning: Do not run `./gradlew clean`. Clean only the specific module instead (e.g., `./gradlew :mpp-core:clean`)

Applied to files:

  • mpp-idea/settings.gradle.kts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build (241)
  • GitHub Check: Build (223)
🔇 Additional comments (10)
mpp-idea/build.gradle.kts (2)

27-39: Verify repository configuration and artifact resolution strategy.

Adding mavenLocal() (line 28) suggests artifacts are published to the local Maven repository. Please clarify:

  1. How are the mpp-ui and mpp-core artifacts published to mavenLocal? This should be documented or automated in the parent build.
  2. Is mavenLocal() necessary, or can this use composite builds instead (like the includeBuild("..") approach in settings.gradle.kts)?

45-47: No group ID declaration needed in mpp-ui—the review comment is based on a flawed assumption.

The review incorrectly assumes mpp-ui should declare group = "AutoDev-Intellij" in its build.gradle.kts. However:

  1. mpp-ui is not a published library: It's a desktop/WASM application with no publishing configuration. The group ID is never meant to be declared there.
  2. Substitution rules are correct: The mpp-idea/settings.gradle.kts correctly maps the Maven coordinate AutoDev-Intellij:mpp-ui to the local project :mpp-ui. The group ID "AutoDev-Intellij" is used only in the substitution mapping, not as a project declaration.
  3. mpp-core is correctly configured: Confirmed at line 16 of mpp-core/build.gradle.kts: group = "cc.unitmesh"
  4. Build succeeds because configuration is correct: The dependency substitutions work as intended.

No action needed.

mpp-idea/settings.gradle.kts (2)

21-26: Documentation of group IDs is clear.

The comments explaining where each group ID comes from are helpful for maintainability. However, these should be verified against the actual module definitions (see the verification script in the prior comment).


30-33: The substitution rules for mpp-codegraph and mpp-viewer are necessary and correct.

These modules are not over-specified. mpp-codegraph is a direct dependency of both mpp-ui and mpp-core, while mpp-viewer is a direct dependency of mpp-ui. When substituting published artifacts with local projects in Gradle, you must also substitute their transitive dependencies to ensure the IDE plugin uses local versions instead of fetching published artifacts. The existing substitution rules are a standard and required pattern for multi-module projects.

mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewViewModel.kt (2)

15-22: KDoc clearly describes the new delegation to CodeReviewViewModel

The documentation accurately reflects the new responsibilities (workspace creation, renderer, Disposable) and that core behavior lives in the shared base ViewModel. No changes needed here.


36-48: Workspace creation helper looks sound for IntelliJ projects

Using project.basePath and project.name to build a Workspace via DefaultWorkspace.create (or createEmpty when basePath is null) is a reasonable mapping from IntelliJ Project → shared Workspace. This keeps IntelliJ‑specific concerns localized in the IDEA module while reusing the shared Workspace abstraction.

No functional issues spotted here.

Also applies to: 4-6

mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/codereview/IdeaCodeReviewContent.kt (4)

19-22: LGTM! Shared types properly imported.

The migration from Idea-prefixed types to shared mpp-ui types is clean and aligns with the PR's goal of removing code duplication.


74-74: LGTM! Function signatures correctly updated.

The type changes from Idea-prefixed types (IdeaCommitInfo, IdeaDiffFileInfo, IdeaAIAnalysisProgress) to shared types (CommitInfo, DiffFileInfo, AIAnalysisProgress) are mechanical and correct.

Also applies to: 129-129, 179-179, 258-258, 318-318


340-340: LGTM! AnalysisStage enum integration is correct.

The usage of the shared AnalysisStage enum values (IDLE, RUNNING_LINT, ANALYZING_LINT, GENERATING_PLAN, WAITING_FOR_USER_INPUT, GENERATING_FIX, COMPLETED, ERROR) is consistent and the control flow logic for button states and loading indicators is sound.

Also applies to: 362-369, 372-374


43-43: The selectCommit API usage is correct—no changes needed.

The selectCommit(index) call at line 43 properly uses single-select behavior. The method signature selectCommit(index: Int, toggle: Boolean = false) defaults to toggle=false, which sets newSelection = setOf(index), replacing the entire selection with a single commit. The state maintains Set<Int> for consistency with the shared ViewModel design, but the IDEA UI intentionally enforces single-selection by omitting the toggle parameter. This differs from CodeReviewSideBySideView in the common code, which explicitly passes toggle = isToggle to support multi-select when needed—both patterns are correct for their respective contexts.

@phodal phodal merged commit 7ff8db9 into master Nov 30, 2025
5 of 6 checks passed
@phodal phodal deleted the refactor/reuse-code-review-viewmodel branch December 2, 2025 10:30
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.

1 participant

Comments