-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
980369b
commit a5fbea7
Showing
6 changed files
with
163 additions
and
80 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
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
100 changes: 100 additions & 0 deletions
100
src/main/java/org/sonarlint/intellij/trigger/EventWatcher.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,100 @@ | ||
/* | ||
* 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.ConcurrentHashMap | ||
import org.sonarlint.intellij.analysis.AnalysisSubmitter | ||
import org.sonarlint.intellij.analysis.Cancelable | ||
import org.sonarlint.intellij.common.util.SonarLintUtils.getService | ||
import org.sonarlint.intellij.config.Settings | ||
import org.sonarlint.intellij.util.SonarLintAppUtils.retainOpenFiles | ||
import org.sonarlint.intellij.util.runOnPooledThread | ||
|
||
class EventWatcher( | ||
private val project: Project, | ||
watcherName: String, | ||
private val eventMap: ConcurrentHashMap<VirtualFile, Long>, | ||
private val triggerType: TriggerType, | ||
private val timer: Int | ||
) : Thread() { | ||
|
||
private var stop: Boolean = false | ||
private var task: Cancelable? = null | ||
|
||
init { | ||
isDaemon = true | ||
name = "sonarlint-auto-trigger-$watcherName-${project.name}" | ||
} | ||
|
||
fun stopWatcher() { | ||
stop = true | ||
interrupt() | ||
} | ||
|
||
override fun run() { | ||
while (!stop) { | ||
checkTimers() | ||
try { | ||
sleep(200) | ||
} catch (e: InterruptedException) { | ||
// continue until stop flag is set | ||
} | ||
} | ||
} | ||
|
||
private fun triggerFiles(files: List<VirtualFile>) { | ||
if (Settings.getGlobalSettings().isAutoTrigger) { | ||
val openFilesToAnalyze = retainOpenFiles(project, files) | ||
if (openFilesToAnalyze.isNotEmpty()) { | ||
task?.let { | ||
it.cancel() | ||
task = null | ||
return | ||
} | ||
files.forEach { eventMap.remove(it) } | ||
task = getService(project, AnalysisSubmitter::class.java).autoAnalyzeFiles(openFilesToAnalyze, triggerType) | ||
} | ||
} | ||
} | ||
|
||
private fun checkTimers() { | ||
val now = System.currentTimeMillis() | ||
|
||
val it = eventMap.entries.iterator() | ||
val filesToTrigger = ArrayList<VirtualFile>() | ||
while (it.hasNext()) { | ||
val event = it.next() | ||
if (!event.key.isValid) { | ||
it.remove() | ||
continue | ||
} | ||
// don't trigger if file currently has errors? | ||
// filter files opened in the editor | ||
// use some heuristics based on analysis time or average pauses? Or make it configurable? | ||
if (event.value + timer < now) { | ||
filesToTrigger.add(event.key) | ||
} | ||
} | ||
runOnPooledThread(project) { triggerFiles(filesToTrigger) } | ||
} | ||
|
||
} |
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