void DocumentEditor::toggleComment(bool lineCommentPrefered_) { QsciLexer* l = lexer(); if(l == 0) return; QString comment = l->commentLine(); QStringList commentBlock = l->commentBlock(); if(comment.isEmpty() && commentBlock.isEmpty()){ qDebug() << "Toggle comment is not supported for " << l->language(); return; } if (!hasSelectedText()) { //if line is empty, skip it int line = getCurrentLine(); if (isLineEmpty(line)) return; QString selText = text(line); selText.remove("\n"); selText.remove("\r"); QString selTextTrimmed = selText.trimmed(); int pos_start = SendScintilla(SCI_POSITIONFROMLINE, line); int pos_end = SendScintilla(SCI_GETLINEENDPOSITION, line); // check for block comments on a line if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) { beginUndoAction(); int idx1 = selText.indexOf(blockStart); selText.remove(idx1, blockEnd.size()); int idx2 = selText.lastIndexOf(blockEnd); selText.remove(idx2, blockEnd.size()); SendScintilla(SCI_SETTARGETSTART, pos_start); SendScintilla(SCI_SETTARGETEND, pos_end); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); endUndoAction(); return; } } // check for single comments if (!comment.isEmpty()) { if (selTextTrimmed.startsWith(comment)) { // remove comment int idx = selText.indexOf(comment); selText = selText.remove(idx, comment.size()); } else { // set comment selText = selText.prepend(comment); } SendScintilla(SCI_SETTARGETSTART, pos_start); SendScintilla(SCI_SETTARGETEND, pos_end); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); return; } }else{ // comment out the selection QString selText = selectedText(); QString selTextTrimmed = selText.trimmed(); if (selTextTrimmed.isEmpty()) return; int lineFrom, lineTo, indexFrom, indexTo; getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); int pos_start = positionFromLineIndex(lineFrom, indexFrom); int pos_end = positionFromLineIndex(lineTo, indexTo); // check if it is double commented block - to do before single check! if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); // comment exists? remove? if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) { beginUndoAction(); int idx1 = selText.indexOf(blockStart); selText.remove(idx1, blockStart.size()); int idx2 = selText.lastIndexOf(blockEnd); selText.remove(idx2, blockEnd.size()); SendScintilla(SCI_TARGETFROMSELECTION); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND)); endUndoAction(); return; } } // check if this block can be single commented if (!comment.isEmpty() && lineCommentPrefered_) { bool empty_start = false, empty_end = false; if (indexFrom == 0) empty_start = true; else empty_start = getTextRange(positionFromLineIndex(lineFrom, 0), pos_start).trimmed().isEmpty(); if (indexTo == 0) empty_end = true; else empty_end = getTextRange(pos_end, positionFromLineIndex(lineTo+1, 0)).trimmed().isEmpty(); if (empty_start && empty_end) { beginUndoAction(); // corrections if (indexTo == 0) lineTo--; if (isLineEmpty(lineFrom)) { lineFrom++; indexFrom = 0; } // a workaround: move cursor to the next line to replace EOL as well setSelection(lineFrom, 0, lineTo+1, 0); QStringList sl; for (int i = lineFrom; i <= lineTo; i++) sl += text(i); bool comm = false; for (int i = 0; i < sl.count(); i++) if (!sl.at(i).trimmed().startsWith(comment)) { comm = true; break; } for (int i = 0; i < sl.count(); i++) { if (comm) sl[i] = sl[i].prepend(comment); else { int idx = sl.at(i).indexOf(comment); sl[i] = sl[i].remove(idx, comment.size()); } } SendScintilla(SCI_TARGETFROMSELECTION); SendScintilla(SCI_REPLACETARGET, -1, sl.join("").toUtf8().data()); SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND)); endUndoAction(); return; } } // else, set double comment if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); beginUndoAction(); // last is first SendScintilla(SCI_INSERTTEXT, pos_end, blockEnd.toUtf8().data()); SendScintilla(SCI_INSERTTEXT, pos_start, blockStart.toUtf8().data()); // select everything if(lineFrom == lineTo) setSelection(lineFrom, indexFrom, lineTo, indexTo + blockStart.size() + blockEnd.size()); else setSelection(lineFrom, indexFrom, lineTo, indexTo + blockEnd.size()); endUndoAction(); return; } } }