@@ -91,14 +91,14 @@ QString DocumentContextReader::getContextBefore(
91
91
effectiveStartLine = qMax (0 , lineNumber - linesCount);
92
92
}
93
93
94
- return getContextBetween (effectiveStartLine, lineNumber, cursorPosition);
94
+ return getContextBetween (effectiveStartLine, - 1 , lineNumber, cursorPosition);
95
95
}
96
96
97
97
QString DocumentContextReader::getContextAfter (
98
98
int lineNumber, int cursorPosition, int linesCount) const
99
99
{
100
100
int endLine = qMin (m_document->blockCount () - 1 , lineNumber + linesCount);
101
- return getContextBetween (lineNumber + 1 , endLine, cursorPosition );
101
+ return getContextBetween (lineNumber + 1 , cursorPosition, endLine, - 1 );
102
102
}
103
103
104
104
QString DocumentContextReader::readWholeFileBefore (int lineNumber, int cursorPosition) const
@@ -110,14 +110,12 @@ QString DocumentContextReader::readWholeFileBefore(int lineNumber, int cursorPos
110
110
111
111
startLine = qMin (startLine, lineNumber);
112
112
113
- QString result = getContextBetween (startLine, lineNumber, cursorPosition);
114
-
115
- return result;
113
+ return getContextBetween (startLine, -1 , lineNumber, cursorPosition);
116
114
}
117
115
118
116
QString DocumentContextReader::readWholeFileAfter (int lineNumber, int cursorPosition) const
119
117
{
120
- return getContextBetween (lineNumber, m_document->blockCount () - 1 , cursorPosition );
118
+ return getContextBetween (lineNumber, cursorPosition, m_document->blockCount () - 1 , - 1 );
121
119
}
122
120
123
121
QString DocumentContextReader::getLanguageAndFileInfo () const
@@ -172,18 +170,71 @@ CopyrightInfo DocumentContextReader::findCopyright()
172
170
return result;
173
171
}
174
172
175
- QString DocumentContextReader::getContextBetween (int startLine, int endLine, int cursorPosition) const
173
+ QString DocumentContextReader::getContextBetween (
174
+ int startLine, int startCursorPosition, int endLine, int endCursorPosition) const
176
175
{
177
176
QString context;
178
- for (int i = startLine; i <= endLine; ++i) {
179
- QTextBlock block = m_document->findBlockByNumber (i);
177
+
178
+ if (startLine > endLine) {
179
+ return context;
180
+ }
181
+
182
+ if (startLine == endLine) {
183
+ auto block = m_document->findBlockByNumber (startLine);
184
+ if (!block.isValid ()) {
185
+ return context;
186
+ }
187
+
188
+ auto text = block.text ();
189
+
190
+ if (startCursorPosition < 0 ) {
191
+ startCursorPosition = 0 ;
192
+ }
193
+ if (endCursorPosition < 0 ) {
194
+ endCursorPosition = text.size ();
195
+ }
196
+
197
+ if (startCursorPosition >= endCursorPosition) {
198
+ return context;
199
+ }
200
+
201
+ return text.mid (startCursorPosition, endCursorPosition - startCursorPosition);
202
+ }
203
+
204
+ // first line
205
+ {
206
+ auto block = m_document->findBlockByNumber (startLine);
207
+ if (!block.isValid ()) {
208
+ return context;
209
+ }
210
+ auto text = block.text ();
211
+ if (startCursorPosition < 0 ) {
212
+ context += text + " \n " ;
213
+ } else {
214
+ context += text.right (text.size () - startCursorPosition) + " \n " ;
215
+ }
216
+ }
217
+
218
+ // intermediate lines, if any
219
+ for (int i = startLine + 1 ; i <= endLine - 1 ; ++i) {
220
+ auto block = m_document->findBlockByNumber (i);
221
+ if (!block.isValid ()) {
222
+ return context;
223
+ }
224
+ context += block.text () + " \n " ;
225
+ }
226
+
227
+ // last line
228
+ {
229
+ auto block = m_document->findBlockByNumber (endLine);
180
230
if (!block.isValid ()) {
181
- break ;
231
+ return context ;
182
232
}
183
- if (i == endLine) {
184
- context += block.text ().left (cursorPosition);
233
+ auto text = block.text ();
234
+ if (endCursorPosition < 0 ) {
235
+ context += text;
185
236
} else {
186
- context += block. text () + " \n " ;
237
+ context += text. left (endCursorPosition) ;
187
238
}
188
239
}
189
240
@@ -222,7 +273,7 @@ QString DocumentContextReader::getContextBefore(int lineNumber, int cursorPositi
222
273
} else {
223
274
effectiveStartLine = qMax (0 , lineNumber - beforeCursor);
224
275
}
225
- return getContextBetween (effectiveStartLine, lineNumber, cursorPosition);
276
+ return getContextBetween (effectiveStartLine, - 1 , lineNumber, cursorPosition);
226
277
}
227
278
}
228
279
@@ -234,7 +285,7 @@ QString DocumentContextReader::getContextAfter(int lineNumber, int cursorPositio
234
285
int endLine = qMin (
235
286
m_document->blockCount () - 1 ,
236
287
lineNumber + Settings::codeCompletionSettings ().readStringsAfterCursor ());
237
- return getContextBetween (lineNumber + 1 , endLine, -1 );
288
+ return getContextBetween (lineNumber + 1 , cursorPosition, endLine, -1 );
238
289
}
239
290
}
240
291
0 commit comments