Search in sources :

Example 1 with SonarLintFacade

use of org.sonarlint.intellij.core.SonarLintFacade in project sonarlint-intellij by SonarSource.

the class SonarLintAnalyzer method analyzeModule.

public AnalysisResults analyzeModule(Module module, Collection<VirtualFile> filesToAnalyze, IssueListener listener, ProgressMonitor progressMonitor) {
    // Configure plugin properties. Nothing might be done if there is no configurator available for the extensions loaded in runtime.
    Map<String, String> pluginProps = new HashMap<>();
    AnalysisConfigurator[] analysisConfigurators = AnalysisConfigurator.EP_NAME.getExtensions();
    if (analysisConfigurators.length > 0) {
        for (AnalysisConfigurator config : analysisConfigurators) {
            console.debug("Configuring analysis with " + config.getClass().getName());
            pluginProps.putAll(config.configure(module));
        }
    } else {
        console.info("No analysis configurator found");
    }
    // configure files
    VirtualFileTestPredicate testPredicate = SonarLintUtils.get(module, VirtualFileTestPredicate.class);
    List<ClientInputFile> inputFiles = getInputFiles(module, testPredicate, filesToAnalyze);
    // Analyze
    long start = System.currentTimeMillis();
    try {
        SonarLintFacade facade = projectBindingManager.getFacade(true);
        String what;
        if (filesToAnalyze.size() == 1) {
            what = "'" + filesToAnalyze.iterator().next().getName() + "'";
        } else {
            what = Integer.toString(filesToAnalyze.size()) + " files";
        }
        console.info("Analysing " + what + "...");
        if (facade.requiresSavingFiles()) {
            console.debug("Saving files");
            LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed(), "Should not be in a read action (risk of dead lock)");
            ApplicationManager.getApplication().invokeAndWait(() -> SonarLintUtils.saveFiles(filesToAnalyze), ModalityState.defaultModalityState());
        }
        AnalysisResults result = facade.startAnalysis(inputFiles, listener, pluginProps, progressMonitor);
        console.debug("Done in " + (System.currentTimeMillis() - start) + "ms\n");
        if (filesToAnalyze.size() == 1) {
            telemetry.analysisDoneOnSingleFile(filesToAnalyze.iterator().next().getExtension(), (int) (System.currentTimeMillis() - start));
        } else {
            telemetry.analysisDoneOnMultipleFiles();
        }
        return result;
    } catch (InvalidBindingException e) {
        // should not happen, as analysis should not have been submitted in this case.
        throw new IllegalStateException(e);
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) HashMap(java.util.HashMap) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 2 with SonarLintFacade

use of org.sonarlint.intellij.core.SonarLintFacade in project sonarlint-intellij by SonarSource.

the class SonarLinkHandler method getDescription.

@Nullable
@Override
public String getDescription(@NotNull String refSuffix, @NotNull Editor editor) {
    Project project = editor.getProject();
    if (project == null || project.isDisposed()) {
        return null;
    }
    ProjectBindingManager projectBindingManager = SonarLintUtils.get(project, ProjectBindingManager.class);
    try {
        SonarLintFacade sonarlint = projectBindingManager.getFacade();
        String description = sonarlint.getDescription(refSuffix);
        String name = sonarlint.getRuleName(refSuffix);
        return transform(refSuffix, name, description);
    } catch (InvalidBindingException e) {
        return "";
    }
}
Also used : Project(com.intellij.openapi.project.Project) InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) ProjectBindingManager(org.sonarlint.intellij.core.ProjectBindingManager) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade) Nullable(javax.annotation.Nullable)

Example 3 with SonarLintFacade

use of org.sonarlint.intellij.core.SonarLintFacade in project sonarlint-intellij by SonarSource.

the class SonarLintSubmitter method filterAndgetByModule.

private Map<Module, Collection<VirtualFile>> filterAndgetByModule(Collection<VirtualFile> files, boolean checkExclusions) throws InvalidBindingException {
    HashMultimap<Module, VirtualFile> filesByModule = HashMultimap.create();
    SonarLintFacade sonarLintFacade = projectBindingManager.getFacade();
    for (VirtualFile file : files) {
        Module m = utils.findModuleForFile(file, myProject);
        if (checkExclusions) {
            LocalFileExclusions.Result result = localFileExclusions.checkExclusions(file, m);
            if (result.isExcluded()) {
                if (result.excludeReason() != null) {
                    logExclusion(file, "excluded: " + result.excludeReason());
                }
                continue;
            }
        } else {
            if (!localFileExclusions.canAnalyze(file, m)) {
                logExclusion(file, "can't be analyzed. Skipping it.");
                continue;
            }
        }
        filesByModule.put(m, file);
    }
    // Apply server file exclusions. This is an expensive operation, so we call the core only once per module.
    if (checkExclusions) {
        // Note: iterating over a copy of keys, because removal of last value removes the key,
        // resulting in ConcurrentModificationException
        List<Module> modules = new ArrayList<>(filesByModule.keySet());
        for (Module module : modules) {
            VirtualFileTestPredicate testPredicate = SonarLintUtils.get(module, VirtualFileTestPredicate.class);
            Collection<VirtualFile> excluded = sonarLintFacade.getExcluded(filesByModule.get(module), testPredicate);
            for (VirtualFile f : excluded) {
                logExclusion(f, "not automatically analyzed due to exclusions configured in the SonarQube Server");
            }
            filesByModule.get(module).removeAll(excluded);
        }
    }
    return filesByModule.asMap();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LocalFileExclusions(org.sonarlint.intellij.analysis.LocalFileExclusions) ArrayList(java.util.ArrayList) VirtualFileTestPredicate(org.sonarlint.intellij.analysis.VirtualFileTestPredicate) Module(com.intellij.openapi.module.Module) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade)

Aggregations

SonarLintFacade (org.sonarlint.intellij.core.SonarLintFacade)3 InvalidBindingException (org.sonarlint.intellij.exception.InvalidBindingException)2 Module (com.intellij.openapi.module.Module)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Nullable (javax.annotation.Nullable)1 LocalFileExclusions (org.sonarlint.intellij.analysis.LocalFileExclusions)1 VirtualFileTestPredicate (org.sonarlint.intellij.analysis.VirtualFileTestPredicate)1 ProjectBindingManager (org.sonarlint.intellij.core.ProjectBindingManager)1 AnalysisResults (org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults)1 ClientInputFile (org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)1