| 182 | } |
| 183 | |
| 184 | PluginLLMCore::ContextData QuickRefactorHandler::prepareContext( |
| 185 | TextEditor::TextEditorWidget *editor, |
| 186 | const Utils::Text::Range &range, |
| 187 | const QString &instructions) |
| 188 | { |
| 189 | PluginLLMCore::ContextData context; |
| 190 | |
| 191 | auto textDocument = editor->textDocument(); |
| 192 | Context::DocumentReaderQtCreator documentReader; |
| 193 | auto documentInfo = documentReader.readDocument(textDocument->filePath().toUrlishString()); |
| 194 | |
| 195 | if (!documentInfo.document) { |
| 196 | LOG_MESSAGE("Error: Document is not available"); |
| 197 | return context; |
| 198 | } |
| 199 | |
| 200 | QTextCursor cursor = editor->textCursor(); |
| 201 | int cursorPos = cursor.position(); |
| 202 | |
| 203 | Context::DocumentContextReader |
| 204 | reader(documentInfo.document, documentInfo.mimeType, documentInfo.filePath); |
| 205 | |
| 206 | QString taggedContent; |
| 207 | bool readFullFile = Settings::quickRefactorSettings().readFullFile(); |
| 208 | |
| 209 | if (cursor.hasSelection()) { |
| 210 | int selStart = cursor.selectionStart(); |
| 211 | int selEnd = cursor.selectionEnd(); |
| 212 | |
| 213 | QTextBlock startBlock = documentInfo.document->findBlock(selStart); |
| 214 | int startLine = startBlock.blockNumber(); |
| 215 | int startColumn = selStart - startBlock.position(); |
| 216 | |
| 217 | QTextBlock endBlock = documentInfo.document->findBlock(selEnd); |
| 218 | int endLine = endBlock.blockNumber(); |
| 219 | int endColumn = selEnd - endBlock.position(); |
| 220 | |
| 221 | QString contextBefore; |
| 222 | if (readFullFile) { |
| 223 | contextBefore = reader.readWholeFileBefore(startLine, startColumn); |
| 224 | } else { |
| 225 | contextBefore = reader.getContextBefore( |
| 226 | startLine, startColumn, Settings::quickRefactorSettings().readStringsBeforeCursor() + 1); |
| 227 | } |
| 228 | |
| 229 | QString selectedText = cursor.selectedText(); |
| 230 | selectedText.replace(QChar(0x2029), "\n"); |
| 231 | |
| 232 | QString contextAfter; |
| 233 | if (readFullFile) { |
| 234 | contextAfter = reader.readWholeFileAfter(endLine, endColumn); |
| 235 | } else { |
| 236 | contextAfter = reader.getContextAfter( |
| 237 | endLine, endColumn, Settings::quickRefactorSettings().readStringsAfterCursor() + 1); |
| 238 | } |
| 239 | |
| 240 | taggedContent = contextBefore; |
| 241 | if (selStart == cursorPos) { |
nothing calls this directly
no test coverage detected