@@ -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,26 @@ 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
177
for (int i = startLine; i <= endLine; ++i) {
179
178
QTextBlock block = m_document->findBlockByNumber (i);
180
179
if (!block.isValid ()) {
181
180
break ;
182
181
}
183
- if (i == endLine) {
184
- context += block.text ().left (cursorPosition);
182
+ auto text = block.text ();
183
+ if (i == startLine && startCursorPosition >= 0 ) {
184
+ context += text.right (text.size () - startCursorPosition);
185
+ } else if (i == endLine && endCursorPosition >= 0 ) {
186
+ context += text.left (endCursorPosition);
185
187
} else {
186
- context += block.text () + " \n " ;
188
+ context += text;
189
+ }
190
+
191
+ if (i < endLine) {
192
+ context += " \n " ;
187
193
}
188
194
}
189
195
@@ -222,7 +228,7 @@ QString DocumentContextReader::getContextBefore(int lineNumber, int cursorPositi
222
228
} else {
223
229
effectiveStartLine = qMax (0 , lineNumber - beforeCursor);
224
230
}
225
- return getContextBetween (effectiveStartLine, lineNumber, cursorPosition);
231
+ return getContextBetween (effectiveStartLine, - 1 , lineNumber, cursorPosition);
226
232
}
227
233
}
228
234
@@ -234,7 +240,7 @@ QString DocumentContextReader::getContextAfter(int lineNumber, int cursorPositio
234
240
int endLine = qMin (
235
241
m_document->blockCount () - 1 ,
236
242
lineNumber + Settings::codeCompletionSettings ().readStringsAfterCursor ());
237
- return getContextBetween (lineNumber + 1 , endLine, -1 );
243
+ return getContextBetween (lineNumber + 1 , cursorPosition, endLine, -1 );
238
244
}
239
245
}
240
246
0 commit comments