Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit a2236d7

Browse files
committed
Fix #377: Underlines are a little off when created by background syntax check and after compilation
1 parent 0b78a71 commit a2236d7

2 files changed

Lines changed: 30 additions & 15 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CSharpSyntaxIssue.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,26 @@ public override IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context,
3939
{
4040
var refactoringContext = context as SDRefactoringContext;
4141
if (refactoringContext == null)
42-
return Enumerable.Empty<CodeIssue>();
42+
yield break;
4343

4444
var syntaxTree = context.RootNode as SyntaxTree;
4545
if (syntaxTree == null)
46-
return Enumerable.Empty<CodeIssue>();
47-
48-
return syntaxTree.Errors.Select(error => CreateCodeIssue(error, refactoringContext)).Where(issue => issue != null);
46+
yield break;
47+
48+
int prevLine = 0;
49+
foreach (var error in syntaxTree.Errors) {
50+
if (error.Region.BeginLine == prevLine)
51+
continue; // show at most one error per line
52+
prevLine = error.Region.BeginLine;
53+
var issue = CreateCodeIssue(error, refactoringContext);
54+
if (issue != null)
55+
yield return issue;
56+
}
57+
}
58+
59+
static bool IsSpaceOrTab(char c)
60+
{
61+
return c == ' ' || c == '\t';
4962
}
5063

5164
CodeIssue CreateCodeIssue(Error error, SDRefactoringContext context)
@@ -55,16 +68,18 @@ CodeIssue CreateCodeIssue(Error error, SDRefactoringContext context)
5568
if (begin.Line <= 0 || begin.Line > document.LineCount)
5669
return null;
5770

58-
// Columns seem to be zero-based, SD expects 1-based columns
59-
int offset = document.GetOffset(begin.Line, begin.Column + 1);
71+
int offset = document.GetOffset(begin.Line, begin.Column);
72+
// the parser sometimes reports errors in the whitespace prior to the invalid token, so search for the next word:
73+
while (offset < document.TextLength && IsSpaceOrTab(document.GetCharAt(offset)))
74+
offset++;
6075
int endOffset = TextUtilities.GetNextCaretPosition(document, offset, System.Windows.Documents.LogicalDirection.Forward, CaretPositioningMode.WordBorderOrSymbol);
6176
if (endOffset < 0) endOffset = document.TextLength;
6277
int length = endOffset - offset;
63-
64-
if (length < 2) {
65-
// marker should be at least 2 characters long, but take care that we don't make
66-
// it longer than the document
67-
length = Math.Min(2, document.TextLength - offset);
78+
79+
if (length < 1) {
80+
// marker should be at least 1 characters long,
81+
// but take care that we don't make it longer than the document
82+
length = Math.Min(1, document.TextLength - offset);
6883
}
6984

7085
TextLocation start = document.GetLocation(offset);

src/Main/Base/Project/Src/Services/Tasks/ErrorPainter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ void AddTask(SDTask task)
206206
if (endOffset < 0) endOffset = textEditor.Document.TextLength;
207207
int length = endOffset - offset;
208208

209-
if (length < 2) {
210-
// marker should be at least 2 characters long, but take care that we don't make
211-
// it longer than the document
212-
length = Math.Min(2, textEditor.Document.TextLength - offset);
209+
if (length < 1) {
210+
// marker should be at least 1 characters long,
211+
// but take care that we don't make it longer than the document
212+
length = Math.Min(1, textEditor.Document.TextLength - offset);
213213
}
214214

215215
ITextMarker marker = this.markerService.Create(offset, length);

0 commit comments

Comments
 (0)