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+ }
0 commit comments