Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/codedocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ void CodeDocument::deleteSymbol(const Symbol &symbol)
Core::SymbolList CodeDocument::symbols() const
{
LOG();
return m_treeSitterHelper->symbols();
RangeMark::temporaryDocumentText = text();
const auto &symbols = m_treeSitterHelper->symbols();
RangeMark::temporaryDocumentText.clear();
return symbols;
}

struct RegexpTransform
Expand Down
40 changes: 28 additions & 12 deletions src/core/cppdocument.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of Knut.

SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Expand Down Expand Up @@ -1223,26 +1223,39 @@ void CppDocument::toggleSection()
gotoLine(line + 3);

} else {
// Check that we are in a function
auto isFunction = [](const Symbol &symbol) {
return symbol.isFunction();
auto matches = query(QString(R"EOF(
(function_definition
(type_qualifier)? @return
type: (_)? @return
declarator: [
(function_declarator
declarator: (_) @name)
(_
(function_declarator
declarator: (_) @name)) @full_name
]
body: (compound_statement) @body)
)EOF"));
auto cursorPos = cursor.position();
auto isInBody = [cursorPos](const QueryMatch &match) {
return match.get("body").contains(cursorPos);
};
auto symbol = currentSymbol(isFunction);
if (!symbol)
const auto functionMatch = kdalgorithms::find_if(matches, isInBody);
if (!functionMatch)
return;

auto cursorPos = cursor.position();
const auto range = functionMatch->get("body");

cursor.beginEditBlock();
// Start from the end
cursor.setPosition(symbol->range().end());
cursor.setPosition(range.end());
cursor.movePosition(QTextCursor::StartOfLine);
cursor.movePosition(QTextCursor::Up, QTextCursor::KeepAnchor);

if (cursor.selectedText().startsWith(endifString)) {
// The function is already commented out, remove the comments
int start = textEdit()->document()->find(elseString, cursor, QTextDocument::FindBackward).selectionStart();
if (start > symbol->range().start())
if (start > range.start())
cursor.setPosition(start, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
cursor.setPosition(moveBlock(cursor.position(), QTextCursor::PreviousCharacter));
Expand All @@ -1252,14 +1265,17 @@ void CppDocument::toggleSection()
cursor.removeSelectedText();
cursorPos -= ifdefString.length() + 1;
} else {
const auto name = functionMatch->get("name").text();
const auto returnType = functionMatch->get("return").text()
+ (functionMatch->get("full_name").text().startsWith("*") ? "*" : "");

// Comment out the function with #if/#def, make sure to return something if needed
cursor.setPosition(symbol->range().end());
cursor.setPosition(range.end());
cursor.movePosition(QTextCursor::PreviousCharacter);

QString text = elseString + newLine;
if (!sectionSettings.debug.isEmpty())
text += tab() + sectionSettings.debug.arg(symbol->name()) + ";\n";
const QString returnType = symbol->toFunction()->returnType();
text += tab() + sectionSettings.debug.arg(name) + ";\n";
auto it = sectionSettings.return_values.find(returnType.toStdString());
if (it != sectionSettings.return_values.end())
text += tab() + QString("return %1;\n").arg(QString::fromStdString(it->second));
Expand Down
22 changes: 15 additions & 7 deletions src/core/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,31 @@
* Log a method, with all its parameters.
*/
#define LOG(...) \
Core::LoggerObject __loggerObject(Core::formatToClassNameFunctionName(std::source_location::current()), false, \
##__VA_ARGS__)
Core::LoggerObject __loggerObject( \
Core::LoggerObject::canLog() ? Core::formatToClassNameFunctionName(std::source_location::current()) : "", \
false, ##__VA_ARGS__)

/**
* Log a method, with all its parameters. If the previous log is also the same method, it will be merged into one
* operation
*/
#define LOG_AND_MERGE(...) \
Core::LoggerObject __loggerObject(Core::formatToClassNameFunctionName(std::source_location::current()), true, \
##__VA_ARGS__)
Core::LoggerObject __loggerObject( \
Core::LoggerObject::canLog() ? Core::formatToClassNameFunctionName(std::source_location::current()) : "", \
true, ##__VA_ARGS__)

/**
* Macro to save the returned value in the historymodel
*/
#define LOG_RETURN(name, value) \
do { \
const auto &__value = value; \
__loggerObject.setReturnValue(name, __value); \
return __value; \
if (Core::LoggerObject::canLog()) { \
const auto &__value = value; \
__loggerObject.setReturnValue(name, __value); \
return __value; \
} else { \
return value; \
} \
} while (false)

namespace Core {
Expand Down Expand Up @@ -293,6 +299,8 @@ class LoggerObject
m_model->setReturnValue(std::move(name), value);
}

static bool canLog() { return m_canLog; }

private:
friend HistoryModel;
friend LoggerDisabler;
Expand Down
13 changes: 10 additions & 3 deletions src/core/rangemark.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of Knut.

SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Expand Down Expand Up @@ -134,9 +134,16 @@ TextDocument *RangeMark::document() const

QString RangeMark::text() const
{
if (!isValid())
return {};

auto text = temporaryDocumentText.isEmpty() ? document()->text() : temporaryDocumentText;
// <= here instead of < because m_end is exclusive
if (isValid() && end() <= document()->text().size())
return document()->text().sliced(start(), end() - start());
if (isValid() && end() <= text.size()) {
text = text.sliced(start(), end() - start());
text.squeeze();
return text;
}
return {};
}

Expand Down
7 changes: 6 additions & 1 deletion src/core/rangemark.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of Knut.

SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Expand Down Expand Up @@ -62,6 +62,11 @@ class RangeMark

bool operator==(const RangeMark &other) const;

protected:
friend class CodeDocument;
// This is used to reduce memory allocations when extracting symbols
inline static QString temporaryDocumentText = {};

private:
std::shared_ptr<class RangeMarkPrivate> d = nullptr;

Expand Down
4 changes: 2 additions & 2 deletions src/core/textdocument.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
This file is part of Knut.

SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Expand Down Expand Up @@ -428,7 +428,7 @@ int TextDocument::positionAt(int line, int column)
QString TextDocument::text() const
{
LOG();
LOG_RETURN("text", m_document->toPlainText());
return m_document->toPlainText();
}

void TextDocument::setText(const QString &newText)
Expand Down
11 changes: 11 additions & 0 deletions test_data/tst_cppdocument/toggleSection/section.cpp.expected
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ Section *Section::create()
return nullptr;
#endif // KDAB_TEMPORARILY_REMOVED
}

Section &Section::reference()
{
#ifdef KDAB_TEMPORARILY_REMOVED
computeText();
return *m_pointer;
#else // KDAB_TEMPORARILY_REMOVED
qDebug("Section::reference is commented out");
return {};
#endif // KDAB_TEMPORARILY_REMOVED
}
6 changes: 6 additions & 0 deletions test_data/tst_cppdocument/toggleSection/section.cpp.original
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ Section *Section::create()
computeText();
return new Section;
}

Section &Section::reference()
{
computeText();
return *m_pointer;
}
3 changes: 3 additions & 0 deletions test_data/tst_cppdocument/toggleSection/section.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ class Section {
int bar();

static Section *create();
Section &reference();
private:
Section *m_pointer;
};
2 changes: 2 additions & 0 deletions tests/tst_cppdocument_clangd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ private slots:
auto cppFile = qobject_cast<Core::CppDocument *>(Core::Project::instance()->get(file.fileName()));

// Comment out full methods (start from the end)
cppFile->gotoLine(50);
cppFile->toggleSection();
cppFile->gotoLine(42);
cppFile->toggleSection();
cppFile->gotoLine(35);
Expand Down
Loading