diff --git a/src/main/java/org/sonarlint/intellij/analysis/AnalysisSubmitter.java b/src/main/java/org/sonarlint/intellij/analysis/AnalysisSubmitter.java index 5a3df9239..17c9001ce 100644 --- a/src/main/java/org/sonarlint/intellij/analysis/AnalysisSubmitter.java +++ b/src/main/java/org/sonarlint/intellij/analysis/AnalysisSubmitter.java @@ -29,7 +29,6 @@ import com.intellij.openapi.vfs.VirtualFile; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -117,10 +116,6 @@ public void autoAnalyzeOpenFiles(TriggerType triggerType) { } } - public void autoAnalyzeFile(VirtualFile file, TriggerType triggerType) { - autoAnalyzeFiles(Collections.singleton(file), triggerType); - } - @CheckForNull public Cancelable autoAnalyzeFiles(Collection files, TriggerType triggerType) { if (!getGlobalSettings().isAutoTrigger()) { diff --git a/src/main/java/org/sonarlint/intellij/core/BackendService.kt b/src/main/java/org/sonarlint/intellij/core/BackendService.kt index b04fb1503..85c3a8136 100644 --- a/src/main/java/org/sonarlint/intellij/core/BackendService.kt +++ b/src/main/java/org/sonarlint/intellij/core/BackendService.kt @@ -915,27 +915,24 @@ class BackendService : Disposable { val contributedLanguages = collectContributedLanguages(module, virtualFiles) - event.filter { it.type != ModuleFileEvent.Type.DELETED } - .mapNotNull { - val relativePath = getRelativePathForAnalysis(module, it.virtualFile) ?: return@mapNotNull null - val forcedLanguage = contributedLanguages[it.virtualFile]?.let { fl -> Language.valueOf(fl.name) } - val uri = VirtualFileUtils.toURI(it.virtualFile) + event.filter { it.type != ModuleFileEvent.Type.DELETED }.mapNotNull { + val relativePath = getRelativePathForAnalysis(module, it.virtualFile) ?: return@mapNotNull null + val forcedLanguage = contributedLanguages[it.virtualFile]?.let { fl -> Language.valueOf(fl.name) } + val uri = VirtualFileUtils.toURI(it.virtualFile) + computeReadActionSafely(it.virtualFile, module.project) { ClientFileDto( uri, Paths.get(relativePath), moduleId, - computeReadActionSafely(module.project) { isTestSources(it.virtualFile, module.project) }, + isTestSources(it.virtualFile, module.project), it.getEncoding(module.project).toString(), Paths.get(it.virtualFile.path), - if (FileUtilRt.isTooLarge(it.virtualFile.length)) null else computeReadActionSafely(module.project) { - getFileContent( - it.virtualFile - ) - }, + if (FileUtilRt.isTooLarge(it.virtualFile.length)) null else getFileContent(it.virtualFile), forcedLanguage, true ) } + } } if (deletedFileUris.isNotEmpty() || events.isNotEmpty()) { notifyBackend { it.fileService.didUpdateFileSystem(DidUpdateFileSystemParams(deletedFileUris, events)) } diff --git a/src/main/java/org/sonarlint/intellij/trigger/EditorOpenTrigger.java b/src/main/java/org/sonarlint/intellij/trigger/EditorOpenTrigger.java index 73990a94b..e3f226be2 100644 --- a/src/main/java/org/sonarlint/intellij/trigger/EditorOpenTrigger.java +++ b/src/main/java/org/sonarlint/intellij/trigger/EditorOpenTrigger.java @@ -29,6 +29,7 @@ import java.util.Map; import javax.annotation.concurrent.ThreadSafe; import org.jetbrains.annotations.NotNull; +import org.sonarlint.intellij.common.util.FileUtils; import org.sonarlint.intellij.core.BackendService; import org.sonarlint.intellij.fs.VirtualFileEvent; import org.sonarsource.sonarlint.plugin.api.module.file.ModuleFileEvent; @@ -59,7 +60,7 @@ public void onProjectOpened() { public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) { runOnPooledThread(source.getProject(), () -> { var module = findModuleForFile(file, source.getProject()); - if (module != null) { + if (module != null && FileUtils.Companion.isFileValidForSonarLintWithExtensiveChecks(file, source.getProject())) { getService(BackendService.class).updateFileSystem(Map.of(module, List.of(new VirtualFileEvent(ModuleFileEvent.Type.CREATED, file)))); } if (source.getProject().equals(myProject)) { diff --git a/src/main/java/org/sonarlint/intellij/trigger/EventScheduler.kt b/src/main/java/org/sonarlint/intellij/trigger/EventScheduler.kt index 546bf8320..42e7b839b 100644 --- a/src/main/java/org/sonarlint/intellij/trigger/EventScheduler.kt +++ b/src/main/java/org/sonarlint/intellij/trigger/EventScheduler.kt @@ -27,7 +27,6 @@ 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, @@ -50,16 +49,19 @@ class EventScheduler( } 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() - } + if (filesToAnalyze.isNotEmpty()) { + getService(project, AnalysisSubmitter::class.java).autoAnalyzeFiles(filesToAnalyze.toList(), triggerType) } + filesToAnalyze.clear() } fun notify(file: VirtualFile) { + if (!Settings.getGlobalSettings().isAutoTrigger) { + return + } + + filesToAnalyze.add(file) + // Remove the previously finished task if (scheduledTask?.isDone == true) { scheduledTask = null @@ -75,8 +77,6 @@ class EventScheduler( scheduledTask?.cancel(false) scheduledTask = scheduler.schedule({ trigger() }, timer, TimeUnit.MILLISECONDS) } - - filesToAnalyze.add(file) } } diff --git a/src/main/java/org/sonarlint/intellij/util/SonarLintAppUtils.java b/src/main/java/org/sonarlint/intellij/util/SonarLintAppUtils.java index 474ab2f25..f4ad6858b 100644 --- a/src/main/java/org/sonarlint/intellij/util/SonarLintAppUtils.java +++ b/src/main/java/org/sonarlint/intellij/util/SonarLintAppUtils.java @@ -20,7 +20,6 @@ package org.sonarlint.intellij.util; -import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectLocator; @@ -35,7 +34,6 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import javax.annotation.CheckForNull; import org.jetbrains.annotations.Nullable; @@ -65,13 +63,6 @@ public static Project guessProjectForFile(VirtualFile file) { return ProjectLocator.getInstance().guessProjectForFile(file); } - public static List retainOpenFiles(Project project, List files) { - if (project.isDisposed() || !project.isOpen()) { - return Collections.emptyList(); - } - return files.stream().filter(f -> FileEditorManager.getInstance(project).isFileOpen(f)).toList(); - } - /** * If you already know the module where the file belongs, use {@link #getRelativePathForAnalysis(Module, VirtualFile)} * Path will always contain forward slashes.