Skip to content

Commit

Permalink
Fixed Line comments
Browse files Browse the repository at this point in the history
* line comments are now properly indented
  • Loading branch information
arBmind committed Aug 27, 2020
1 parent d79f0e6 commit 70faac5
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 90 deletions.
83 changes: 56 additions & 27 deletions src/libs/utils/uncommentselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@

using namespace Utils;

CommentDefinition CommentDefinition::CppStyle = CommentDefinition("//", "/*", "*/");
CommentDefinition CommentDefinition::CppStyle = CommentDefinition("//", "/*", "*/", true);
CommentDefinition CommentDefinition::HashStyle = CommentDefinition("#");

CommentDefinition::CommentDefinition() = default;

CommentDefinition::CommentDefinition(const QString &single, const QString &multiStart,
const QString &multiEnd)
: singleLine(single),
const QString &multiEnd, bool isAfterWhiteSpaces)
: isAfterWhiteSpaces(isAfterWhiteSpaces),
singleLine(single),
singleLineInsert(single + " "),
multiLineStart(multiStart),
multiLineEnd(multiEnd)
{
Expand Down Expand Up @@ -180,45 +182,72 @@ void Utils::unCommentSelection(QPlainTextEdit *edit, const CommentDefinition &de
cursor.insertText(definition.multiLineStart);
} else {
endBlock = endBlock.next();
doSingleLineStyleUncomment = true;
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text().trimmed();
if (!text.isEmpty() && !text.startsWith(definition.singleLine)) {
doSingleLineStyleUncomment = false;
break;
doSingleLineStyleUncomment = [&] {
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text().trimmed();
if (!text.isEmpty() && !text.startsWith(definition.singleLine)) {
return false;
}
}
}

const int singleLineLength = definition.singleLine.length();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
if (doSingleLineStyleUncomment) {
return true;
}();
if (doSingleLineStyleUncomment) {
auto remove = [&] {
if (!definition.isAfterWhiteSpaces) return definition.singleLine;
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text().trimmed();
if (text.isEmpty()) continue;
if (!text.startsWith(definition.singleLineInsert)) {
return definition.singleLine;
}
}
return definition.singleLineInsert;
}();
const int removeLength = remove.length();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
int i = 0;
while (i <= text.size() - singleLineLength) {
if (isComment(text, i, definition.singleLine)) {
while (i <= text.size() - removeLength) {
if (isComment(text, i, remove)) {
cursor.setPosition(block.position() + i);
cursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor,
singleLineLength);
removeLength);
cursor.removeSelectedText();
break;
}
if (!text.at(i).isSpace())
break;
++i;
}
} else {
const QString text = block.text();
for (QChar c : text) {
if (!c.isSpace()) {
if (definition.isAfterWhiteSpaces)
cursor.setPosition(block.position() + text.indexOf(c));
else
cursor.setPosition(block.position());
cursor.insertText(definition.singleLine);
break;
}
}
else {
auto insert = [&] {
if (!definition.isAfterWhiteSpaces) return definition.singleLine;
return definition.singleLineInsert;
}();
auto indent = [&] {
if (!definition.isAfterWhiteSpaces) return 0;
auto best = 9999;
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
const QString text = block.text();
auto lineIndent = 0;
for (QChar c : text) {
if (!c.isSpace()) {
if (lineIndent < best) best = lineIndent;
break;
}
lineIndent++;
}
}
return best;
}();
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text().trimmed();
if (text.isEmpty()) continue;
cursor.setPosition(block.position() + indent);
cursor.insertText(definition.singleLineInsert);
}
}
}
Expand Down
128 changes: 65 additions & 63 deletions src/libs/utils/uncommentselection.h
Original file line number Diff line number Diff line change
@@ -1,63 +1,65 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "utils_global.h"

#include <QString>

QT_BEGIN_NAMESPACE
class QPlainTextEdit;
QT_END_NAMESPACE

namespace Utils {

class QTCREATOR_UTILS_EXPORT CommentDefinition
{
public:
static CommentDefinition CppStyle;
static CommentDefinition HashStyle;

CommentDefinition();
CommentDefinition(const QString &single,
const QString &multiStart = QString(), const QString &multiEnd = QString());

bool isValid() const;
bool hasSingleLineStyle() const;
bool hasMultiLineStyle() const;

public:
bool isAfterWhiteSpaces = false;
QString singleLine;
QString multiLineStart;
QString multiLineEnd;
};

QTCREATOR_UTILS_EXPORT
void unCommentSelection(QPlainTextEdit *edit,
const CommentDefinition &definiton = CommentDefinition());

} // namespace Utils
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#pragma once

#include "utils_global.h"

#include <QString>

QT_BEGIN_NAMESPACE
class QPlainTextEdit;
QT_END_NAMESPACE

namespace Utils {

class QTCREATOR_UTILS_EXPORT CommentDefinition
{
public:
static CommentDefinition CppStyle;
static CommentDefinition HashStyle;

CommentDefinition();
CommentDefinition(const QString &single,
const QString &multiStart = QString(), const QString &multiEnd = QString(),
bool isAfterWhiteSpaces = false);

bool isValid() const;
bool hasSingleLineStyle() const;
bool hasMultiLineStyle() const;

public:
bool isAfterWhiteSpaces = false;
QString singleLine;
QString singleLineInsert;
QString multiLineStart;
QString multiLineEnd;
};

QTCREATOR_UTILS_EXPORT
void unCommentSelection(QPlainTextEdit *edit,
const CommentDefinition &definiton = CommentDefinition());

} // namespace Utils

0 comments on commit 70faac5

Please sign in to comment.