Skip to content

Commit

Permalink
refactor: Use std::source_location for the LOG
Browse files Browse the repository at this point in the history
Fixes: #109
  • Loading branch information
mgiroday committed Aug 22, 2024
1 parent 6041dbf commit fc65df2
Show file tree
Hide file tree
Showing 38 changed files with 413 additions and 361 deletions.
2 changes: 1 addition & 1 deletion src/core/astnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool AstNode::isValid() const
std::optional<treesitter::Node> AstNode::node() const
{
if (!isValid()) {
spdlog::warn("AstNode is invalid");
spdlog::warn("{}: AstNode is invalid", FUNCTION);
return std::nullopt;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/classsymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ QList<Symbol *> ClassSymbol::findMembers() const
}
return members;
}
spdlog::warn("Parent of CppClass {} is not an CodeDocument!", m_name);
spdlog::warn("{}: Parent of CppClass {} is not an CodeDocument!", FUNCTION, m_name);
return {};
}

Expand Down
60 changes: 31 additions & 29 deletions src/core/codedocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void CodeDocument::deleteSymbol(const Symbol &symbol)
*/
Core::SymbolList CodeDocument::symbols() const
{
LOG("CodeDocument::symbols");
LOG(FUNCTION);
return m_treeSitterHelper->symbols();
}

