Skip to content

Commit 246a283

Browse files
Nicolas Arnaud-Cormosnarnaud
authored andcommitted
perf: Use a query for toggling section
This is faster than getting all the symbols, as we don't need to do any kind of processing of the symbols.
1 parent fc77f81 commit 246a283

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

src/core/cppdocument.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
This file is part of Knut.
33
44
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
@@ -1223,26 +1223,39 @@ void CppDocument::toggleSection()
12231223
gotoLine(line + 3);
12241224

12251225
} else {
1226-
// Check that we are in a function
1227-
auto isFunction = [](const Symbol &symbol) {
1228-
return symbol.isFunction();
1226+
auto matches = query(QString(R"EOF(
1227+
(function_definition
1228+
(type_qualifier)? @return
1229+
type: (_)? @return
1230+
declarator: [
1231+
(function_declarator
1232+
declarator: (_) @name)
1233+
(_
1234+
(function_declarator
1235+
declarator: (_) @name)) @full_name
1236+
]
1237+
body: (compound_statement) @body)
1238+
)EOF"));
1239+
auto cursorPos = cursor.position();
1240+
auto isInBody = [cursorPos](const QueryMatch &match) {
1241+
return match.get("body").contains(cursorPos);
12291242
};
1230-
auto symbol = currentSymbol(isFunction);
1231-
if (!symbol)
1243+
const auto functionMatch = kdalgorithms::find_if(matches, isInBody);
1244+
if (!functionMatch)
12321245
return;
12331246

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

12361249
cursor.beginEditBlock();
12371250
// Start from the end
1238-
cursor.setPosition(symbol->range().end());
1251+
cursor.setPosition(range.end());
12391252
cursor.movePosition(QTextCursor::StartOfLine);
12401253
cursor.movePosition(QTextCursor::Up, QTextCursor::KeepAnchor);
12411254

12421255
if (cursor.selectedText().startsWith(endifString)) {
12431256
// The function is already commented out, remove the comments
12441257
int start = textEdit()->document()->find(elseString, cursor, QTextDocument::FindBackward).selectionStart();
1245-
if (start > symbol->range().start())
1258+
if (start > range.start())
12461259
cursor.setPosition(start, QTextCursor::KeepAnchor);
12471260
cursor.removeSelectedText();
12481261
cursor.setPosition(moveBlock(cursor.position(), QTextCursor::PreviousCharacter));
@@ -1252,14 +1265,17 @@ void CppDocument::toggleSection()
12521265
cursor.removeSelectedText();
12531266
cursorPos -= ifdefString.length() + 1;
12541267
} else {
1268+
const auto name = functionMatch->get("name").text();
1269+
const auto returnType = functionMatch->get("return").text()
1270+
+ (functionMatch->get("full_name").text().startsWith("*") ? "*" : "");
1271+
12551272
// Comment out the function with #if/#def, make sure to return something if needed
1256-
cursor.setPosition(symbol->range().end());
1273+
cursor.setPosition(range.end());
12571274
cursor.movePosition(QTextCursor::PreviousCharacter);
12581275

12591276
QString text = elseString + newLine;
12601277
if (!sectionSettings.debug.isEmpty())
1261-
text += tab() + sectionSettings.debug.arg(symbol->name()) + ";\n";
1262-
const QString returnType = symbol->toFunction()->returnType();
1278+
text += tab() + sectionSettings.debug.arg(name) + ";\n";
12631279
auto it = sectionSettings.return_values.find(returnType.toStdString());
12641280
if (it != sectionSettings.return_values.end())
12651281
text += tab() + QString("return %1;\n").arg(QString::fromStdString(it->second));

src/core/rangemark.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ TextDocument *RangeMark::document() const
134134

135135
QString RangeMark::text() const
136136
{
137+
if (!isValid())
138+
return {};
139+
137140
auto text = temporaryDocumentText.isEmpty() ? document()->text() : temporaryDocumentText;
138141
// <= here instead of < because m_end is exclusive
139142
if (isValid() && end() <= text.size()) {

test_data/tst_cppdocument/toggleSection/section.cpp.expected

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,14 @@ Section *Section::create()
6464
return nullptr;
6565
#endif // KDAB_TEMPORARILY_REMOVED
6666
}
67+
68+
Section &Section::reference()
69+
{
70+
#ifdef KDAB_TEMPORARILY_REMOVED
71+
computeText();
72+
return *m_pointer;
73+
#else // KDAB_TEMPORARILY_REMOVED
74+
qDebug("Section::reference is commented out");
75+
return {};
76+
#endif // KDAB_TEMPORARILY_REMOVED
77+
}

test_data/tst_cppdocument/toggleSection/section.cpp.original

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ Section *Section::create()
4444
computeText();
4545
return new Section;
4646
}
47+
48+
Section &Section::reference()
49+
{
50+
computeText();
51+
return *m_pointer;
52+
}

test_data/tst_cppdocument/toggleSection/section.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ class Section {
1010
int bar();
1111

1212
static Section *create();
13+
Section &reference();
14+
private:
15+
Section *m_pointer;
1316
};

tests/tst_cppdocument_clangd.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private slots:
6868
auto cppFile = qobject_cast<Core::CppDocument *>(Core::Project::instance()->get(file.fileName()));
6969

7070
// Comment out full methods (start from the end)
71+
cppFile->gotoLine(50);
72+
cppFile->toggleSection();
7173
cppFile->gotoLine(42);
7274
cppFile->toggleSection();
7375
cppFile->gotoLine(35);

0 commit comments

Comments
 (0)