Skip to content

Commit b413d73

Browse files
SeeminglySciencedaviwil
authored andcommitted
Add position classes for new functions
This change adds a custom implementation of IScriptExtent similar to InternalScriptExtent. The major difference being the replacement of PositionHelper with FileContext. The PositionHelper class was used to store the full script text and line start mapping for all ScriptExtent objects of the same file. FileContext is a great replacement for this because it's already kept unique by PSES and it adapts to changes. - Added FullScriptExtent and FullScriptPosition classes. - Changed the scriptFile field on FileContext from private to internal.
1 parent 0aefab6 commit b413d73

File tree

3 files changed

+226
-1
lines changed

3 files changed

+226
-1
lines changed

src/PowerShellEditorServices/Extensions/FileContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class FileContext
1616
{
1717
#region Private Fields
1818

19-
private ScriptFile scriptFile;
19+
internal ScriptFile scriptFile;
2020
private EditorContext editorContext;
2121
private IEditorOperations editorOperations;
2222

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using System.Management.Automation.Language;
2+
using Microsoft.PowerShell.EditorServices.Extensions;
3+
4+
namespace Microsoft.PowerShell.EditorServices
5+
{
6+
/// <summary>
7+
/// Provides an IScriptExtent implementation that is aware of editor context
8+
/// and can adjust to changes.
9+
/// </summary>
10+
public class FullScriptExtent : IScriptExtent
11+
{
12+
#region Properties
13+
14+
/// <summary>
15+
/// Gets the buffer range of the extent.
16+
/// </summary>
17+
public BufferRange BufferRange { get; private set; }
18+
19+
/// <summary>
20+
/// Gets the FileContext that this extent refers to.
21+
/// </summary>
22+
public FileContext FileContext { get; }
23+
24+
/// <summary>
25+
/// Gets the file path of the script file in which this extent is contained.
26+
/// </summary>
27+
public string File
28+
{
29+
get { return FileContext.Path; }
30+
}
31+
32+
/// <summary>
33+
/// Gets the starting script position of the extent.
34+
/// </summary>
35+
public IScriptPosition StartScriptPosition
36+
{
37+
get { return new FullScriptPosition(FileContext, BufferRange.Start, StartOffset); }
38+
}
39+
40+
/// <summary>
41+
/// Gets the ending script position of the extent.
42+
/// </summary>
43+
public IScriptPosition EndScriptPosition
44+
{
45+
get { return new FullScriptPosition(FileContext, BufferRange.End, EndOffset); }
46+
}
47+
48+
/// <summary>
49+
/// Gets the starting line number of the extent.
50+
/// </summary>
51+
public int StartLineNumber
52+
{
53+
get { return BufferRange.Start.Line; }
54+
}
55+
56+
57+
/// <summary>
58+
/// Gets the starting column number of the extent.
59+
/// </summary>
60+
public int StartColumnNumber
61+
{
62+
get { return BufferRange.Start.Column; }
63+
}
64+
65+
/// <summary>
66+
/// Gets the ending line number of the extent.
67+
/// </summary>
68+
public int EndLineNumber
69+
{
70+
get { return BufferRange.End.Line; }
71+
}
72+
73+
/// <summary>
74+
/// Gets the ending column number of the extent.
75+
/// </summary>
76+
public int EndColumnNumber
77+
{
78+
get { return BufferRange.End.Column; }
79+
}
80+
81+
/// <summary>
82+
/// Gets the text that is contained within the extent.
83+
/// </summary>
84+
public string Text
85+
{
86+
get
87+
{
88+
// StartOffset can be > the length for the EOF token.
89+
if (StartOffset > FileContext.scriptFile.Contents.Length)
90+
{
91+
return "";
92+
}
93+
94+
return FileContext.GetText(BufferRange);
95+
}
96+
}
97+
98+
/// <summary>
99+
/// Gets the starting file offset of the extent.
100+
/// </summary>
101+
public int StartOffset { get; private set; }
102+
103+
/// <summary>
104+
/// Gets the ending file offset of the extent.
105+
/// </summary>
106+
public int EndOffset { get; private set; }
107+
108+
#endregion
109+
110+
#region Constructors
111+
112+
/// <summary>
113+
/// Creates a new instance of the FullScriptExtent class.
114+
/// </summary>
115+
/// <param name="fileContext">The FileContext this extent refers to.</param>
116+
/// <param name="bufferRange">The buffer range this extent is located at.</param>
117+
public FullScriptExtent(FileContext fileContext, BufferRange bufferRange)
118+
{
119+
BufferRange = bufferRange;
120+
FileContext = fileContext;
121+
122+
StartOffset = fileContext.scriptFile.GetOffsetAtPosition(
123+
bufferRange.Start.Line,
124+
bufferRange.Start.Column);
125+
126+
EndOffset = fileContext.scriptFile.GetOffsetAtPosition(
127+
bufferRange.End.Line,
128+
bufferRange.End.Column);
129+
}
130+
131+
/// <summary>
132+
/// Creates an new instance of the FullScriptExtent class.
133+
/// </summary>
134+
/// <param name="fileContext">The FileContext this extent refers to.</param>
135+
/// <param name="startOffset">The zero based offset this extent starts at.</param>
136+
/// <param name="endOffset">The zero based offset this extent ends at.</param>
137+
public FullScriptExtent(FileContext fileContext, int startOffset, int endOffset)
138+
{
139+
FileContext = fileContext;
140+
StartOffset = startOffset;
141+
EndOffset = endOffset;
142+
BufferRange = fileContext.scriptFile.GetRangeBetweenOffsets(startOffset, endOffset);
143+
}
144+
145+
#endregion
146+
147+
#region Public Methods
148+
149+
public override string ToString()
150+
{
151+
return Text;
152+
}
153+
154+
/// <summary>
155+
/// Moves the start and end positions of the extent by an offset. Can
156+
/// be used to move forwards or backwards.
157+
/// </summary>
158+
/// <param name="offset">The amount to move the extent.</param>
159+
public void AddOffset(int offset) {
160+
StartOffset += offset;
161+
EndOffset += offset;
162+
163+
BufferRange = FileContext.scriptFile.GetRangeBetweenOffsets(StartOffset, EndOffset);
164+
}
165+
166+
#endregion
167+
}
168+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Management.Automation.Language;
2+
using Microsoft.PowerShell.EditorServices.Extensions;
3+
4+
namespace Microsoft.PowerShell.EditorServices
5+
{
6+
internal class FullScriptPosition : IScriptPosition
7+
{
8+
#region Fields
9+
private readonly FileContext fileContext;
10+
11+
private readonly BufferPosition bufferPosition;
12+
13+
#endregion
14+
15+
#region Properties
16+
public string File
17+
{
18+
get { return fileContext.Path; }
19+
}
20+
public int LineNumber
21+
{
22+
get { return bufferPosition.Line; }
23+
}
24+
public int ColumnNumber
25+
{
26+
get { return bufferPosition.Column; }
27+
}
28+
public string Line
29+
{
30+
get { return fileContext.scriptFile.GetLine(LineNumber); }
31+
}
32+
public int Offset { get; }
33+
34+
#endregion
35+
36+
#region Constructors
37+
38+
internal FullScriptPosition(FileContext context, BufferPosition position, int offset)
39+
{
40+
fileContext = context;
41+
bufferPosition = position;
42+
Offset = offset;
43+
}
44+
45+
#endregion
46+
47+
48+
#region Public Methods
49+
50+
public string GetFullScript()
51+
{
52+
return fileContext.GetText();
53+
}
54+
55+
#endregion
56+
}
57+
}

0 commit comments

Comments
 (0)