improved (un)comment handling

This commit is contained in:
nemu
2009-05-22 18:13:37 +00:00
parent 5d99f6cf8c
commit 3805013b5d

View File

@ -996,11 +996,21 @@ void PTextEdit::editComment()
// get selection text // get selection text
str = currentEditor()->selectedText(); str = currentEditor()->selectedText();
// check line by line if (un)comment is needed // check line by line if (un)comment is needed
QStringList strList = QStringList::split("\n", str); QStringList strList = QStringList::split("\n", str, TRUE);
for (QStringList::Iterator it = strList.begin(); it != strList.end(); ++it) { for (QStringList::Iterator it = strList.begin(); it != strList.end(); ++it) {
if ((*it).stripWhiteSpace().startsWith("#")) { // comment -> uncomment it str = *it;
(*it).remove(0,1); if (str.stripWhiteSpace().startsWith("#")) { // comment -> uncomment it
(*it) = (*it).stripWhiteSpace(); int idx = -1;
for (unsigned int i=0; i<(*it).length(); i++) {
if ((*it)[i] == '#') {
idx = i;
break;
}
}
(*it).remove(idx,1);
if ((*it)[idx] == ' ') {
(*it).remove(idx,1);
}
} else { // no comment -> comment it } else { // no comment -> comment it
(*it).prepend("# "); (*it).prepend("# ");
} }
@ -1016,9 +1026,20 @@ void PTextEdit::editComment()
str = currentEditor()->text(para); str = currentEditor()->text(para);
// check if it is a comment line or not // check if it is a comment line or not
if (str.stripWhiteSpace().startsWith("#")) { // comment -> uncomment it if (str.stripWhiteSpace().startsWith("#")) { // comment -> uncomment it
str.remove(0,1); str = currentEditor()->text(para);
str = str.stripWhiteSpace(); int idx = -1;
for (unsigned int i=0; i<str.length(); i++) {
if (str[i] == '#') {
idx = i;
break;
}
}
str.remove(idx,1);
if (str[idx] == ' ') {
str.remove(idx,1);
}
} else { // no comment -> comment it } else { // no comment -> comment it
if (!str.isEmpty())
str.prepend("# "); str.prepend("# ");
} }
// select current line // select current line