Moved unCommentSelection() in creator/libs/util so we can use it for other C-like languages (e.g QML and JS).

This commit is contained in:
Roberto Raggi
2009-05-14 16:37:17 +02:00
parent 17a078d5bb
commit 6195035bbf
6 changed files with 222 additions and 122 deletions

View File

@@ -59,6 +59,7 @@
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/editormanager/editormanager.h>
#include <utils/uncommentselection.h>
#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <texteditor/basetextdocument.h>
@@ -1036,126 +1037,7 @@ void CPPEditor::setDisplaySettings(const TextEditor::DisplaySettings &ds)
void CPPEditor::unCommentSelection()
{
QTextCursor cursor = textCursor();
cursor.beginEditBlock();
int pos = cursor.position();
int anchor = cursor.anchor();
int start = qMin(anchor, pos);
int end = qMax(anchor, pos);
bool anchorIsStart = (anchor == start);
QTextBlock startBlock = document()->findBlock(start);
QTextBlock endBlock = document()->findBlock(end);
if (end > start && endBlock.position() == end) {
--end;
endBlock = endBlock.previous();
}
bool doCStyleUncomment = false;
bool doCStyleComment = false;
bool doCppStyleUncomment = false;
bool hasSelection = cursor.hasSelection();
if (hasSelection) {
QString startText = startBlock.text();
int startPos = start - startBlock.position();
bool hasLeadingCharacters = !startText.left(startPos).trimmed().isEmpty();
if ((startPos >= 2
&& startText.at(startPos-2) == QLatin1Char('/')
&& startText.at(startPos-1) == QLatin1Char('*'))) {
startPos -= 2;
start -= 2;
}
bool hasSelStart = (startPos < startText.length() - 2
&& startText.at(startPos) == QLatin1Char('/')
&& startText.at(startPos+1) == QLatin1Char('*'));
QString endText = endBlock.text();
int endPos = end - endBlock.position();
bool hasTrailingCharacters = !endText.left(endPos).remove(QLatin1String("//")).trimmed().isEmpty()
&& !endText.mid(endPos).trimmed().isEmpty();
if ((endPos <= endText.length() - 2
&& endText.at(endPos) == QLatin1Char('*')
&& endText.at(endPos+1) == QLatin1Char('/'))) {
endPos += 2;
end += 2;
}
bool hasSelEnd = (endPos >= 2
&& endText.at(endPos-2) == QLatin1Char('*')
&& endText.at(endPos-1) == QLatin1Char('/'));
doCStyleUncomment = hasSelStart && hasSelEnd;
doCStyleComment = !doCStyleUncomment && (hasLeadingCharacters || hasTrailingCharacters);
}
if (doCStyleUncomment) {
cursor.setPosition(end);
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor, 2);
cursor.removeSelectedText();
cursor.setPosition(start);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
cursor.removeSelectedText();
} else if (doCStyleComment) {
cursor.setPosition(end);
cursor.insertText(QLatin1String("*/"));
cursor.setPosition(start);
cursor.insertText(QLatin1String("/*"));
} else {
endBlock = endBlock.next();
doCppStyleUncomment = true;
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
QString text = block.text();
if (!text.trimmed().startsWith(QLatin1String("//"))) {
doCppStyleUncomment = false;
break;
}
}
for (QTextBlock block = startBlock; block != endBlock; block = block.next()) {
if (doCppStyleUncomment) {
QString text = block.text();
int i = 0;
while (i < text.size() - 1) {
if (text.at(i) == QLatin1Char('/')
&& text.at(i + 1) == QLatin1Char('/')) {
cursor.setPosition(block.position() + i);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 2);
cursor.removeSelectedText();
break;
}
if (!text.at(i).isSpace())
break;
++i;
}
} else {
cursor.setPosition(block.position());
cursor.insertText(QLatin1String("//"));
}
}
}
// adjust selection when commenting out
if (hasSelection && !doCStyleUncomment && !doCppStyleUncomment) {
cursor = textCursor();
if (!doCStyleComment)
start = startBlock.position(); // move the double slashes into the selection
int lastSelPos = anchorIsStart ? cursor.position() : cursor.anchor();
if (anchorIsStart) {
cursor.setPosition(start);
cursor.setPosition(lastSelPos, QTextCursor::KeepAnchor);
} else {
cursor.setPosition(lastSelPos);
cursor.setPosition(start, QTextCursor::KeepAnchor);
}
setTextCursor(cursor);
}
cursor.endEditBlock();
Core::Utils::unCommentSelection(this);
}
CPPEditor::Link CPPEditor::linkToSymbol(CPlusPlus::Symbol *symbol)