Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLE-920: Enhance the extension point for diff viewers #723

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion org.sonarlint.eclipse.cdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Require-Bundle: org.eclipse.core.runtime,
org.sonarlint.eclipse.core,
org.eclipse.core.filesystem,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.text
org.eclipse.text,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do these lines do? just curious

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is basically the Eclipse way of defining dependencies at runtime (and therefore also compile time).

The MANIFEST.MF and specifically this section will contain all bundles that we require at the runtime of the Eclipse installation. Most of them are present due to them being part of the Eclipse platform itself, but for some we have an optional.

E.g. here we have the org.eclipse.cdt.core;resolution:=optional statement that more or less is used for us to only make use of this very sub-plugin if CDT (the C development tools) are installed.

What I added is the compare bundle that brings in the TextMergeViewer and SWT that is the UI system of Eclipse.

org.eclipse.compare,
org.eclipse.swt
Import-Package: org.eclipse.ui.texteditor,
org.eclipse.jface.preference
Export-Package: org.sonarlint.eclipse.cdt.internal;x-friends:="org.sonarlint.eclipse.core.tests"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@
import java.util.Set;
import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Composite;
import org.sonarlint.eclipse.core.SonarLintLogger;
import org.sonarlint.eclipse.core.analysis.IAnalysisConfigurator;
import org.sonarlint.eclipse.core.analysis.IFileLanguageProvider;
Expand Down Expand Up @@ -64,6 +67,15 @@ private static boolean isCdtPresent() {
}
}

