From 59846eac91b85b92dfe04cfc13a2983ea24f00d6 Mon Sep 17 00:00:00 2001 From: Guodong Zhu Date: Sun, 29 Mar 2026 01:36:23 -0400 Subject: [PATCH] fix(plugin): resolve deprecated API warnings and redundant code - Replace `makeRootsChange(Runnable, Boolean, Boolean)` with `makeRootsChange(Runnable, RootsChangeRescanningInfo)` in SymlinkSwitchService - Use explicit `getDirectoryMappings()` call instead of deprecated Kotlin synthetic property accessor in SymlinkSwitchService - Replace deprecated `ActionUtil.invokeAction(action, component, ...)` with `ActionUtil.performAction(action, event)` in WorktreePanel - Remove unnecessary safe calls on smart-cast non-null receivers in ExternalChangeWatcher - Remove redundant `else` branch in exhaustive `when` on sealed class in WorktreePanel - Remove always-true `is String` type checks in TerminalNavigatorTest Co-Authored-By: Claude Opus 4.6 (1M context) --- .../wt/services/ExternalChangeWatcher.kt | 2 +- .../block/wt/services/SymlinkSwitchService.kt | 6 +++-- .../kotlin/com/block/wt/ui/WorktreePanel.kt | 23 +++++++++++++++---- .../terminalnav/TerminalNavigatorTest.kt | 5 ++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/ExternalChangeWatcher.kt b/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/ExternalChangeWatcher.kt index f1ac19e..ad164f6 100644 --- a/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/ExternalChangeWatcher.kt +++ b/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/ExternalChangeWatcher.kt @@ -204,7 +204,7 @@ class ExternalChangeWatcher( // Active worktree symlink changed (scoped to its parent directory) if (fileName == config?.activeWorktree?.fileName?.toString() - && watchedPath == config?.activeWorktree?.parent) return true + && watchedPath == config.activeWorktree.parent) return true // .git/worktrees/ changes — worktree created/removed val gitWorktreesDir = config?.mainRepoRoot?.resolve(".git/worktrees") diff --git a/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/SymlinkSwitchService.kt b/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/SymlinkSwitchService.kt index 545a799..5e27fa8 100644 --- a/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/SymlinkSwitchService.kt +++ b/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/services/SymlinkSwitchService.kt @@ -17,6 +17,7 @@ import com.intellij.openapi.progress.ProgressIndicator import com.intellij.openapi.progress.ProgressManager import com.intellij.openapi.progress.Task import com.intellij.openapi.project.Project +import com.intellij.openapi.project.RootsChangeRescanningInfo import com.intellij.openapi.roots.ex.ProjectRootManagerEx import com.intellij.openapi.vcs.ProjectLevelVcsManager import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager @@ -132,7 +133,8 @@ class SymlinkSwitchService( // Fire synthetic roots-changed event so indexer and module system see the new tree WriteAction.run { - ProjectRootManagerEx.getInstanceEx(project).makeRootsChange({}, false, true) + ProjectRootManagerEx.getInstanceEx(project) + .makeRootsChange({}, RootsChangeRescanningInfo.TOTAL_RESCAN) } ProjectView.getInstance(project).refresh() @@ -147,7 +149,7 @@ class SymlinkSwitchService( indicator?.text = "Updating git state..." app.invokeAndWait({ val vcsManager = ProjectLevelVcsManager.getInstance(project) - vcsManager.setDirectoryMappings(vcsManager.directoryMappings.toList()) + vcsManager.setDirectoryMappings(vcsManager.getDirectoryMappings().toList()) }, ModalityState.defaultModalityState()) withContext(Dispatchers.IO) { val repos = GitRepositoryManager.getInstance(project).repositories diff --git a/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/ui/WorktreePanel.kt b/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/ui/WorktreePanel.kt index 5df6a6f..1aebe1b 100644 --- a/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/ui/WorktreePanel.kt +++ b/wt-jetbrains-plugin/src/main/kotlin/com/block/wt/ui/WorktreePanel.kt @@ -12,8 +12,12 @@ import com.block.wt.services.WorktreeService import com.block.wt.settings.WtPluginSettings import com.intellij.ide.BrowserUtil import com.intellij.openapi.Disposable +import com.intellij.ide.DataManager import com.intellij.openapi.actionSystem.ActionManager import com.intellij.openapi.actionSystem.ActionPlaces +import com.intellij.openapi.actionSystem.ActionUiKind +import com.intellij.openapi.actionSystem.AnAction +import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.DataKey import com.intellij.openapi.actionSystem.DataProvider import com.intellij.openapi.actionSystem.DefaultActionGroup @@ -203,7 +207,7 @@ class WorktreePanel(private val project: Project) : JPanel(BorderLayout()), Data button("Add Context...") { val action = ActionManager.getInstance().getAction("Wt.AddContext") if (action != null) { - ActionUtil.invokeAction(action, this@WorktreePanel, ActionPlaces.TOOLWINDOW_CONTENT, null, null) + invokeAction(action) } }.align(Align.CENTER) } @@ -401,6 +405,18 @@ class WorktreePanel(private val project: Project) : JPanel(BorderLayout()), Data } } + private fun invokeAction(action: AnAction) { + val dataContext = DataManager.getInstance().getDataContext(this@WorktreePanel) + val event = AnActionEvent.createEvent( + dataContext, + action.templatePresentation.clone(), + ActionPlaces.TOOLWINDOW_CONTENT, + ActionUiKind.NONE, + null + ) + ActionUtil.performAction(action, event) + } + private fun handlePRClick(wt: WorktreeInfo) { when (val pr = wt.prInfo) { is PullRequestInfo.Open, @@ -410,14 +426,13 @@ class WorktreePanel(private val project: Project) : JPanel(BorderLayout()), Data // Try to show the PR for the current branch in the Pull Requests tool window val showAction = ActionManager.getInstance().getAction("Github.Pull.Request.Show.In.Toolwindow") if (showAction != null) { - ActionUtil.invokeAction(showAction, this@WorktreePanel, ActionPlaces.TOOLWINDOW_CONTENT, null, null) + invokeAction(showAction) } else { val url = when (pr) { is PullRequestInfo.Open -> pr.url is PullRequestInfo.Draft -> pr.url is PullRequestInfo.Merged -> pr.url is PullRequestInfo.Closed -> pr.url - else -> return } BrowserUtil.browse(url) } @@ -425,7 +440,7 @@ class WorktreePanel(private val project: Project) : JPanel(BorderLayout()), Data is PullRequestInfo.NoPR -> { val createAction = ActionManager.getInstance().getAction("Github.Create.Pull.Request") if (createAction != null) { - ActionUtil.invokeAction(createAction, this@WorktreePanel, ActionPlaces.TOOLWINDOW_CONTENT, null, null) + invokeAction(createAction) } else { val branch = wt.branch ?: return val slug = WorktreeService.getInstance(project).repoSlug ?: return diff --git a/wt-jetbrains-plugin/src/test/kotlin/com/block/wt/experiment/terminalnav/TerminalNavigatorTest.kt b/wt-jetbrains-plugin/src/test/kotlin/com/block/wt/experiment/terminalnav/TerminalNavigatorTest.kt index bfb8295..7ba08e0 100644 --- a/wt-jetbrains-plugin/src/test/kotlin/com/block/wt/experiment/terminalnav/TerminalNavigatorTest.kt +++ b/wt-jetbrains-plugin/src/test/kotlin/com/block/wt/experiment/terminalnav/TerminalNavigatorTest.kt @@ -37,8 +37,7 @@ class TerminalNavigatorTest { // PID 999999999 should not exist — should not crash val tty = TerminalNavigator.resolveTty(999999999L) // Result is null when PID doesn't exist (ps returns no output or error) - // This test just verifies no exception is thrown - assertTrue(tty == null || tty is String) + // This test just verifies no exception is thrown — reaching here means success } // --- Terminal owner resolution --- @@ -53,7 +52,7 @@ class TerminalNavigatorTest { fun testGetProcessCommForNonexistentPid() { // Should not crash for non-existent PID val comm = TerminalNavigator.getProcessComm(999999999L) - assertTrue(comm == null || comm is String) + // This test just verifies no exception is thrown — reaching here means success } @Test