-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeFragment.cs
More file actions
71 lines (59 loc) · 2.01 KB
/
CodeFragment.cs
File metadata and controls
71 lines (59 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CodeNavigator
{
class CodeFragment
{
public CodeFragment()
{
this.Start = new CodeLocation();
this.End = new CodeLocation();
}
public CodeFragment(string code)
:this()
{
this.Code = code;
}
public CodeLocation Start { get; set; }
public CodeLocation End { get; set; }
public int Length {
get { return Code == null ? 0 : Code.Length; }
}
public string Code { get; set; }
public override string ToString()
{
return Code;
}
internal virtual bool UpdateOffsetFromMapping(CodeItem code)
{
//check if we have already done this
if (Start.HasOffset())
return false;
_code = code;
List<CodeLocation> mapping = _code.GetSourceCharMapping();
//scan through the mapping and correct start and end position
int diff = 0;
foreach (CodeLocation loc in mapping)
{
int pWithComments = loc.CharOffsetWithComments;
int pWithoutComments = loc.CharOffsetWithoutComments;
diff = pWithComments - pWithoutComments;
//if the location is before the fragments position, update the
//position in code with comments
if (pWithoutComments <= Start.CharOffsetWithoutComments)
Start.CharOffsetDifference = diff;
else if (Start.CharOffsetDifference == -1)
Start.CharOffsetDifference = 0;
//the same for the end pos; break if bigger than end
if (pWithoutComments <= End.CharOffsetWithoutComments)
End.CharOffsetDifference = diff;
else
break;
}
return true;
}
CodeItem _code;
}
}