private static boolean isCdtUiPresent() {
try {
Class.forName("org.eclipse.cdt.ui.CUIPlugin");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

@Override
public Set<SonarLintLanguage> enableLanguages() {
// Objective-C is not supported by CDT!
Expand Down Expand Up @@ -103,17 +115,26 @@ public SonarLintLanguage language(ISonarLintFile file) {

@Override
public Optional<SourceViewerConfiguration> sourceViewerConfiguration(String ruleLanguage) {
if (isCdtPresent() && (ruleLanguage.equals(C_LANGUAGE_KEY) || ruleLanguage.equals(CPP_LANGUAGE_KEY))) {
if (isCdtUiPresent() && (ruleLanguage.equals(C_LANGUAGE_KEY) || ruleLanguage.equals(CPP_LANGUAGE_KEY))) {
return Optional.of(CdtUiUtils.sourceViewerConfiguration());
}
return Optional.empty();
}

@Override
public Optional<IDocumentPartitioner> documentPartitioner(String ruleLanguage) {
if (isCdtPresent() && (ruleLanguage.equals(C_LANGUAGE_KEY) || ruleLanguage.equals(CPP_LANGUAGE_KEY))) {
if (isCdtUiPresent() && (ruleLanguage.equals(C_LANGUAGE_KEY) || ruleLanguage.equals(CPP_LANGUAGE_KEY))) {
sophio-japharidze-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
return Optional.of(CdtUiUtils.documentPartitioner());
}
return Optional.empty();
}

@Nullable
@Override
public TextMergeViewer getTextMergeViewer(String ruleLanguage, Composite parent, CompareConfiguration mp) {
if (isCdtUiPresent() && (ruleLanguage.equals(C_LANGUAGE_KEY) || ruleLanguage.equals(CPP_LANGUAGE_KEY))) {
return CdtUiUtils.getTextMergeViewer(parent, mp);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
*/
package org.sonarlint.eclipse.cdt.internal;

import org.eclipse.cdt.internal.ui.compare.CMergeViewer;
import org.eclipse.cdt.internal.ui.text.doctools.DocCommentOwnerManager;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.CSourceViewerConfiguration;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Composite;

public class CdtUiUtils {

Expand All @@ -42,4 +46,8 @@ public static IDocumentPartitioner documentPartitioner() {
var owner = DocCommentOwnerManager.getInstance().getWorkspaceCommentOwner();
return CUIPlugin.getDefault().getTextTools().createDocumentPartitioner(owner);
}

public static TextMergeViewer getTextMergeViewer(Composite parent, CompareConfiguration mp) {
return new CMergeViewer(parent, 0, mp);
}
}
2 changes: 2 additions & 0 deletions org.sonarlint.eclipse.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ Require-Bundle: org.eclipse.equinox.security,
org.eclipse.core.net,
org.eclipse.core.filesystem,
org.eclipse.team.core,
org.eclipse.compare,
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.jgit;resolution:=optional,
org.eclipse.egit.core;resolution:=optional,
org.eclipse.egit.ui;resolution:=optional,
org.eclipse.text,
org.eclipse.swt,
org.sonarsource.sonarlint.core.sonarlint-java-client-osgi;bundle-version="[10.4.0,10.5.0)"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-11
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
package org.sonarlint.eclipse.core.rule;

import java.util.Optional;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Composite;

/**
* The rule descriptions containing code snippet require the correct syntax highlighting of the language to display
Expand All @@ -37,7 +41,26 @@ public interface ISyntaxHighlightingProvider {
Optional<SourceViewerConfiguration> sourceViewerConfiguration(String ruleLanguage);

/**
* @return the SourceViewer and its document requiring
* @return the SourceViewer and its document requiring a partitioner that is based on the plug-in implementing it
*/
Optional<IDocumentPartitioner> documentPartitioner(String ruleLanguage);

/**
* This is used for displaying language (and therefore implementing plug-in) -specific difference viewers. The
* plug-in directly provides its implementation based on the parent element and the compare configuration that will
* be enhanced by the plug-in specific viewer.
* This is used for example by the "Open fix suggestion" feature in order to provide difference viewers based on the
* language the file has - enhancing the user experience.
*
* @since 10.6
*
* @param ruleLanguage to be used to check whether this plug-in can contribute a viewer for this language
* @param parent the UI parent element which will embed the viewer
* @param used for configuring the viewer by providing information on the left and right side, e.g. label
* @return
*/
@Nullable
default TextMergeViewer getTextMergeViewer(String ruleLanguage, Composite parent, CompareConfiguration mp) {
return null;
}
}
1 change: 1 addition & 0 deletions org.sonarlint.eclipse.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.jdt.ui;resolution:=optional,
org.sonarlint.eclipse.ui,
org.eclipse.ui.ide,
org.eclipse.compare,
org.eclipse.swt
Import-Package: org.eclipse.ui.texteditor,
org.eclipse.jface.preference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Composite;
import org.sonarlint.eclipse.core.analysis.IAnalysisConfigurator;
import org.sonarlint.eclipse.core.analysis.IFileTypeProvider;
import org.sonarlint.eclipse.core.analysis.IPreAnalysisContext;
Expand Down Expand Up @@ -128,4 +131,13 @@ public Optional<IDocumentPartitioner> documentPartitioner(String ruleLanguage) {
}
return Optional.empty();
}

@Nullable
@Override
public TextMergeViewer getTextMergeViewer(String ruleLanguage, Composite parent, CompareConfiguration mp) {
if (jdtUiPresent && ruleLanguage.equals(JAVA_LANGUAGE_KEY)) {
return JdtUiUtils.getTextMergeViewer(parent, mp);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@
*/
package org.sonarlint.eclipse.jdt.internal;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.contentmergeviewer.TextMergeViewer;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.compare.JavaMergeViewer;
import org.eclipse.jdt.internal.ui.text.FastJavaPartitionScanner;
import org.eclipse.jdt.ui.text.IJavaPartitions;
import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Composite;
import org.sonarlint.eclipse.core.internal.utils.CompatibilityUtils;
import org.sonarlint.eclipse.ui.quickfixes.ISonarLintMarkerResolver;

Expand All @@ -52,4 +56,8 @@ public static IDocumentPartitioner documentPartitioner() {
IJavaPartitions.JAVA_STRING, IJavaPartitions.JAVA_CHARACTER, IJavaPartitions.JAVA_MULTI_LINE_STRING
});
}

public static TextMergeViewer getTextMergeViewer(Composite parent, CompareConfiguration mp) {
return new JavaMergeViewer(parent, 0, mp);
}
}