Skip to content

Commit 98918ab

Browse files
authored
feat: add a removeLines method in CppDocument (#154)
Fixes #24
1 parent 67349ef commit 98918ab

File tree

6 files changed

+105
-0
lines changed

6 files changed

+105
-0
lines changed

docs/API/knut/cppdocument.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Inherited properties: [CodeDocument properties](../knut/codedocument.md#properti
4747
|array<[QueryMatch](../knut/querymatch.md)> |**[queryMethodDeclaration](#queryMethodDeclaration)**(string className, string functionName)|
4848
|array<[QueryMatch](../knut/querymatch.md)> |**[queryMethodDefinition](#queryMethodDefinition)**(string scope, string methodName)|
4949
||**[removeInclude](#removeInclude)**(string include)|
50+
|void |**[removeLines](#removeLines)**(const [RangeMark](../knut/rangemark.md) &rangeMark)|
5051
|int |**[selectBlockEnd](#selectBlockEnd)**()|
5152
|int |**[selectBlockStart](#selectBlockStart)**()|
5253
|int |**[selectBlockUp](#selectBlockUp)**()|
@@ -332,6 +333,10 @@ Remove `include` from the file. If the include is not in the file, do nothing (a
332333
333334
The `include` string should be either `<foo.h>` or `"foo.h"`, it will returns false otherwise.
334335
336+
#### <a name="removeLines"></a>void **removeLines**(const [RangeMark](../knut/rangemark.md) &rangeMark)
337+
338+
Removes the line specified by the given RangeMark, including any comments attached to it.
339+
335340
#### <a name="selectBlockEnd"></a>int **selectBlockEnd**()
336341
337342
Selects the text from current cursor position to the end of the block, and returns the new cursor position.

src/core/cppdocument.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,40 @@ CppDocument *CppDocument::openHeaderSource()
504504
return nullptr;
505505
}
506506

507+
/*!
508+
* \qmlmethod void CppDocument::removeLines(const RangeMark &rangeMark)
509+
* Removes the line specified by the given RangeMark, including any comments attached to it.
510+
*/
511+
void CppDocument::removeLines(const RangeMark &rangeMark)
512+
{
513+
LOG("CppDocument::removeLines" + rangeMark.text());
514+
515+
QTextCursor cursor = textEdit()->textCursor();
516+
cursor.setPosition(rangeMark.start());
517+
cursor.movePosition(QTextCursor::StartOfBlock);
518+
cursor.setPosition(rangeMark.end(), QTextCursor::KeepAnchor);
519+
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
520+
521+
cursor.removeSelectedText();
522+
cursor.deleteChar();
523+
524+
while (cursor.movePosition(QTextCursor::PreviousBlock)) {
525+
cursor.select(QTextCursor::LineUnderCursor);
526+
const QString lineText = cursor.selectedText().trimmed();
527+
528+
if (lineText.isEmpty()) {
529+
cursor.removeSelectedText();
530+
cursor.deleteChar();
531+
break;
532+
} else if (lineText.startsWith("//") || lineText.startsWith("/*")) {
533+
cursor.removeSelectedText();
534+
cursor.deleteChar();
535+
} else {
536+
break;
537+
}
538+
}
539+
}
540+
507541
/*!
508542
* \qmlmethod QueryMatch CppDocument::queryClassDefinition(string className)
509543
*

src/core/cppdocument.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class CppDocument : public CodeDocument
4444

4545
Q_INVOKABLE QString correspondingHeaderSource() const;
4646

47+
Q_INVOKABLE void removeLines(const Core::RangeMark &rangeMark);
48+
4749
Q_INVOKABLE Core::QueryMatch queryClassDefinition(const QString &className);
4850
Q_INVOKABLE Core::QueryMatchList queryMethodDeclaration(const QString &className, const QString &functionName);
4951
Q_INVOKABLE Core::QueryMatch queryMember(const QString &className, const QString &memberName);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
class MyObject {
6+
public:
7+
MyObject(const std::string& message);
8+
// this comment should stay
9+
10+
private:
11+
std::string m_message;
12+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
class MyObject {
6+
public:
7+
MyObject(const std::string& message);
8+
// this comment should stay
9+
10+
~MyObject();// this comment should be removed
11+
12+
// this comment should be removed
13+
void sayMessage();
14+
15+
/* this comment should be removed */
16+
void sayMessage(const std::string& test);
17+
18+
// this comment should be removed
19+
// this one too
20+
void sayMessage(const std::string& test, int num);
21+
22+
private:
23+
std::string m_message;
24+
};

tests/tst_cppdocument.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "common/test_utils.h"
1313
#include "core/cppdocument.h"
1414
#include "core/knutcore.h"
15+
#include "core/rangemark.h"
1516
#include "core/utils.h"
1617

1718
#include <QFileInfo>
@@ -233,6 +234,33 @@ private slots:
233234
QVERIFY(sourceFile.compare());
234235
}
235236

237+
void removeLines()
238+
{
239+
Test::FileTester headerFile(Test::testDataPath() + "/tst_cppdocument/removeLines/myobject.h");
240+
241+
Test::testCppDocument("/tst_cppdocument/removeLines", headerFile.fileName(), [](auto *header) {
242+
header->find("~MyObject();");
243+
Core::RangeMark rangeMark = header->createRangeMark();
244+
header->removeLines(rangeMark);
245+
246+
header->find("void sayMessage();");
247+
rangeMark = header->createRangeMark();
248+
header->removeLines(rangeMark);
249+
250+
header->find("void sayMessage(const std::string& test);");
251+
rangeMark = header->createRangeMark();
252+
header->removeLines(rangeMark);
253+
254+
header->find("void sayMessage(const std::string& test, int num);");
255+
rangeMark = header->createRangeMark();
256+
header->removeLines(rangeMark);
257+
258+
header->save();
259+
});
260+
261+
QVERIFY(headerFile.compare());
262+
}
263+
236264
void queryMethod()
237265
{
238266
Test::testCppDocument("projects/cpp-project", "myobject.cpp", [](auto *document) {

0 commit comments

Comments
 (0)