Expand Down Expand Up @@ -199,7 +199,7 @@ QString CodeDocument::hover(int position, std::function<void(const QString &)> a
std::pair<QString, std::optional<RangeMark>> CodeDocument::hoverWithRange(
int position, std::function<void(const QString &, std::optional<RangeMark>)> asyncCallback /* = {} */) const
{
spdlog::debug("CodeDocument::hover");
spdlog::debug("{}", FUNCTION);

if (!checkClient())
return {"", {}};
Expand All @@ -226,8 +226,10 @@ std::pair<QString, std::optional<RangeMark>> CodeDocument::hoverWithRange(
if (const auto *content = std::get_if<Lsp::MarkupContent>(&hover.contents)) {
return {QString::fromStdString(content->value), range};
} else {
spdlog::warn("LSP returned deprecated MarkedString type which is unsupported by Knut\n - Consider updating "
"your LSP server");
spdlog::warn(
"{}: LSP returned deprecated MarkedString type which is unsupported by Knut\n - Consider updating "
"your LSP server",
FUNCTION);
return {"", {}};
}
};
Expand All @@ -245,7 +247,7 @@ std::pair<QString, std::optional<RangeMark>> CodeDocument::hoverWithRange(
// a Tooltip is requested.
// See: TextView::eventFilter.
if (!std::holds_alternative<Lsp::Hover>(result.value())) {
spdlog::debug("LSP server returned no result for Hover");
spdlog::debug("{}: LSP server returned no result for Hover", FUNCTION);
}
return convertResult(result.value());
}
Expand All @@ -256,7 +258,7 @@ std::pair<QString, std::optional<RangeMark>> CodeDocument::hoverWithRange(

RangeMarkList CodeDocument::references(int position) const
{
spdlog::debug("CodeDocument::references");
spdlog::debug("{}", FUNCTION);

if (!checkClient()) {
return {};
Expand All @@ -271,10 +273,10 @@ RangeMarkList CodeDocument::references(int position) const
if (const auto *locations = std::get_if<std::vector<Lsp::Location>>(&value)) {
return Utils::lspToRangeMarkList(*locations);
} else {
spdlog::warn("CodeDocument::references: Language server returned unsupported references type!");
spdlog::warn("{}: Language server returned unsupported references type!", FUNCTION);
}
} else {
spdlog::warn("CodeDocument::references: LSP call to references returned nothing!");
spdlog::warn("{}: LSP call to references returned nothing!", FUNCTION);
}

return {};
Expand All @@ -283,7 +285,7 @@ RangeMarkList CodeDocument::references(int position) const
// Follows the symbol under the cursor.
Document *CodeDocument::followSymbol()
{
spdlog::debug("CodeDocument::followSymbol");
spdlog::debug("{}", FUNCTION);
if (!checkClient())
return {};

Expand Down Expand Up @@ -332,7 +334,7 @@ Document *CodeDocument::followSymbol(int pos)
return nullptr;

if (locations.size() > 1)
spdlog::warn("CodeDocument::followSymbol: Multiple locations returned!");
spdlog::warn("{}: Multiple locations returned!", FUNCTION);
// Heuristic: If multiple locations were found, use the last one.
auto location = locations.back();

Expand All @@ -347,8 +349,7 @@ Document *CodeDocument::followSymbol(int pos)
if (auto *codeDocument = qobject_cast<CodeDocument *>(document)) {
codeDocument->selectRange(Utils::lspToRange(*codeDocument, location.range));
} else {
spdlog::warn("CodeDocument::followSymbol: Opened document '{}' is not an CodeDocument",
document->fileName());
spdlog::warn("{}: Opened document '{}' is not an CodeDocument", FUNCTION, document->fileName());
}
}

Expand All @@ -358,7 +359,7 @@ Document *CodeDocument::followSymbol(int pos)
// Switches between the function declaration or definition.
Document *CodeDocument::switchDeclarationDefinition()
{
spdlog::debug("CodeDocument::switchDeclarationDefinition");
spdlog::debug("{}", FUNCTION);
if (!checkClient())
return {};

Expand All @@ -371,7 +372,7 @@ Document *CodeDocument::switchDeclarationDefinition()
});

if (!currentFunction) {
spdlog::info("CodeDocument::switchDeclarationDefinition: Cursor is currently not within a function!");
spdlog::info("{}: Cursor is currently not within a function!", FUNCTION);
return nullptr;
}

Expand All @@ -390,7 +391,7 @@ Document *CodeDocument::switchDeclarationDefinition()
*/
void CodeDocument::selectSymbol(const QString &name, int options)
{
LOG("CodeDocument::selectSymbol", LOG_ARG("text", name), options);
LOG(FUNCTION, LOG_ARG("text", name), options);

if (auto symbol = findSymbol(name, options))
selectRange(symbol->selectionRange());
Expand Down Expand Up @@ -469,8 +470,7 @@ int CodeDocument::selectSmallerSyntaxNode(int count /* = 1*/)
if (node.has_value()) {
selectRegion(node->startPosition(), node->endPosition());
} else {
spdlog::warn(
"CodeDocument::selectSmallerSyntaxNode: No smaller node found! Do you currently not have a selection?");
spdlog::warn("{}: No smaller node found! Do you currently not have a selection?", FUNCTION);
}

LOG_RETURN("pos", position());
Expand Down Expand Up @@ -566,7 +566,7 @@ int CodeDocument::selectPreviousSyntaxNode(int count /*= 1*/)
*/
Symbol *CodeDocument::findSymbol(const QString &name, int options) const
{
LOG("CodeDocument::findSymbol", LOG_ARG("text", name), options);
LOG(FUNCTION, LOG_ARG("text", name), options);

auto symbols = this->symbols();
const auto regexp =
Expand Down Expand Up @@ -681,7 +681,7 @@ Core::QueryMatchList CodeDocument::query(const std::shared_ptr<treesitter::Query
*/
Core::QueryMatchList CodeDocument::query(const QString &query)
{
LOG("CodeDocument::query", LOG_ARG("query", query));
LOG(FUNCTION, LOG_ARG("query", query));

return this->query(m_treeSitterHelper->constructQuery(query));
}
Expand All @@ -701,7 +701,7 @@ Core::QueryMatchList CodeDocument::query(const QString &query)
*/
Core::QueryMatch CodeDocument::queryFirst(const QString &query)
{
LOG("CodeDocument::queryOne", LOG_ARG("query", query));
LOG(FUNCTION, LOG_ARG("query", query));

return this->queryFirst(m_treeSitterHelper->constructQuery(query));
}
Expand All @@ -715,20 +715,20 @@ Core::QueryMatch CodeDocument::queryFirst(const QString &query)
*/
Core::QueryMatchList CodeDocument::queryInRange(const Core::RangeMark &range, const QString &query)
{
LOG("CodeDocument::queryInRange", LOG_ARG("range", range), LOG_ARG("query", query));
LOG(FUNCTION, LOG_ARG("range", range), LOG_ARG("query", query));

if (!range.isValid()) {
spdlog::warn("CodeDocument::queryInRange: Range is not valid");
spdlog::warn("{}: Range is not valid", FUNCTION);
return {};
}

const auto nodes = m_treeSitterHelper->nodesInRange(range);

if (nodes.isEmpty()) {
spdlog::warn("CodeDocument::queryInRange: No nodes in range");
spdlog::warn("{}: No nodes in range", FUNCTION);
return {};
}
spdlog::debug("CodeDocument::queryInRange: Found {} nodes in range", nodes.size());
spdlog::debug("{}: Found {} nodes in range", FUNCTION, nodes.size());

auto tsQuery = m_treeSitterHelper->constructQuery(query);
if (!tsQuery)
Expand All @@ -755,7 +755,7 @@ bool CodeDocument::checkClient() const
{
Q_ASSERT(textEdit());
if (!client()) {
spdlog::error("CodeDocument {} has no LSP client - API not available", fileName());
spdlog::error("{}: CodeDocument {} has no LSP client - API not available", FUNCTION, fileName());
return false;
}
return true;
Expand All @@ -770,14 +770,16 @@ void CodeDocument::changeContentLsp(int position, int charsRemoved, int charsAdd
// TODO: Keep copy of previous string around, so we can find the oldEndPosition.
// const auto document = textEdit()->document();
// const auto startblock = document->findBlock(position);
// spdlog::warn("start point: {}, {}", startblock.blockNumber(), position - startblock.position());
// spdlog::warn("{} - start point: {}, {}", FUNCTION, startblock.blockNumber(), position -
// startblock.position());

// const auto newEndPosition = position + charsAdded;
// const auto newEndBlock = document->findBlock(newEndPosition);
// spdlog::warn("new end point: {}, {}", newEndBlock.blockNumber(), newEndPosition - newEndBlock.position());
// spdlog::warn("{} - new end point: {}, {}", FUNCTION, newEndBlock.blockNumber(), newEndPosition -
// newEndBlock.position());

// const auto plain = document->toPlainText();
// spdlog::warn("added: {}", plain.sliced(position, charsAdded));
// spdlog::warn("{} - added: {}", FUNCTION, plain.sliced(position, charsAdded));

if (!checkClient()) {
return;
Expand Down Expand Up @@ -811,7 +813,7 @@ void CodeDocument::changeContentLsp(int position, int charsRemoved, int charsAdd

client()->didChange(std::move(params));
} else {
spdlog::error("LSP server does not support Document changes!");
spdlog::error("{}: LSP server does not support Document changes!", FUNCTION);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/core/codedocument_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ std::optional<treesitter::Tree> &TreeSitterHelper::syntaxTree()
if (!m_tree) {
auto &parser = this->parser();
if (!parser.setIncludedRanges(m_document->includedRanges())) {
spdlog::warn("TreeSitterHelper::syntaxTree: Unable to set the included ranges on the treesitter parser!");
spdlog::warn("{}: Unable to set the included ranges on the treesitter parser!", FUNCTION);
parser.setIncludedRanges({});
}
m_tree = parser.parseString(m_document->text());
if (!m_tree) {
spdlog::warn("CodeDocument::syntaxTree: Failed to parse document {}!", m_document->fileName());
spdlog::warn("{}: Failed to parse document {}!", FUNCTION, m_document->fileName());
}
}
return m_tree;
Expand All @@ -67,8 +67,8 @@ std::shared_ptr<treesitter::Query> TreeSitterHelper::constructQuery(const QStrin
try {
tsQuery = std::make_shared<treesitter::Query>(parser().language(), query);
} catch (treesitter::Query::Error &error) {
spdlog::error("CodeDocument::constructQuery: Failed to parse query `{}` error: {} at: {}", query,
error.description, error.utf8_offset);
spdlog::error("{}: Failed to parse query `{}` error: {} at: {}", FUNCTION, query, error.description,
error.utf8_offset);
return {};
}
return tsQuery;
Expand Down
Loading

0 comments on commit fc65df2

Please sign in to comment.