Skip to content

Commit

Permalink
Show details rule when click on annotation, add rule name on details,…
Browse files Browse the repository at this point in the history
… open browser when click on hyperlink in details
  • Loading branch information
philippefichet committed Apr 4, 2020
1 parent 2fa3ec5 commit e7dfa61
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 174 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ image::docs/JavaEditorAnnotationAndActionItems.jpg[]
image::docs/JavascriptActionItems.jpg[]

.SonarLint analyzer name and version
image::docs/OptionsJavaSonarLintAnalyzers.jpg[]
image::docs/OptionsSonarLintAnalyzers.jpg[]

.SonarLint rules enabled or disabled
image::docs/OptionsJavaSonarLintRules.jpg[]
image::docs/OptionsSonarLintRules.jpg[]

.Sonar rule details window
image::docs/SonarRuleDetailsWindow.jpg[]
Binary file removed docs/OptionsJavaSonarLintAnalyzers.JPG
Binary file not shown.
Binary file removed docs/OptionsJavaSonarLintAnalyzers.jpg
Binary file not shown.
Binary file removed docs/OptionsJavaSonarLintRules.JPG
Binary file not shown.
Binary file removed docs/OptionsJavaSonarLintRules.jpg
Binary file not shown.
Binary file added docs/OptionsSonarLintAnalyzers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/OptionsSonarLintRules.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/SonarRuleDetailsWindow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fr.philippefichet.sonarlint</groupId>
<artifactId>sonarlint4netbeans</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<packaging>nbm</packaging>
<name>SonarLint for Netbeans</name>
<organization>
Expand Down Expand Up @@ -151,6 +151,26 @@
<artifactId>org-netbeans-modules-parsing-api</artifactId>
<version>RELEASE110</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-lib2</artifactId>
<version>RELEASE110</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-lib</artifactId>
<version>RELEASE110</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor</artifactId>
<version>RELEASE110</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-document</artifactId>
<version>RELEASE110</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,60 +1,71 @@
/*
* sonarlint4netbeans: SonarLint integration for Apache Netbeans
* Copyright (C) 2019 Philippe FICHET.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package fr.philippefichet.sonarlint.netbeans;

import org.openide.text.Annotation;

/**
*
* @author FICHET Philippe
*/
public class SonarLintAnnotation extends Annotation {

private static final String ANNOTATION_TYPE = "fr-philippefichet-sonarlint-netbeans-annotation";
private final long startOffest;
private final int length;
private final String shortDescription;

public SonarLintAnnotation(String shortDescription, long startOffest, int length) {
super();
this.startOffest = startOffest;
this.length = length;
this.shortDescription = shortDescription;
}

public long getStartOffest() {
return startOffest;
}

public int getLength() {
return length;
}

@Override
public String getAnnotationType() {
return ANNOTATION_TYPE;
}

@Override
public String getShortDescription() {
return shortDescription;
}

}
/*
* sonarlint4netbeans: SonarLint integration for Apache Netbeans
* Copyright (C) 2019 Philippe FICHET.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package fr.philippefichet.sonarlint.netbeans;

import org.openide.text.Annotation;

/**
*
* @author FICHET Philippe
*/
public class SonarLintAnnotation extends Annotation {

public static final String ANNOTATION_TYPE = "fr-philippefichet-sonarlint-netbeans-annotation";
private final long startOffest;
private final int length;
private final String shortDescription;
private final String ruleKey;
private final String ruleName;

public SonarLintAnnotation(String ruleKey, String ruleName, long startOffest, int length) {
super();
this.startOffest = startOffest;
this.length = length;
this.shortDescription = ruleKey + "\n" + ruleName + "\nClick to show details";
this.ruleKey = ruleKey;
this.ruleName = ruleName;
}

public long getStartOffest() {
return startOffest;
}

public int getLength() {
return length;
}

@Override
public String getAnnotationType() {
return ANNOTATION_TYPE;
}

@Override
public String getShortDescription() {
return shortDescription;
}

public String getRuleKey() {
return ruleKey;
}

public String getRuleName() {
return ruleName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.swing.text.Position;
import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
Expand All @@ -45,6 +46,27 @@ public final class SonarLintAnnotationHandler {

private SonarLintAnnotationHandler() {
}

public static Optional<SonarLintAnnotation> getSonarLintAnnotation(
FileObject fileObject,
long startOffset,
int length,
String shortDescription
)
{
List<SonarLintAnnotation> sonarlintAnnotaions = ANNOTATIONS_BY_FILEOBJECT.get(fileObject);
if (sonarlintAnnotaions != null) {
for (SonarLintAnnotation sonarlintAnnotaion : sonarlintAnnotaions) {
if (sonarlintAnnotaion.getStartOffest() == startOffset
&& sonarlintAnnotaion.getLength() == length
&& sonarlintAnnotaion.getShortDescription().equals(shortDescription)
) {
return Optional.of(sonarlintAnnotaion);
}
}
}
return Optional.empty();
}

public static void analyze(SonarLintEngine standaloneSonarLintEngineImpl, FileObject fileObject, String textToAnalyze) throws DataObjectNotFoundException, IOException {
// Sonarlint not ready
Expand Down Expand Up @@ -94,7 +116,7 @@ public void propertyChange(PropertyChangeEvent evt) {
int nbEndLineOffset = NbDocument.findLineOffset(editorCookie.getDocument(), endLine - 1);
int endOffset = nbEndLineOffset + endLineOffset;
int length = endOffset - startOffset;
currentAnnocationOnFileObject.add(new SonarLintAnnotation(sue.getRuleKey() + " = " + sue.getRuleName(), startOffset, length));
currentAnnocationOnFileObject.add(new SonarLintAnnotation(sue.getRuleKey(), sue.getRuleName(), startOffset, length));
});

// Remove all previous Sonarlint annotations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (C) 2020 Philippe FICHET.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package fr.philippefichet.sonarlint.netbeans;

import java.awt.event.ActionEvent;
import java.util.Optional;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import static javax.swing.Action.NAME;
import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import org.netbeans.api.editor.document.LineDocumentUtils;
import org.netbeans.editor.AnnotationDesc;
import org.netbeans.editor.Annotations;
import org.netbeans.editor.BaseDocument;
import org.openide.ErrorManager;
import org.openide.loaders.DataObject;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
*
* @author FICHET Philippe <philippe.fichet@laposte.net>
*/
public class SonarLintGlyphAction extends AbstractAction
{
private static final Logger LOG = Logger.getLogger(SonarLintGlyphAction.class.getCanonicalName());

public SonarLintGlyphAction() {
LOG.severe("SonarLintGlyphAction");
putValue(NAME,
NbBundle.getMessage(
SonarLintGlyphAction.class,
"SonarLintGlyphAction.TXT_GlyphActionName" // NOI18N
)
);
putValue("supported-annotation-types", new String[] {
SonarLintAnnotation.ANNOTATION_TYPE
});
}

@Override
public void actionPerformed(ActionEvent e) {
JEditorPane editorPane = (JEditorPane)e.getSource();
final Document doc = editorPane.getDocument();
if (doc instanceof BaseDocument) {
final int currentPosition = editorPane.getCaretPosition();
final Annotations annotations = ((BaseDocument) doc).getAnnotations();
final DataObject od = (DataObject) doc.getProperty(Document.StreamDescriptionProperty);
if (od == null) {
return;
}

doc.render(() -> {
try {
int line = LineDocumentUtils.getLineIndex((BaseDocument)doc, currentPosition);
AnnotationDesc desc = annotations.getActiveAnnotation(line);
Optional<SonarLintAnnotation> sonarLintAnnotation = SonarLintAnnotationHandler.getSonarLintAnnotation(
od.getPrimaryFile(),
desc.getOffset(),
desc.getLength(),
desc.getShortDescription()
);
sonarLintAnnotation.ifPresent(sla -> {
TopComponent topComponent = WindowManager.getDefault().findTopComponent("SonarRuleDetailsTopComponent");
if (topComponent instanceof SonarRuleDetailsTopComponent) {
SonarRuleDetailsTopComponent sonarRuleDetailsTopComponent = (SonarRuleDetailsTopComponent)topComponent;
sonarRuleDetailsTopComponent.open();
sonarRuleDetailsTopComponent.requestActive();
sonarRuleDetailsTopComponent.requestFocusInWindow();
sonarRuleDetailsTopComponent.setSonarRuleKeyFilter(sla.getRuleKey());
}
});
} catch (BadLocationException ex) {
ErrorManager.getDefault().notify(ex);
}
});
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public final class SonarLintUtils {

private SonarLintUtils() {
}

public static String toURL(RuleDetails ruleDetails)
{
String[] keySplit = ruleDetails.getKey().split(":");
return "https://rules.sonarsource.com/" + keySplit[0] + "/RSPEC-" + keySplit[1].substring(1);
}

public static List<Issue> analyze(FileObject fileObject, String contentToAnalyze) throws IOException {
SonarLintEngine sonarLintEngine = Lookup.getDefault().lookup(SonarLintEngine.class);
Expand Down
Loading

0 comments on commit e7dfa61

Please sign in to comment.