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 16, 2024
1 parent 8227867 commit 0637adc
Show file tree
Hide file tree
Showing 38 changed files with 281 additions and 206 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", LOGFUNCTION());
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!", LOGFUNCTION(), m_name);
return {};
}

Expand Down
48 changes: 25 additions & 23 deletions src/core/codedocument.cpp
Original file line number Diff line number Diff line change
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("{}", LOGFUNCTION());

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",
LOGFUNCTION());
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", LOGFUNCTION());
}
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("{}", LOGFUNCTION());

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!", LOGFUNCTION());
}
} else {
spdlog::warn("CodeDocument::references: LSP call to references returned nothing!");
spdlog::warn("{}: LSP call to references returned nothing!", LOGFUNCTION());
}

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("{}", LOGFUNCTION());
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!", LOGFUNCTION());
// 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", LOGFUNCTION(), 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("{}", LOGFUNCTION());
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!", LOGFUNCTION());
return nullptr;
}

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?", LOGFUNCTION());
}

LOG_RETURN("pos", position());
Expand Down Expand Up @@ -718,17 +718,17 @@ Core::QueryMatchList CodeDocument::queryInRange(const Core::RangeMark &range, co
LOG("CodeDocument::queryInRange", 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", LOGFUNCTION());
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", LOGFUNCTION());
return {};
}
spdlog::debug("CodeDocument::queryInRange: Found {} nodes in range", nodes.size());
spdlog::debug("{}: Found {} nodes in range", LOGFUNCTION(), 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", LOGFUNCTION(), 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: {}, {}", LOGFUNCTION(), 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: {}, {}", LOGFUNCTION(), newEndBlock.blockNumber(), newEndPosition -
// newEndBlock.position());

// const auto plain = document->toPlainText();
// spdlog::warn("added: {}", plain.sliced(position, charsAdded));
// spdlog::warn("{} - added: {}", LOGFUNCTION(), 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!", LOGFUNCTION());
}
}

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!", LOGFUNCTION());
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 {}!", LOGFUNCTION(), 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: {}", LOGFUNCTION(), query, error.description,
error.utf8_offset);
return {};
}
return tsQuery;
Expand Down
Loading

0 comments on commit 0637adc

Please sign in to comment.