@@ -46,7 +46,11 @@ export class FocusAreaContextExtractor {
46
46
47
47
importantRange = this . trimRangeAccordingToLimits ( editor . document , importantRange )
48
48
const codeBlock = this . getRangeText ( editor . document , importantRange )
49
- const extendedCodeBlockRange = this . getExtendedCodeBlockRange ( editor . document , importantRange )
49
+ const extendedCodeBlockRange = this . getExtendedCodeBlockRange (
50
+ editor . document ,
51
+ importantRange ,
52
+ focusAreaCharLimit
53
+ )
50
54
51
55
return {
52
56
extendedCodeBlock : this . getRangeText ( editor . document , extendedCodeBlockRange ) ,
@@ -86,45 +90,77 @@ export class FocusAreaContextExtractor {
86
90
)
87
91
}
88
92
89
- private getExtendedCodeBlockRange ( document : TextDocument , importantRange : Range ) : Range {
93
+ // Function to extend the code block range
94
+ private getExtendedCodeBlockRange (
95
+ document : TextDocument ,
96
+ importantRange : Range ,
97
+ focusAreaCharLimit : number
98
+ ) : Range {
99
+ // Flag to add line before or after
90
100
let addLineBefore = true
101
+ // Loop while range can still be extended
91
102
while (
92
- this . getRangeText ( document , importantRange ) . length < focusAreaCharLimit &&
93
- ( importantRange . start . line !== 0 || importantRange . end . line !== document . lineCount - 1 )
103
+ ! ( importantRange . start . line === 0 && importantRange . start . character === 0 ) ||
104
+ ! (
105
+ importantRange . end . line === document . lineCount - 1 &&
106
+ importantRange . end . character === document . lineAt ( document . lineCount - 1 ) . range . end . character
107
+ )
94
108
) {
95
- if ( addLineBefore && importantRange . start . line !== 0 ) {
96
- const tmpRange = new Range (
97
- importantRange . start . line - 1 ,
98
- 0 ,
99
- importantRange . end . line ,
100
- importantRange . end . character
101
- )
102
- addLineBefore = false
109
+ let tmpRange : Range | undefined = undefined
110
+ if ( addLineBefore ) {
111
+ // Check if we have characters left in the beginning of the current line
112
+ if ( importantRange . start . character !== 0 ) {
113
+ tmpRange = new Range (
114
+ importantRange . start . line ,
115
+ 0 ,
116
+ importantRange . end . line ,
117
+ importantRange . end . character
118
+ )
119
+ } else if ( importantRange . start . line !== 0 ) {
120
+ tmpRange = new Range (
121
+ importantRange . start . line - 1 ,
122
+ document . lineAt ( importantRange . start . line - 1 ) . range . end . character ,
123
+ importantRange . end . line ,
124
+ importantRange . end . character
125
+ )
126
+ }
103
127
104
- if ( this . getRangeText ( document , tmpRange ) . length >= focusAreaCharLimit ) {
105
- break
128
+ // Flip flag for next iteration
129
+ addLineBefore = false
130
+ } else {
131
+ // Check if we have characters left in the end of the current line
132
+ if ( importantRange . end . character !== document . lineAt ( importantRange . end . line ) . range . end . character ) {
133
+ tmpRange = new Range (
134
+ importantRange . start . line ,
135
+ importantRange . start . character ,
136
+ importantRange . end . line ,
137
+ document . lineAt ( importantRange . end . line ) . range . end . character
138
+ )
139
+ } else if ( importantRange . end . line !== document . lineCount - 1 ) {
140
+ tmpRange = new Range (
141
+ importantRange . start . line ,
142
+ importantRange . start . character ,
143
+ importantRange . end . line + 1 ,
144
+ 0
145
+ )
106
146
}
107
147
108
- importantRange = tmpRange
109
- continue
148
+ // Flip flag for next iteration
149
+ addLineBefore = true
110
150
}
111
151
112
- if ( importantRange . end . line !== document . lineCount - 1 ) {
113
- const tmpRange = new Range (
114
- importantRange . start . line ,
115
- importantRange . start . character ,
116
- importantRange . end . line + 1 ,
117
- document . lineAt ( importantRange . end . line + 1 ) . range . end . character
118
- )
119
-
120
- if ( this . getRangeText ( document , tmpRange ) . length >= focusAreaCharLimit ) {
121
- break
122
- }
123
-
124
- importantRange = tmpRange
152
+ // Check if tmp range was set
153
+ if ( tmpRange === undefined ) {
154
+ continue
155
+ }
156
+ // Check character length of tmp range
157
+ if ( this . getRangeText ( document , tmpRange ) . length >= focusAreaCharLimit ) {
158
+ // Break loop if too long
159
+ break
125
160
}
126
161
127
- addLineBefore = true
162
+ // Update important range
163
+ importantRange = tmpRange
128
164
}
129
165
130
166
return importantRange
0 commit comments