Skip to content

Commit

Permalink
SLI-1821 Handle errors when code does not match
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel committed Feb 7, 2025
1 parent 17a30a4 commit 7d28242
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
28 changes: 17 additions & 11 deletions src/main/java/org/sonarlint/intellij/finding/TextRangeMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TextRangeWithHashDto;
import org.sonarsource.sonarlint.core.rpc.protocol.common.TextRangeDto;
Expand All @@ -50,19 +51,24 @@ public RangeMarker match(VirtualFile file, TextRangeWithHashDto textRange) throw
return match(toPsiFile(project, file), textRange.getStartLine(), textRange.getStartLineOffset(), textRange.getEndLine(), textRange.getEndLineOffset());
}

public RangeMarker matchWithCode(VirtualFile file, TextRangeDto textRange, String codeSnippet) throws NoMatchException {
var psiFile = toPsiFile(project, file);
var docManager = PsiDocumentManager.getInstance(project);
var doc = docManager.getDocument(psiFile);
if (doc == null) {
throw new NoMatchException("No document found for file: " + file.getName());
}
var range = getIssueTextRange(psiFile, doc, textRange.getStartLine(), textRange.getStartLineOffset(), textRange.getEndLine(), textRange.getEndLineOffset());
var codeAtRange = doc.getText(range);
if (!codeAtRange.equals(codeSnippet)) {
@CheckForNull
public RangeMarker matchWithCode(VirtualFile file, TextRangeDto textRange, String codeSnippet) {
try {
var psiFile = toPsiFile(project, file);
var docManager = PsiDocumentManager.getInstance(project);
var doc = docManager.getDocument(psiFile);
if (doc == null) {
return null;
}
var range = getIssueTextRange(psiFile, doc, textRange.getStartLine(), textRange.getStartLineOffset(), textRange.getEndLine(), textRange.getEndLineOffset());
var codeAtRange = doc.getText(range);
if (!codeAtRange.equals(codeSnippet)) {
return null;
}
return doc.createRangeMarker(range.getStartOffset(), range.getEndOffset());
} catch (NoMatchException e) {
return null;
}
return doc.createRangeMarker(range.getStartOffset(), range.getEndOffset());
}

public RangeMarker match(PsiFile file, TextRangeDto textRange) throws NoMatchException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataProvider
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.intellij.openapi.actionSystem.ex.ActionUtil
import com.intellij.openapi.editor.RangeMarker
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.SimpleToolWindowPanel
import com.intellij.openapi.ui.VerticalFlowLayout
Expand Down Expand Up @@ -378,7 +377,7 @@ class TaintVulnerabilitiesPanel(private val project: Project) : SimpleToolWindow
return@runOnPooledThread
}
val matcher = TextRangeMatcher(project)
val rangeMarker = computeReadActionSafely<RangeMarker>(project) {
val rangeMarker = computeReadActionSafely(project) {
matcher.matchWithCode(showFinding.file, showFinding.textRange, showFinding.codeSnippet)
}
if (rangeMarker == null) {
Expand Down

0 comments on commit 7d28242

Please sign in to comment.