-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLI-1239 Avoid busy-waiting for automatic analysis trigger
- Loading branch information
1 parent
c7a4eed
commit 598d34c
Showing
6 changed files
with
206 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/org/sonarlint/intellij/trigger/EventScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* SonarLint for IntelliJ IDEA | ||
* Copyright (C) 2015-2024 SonarSource | ||
* [email protected] | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | ||
*/ | ||
package org.sonarlint.intellij.trigger | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.vfs.VirtualFile | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.ScheduledFuture | ||
import java.util.concurrent.TimeUnit | ||
import org.sonarlint.intellij.analysis.AnalysisSubmitter | ||
import org.sonarlint.intellij.common.util.SonarLintUtils.getService | ||
import org.sonarlint.intellij.config.Settings | ||
import org.sonarlint.intellij.util.SonarLintAppUtils.retainOpenFiles | ||
|
||
class EventScheduler( | ||
private val project: Project, | ||
private val schedulerName: String, | ||
private val triggerType: TriggerType, | ||
private val timer: Long, | ||
// True -> Schedule tasks at specific intervals | ||
// False -> Cancel the scheduled task and reschedule a new one | ||
private val atInterval: Boolean | ||
) { | ||
|
||
private val filesToAnalyze = mutableSetOf<VirtualFile>() | ||
private val scheduler = Executors.newScheduledThreadPool(1) { r -> Thread(r, "sonarlint-auto-trigger-$schedulerName-${project.name}") } | ||
private var scheduledTask: ScheduledFuture<*>? = null | ||
|
||
fun stopScheduler() { | ||
scheduledTask?.cancel(true) | ||
filesToAnalyze.clear() | ||
scheduler.shutdownNow() | ||
} | ||
|
||
private fun trigger() { | ||
if (Settings.getGlobalSettings().isAutoTrigger) { | ||
val openFileToAnalyze = retainOpenFiles(project, filesToAnalyze.toList()) | ||
if (openFileToAnalyze.isNotEmpty()) { | ||
getService(project, AnalysisSubmitter::class.java).autoAnalyzeFiles(openFileToAnalyze, triggerType) | ||
filesToAnalyze.clear() | ||
} | ||
} | ||
} | ||
|
||
fun notify(file: VirtualFile) { | ||
// Remove the previously finished task | ||
if (scheduledTask?.isDone == true) { | ||
scheduledTask = null | ||
} | ||
|
||
if (atInterval) { | ||
scheduledTask ?: let { | ||
// Schedule new task only if no task currently scheduled | ||
scheduledTask = scheduler.schedule({ trigger() }, timer, TimeUnit.MILLISECONDS) | ||
} | ||
} else { | ||
// Cancelling the scheduled task and postponing it later | ||
scheduledTask?.cancel(false) | ||
scheduledTask = scheduler.schedule({ trigger() }, timer, TimeUnit.MILLISECONDS) | ||
} | ||
|
||
filesToAnalyze.add(file) | ||
} | ||
|
||
} |
100 changes: 0 additions & 100 deletions
100
src/main/java/org/sonarlint/intellij/trigger/EventWatcher.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.