Skip to content

Commit

Permalink
SLE-920: Enhance the extension point for diff viewers
Browse files Browse the repository at this point in the history
Enhance the extension point used for syntax highlighting with a new method to take into account the compare/diff viewer.

Added implementations for JDT (Java/JSP) and CDT (C/C++) but not PyDev due to a missing `Export-Package` statement in the plug-in.
  • Loading branch information
thahnen committed Aug 19, 2024
1 parent 0df0eb2 commit 5b81288
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
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,
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))) {
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);
}
}

0 comments on commit 5b81288

Please sign in to comment.