void ScCodeEditor::toggleCommentSingleLine(QTextCursor cursor) { QTextBlock currentBlock(cursor.block()); cursor.beginEditBlock(); if (!isSingleLineComment(currentBlock)) { int blockIndentation = indentationLevel(cursor); addSingleLineComment(cursor, blockIndentation); } else removeSingleLineComment(cursor); cursor.endEditBlock(); indent(cursor); }
static bool isSingleLineComment(QTextCursor const & selection) { QTextCursor cursor(selection); cursor.setPosition(selection.selectionStart()); QTextBlock startBlock = cursor.block(); cursor.setPosition(selection.selectionEnd() - 1); QTextBlock endBlock = cursor.block(); for (QTextBlock block = startBlock; block != endBlock.next(); block = block.next()) { if (!isSingleLineComment(block)) return false; } return true; }
void Preprocessor::parse(const char* name, const char* text) { if (std::find(names.begin(), names.end(), name) != names.end()) return; files.push_back(File(name, text)); names.push_back(name); list += format("( file %u: %s )\n", (uint) names.size(), name); output.reserve(output.size() + std::strlen(text)); appendToOutput(format("#line 0 %u /* entering %s */\n", (uint) files.size(), files.back().name).c_str()); while (hasMore()) { if (isMultiLineComment()) parseMultiLineComment(); else if (isSingleLineComment()) parseSingleLineComment(); else if (isNewLine()) parseNewLine(); else if (isWhitespace()) parseWhitespace(); else if (isCommand()) parseCommand(); else { advance(1); appendToOutput(); setFirstOnLine(false); } } files.pop_back(); if (!files.empty()) { appendToOutput(format("\n#line %u %u /* returning to %s */", files.back().line, (uint) files.size(), files.back().name).c_str()); } }
void Preprocessor::parseCommand() { advance(1); setFirstOnLine(false); passWhitespace(); const String command = passIdentifier(); if (command == "include") { passWhitespace(); const String name = passShaderName(); discard(); parse(name.c_str()); } else if (command == "version") { if (!version.empty()) { logError("%s:%u: Duplicate #version directive", files.back().name, files.back().line); throw Exception("Duplicate #version directive"); } passWhitespace(); version = passNumber(); discard(); } while (hasMore()) { if (isNewLine() || isSingleLineComment() || isMultiLineComment()) break; advance(1); } appendToOutput(); }
void ScCodeEditor::toggleCommentSelection() { QTextCursor cursor = textCursor(); cursor.beginEditBlock(); if (isBlockOnlySelection(cursor)) { const bool isComment = isSingleLineComment(cursor); QTextCursor selectionCursor(cursor); selectionCursor.setPosition(cursor.selectionStart()); QTextBlock currentBlock = selectionCursor.block(); int firstBlockIndentation = isComment ? 0 : indentationLevel(selectionCursor); do { QTextCursor blockCursor(currentBlock); if (!isComment) addSingleLineComment(blockCursor, firstBlockIndentation); else removeSingleLineComment(blockCursor); currentBlock = currentBlock.next(); } while (currentBlock.isValid() && currentBlock.position() < cursor.selectionEnd()); if (!isComment) { // fix up selection QTextCursor newSelection(cursor); if (cursor.anchor() < cursor.position()) { newSelection.setPosition(newSelection.selectionStart()); newSelection.movePosition(QTextCursor::StartOfBlock); newSelection.setPosition(cursor.selectionEnd(), QTextCursor::KeepAnchor); } else { newSelection.setPosition(newSelection.selectionEnd()); newSelection.setPosition(cursor.selectionStart(), QTextCursor::KeepAnchor); newSelection.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor); } setTextCursor(newSelection); } } else { QString selectionText = cursor.selectedText(); QTextCursor selectionCursor(cursor); if (isSelectionComment(selectionText)) { selectionText = selectionText.trimmed().remove(0, 2); selectionText.chop(2); selectionCursor.insertText(selectionText); } else { selectionText = QString("/*") + selectionText + QString("*/"); selectionCursor.insertText(selectionText); } // fix up selection const int position = selectionCursor.position(); const int anchor = selectionCursor.anchor(); if (position > anchor) { cursor.setPosition(position - selectionText.size()); cursor.setPosition(position, QTextCursor::KeepAnchor); } else { cursor.setPosition(position); cursor.setPosition(position - selectionText.size(), QTextCursor::KeepAnchor); } setTextCursor(cursor); } cursor.endEditBlock(); }