Skip to content

Commit f47c1ba

Browse files
committedMar 8, 2025
refactor: Reuse extractFilePathFromRequest() more
1 parent 6c32364 commit f47c1ba

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed
 

‎LLMClientInterface.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,13 @@
3131
#include "context/DocumentContextReader.hpp"
3232
#include "llmcore/PromptTemplateManager.hpp"
3333
#include "llmcore/ProvidersManager.hpp"
34+
#include "llmcore/Utils.hpp"
3435
#include "logger/Logger.hpp"
3536
#include "settings/CodeCompletionSettings.hpp"
3637
#include "settings/GeneralSettings.hpp"
3738

3839
namespace QodeAssist {
3940

40-
QString extractFilePathFromRequest(const QJsonObject &request)
41-
{
42-
QJsonObject params = request["params"].toObject();
43-
QJsonObject doc = params["doc"].toObject();
44-
QString uri = doc["uri"].toString();
45-
return QUrl(uri).toLocalFile();
46-
}
47-
4841
LLMClientInterface::LLMClientInterface(
4942
const Settings::GeneralSettings &generalSettings,
5043
const Settings::CodeCompletionSettings &completeSettings)
@@ -263,20 +256,20 @@ LLMCore::ContextData LLMClientInterface::prepareContext(
263256
QJsonObject doc = params["doc"].toObject();
264257
QJsonObject position = doc["position"].toObject();
265258

266-
Utils::FilePath filePath = Utils::FilePath::fromString(extractFilePathFromRequest(request));
259+
auto filePath = LLMCore::extractFilePathFromRequest(request);
267260
TextEditor::TextDocument *textDocument = TextEditor::TextDocument::textDocumentForFilePath(
268-
filePath);
261+
Utils::FilePath::fromString(filePath));
269262

270263
if (!textDocument) {
271-
LOG_MESSAGE("Error: Document is not available for" + filePath.toString());
264+
LOG_MESSAGE("Error: Document is not available for" + filePath);
272265
return LLMCore::ContextData{};
273266
}
274267

275268
int cursorPosition = position["character"].toInt();
276269
int lineNumber = position["line"].toInt();
277270

278-
Context::DocumentContextReader reader(
279-
textDocument->document(), textDocument->mimeType(), textDocument->filePath().toString());
271+
Context::DocumentContextReader
272+
reader(textDocument->document(), textDocument->mimeType(), filePath);
280273
return reader.prepareContext(lineNumber, cursorPosition, m_completeSettings);
281274
}
282275

@@ -306,7 +299,7 @@ void LLMClientInterface::sendCompletionToClient(
306299
QString processedCompletion
307300
= promptTemplate->type() == LLMCore::TemplateType::Chat
308301
&& m_completeSettings.smartProcessInstuctText()
309-
? CodeHandler::processText(completion, extractFilePathFromRequest(request))
302+
? CodeHandler::processText(completion, LLMCore::extractFilePathFromRequest(request))
310303
: completion;
311304

312305
completionItem[LanguageServerProtocol::textKey] = processedCompletion;

‎context/ContextManager.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "GeneralSettings.hpp"
2828
#include "Logger.hpp"
29+
#include <llmcore/Utils.hpp>
2930
#include <texteditor/textdocument.h>
3031
#include <utils/filepath.h>
3132

@@ -72,16 +73,12 @@ ContentFile ContextManager::createContentFile(const QString &filePath) const
7273

7374
ProgrammingLanguage ContextManager::getDocumentLanguage(const QJsonObject &request)
7475
{
75-
QJsonObject params = request["params"].toObject();
76-
QJsonObject doc = params["doc"].toObject();
77-
QString uri = doc["uri"].toString();
78-
79-
Utils::FilePath filePath = Utils::FilePath::fromString(QUrl(uri).toLocalFile());
76+
auto filePath = LLMCore::extractFilePathFromRequest(request);
8077
TextEditor::TextDocument *textDocument = TextEditor::TextDocument::textDocumentForFilePath(
81-
filePath);
78+
Utils::FilePath::fromString(filePath));
8279

8380
if (!textDocument) {
84-
LOG_MESSAGE("Error: Document is not available for" + filePath.toString());
81+
LOG_MESSAGE("Error: Document is not available for" + filePath);
8582
return Context::ProgrammingLanguage::Unknown;
8683
}
8784

‎llmcore/Utils.hpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2025 Povilas Kanapickas <povilas@radix.lt>
3+
*
4+
* This file is part of QodeAssist.
5+
*
6+
* QodeAssist is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* QodeAssist is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#pragma once
21+
22+
#include <QJsonObject>
23+
24+
namespace QodeAssist::LLMCore {
25+
26+
inline QString extractFilePathFromRequest(const QJsonObject &request)
27+
{
28+
QJsonObject params = request["params"].toObject();
29+
QJsonObject doc = params["doc"].toObject();
30+
QString uri = doc["uri"].toString();
31+
return QUrl(uri).toLocalFile();
32+
}
33+
34+
} // namespace QodeAssist::LLMCore

0 commit comments

Comments
 (0)