Skip to content

Commit

Permalink
SLI-1589 Investigate and improve UI thread usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel committed Sep 17, 2024
1 parent 932a086 commit ad40e0a
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static VirtualFile getSelectedFile(Project project) {
if (project.isDisposed()) {
return null;
}
ApplicationManager.getApplication().assertIsDispatchThread();
var editorManager = FileEditorManager.getInstance(project);

var editor = editorManager.getSelectedTextEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void actionPerformed(AnActionEvent e) {
}

public Collection<PsiFile> findFiles(Project project, VirtualFile[] files) {
ApplicationManager.getApplication().assertReadAccessAllowed();
var psiManager = PsiManager.getInstance(project);
var psiFiles = new ArrayList<PsiFile>(files.length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.sonarlint.intellij.analysis.AnalysisStatus
import org.sonarlint.intellij.analysis.AnalysisSubmitter
import org.sonarlint.intellij.common.util.SonarLintUtils
import org.sonarlint.intellij.common.util.SonarLintUtils.getService
import org.sonarlint.intellij.config.Settings
import org.sonarlint.intellij.config.project.ExclusionItem
import org.sonarlint.intellij.messages.ProjectConfigurationListener
import org.sonarlint.intellij.trigger.TriggerType
import org.sonarlint.intellij.ui.UiUtils.Companion.runOnUiThread
import org.sonarlint.intellij.util.SonarLintAppUtils
import org.sonarlint.intellij.util.runOnPooledThread

Expand Down Expand Up @@ -64,11 +63,8 @@ class ExcludeFileAction : AbstractSonarAction {
if (newExclusions.isNotEmpty()) {
exclusions.addAll(newExclusions)
settings.fileExclusions = exclusions
SonarLintUtils.getService(project, AnalysisSubmitter::class.java).autoAnalyzeOpenFiles(TriggerType.CONFIG_CHANGE)
runOnUiThread(project) {
val projectListener = project.messageBus.syncPublisher(ProjectConfigurationListener.TOPIC)
projectListener.changed(settings)
}
getService(project, AnalysisSubmitter::class.java).autoAnalyzeOpenFiles(TriggerType.CONFIG_CHANGE)
project.messageBus.syncPublisher(ProjectConfigurationListener.TOPIC).changed(settings)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarlint.intellij.actions;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
Expand Down Expand Up @@ -78,11 +79,9 @@ public SonarLintToolWindow(Project project) {
project.getMessageBus().connect().subscribe(ProjectBindingListenerKt.getPROJECT_BINDING_TOPIC(), this);
}

/**
* Must run in EDT
*/
public void openReportTab(AnalysisResult analysisResult) {
this.<ReportPanel>openTab(SonarLintToolWindowFactory.REPORT_TAB_TITLE, panel -> panel.updateFindings(analysisResult));
runOnUiThread(project, ModalityState.stateForComponent(getToolWindow().getComponent()),
() -> this.<ReportPanel>openTab(SonarLintToolWindowFactory.REPORT_TAB_TITLE, panel -> panel.updateFindings(analysisResult)));
}

public void clearReportTab() {
Expand Down Expand Up @@ -131,7 +130,6 @@ private <T> void openTab(String displayName, Consumer<T> tabPanelConsumer) {
}

private <T> ToolWindow updateTab(String displayName, Consumer<T> tabPanelConsumer) {
ApplicationManager.getApplication().assertIsDispatchThread();
var toolWindow = getToolWindow();
if (toolWindow != null) {
var contentManager = toolWindow.getContentManager();
Expand All @@ -156,16 +154,10 @@ public void filterTaintVulnerabilityTab(boolean isResolved) {
}
}

/**
* Must run in EDT
*/
public void openCurrentFileTab() {
openTab(SonarLintToolWindowFactory.CURRENT_FILE_TAB_TITLE);
runOnUiThread(project, ModalityState.stateForComponent(getToolWindow().getComponent()), () -> openTab(SonarLintToolWindowFactory.CURRENT_FILE_TAB_TITLE));
}

/**
* Must run in EDT
*/
public void openTaintVulnerabilityTab() {
ApplicationManager.getApplication().assertIsDispatchThread();
var toolWindow = getToolWindow();
Expand Down Expand Up @@ -469,6 +461,6 @@ private Content getTaintVulnerabilitiesContent() {

@Override
public void bindingChanged() {
runOnUiThread(project, this::refreshViews);
refreshViews();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class OnTheFlyFindingsHolder(private val project: Project) : FileEditorManagerLi
runOnUiThread(project) {
updateCurrentFileTab()
updateSecurityHotspots()
getService(project, CodeAnalyzerRestarter::class.java).refreshFiles(findings.onlyFor(openedFiles).filesInvolved)
}
getService(project, CodeAnalyzerRestarter::class.java).refreshFiles(findings.onlyFor(openedFiles).filesInvolved)
}

fun updateViewsWithNewIssues(module: Module, raisedIssues: Map<URI, List<RaisedIssueDto>>) {
Expand All @@ -90,8 +90,8 @@ class OnTheFlyFindingsHolder(private val project: Project) : FileEditorManagerLi
selectedFile = SonarLintUtils.getSelectedFile(project)
}
updateCurrentFileTab()
getService(project, CodeAnalyzerRestarter::class.java).refreshFiles(issues.keys)
}
getService(project, CodeAnalyzerRestarter::class.java).refreshFiles(issues.keys)
}

fun updateViewsWithNewSecurityHotspots(module: Module, raisedSecurityHotspots: Map<URI, List<RaisedHotspotDto>>) {
Expand All @@ -108,8 +108,8 @@ class OnTheFlyFindingsHolder(private val project: Project) : FileEditorManagerLi
selectedFile = SonarLintUtils.getSelectedFile(project)
}
updateSecurityHotspots()
getService(project, CodeAnalyzerRestarter::class.java).refreshFiles(securityHotspots.keys)
}
getService(project, CodeAnalyzerRestarter::class.java).refreshFiles(securityHotspots.keys)
}

override fun selectionChanged(event: FileEditorManagerEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import org.sonarlint.intellij.analysis.AnalysisCallback
import org.sonarlint.intellij.analysis.AnalysisResult
import org.sonarlint.intellij.common.util.SonarLintUtils
import org.sonarlint.intellij.editor.CodeAnalyzerRestarter
import org.sonarlint.intellij.ui.UiUtils.Companion.runOnUiThread

class ShowReportCallable(private val project: Project) : AnalysisCallback {

Expand All @@ -45,10 +44,8 @@ class ShowReportCallable(private val project: Project) : AnalysisCallback {

private fun showReportTab() {
results?.let {
runOnUiThread(project) {
SonarLintUtils.getService(project, SonarLintToolWindow::class.java).openReportTab(it)
SonarLintUtils.getService(project, CodeAnalyzerRestarter::class.java).refreshOpenFiles()
}
SonarLintUtils.getService(project, SonarLintToolWindow::class.java).openReportTab(it)
SonarLintUtils.getService(project, CodeAnalyzerRestarter::class.java).refreshOpenFiles()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.sonarlint.intellij.analysis.OnTheFlyFindingsHolder;
import org.sonarlint.intellij.common.util.SonarLintUtils;

import static org.sonarlint.intellij.ui.UiUtils.runOnUiThread;

public class ShowUpdatedCurrentFileCallable implements AnalysisCallback {
private final Project project;
private final UpdateOnTheFlyFindingsCallable updateOnTheFlyFindingsCallable;
Expand All @@ -54,9 +52,7 @@ public void onSuccess(AnalysisResult analysisResult) {
}

private void showCurrentFileTab() {
runOnUiThread(project, () -> {
var toolWindow = SonarLintUtils.getService(project, SonarLintToolWindow.class);
toolWindow.openCurrentFileTab();
});
var toolWindow = SonarLintUtils.getService(project, SonarLintToolWindow.class);
toolWindow.openCurrentFileTab();
}
}
8 changes: 2 additions & 6 deletions src/main/java/org/sonarlint/intellij/core/BackendService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ class BackendService : Disposable {

private fun handleSloopExited() {
ProjectManager.getInstance().openProjects.forEach { project ->
runOnUiThread(project) {
getService(project, SonarLintToolWindow::class.java).refreshViews()
}
getService(project, SonarLintToolWindow::class.java).refreshViews()
}
projectLessNotification(
null,
Expand Down Expand Up @@ -784,9 +782,7 @@ class BackendService : Disposable {

private fun catchUpWithBackend(rpcServer: SonarLintRpcServer) {
ProjectManager.getInstance().openProjects.forEach { project ->
runOnUiThread(project) {
getService(project, SonarLintToolWindow::class.java).refreshViews()
}
getService(project, SonarLintToolWindow::class.java).refreshViews()

val binding = getService(project, ProjectBindingManager::class.java).binding
rpcServer.configurationService.didAddConfigurationScopes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonarlint.intellij.editor

import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.Service
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -55,6 +56,7 @@ class CodeAnalyzerRestarter @NonInjectable internal constructor(private val myPr
if (!virtualFile.isValid) {
return null
}
ApplicationManager.getApplication().assertReadAccessAllowed()
return PsiManager.getInstance(myProject).findFile(virtualFile)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.sonarlint.intellij.finding.FindingContext;
import org.sonarlint.intellij.finding.LiveFinding;

import static org.sonarlint.intellij.ui.UiUtils.runOnUiThread;

public class ShowLocationsIntentionAction implements IntentionAction, PriorityAction, Iconable {
private final LiveFinding finding;
private final FindingContext context;
Expand All @@ -60,7 +58,7 @@ public ShowLocationsIntentionAction(LiveFinding finding, FindingContext context)
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
SonarLintUtils.getService(project, EditorDecorator.class).highlightFinding(finding);
var sonarLintToolWindow = SonarLintUtils.getService(project, SonarLintToolWindow.class);
runOnUiThread(project, () -> sonarLintToolWindow.showFindingLocations(finding));
sonarLintToolWindow.showFindingLocations(finding);
}

@Override public boolean startInWriteAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import org.sonarlint.intellij.common.util.SonarLintUtils;
import org.sonarlint.intellij.finding.LiveFinding;

import static org.sonarlint.intellij.ui.UiUtils.runOnUiThread;

public class ShowRuleDescriptionIntentionAction implements IntentionAction, PriorityAction, Iconable {
private final LiveFinding liveFinding;

Expand All @@ -59,8 +57,7 @@ public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
runOnUiThread(project, () -> SonarLintUtils.getService(project, SonarLintToolWindow.class)
.showFindingDescription(liveFinding));
SonarLintUtils.getService(project, SonarLintToolWindow.class).showFindingDescription(liveFinding);
}

@Override
Expand Down
35 changes: 18 additions & 17 deletions src/main/java/org/sonarlint/intellij/fix/ShowFixSuggestion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import org.sonarlint.intellij.common.ui.ReadActionUtils.Companion.computeReadActionSafely
import org.sonarlint.intellij.common.ui.SonarLintConsole
import org.sonarlint.intellij.notifications.SonarLintProjectNotifications.Companion.get
import org.sonarlint.intellij.ui.UiUtils.Companion.runOnUiThread
Expand All @@ -37,25 +36,25 @@ import org.sonarlint.intellij.ui.inlay.InlayManager
import org.sonarlint.intellij.util.getDocument
import org.sonarsource.sonarlint.core.rpc.protocol.client.fix.FixSuggestionDto

class ShowFixSuggestion(private val project: Project, private val file: VirtualFile, private val fixSuggestion: FixSuggestionDto) {
class ShowFixSuggestion(
private val project: Project, private val file: VirtualFile, private val fixSuggestion: FixSuggestionDto
) {

fun show() {
val fileEditorManager = FileEditorManager.getInstance(project)
val psiFile = PsiManager.getInstance(project).findFile(file) ?: return
val document = computeReadActionSafely(project) { file.getDocument() } ?: return

if (!isWithinBounds(document)) {
get(project).simpleNotification(
null,
"Unable to open the fix suggestion, your file has probably changed",
NotificationType.WARNING
)
return
}
runOnUiThread(project, ModalityState.defaultModalityState()) {
val fileEditorManager = FileEditorManager.getInstance(project)
val psiFile = PsiManager.getInstance(project).findFile(file) ?: return@runOnUiThread
val document = file.getDocument() ?: return@runOnUiThread

if (!isWithinBounds(document)) {
get(project).simpleNotification(
null, "Unable to open the fix suggestion, your file has probably changed", NotificationType.WARNING
)
return@runOnUiThread
}

var successfullyOpened = true
var successfullyOpened = true

runOnUiThread(project, ModalityState.defaultModalityState()) {
fixSuggestion.fileEdit().changes().forEachIndexed { index, change ->
val startLine = change.beforeLineRange().startLine
val endLine = change.beforeLineRange().endLine
Expand All @@ -74,7 +73,9 @@ class ShowFixSuggestion(private val project: Project, private val file: VirtualF
fileEditorManager.selectedTextEditor?.let {
val doc = it.document
try {
val rangeMarker = doc.createRangeMarker(doc.getLineStartOffset(startLine - 1), doc.getLineEndOffset(endLine - 1))
val rangeMarker = doc.createRangeMarker(
doc.getLineStartOffset(startLine - 1), doc.getLineEndOffset(endLine - 1)
)
val currentCode = doc.getText(TextRange(rangeMarker.startOffset, rangeMarker.endOffset))
val fixSuggestionSnippet = FixSuggestionSnippet(
currentCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ class ConfigureNotificationsAction(private val connectionName: String, private v

override fun actionPerformed(e: AnActionEvent, notification: Notification) {
WindowManager.getInstance().getFrame(e.project) ?: return
runOnUiThread(project) {
val connectionToEdit = Settings.getGlobalSettings().serverConnections.find { it.name == connectionName }
if (connectionToEdit != null) {
val wizard = ServerConnectionWizard.forNotificationsEdition(connectionToEdit)
val connectionToEdit = Settings.getGlobalSettings().serverConnections.find { it.name == connectionName }
if (connectionToEdit != null) {
val wizard = ServerConnectionWizard.forNotificationsEdition(connectionToEdit)
runOnUiThread(project) {
if (wizard.showAndGet()) {
val editedConnection = wizard.connection
val serverConnections = Settings.getGlobalSettings().serverConnections.toMutableList()
serverConnections[serverConnections.indexOf(connectionToEdit)] = editedConnection
Settings.getGlobalSettings().serverConnections = serverConnections
}
} else if (e.project != null) {
SonarLintConsole.get(e.project!!).error("Unable to find connection with name: $connectionName")
notification.expire()
}
} else if (e.project != null) {
SonarLintConsole.get(e.project!!).error("Unable to find connection with name: $connectionName")
notification.expire()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@ class BindProjectAction(private val bindingSuggestion: BindingSuggestion, privat
SonarLintConsole.get(project)
.debug("Cannot bind project as suggested, connection $connectionId has been removed")
})

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ReturnResult beforeCheckin(@Nullable CommitExecutor executor, PairConsume
var affectedFiles = new HashSet<>(checkinPanel.getVirtualFiles());
// this will block EDT (modal)
try {
var analysisIdsByCallback = SonarLintUtils.getService(project, AnalysisSubmitter.class).analyzeFilesPreCommit(affectedFiles);
var analysisIdsByCallback = getService(project, AnalysisSubmitter.class).analyzeFilesPreCommit(affectedFiles);
if (analysisIdsByCallback == null) {
return ReturnResult.CANCEL;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ private ReturnResult showYesNoCancel(String resultStr) {
}

private void showChangedFilesTab(AnalysisResult analysisResult) {
SonarLintUtils.getService(project, SonarLintToolWindow.class).openReportTab(analysisResult);
getService(project, SonarLintToolWindow.class).openReportTab(analysisResult);
}

private class MyRefreshableOnComponent implements RefreshableOnComponent, UnnamedConfigurable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.sonarlint.intellij.ui.tree.IssueTreeModelBuilder;

import static org.sonarlint.intellij.common.ui.ReadActionUtils.computeReadActionSafely;
import static org.sonarlint.intellij.ui.UiUtils.runOnUiThread;
import static org.sonarlint.intellij.util.ThreadUtilsKt.runOnPooledThread;

public abstract class AbstractIssuesPanel extends SimpleToolWindowPanel implements Disposable {
Expand Down Expand Up @@ -242,9 +241,8 @@ public <T extends Finding> void updateOnSelect(@Nullable LiveFinding issue, Show
return;
}

runOnUiThread(project, () ->
findingDetailsPanel.showServerOnlyIssue(showFinding.getModule(), showFinding.getFile(), showFinding.getRuleKey(), rangeMarker, showFinding.getFlows(),
showFinding.getFlowMessage()));
findingDetailsPanel.showServerOnlyIssue(showFinding.getModule(), showFinding.getFile(), showFinding.getRuleKey(), rangeMarker, showFinding.getFlows(),
showFinding.getFlowMessage());
});
}
}
Expand Down
Loading

0 comments on commit ad40e0a

Please sign in to comment.