Skip to content

Commit

Permalink
feat: add a removeLines method in CppDocument
Browse files Browse the repository at this point in the history
Fixes #24
  • Loading branch information
smnppKDAB committed Aug 27, 2024
1 parent 67349ef commit cee8ded
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/API/knut/cppdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Inherited properties: [CodeDocument properties](../knut/codedocument.md#properti
|array<[QueryMatch](../knut/querymatch.md)> |**[queryMethodDeclaration](#queryMethodDeclaration)**(string className, string functionName)|
|array<[QueryMatch](../knut/querymatch.md)> |**[queryMethodDefinition](#queryMethodDefinition)**(string scope, string methodName)|
||**[removeInclude](#removeInclude)**(string include)|
|void |**[removeLines](#removeLines)**(const [RangeMark](../knut/rangemark.md) &rangeMark)|
|int |**[selectBlockEnd](#selectBlockEnd)**()|
|int |**[selectBlockStart](#selectBlockStart)**()|
|int |**[selectBlockUp](#selectBlockUp)**()|
Expand Down Expand Up @@ -332,6 +333,10 @@ Remove `include` from the file. If the include is not in the file, do nothing (a
The `include` string should be either `<foo.h>` or `"foo.h"`, it will returns false otherwise.
#### <a name="removeLines"></a>void **removeLines**(const [RangeMark](../knut/rangemark.md) &rangeMark)
Removes the line specified by the given RangeMark, including any comments attached to it.
#### <a name="selectBlockEnd"></a>int **selectBlockEnd**()
Selects the text from current cursor position to the end of the block, and returns the new cursor position.
Expand Down
34 changes: 34 additions & 0 deletions src/core/cppdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,40 @@ CppDocument *CppDocument::openHeaderSource()
return nullptr;
}

/*!
* \qmlmethod void CppDocument::removeLines(const RangeMark &rangeMark)
* Removes the line specified by the given RangeMark, including any comments attached to it.
*/
void CppDocument::removeLines(const RangeMark &rangeMark)
{
LOG("CppDocument::removeLines" + rangeMark.text());

QTextCursor cursor = textEdit()->textCursor();
cursor.setPosition(rangeMark.start());
cursor.movePosition(QTextCursor::StartOfBlock);
cursor.setPosition(rangeMark.end(), QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);

cursor.removeSelectedText();
cursor.deleteChar();

while (cursor.movePosition(QTextCursor::PreviousBlock)) {
cursor.select(QTextCursor::LineUnderCursor);
const QString lineText = cursor.selectedText().trimmed();

if (lineText.isEmpty()) {
cursor.removeSelectedText();
cursor.deleteChar();
break;
} else if (lineText.startsWith("//") || lineText.startsWith("/*")) {
cursor.removeSelectedText();
cursor.deleteChar();
} else {
break;
}
}
}

/*!
* \qmlmethod QueryMatch CppDocument::queryClassDefinition(string className)
*
Expand Down
2 changes: 2 additions & 0 deletions src/core/cppdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CppDocument : public CodeDocument

Q_INVOKABLE QString correspondingHeaderSource() const;

Q_INVOKABLE void removeLines(const Core::RangeMark &rangeMark);

Q_INVOKABLE Core::QueryMatch queryClassDefinition(const QString &className);
Q_INVOKABLE Core::QueryMatchList queryMethodDeclaration(const QString &className, const QString &functionName);
Q_INVOKABLE Core::QueryMatch queryMember(const QString &className, const QString &memberName);
Expand Down
12 changes: 12 additions & 0 deletions test_data/tst_cppdocument/removeLines/myobject.h.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>

class MyObject {
public:
MyObject(const std::string& message);
// this comment should stay

private:
std::string m_message;
};
24 changes: 24 additions & 0 deletions test_data/tst_cppdocument/removeLines/myobject.h.original
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <string>

class MyObject {
public:
MyObject(const std::string& message);
// this comment should stay

~MyObject();// this comment should be removed

// this comment should be removed
void sayMessage();

/* this comment should be removed */
void sayMessage(const std::string& test);

// this comment should be removed
// this one too
void sayMessage(const std::string& test, int num);

private:
std::string m_message;
};
28 changes: 28 additions & 0 deletions tests/tst_cppdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "common/test_utils.h"
#include "core/cppdocument.h"
#include "core/knutcore.h"
#include "core/rangemark.h"
#include "core/utils.h"

#include <QFileInfo>
Expand Down Expand Up @@ -233,6 +234,33 @@ private slots:
QVERIFY(sourceFile.compare());
}

void removeLines()
{
Test::FileTester headerFile(Test::testDataPath() + "/tst_cppdocument/removeLines/myobject.h");

Test::testCppDocument("/tst_cppdocument/removeLines", headerFile.fileName(), [](auto *header) {
header->find("~MyObject();");
Core::RangeMark rangeMark = header->createRangeMark();
header->removeLines(rangeMark);

header->find("void sayMessage();");
rangeMark = header->createRangeMark();
header->removeLines(rangeMark);

header->find("void sayMessage(const std::string& test);");
rangeMark = header->createRangeMark();
header->removeLines(rangeMark);

header->find("void sayMessage(const std::string& test, int num);");
rangeMark = header->createRangeMark();
header->removeLines(rangeMark);

header->save();
});

QVERIFY(headerFile.compare());
}

void queryMethod()
{
Test::testCppDocument("projects/cpp-project", "myobject.cpp", [](auto *document) {
Expand Down

0 comments on commit cee8ded

Please sign in to comment.