Skip to content

Commit

Permalink
SLI-1656 Should automatically analyze opened file on remote env
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel committed Oct 16, 2024
1 parent 4dbe93e commit d418c94
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<VirtualFile> files, TriggerType triggerType) {
if (!getGlobalSettings().isAutoTrigger()) {
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/org/sonarlint/intellij/core/BackendService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/sonarlint/intellij/trigger/EventScheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -75,8 +77,6 @@ class EventScheduler(
scheduledTask?.cancel(false)
scheduledTask = scheduler.schedule({ trigger() }, timer, TimeUnit.MILLISECONDS)
}

filesToAnalyze.add(file)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -65,13 +63,6 @@ public static Project guessProjectForFile(VirtualFile file) {
return ProjectLocator.getInstance().guessProjectForFile(file);
}

public static List<VirtualFile> retainOpenFiles(Project project, List<VirtualFile> 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.
Expand Down

0 comments on commit d418c94

Please sign in to comment.