public void Ctor_ValidIssue_CorrectlyInitialized() { // Arrange var issue = new Sonarlint.Issue { FilePath = "path", RuleKey = "rule id", StartLine = 111 }; // Act var result = new DaemonIssueAdapter(issue, "line text", "line hash"); // Assert result.SonarLintIssue.Should().BeSameAs(issue); var filterableResult = (IFilterableIssue)result; filterableResult.FilePath.Should().Be("path"); filterableResult.RuleId.Should().Be("rule id"); filterableResult.StartLine.Should().Be(111); filterableResult.ProjectGuid.Should().BeNull(); filterableResult.WholeLineText.Should().Be("line text"); filterableResult.LineHash.Should().Be("line hash"); }
public DaemonIssueAdapter(Sonarlint.Issue sonarLintIssue, string wholeLineText, string lineHash) { SonarLintIssue = sonarLintIssue ?? throw new ArgumentNullException(nameof(sonarLintIssue)); this.wholeLineText = wholeLineText; this.lineHash = lineHash; }
public IssueMarker ToMarker(Sonarlint.Issue issue, ITextSnapshot currentSnapshot) { // SonarLint issues line numbers are 1-based, spans lines are 0-based var maxLength = currentSnapshot.Length; var startLine = currentSnapshot.GetLineFromLineNumber(issue.StartLine - 1); int startPos = startLine.Start.Position + issue.StartLineOffset; int endPos; if (issue.EndLine == 0 || // Special case : EndLine = 0 means "select whole of the start line, ignoring the offset" startPos > maxLength) // Defensive : issue start position is beyond the end of the file. Just select the last line. { startPos = startLine.Start.Position; endPos = startLine.Start.Position + startLine.Length; } else { endPos = currentSnapshot.GetLineFromLineNumber(issue.EndLine - 1).Start.Position + issue.EndLineOffset; // Make sure the end position isn't beyond the end of the snapshot either endPos = Math.Min(maxLength, endPos); } var start = new SnapshotPoint(currentSnapshot, startPos); var end = new SnapshotPoint(currentSnapshot, endPos); return(new IssueMarker(issue, new SnapshotSpan(start, end))); }
public void ToMarker_EndPositionExceedsSnapshotLength() { // Defensive: handle the issue end position being beyond the end of the snapshot // (we have not seen this in practice so far) // Arrange var issue = new Sonarlint.Issue { StartLine = 53, StartLineOffset = 2, EndLine = 53, EndLineOffset = 12, }; // The issue is on a single line in this case, but the issue end position // is beyond the end of the line. var vsLine = new VSLineDescription { ZeroBasedLineNumber = 52, LineLength = 10, LineStartPosition = 1599 }; var textSnapshotMock = CreateSnapshotMock(vsLine, vsLine, 1602); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(1601); // vsLine.LineStartPosition + issue.StartLineOffset result.Span.End.Position.Should().Be(1602); // snapshot length }
public IssueMarker ToMarker(Sonarlint.Issue issue, ITextSnapshot currentSnapshot) { int startPos = currentSnapshot.GetLineFromLineNumber(issue.StartLine - 1).Start.Position + issue.StartLineOffset; var start = new SnapshotPoint(currentSnapshot, startPos); int endPos = currentSnapshot.GetLineFromLineNumber(issue.EndLine - 1).Start.Position + issue.EndLineOffset; var end = new SnapshotPoint(currentSnapshot, endPos); return(new IssueMarker(issue, new SnapshotSpan(start, end))); }
public void ToMarker_Calculates_Span_Positions() { const int issueStartLine = 3; const int issueEndLine = 4; // Arrange var issue = new Sonarlint.Issue { StartLine = issueStartLine, StartLineOffset = 10, EndLine = issueEndLine, EndLineOffset = 20, }; var textSnapshotMock = new Mock <ITextSnapshot>(); var startLineMock = new Mock <ITextSnapshotLine>(); startLineMock.SetupGet(x => x.Start) .Returns(() => new SnapshotPoint(textSnapshotMock.Object, 35)); var endLineMock = new Mock <ITextSnapshotLine>(); endLineMock.SetupGet(x => x.Start) .Returns(() => new SnapshotPoint(textSnapshotMock.Object, 47)); textSnapshotMock .SetupGet(x => x.Length) .Returns(1000); // some big number to avoid ArgumentOutOfRange exceptions textSnapshotMock .Setup(x => x.GetLineFromLineNumber(issueStartLine - 1)) .Returns(() => startLineMock.Object); textSnapshotMock .Setup(x => x.GetLineFromLineNumber(issueEndLine - 1)) .Returns(() => endLineMock.Object); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(45); // first SnapshotPoint.Position + issue.StartLineOffset result.Span.End.Position.Should().Be(67); // second SnapshotPoint.Position + issue.EndLineOffset }
public void ToMarker_StartPositionExceedsSnapshotLength() { // These values were taken from a real analysis issue // Rule "cpp:S113 - no newline at end of file" returns an offset after the end of the file. // Arrange var issue = new Sonarlint.Issue { StartLine = 53, StartLineOffset = 2, EndLine = 53, EndLineOffset = 3, }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 52, LineLength = 1, LineStartPosition = 1599 }; // Second line should not be used in this case var secondLine = new VSLineDescription { ZeroBasedLineNumber = -999, LineLength = -999, LineStartPosition = -999 }; var textSnapshotMock = CreateSnapshotMock(firstLine, secondLine, 1600); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(1599); // firstLine.LineStartPosition. Ignore offset because that will take us beyond the end of file result.Span.End.Position.Should().Be(1600); // snapshot length }
public void ToMarker_EndLineIsZero() { // If issue.EndLine is zero the whole of the start line should be selected // Arrange var issue = new Sonarlint.Issue { StartLine = 22, StartLineOffset = 2, EndLine = 0, EndLineOffset = 0, }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 21, LineLength = 34, LineStartPosition = 103 }; // The second VS line shouldn't be used in this case var secondLine = new VSLineDescription { ZeroBasedLineNumber = 25, LineLength = 100, LineStartPosition = 1010 }; var textSnapshotMock = CreateSnapshotMock(firstLine, secondLine); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(103); // firstLine.LineStartPosition. Ignore issue.StartLineOffset in this case result.Span.End.Position.Should().Be(137); // firstLine.LineStartPosition + firstLine.LineLength }
public void ToMarker_Calculates_Span_Positions() { // Arrange var issue = new Sonarlint.Issue { StartLine = 3, StartLineOffset = 10, EndLine = 4, EndLineOffset = 20, }; var firstLine = new VSLineDescription { ZeroBasedLineNumber = 2, LineLength = 10, LineStartPosition = 35 }; var secondLine = new VSLineDescription { ZeroBasedLineNumber = 3, LineLength = 25, LineStartPosition = 47 }; var textSnapshotMock = CreateSnapshotMock(firstLine, secondLine); // Act var result = new IssueConverter() .ToMarker(issue, textSnapshotMock.Object); // Assert result.Should().NotBeNull(); result.Span.Start.Position.Should().Be(45); // firstLine.LineStartPosition + issue.StartLineOffset result.Span.End.Position.Should().Be(67); // secondLine.LineStartPosition + issue.EndLineOffset }
private IssueMarker CreateMarker(Sonarlint.Issue issue) => issueConverter.ToMarker(issue, subjectBuffer.CurrentSnapshot);