bool	QsvTextOperationsWidget::issue_search( const QString &text, QTextCursor newCursor, QFlags<QTextDocument::FindFlag> findOptions, QLineEdit *l, bool moveCursor )
{
	QTextCursor c = m_document->find( text, newCursor, findOptions );
	bool found = ! c.isNull();

	//lets try again, from the start
	if (!found) {
		c.movePosition(findOptions.testFlag(QTextDocument::FindBackward)? QTextCursor::End : QTextCursor::Start);
		c = m_document->find(text, c, findOptions);
		found = ! c.isNull();
	}

	QPalette p = l->palette();
	if (found) {
		p.setColor(QPalette::Base, searchFoundColor);
	} else {
		if (!text.isEmpty())
			p.setColor(QPalette::Base, searchNotFoundColor);
		else
			p.setColor(QPalette::Base,
				l->style()->standardPalette().base().color()
			);
		c =  m_searchCursor;
	}
	l->setPalette(p);

	if (moveCursor){
		int start = c.selectionStart();
		int end   = c.selectionEnd();
		c.setPosition(end  ,QTextCursor::MoveAnchor);
		c.setPosition(start,QTextCursor::KeepAnchor);
		setTextCursor(c);
	}
	return found;
}
Example #2
0
void
FormText::underline( bool on )
{
	QTextCursor c = textCursor();

	if( c.hasSelection() )
	{
		if( c.position() != c.selectionEnd() )
			c.setPosition( c.selectionEnd() );

		QTextCharFormat fmt = c.charFormat();

		fmt.setFontUnderline( on );

		textCursor().setCharFormat( fmt );
	}
	else
	{
		QFont f = font();

		f.setUnderline( on );

		setFont( f );
	}

	QRectF r = boundingRect();
	r.moveTo( pos() );

	d->m_proxy->setRect( r );
}
Example #3
0
void EditorUtil::EnumEditor(QPlainTextEdit *ed, EnumEditorProc proc, void *param)
{
    if (!ed) {
        return;
    }
    QTextCursor cur = ed->textCursor();
    cur.beginEditBlock();
    if (cur.hasSelection()) {
        QTextBlock begin = ed->document()->findBlock(cur.selectionStart());
        QTextBlock end = ed->document()->findBlock(cur.selectionEnd());
        if (end.position() == cur.selectionEnd()) {
            end = end.previous();
        }
        QTextBlock block = begin;
        do {
            if (block.text().length() > 0) {
                proc(cur,block,param);
            }
            block = block.next();
        } while(block.isValid() && block.position() <= end.position());
    } else {
        QTextBlock block = cur.block();
        proc(cur,block,param);
    }
    cur.endEditBlock();
    ed->setTextCursor(cur);
}
Example #4
0
void GolangEdit::updateLink(const QTextCursor &_cursor)
{
    QTextCursor cursor = _cursor;
    LiteApi::selectWordUnderCursor(cursor);

    if (cursor.selectionStart() == cursor.selectionEnd()) {
        m_editor->clearLink();
        return;
    }

    if (m_linkCursor.selectionStart() == cursor.selectionStart() &&
            m_linkCursor.selectionEnd() == cursor.selectionEnd()) {
        if (m_lastLink.hasValidTarget()) {
            m_editor->showLink(m_lastLink);
        }
        return;
    }
    m_linkCursor = cursor;
    m_lastLink = LiteApi::Link();
    if (m_findLinkProcess->isRunning()) {
        m_findLinkProcess->kill();
        m_findLinkProcess->waitForFinished(100);
        //return;
    }
    QString cmd = LiteApi::liteide_stub_cmd(m_liteApp);
    QString src = cursor.document()->toPlainText();
    m_srcData = src.toUtf8();
    int offset = src.left(cursor.selectionStart()).length();
    QFileInfo info(m_editor->filePath());
    m_findLinkProcess->setEnvironment(LiteApi::getGoEnvironment(m_liteApp).toStringList());
    m_findLinkProcess->setWorkingDirectory(info.path());
    m_findLinkProcess->startEx(cmd,QString("type -cursor %1:%2 -cursor_stdin -def -info .").
                             arg(info.fileName()).
                               arg(offset));
}
Example #5
0
void CodeEditor::deleteTab()
{
    QString deletion = "    ";

    QTextCursor cursor = textCursor();
    if (cursor.selectionEnd() - cursor.selectionStart() <= 0) {
        //delete 4 spaces (tab)
        cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, deletion.length());
        QString selected = cursor.selectedText();
        if (selected.startsWith(deletion))
            cursor.deletePreviousChar();
    } else {
        QTextBlock firstBlock = document()->findBlock(cursor.selectionStart());
        QTextBlock lastBlock = document()->findBlock(cursor.selectionEnd() - 1);

        cursor.setPosition(firstBlock.position());
        cursor.beginEditBlock();
        do {
            if (cursor.block().text().startsWith(deletion)) {
                cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, deletion.length());
                cursor.removeSelectedText();
            }
        } while (cursor.movePosition(QTextCursor::NextBlock) && cursor.position() <= lastBlock.position());
        cursor.endEditBlock();
    }
}
Example #6
0
void QConsoleWidget::dragEnterEvent(QDragEnterEvent *e){
    TP::dragEnterEvent(e);

	if(e->isAccepted()){/*调整选区为可编辑区域*/
		QTextCursor tc = this->textCursor();
		if (tc.hasSelection()) {
			if (tc.selectionStart()>= this->promptEndPos_) {
				return;
			}
			else {
				if (tc.selectionEnd()<= this->promptEndPos_) {
					tc.clearSelection();
					this->setTextCursor(tc);
				}
				else {
					auto se_ = tc.selectionEnd();
					tc.setPosition(this->promptEndPos_);
					tc.setPosition(se_,QTextCursor::KeepAnchor);
					this->setTextCursor(tc);
				}
			}
		}
	}

}
Example #7
0
bool GlslCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
{
    const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
                                          BackwardsScanner::previousBlockState(cursor.block()),
                                          LanguageFeatures::defaultFeatures());

    // XXX Duplicated from CppEditor::isInComment to avoid tokenizing twice
    if (tk.isComment()) {
        const unsigned pos = cursor.selectionEnd() - cursor.block().position();

        if (pos == tk.utf16charsEnd()) {
            if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
                return false;

            const int state = cursor.block().userState() & 0xFF;
            if (state > 0)
                return false;
        }

        if (pos < tk.utf16charsEnd())
            return false;
    } else if (tk.isStringLiteral() || tk.isCharLiteral()) {
        const unsigned pos = cursor.selectionEnd() - cursor.block().position();
        if (pos <= tk.utf16charsEnd())
            return false;
    }

    return true;
}
bool CppAutoCompleter::contextAllowsElectricCharacters(const QTextCursor &cursor) const
{
    const Token tk = SimpleLexer::tokenAt(cursor.block().text(), cursor.positionInBlock(),
                                          BackwardsScanner::previousBlockState(cursor.block()));

    // XXX Duplicated from CPPEditor::isInComment to avoid tokenizing twice
    if (tk.isComment()) {
        const unsigned pos = cursor.selectionEnd() - cursor.block().position();

        if (pos == tk.end()) {
            if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
                return false;

            const int state = cursor.block().userState() & 0xFF;
            if (state > 0)
                return false;
        }

        if (pos < tk.end())
            return false;
    }
    else if (tk.is(T_STRING_LITERAL) || tk.is(T_WIDE_STRING_LITERAL)
        || tk.is(T_CHAR_LITERAL) || tk.is(T_WIDE_CHAR_LITERAL)) {

        const unsigned pos = cursor.selectionEnd() - cursor.block().position();
        if (pos <= tk.end())
            return false;
    }

    return true;
}
Example #9
0
void MarkdownEdit::insert_head(const QString &tag, bool blockStart)
{
    QTextCursor cur = m_ed->textCursor();
    cur.beginEditBlock();
    if (cur.hasSelection()) {
        QTextBlock begin = m_ed->document()->findBlock(cur.selectionStart());
        QTextBlock end = m_ed->document()->findBlock(cur.selectionEnd());
        if (end.position() == cur.selectionEnd()) {
            end = end.previous();
        }
        QTextBlock block = begin;
        do {
            if (block.text().length() > 0) {
                if (blockStart) {
                    cur.setPosition(block.position());
                } else {
                    QString text = block.text();
                    foreach(QChar c, text) {
                        if (!c.isSpace()) {
                            cur.setPosition(block.position()+text.indexOf(c));
                            break;
                        }
                    }
                }
                cur.insertText(tag);
            }
            block = block.next();
        } while(block.isValid() && block.position() <= end.position());
    } else {
void TextEdit::cursorPositionChanged()
{
  //alignmentChanged(textEdit->alignment());
  QColor col = Qt::red;
  QTextCharFormat fmt;
  fmt.setForeground(col);
  QTextCursor cursor = textEdit->textCursor();
  cout<<"#TextEdit::cursorPositionChanged:";
  cout<<"cursor.selectionStart:"<<cursor.selectionStart()<<","<<cursor.selectionEnd();
  int selectionStart=cursor.selectionStart(),
      selectionEnd=cursor.selectionEnd();
  cursor.mergeCharFormat(fmt);
  colorChanged(col);

  if(!cursor.hasSelection()) return;
  QTextBlock block=cursor.block();
  int blockStart=block.position();
  QTextLayout* layout=cursor.block().layout();
  QTextLine layoutLine=layout->lineForTextPosition(selectionStart-blockStart);
  cout<<"layout line:";
  int lineNumber= layoutLine.lineNumber();
  QPoint blockOffset=layout->position().toPoint();
  QPoint lineOffset=layoutLine.position().toPoint();
  printPoint(blockOffset);
  printPoint(lineOffset);
  QPoint linePoint(blockOffset.x()+lineOffset.x(),blockOffset.y()+lineOffset.y());
  QPoint lineEndPoint=QPoint(linePoint.x()+PAGEWIDTH,linePoint.y());
//  cout<<"block:"<<rect.left()<<","<<rect.right()<<":"<<rect.top()<<","<<rect.bottom();
//  cout<<"blockstart:"<<blockStart;

//  int x=blockPoint.x()+(float)((selectionStart-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH;
//  int y=blockPoint.y()+((selectionStart-blockStart)/LINELENGTH+1)*LINEHEIGHT;
//  int x1=blockPoint.x()+(float)((selectionEnd-blockStart)%LINELENGTH)/LINELENGTH*PAGEWIDTH;
//  cout<<"block position:"<<blockPoint.x()<<","<<blockPoint.y();
//  cout<<"selection position:"<<x<<","<<y<<":"<<x1<<","<<y;
////  int y1=blockPoint.y()+((cursor.selectionEnd()-blockStart)/LINELENGTH+1)*15;
//  QPoint selectionPoint(x,y);
//  QPoint selectionEndPoint(x1,y);
  image->paintLine(linePoint,lineEndPoint);

//  int lineStart=blockStart,lineEnd;
//  for(int i=0;i<block.lineCount();i++){
//  QTextLine line = layout->lineAt(i);
//  qreal lineWidth=line.width(),lineHeight=line.height();
//  lineEnd=lineStart+line.textLength();
//  int a=line.textStart(),b=line.textLength()+a;
//  cout<<"line.cursorToX:"<<line.cursorToX(selectionStart)<<","<<line.cursorToX(selectionEnd);
//  cout<<"line.textstart:"<<a<<","<<b;
//  cout<<"line.width:"<<lineWidth<<" height:"<<lineHeight;
//  rect=line.naturalTextRect();
//  cout<<"line.naturaltextrect:"<<rect.left()<<":"<<rect.top()<<":"<<rect.right()<<":"<<rect.bottom();
//  lineStart=lineEnd;
//  }
}
Example #11
0
void CodeEditer::indent(QTextDocument *doc, const QTextCursor &cursor, QChar typedChar)
{
	if (cursor.hasSelection()) {
		QTextBlock block = doc->findBlock(qMin(cursor.selectionStart(), cursor.selectionEnd()));
		const QTextBlock end = doc->findBlock(qMax(cursor.selectionStart(), cursor.selectionEnd())).next();
		do {
			indentBlock(doc, block, typedChar);
			block = block.next();
		} while (block.isValid() && block != end);
	} else {
		indentBlock(doc, cursor.block(), typedChar);
	}
}
Example #12
0
static void get_selection(QTextEdit *wid, int *start, int *length)
{
	QTextCursor cursor = wid->textCursor();
	
	*start = cursor.selectionStart();
	*length = cursor.selectionEnd() - *start;
}
Example #13
0
inline void extendSelectionForEnvVar(QPlainTextEdit * textEdit, QTextCursor selection)
{
    if (selection.hasSelection()) {
        if (selection.selectedText() == QStringLiteral("~")) {
            QTextCursor wordAfter(selection);
            wordAfter.movePosition(QTextCursor::NextCharacter);
            wordAfter.select(QTextCursor::WordUnderCursor);
            if ( wordAfter.hasSelection() && (selection.block() == wordAfter.block()) ) {
                selection.setPosition(selection.selectionStart());
                selection.setPosition(wordAfter.selectionEnd(), QTextCursor::KeepAnchor);
                textEdit->setTextCursor(selection);
            }
        } else {
            int positionBeforeSelection = selection.selectionStart() - 1;
            if (positionBeforeSelection >= 0) {
                QChar charBeforeSelection = textEdit->document()->characterAt(positionBeforeSelection);
                if (charBeforeSelection == QChar('~')) {
                    QTextCursor extendedSelection = textEdit->textCursor();
                    extendedSelection.setPosition(positionBeforeSelection);
                    extendedSelection.setPosition(selection.selectionEnd(), QTextCursor::KeepAnchor);
                    textEdit->setTextCursor(extendedSelection);
                }
            }
        }
    }
}
Example #14
0
template<> bool
QConsoleWidget::_pf<bool, SelectKeyPressedAll>(
        QConsoleWidget * thisp,
        QKeyEvent * e) {

    auto resetSelect = [](QConsoleWidget * _thisp)->void{
        QTextCursor textCursor = _thisp->textCursor();
        auto epos = textCursor.selectionEnd();
        textCursor.setPosition( _thisp->promptEndPos_);
        textCursor.setPosition( epos, QTextCursor::KeepAnchor);
        _thisp->setTextCursor(textCursor);
    };

    auto key_ = e->key();

    switch (key_)
    {
    case Qt::Key_Shift:
    case Qt::Key_Control:
    case Qt::Key_Meta:
    case Qt::Key_Alt:
    case Qt::Key_CapsLock:
    case Qt::Key_NumLock:
    case Qt::Key_ScrollLock:
    case Qt::Key_Up:
    case Qt::Key_Down:
    case Qt::Key_Left:
    case Qt::Key_Right:
    case Qt::Key_PageDown:
    case Qt::Key_PageUp:
    case Qt::Key_Home:
    case Qt::Key_End:return false;
    }

    if (e->modifiers() & Qt::ControlModifier) {
        switch (key_)
        {
        case Qt::Key_C:
        case Qt::Key_A:return false;
        case Qt::Key_X:/*剪贴*/ {
            class EventKey :public QKeyEvent {
            public:
                void setKey() { k = Qt::Key_C; }
            };
            EventKey * fk = (EventKey*)(e);
            fk->setKey();
            thisp->TP::keyPressEvent(fk);
            resetSelect(thisp);
            QTextCursor textCursor = thisp->textCursor();
            textCursor.removeSelectedText();
            thisp->setTextCursor(textCursor);
            return true;
        }
        }
    }

    resetSelect(thisp);

    return false;
}
Example #15
0
bool LuaAutoCompleter::isInCommentHelper(const QTextCursor &cursor, Token *retToken) const
{
    LanguageFeatures features;
    features.qtEnabled = false;
    features.qtKeywordsEnabled = false;
    features.qtMocRunEnabled = false;
    features.cxx11Enabled = true;

    SimpleLexer tokenize;
    tokenize.setLanguageFeatures(features);

    const int prevState = BackwardsScanner::previousBlockState(cursor.block()) & 0xFF;
    const QList<Token> tokens = tokenize(cursor.block().text(), prevState);

    const unsigned pos = cursor.selectionEnd() - cursor.block().position();

    if (tokens.isEmpty() || pos < tokens.first().begin())
        return prevState > 0;

    if (pos >= tokens.last().end()) {
        const Token tk = tokens.last();
        if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
            return true;
        return tk.isComment() && (cursor.block().userState() & 0xFF);
    }

    Token tk = tokenAtPosition(tokens, pos);

    if (retToken)
        *retToken = tk;

    return tk.isComment();
}
Example #16
0
void CppQtStyleIndenter::indent(QTextDocument *doc,
                                const QTextCursor &cursor,
                                const QChar &typedChar,
                                const TextEditor::TabSettings &tabSettings)
{
    if (cursor.hasSelection()) {
        QTextBlock block = doc->findBlock(cursor.selectionStart());
        const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();

        CppTools::QtStyleCodeFormatter codeFormatter(tabSettings, codeStyleSettings());
        codeFormatter.updateStateUntil(block);

        QTextCursor tc = cursor;
        tc.beginEditBlock();
        do {
            int indent;
            int padding;
            codeFormatter.indentFor(block, &indent, &padding);
            tabSettings.indentLine(block, indent + padding, padding);
            codeFormatter.updateLineStateChange(block);
            block = block.next();
        } while (block.isValid() && block != end);
        tc.endEditBlock();
    } else {
        indentBlock(doc, cursor.block(), typedChar, tabSettings);
    }
}
Example #17
0
 void mousePressEvent(QMouseEvent * event)
 {
     if( event->buttons() != Qt::LeftButton )
         return;
     if( event->modifiers() == Qt::ShiftModifier )
     {
         QTextCursor cur = d_codeEditor->textCursor();
         const int selStart = d_codeEditor->document()->findBlock( cur.selectionStart() ).blockNumber();
         const int selEnd = d_codeEditor->document()->findBlock( cur.selectionEnd() ).blockNumber();
         Q_ASSERT( selStart <= selEnd ); // bei position und anchor nicht erfllt
         const int clicked = d_codeEditor->lineAt( event->pos() );
         if( clicked <= selEnd )
         {
             if( cur.selectionStart() == cur.position() )
                 d_codeEditor->selectLines( selEnd, clicked );
             else
                 d_codeEditor->selectLines( selStart, clicked );
         }else
             d_codeEditor->selectLines( selStart, clicked );
     }else
     {
         // Beginne Drag
         d_start = d_codeEditor->lineAt( event->pos() );
         d_codeEditor->selectLines( d_start, d_start );
     }
 }
Example #18
0
bool Text::replaceSpecialChars()
      {
      QTextCursor startCur = *_cursor;
      foreach (const char* s, charReplaceMap.keys()) {
            SymCode sym = *charReplaceMap.value(s);
            switch (sym.type) {
                  case SYMBOL_FRACTION:
                        if (!MScore::replaceFractions)
                              continue;
                        break;
                  default:
                        ;
                  }
            QTextCursor cur = _doc->find(s, _cursor->position() - 1 - strlen(s),
                  QTextDocument::FindWholeWords);
            if (cur.isNull())
                  continue;
            // do not go beyond the cursor
            if (cur.selectionEnd() > _cursor->selectionEnd())
                  continue;
            addSymbol(sym, &cur);
            return true;
            }
      return false;
      }
Example #19
0
void ClangFormat::disableFormattingSelectedText()
{
    TextEditor::TextEditorWidget *widget = TextEditor::TextEditorWidget::currentTextEditorWidget();
    if (!widget)
        return;

    const QTextCursor tc = widget->textCursor();
    if (!tc.hasSelection())
        return;

    // Insert start marker
    const QTextBlock selectionStartBlock = tc.document()->findBlock(tc.selectionStart());
    QTextCursor insertCursor(tc.document());
    insertCursor.beginEditBlock();
    insertCursor.setPosition(selectionStartBlock.position());
    insertCursor.insertText("// clang-format off\n");
    const int positionToRestore = tc.position();

    // Insert end marker
    QTextBlock selectionEndBlock = tc.document()->findBlock(tc.selectionEnd());
    insertCursor.setPosition(selectionEndBlock.position() + selectionEndBlock.length() - 1);
    insertCursor.insertText("\n// clang-format on");
    insertCursor.endEditBlock();

    // Reset the cursor position in order to clear the selection.
    QTextCursor restoreCursor(tc.document());
    restoreCursor.setPosition(positionToRestore);
    widget->setTextCursor(restoreCursor);

    // The indentation of these markers might be undesired, so reformat.
    // This is not optimal because two undo steps will be needed to remove the markers.
    const int reformatTextLength = insertCursor.position() - selectionStartBlock.position();
    BeautifierPlugin::formatCurrentFile(command(selectionStartBlock.position(),
                                                  reformatTextLength));
}
Example #20
0
void TikzEditorView::editComment()
{
	bool go = true;
	QTextCursor textCursor = m_tikzEditor->textCursor();
	if (textCursor.hasSelection())
	{
		textCursor.beginEditBlock();
		const int start = textCursor.selectionStart();
		int end = textCursor.selectionEnd();
		textCursor.setPosition(start, QTextCursor::MoveAnchor);
		textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
		while (textCursor.position() < end && go)
		{
			textCursor.insertText("% ");
			++end;
			++end;
			go = textCursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor);
		}
		textCursor.endEditBlock();
	}
	else
	{
		textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
		textCursor.insertText("% ");
	}
}
Example #21
0
static bool isInStringHelper(const QTextCursor &cursor, Token *retToken = 0)
{
    LanguageFeatures features;
    features.qtEnabled = false;
    features.qtKeywordsEnabled = false;
    features.qtMocRunEnabled = false;
    features.cxx11Enabled = true;
    features.c99Enabled = true;

    SimpleLexer tokenize;
    tokenize.setLanguageFeatures(features);

    const int prevState = BackwardsScanner::previousBlockState(cursor.block()) & 0xFF;
    const Tokens tokens = tokenize(cursor.block().text(), prevState);

    const unsigned pos = cursor.selectionEnd() - cursor.block().position();

    if (tokens.isEmpty() || pos <= tokens.first().utf16charsBegin())
        return false;

    if (pos >= tokens.last().utf16charsEnd()) {
        const Token tk = tokens.last();
        return tk.isStringLiteral() && prevState > 0;
    }

    Token tk = tokenAtPosition(tokens, pos);
    if (retToken)
        *retToken = tk;
    return tk.isStringLiteral() && pos > tk.utf16charsBegin();
}
Example #22
0
void TikzEditorView::editUncomment()
{
	bool go = true;
	QTextCursor textCursor = m_tikzEditor->textCursor();
	if (textCursor.hasSelection())
	{
		textCursor.beginEditBlock();
		const int start = textCursor.selectionStart();
		int end = textCursor.selectionEnd() - 2;
		textCursor.setPosition(start, QTextCursor::MoveAnchor);
		textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
		while (textCursor.position() < end && go)
		{
			textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
			if (textCursor.selectedText() == QLatin1String("% "))
			{
				textCursor.removeSelectedText();
				--end;
			}
			go = textCursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor);
		}
		textCursor.endEditBlock();
	}
	else
	{
		textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
		textCursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, 2);
		if (textCursor.selectedText() == QLatin1String("% "))
			textCursor.removeSelectedText();
	}
}
bool CppAutoCompleter::isInCommentHelper(const QTextCursor &cursor, Token *retToken) const
{
    SimpleLexer tokenize;
    tokenize.setQtMocRunEnabled(false);
    tokenize.setObjCEnabled(false);
    tokenize.setCxx0xEnabled(true);

    const int prevState = BackwardsScanner::previousBlockState(cursor.block()) & 0xFF;
    const QList<Token> tokens = tokenize(cursor.block().text(), prevState);

    const unsigned pos = cursor.selectionEnd() - cursor.block().position();

    if (tokens.isEmpty() || pos < tokens.first().begin())
        return prevState > 0;

    if (pos >= tokens.last().end()) {
        const Token tk = tokens.last();
        if (tk.is(T_CPP_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))
            return true;
        return tk.isComment() && (cursor.block().userState() & 0xFF);
    }

    Token tk = tokenAtPosition(tokens, pos);

    if (retToken)
        *retToken = tk;

    return tk.isComment();
}
void	QsvTextOperationsWidget::on_replaceOldText_returnPressed()
{
	if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier) ||
	    QApplication::keyboardModifiers().testFlag(Qt::AltModifier) ||
	    QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier) ) {
		on_replaceAll_clicked();
		showReplace();
		return;
	}

	QTextCursor c = m_searchCursor;
	QTextDocument *doc = getTextDocument();
	if (!doc){
		qDebug("%s:%d - no document found, using a wrong class? wrong parent?", __FILE__,__LINE__);
		return;
	}
	c = doc->find( replaceFormUi->findText->text(), c, getReplaceFlags() );
	if (c.isNull())
		return;

	int start = c.selectionStart();
	int end   = c.selectionEnd();
	c.beginEditBlock();
	c.deleteChar();
	c.insertText( replaceFormUi->replaceText->text() );
	c.setPosition(start,QTextCursor::KeepAnchor);
	c.setPosition(end  ,QTextCursor::MoveAnchor);
	c.endEditBlock();
	setTextCursor( c );

	// is there any other apperance of this text?
	m_searchCursor = c;
	updateReplaceInput();
}
Example #25
0
void GolangEdit::findLinkOutput(QByteArray data, bool bStdErr)
{
    if (bStdErr) {
        return;
    }

    if ( m_editor == m_liteApp->editorManager()->currentEditor()) {
        QTextCursor cur = this->textCursorForPos(QCursor::pos());
        cur.select(QTextCursor::WordUnderCursor);
        if (cur.selectionStart() == m_linkCursor.selectionStart() &&
                cur.selectionEnd() == m_linkCursor.selectionEnd()) {
            QStringList info = QString::fromUtf8(data).trimmed().split("\n");
            if (info.size() == 2) {
                if (info[0] != "-") {
                    QRegExp reg(":(\\d+):(\\d+)");
                    int pos = reg.lastIndexIn(info[0]);
                    if (pos >= 0) {
                        QString fileName = info[0].left(pos);
                        int line = reg.cap(1).toInt();
                        int col = reg.cap(2).toInt();
                        LiteApi::Link link(fileName,line-1,col-1);
                        m_lastCursor.select(QTextCursor::WordUnderCursor);
                        link.linkTextStart = m_linkCursor.selectionStart();
                        link.linkTextEnd = m_linkCursor.selectionEnd();
                        m_lastLink = link;
                        m_editor->showLink(link);
                    }
                }
                QRect rc = m_plainTextEdit->cursorRect(m_linkCursor);
                QPoint pt = m_plainTextEdit->mapToGlobal(rc.topRight());
                QToolTip::showText(pt,info[1],m_plainTextEdit);
            }
        }
    }
}
Example #26
0
int GenericCodeEditor::findAll( const QRegExp &expr, QTextDocument::FindFlags options )
{
    mSearchSelections.clear();

    if(expr.isEmpty()) {
        this->updateExtraSelections();
        return 0;
    }

    QTextEdit::ExtraSelection selection;
    selection.format = mSearchResultTextFormat;

    QTextDocument *doc = QPlainTextEdit::document();
    QTextBlock block = doc->begin();
    QTextCursor cursor;

    while (block.isValid()) {
        int blockPos = block.position();
        int offset = 0;
        while(findInBlock(doc, block, expr, offset, options, cursor)) {
            offset = cursor.selectionEnd() - blockPos;

            if (cursor.hasSelection()) {
                selection.cursor = cursor;
                mSearchSelections.append(selection);
            } else
                offset += 1;
        }
        block = block.next();
    }

    this->updateExtraSelections();

    return mSearchSelections.count();
}
Example #27
0
void GenericCodeEditor::updateDocLastSelection()
{
    QTextCursor cursor = textCursor();
    int start = cursor.selectionStart();
    int range = cursor.selectionEnd() - start;
    Main::scProcess()->updateSelectionMirrorForDocument(mDoc, start, range);
}
QString CWidgetReturnEmitTextEdit::textUnderCursor() const
{
	QTextCursor tc = textCursor();
	tc.select(QTextCursor::WordUnderCursor);

	// Save selected positions of current word.
	int selectionStart = tc.selectionStart();
	int selectionEnd = tc.selectionEnd();

	// If selection is at beginning of text edit then there can't be a slash to check for
	if (selectionStart == 0)
		return tc.selectedText();

	// Modify selection to include previous character
	tc.setPosition(selectionStart - 1, QTextCursor::MoveAnchor);
	tc.setPosition(selectionEnd, QTextCursor::KeepAnchor);

	// If previous character was / return current selection for command completion
	if(tc.selectedText().startsWith('/'))
		return tc.selectedText();
	else {
		// Else restore original selection and return for nick completion
		tc.setPosition(selectionStart, QTextCursor::MoveAnchor);
		tc.setPosition(selectionEnd, QTextCursor::KeepAnchor);
		return tc.selectedText();
	}
}
Example #29
0
void Indenter::reindent(QTextDocument *doc, const QTextCursor &cursor, BaseTextEditorWidget *editor)
{
    if (cursor.hasSelection()) {
        QTextBlock block = doc->findBlock(cursor.selectionStart());
        const QTextBlock end = doc->findBlock(cursor.selectionEnd()).next();

        const TabSettings &ts = editor->tabSettings();

        // skip empty blocks
        while (block.isValid() && block != end) {
            QString bt = block.text();
            if (ts.firstNonSpace(bt) < bt.size())
                break;
            indentBlock(doc, block, QChar::Null, editor);
            block = block.next();
        }

        int previousIndentation = ts.indentationColumn(block.text());
        indentBlock(doc, block, QChar::Null, editor);
        int currentIndentation = ts.indentationColumn(block.text());
        int delta = currentIndentation - previousIndentation;

        block = block.next();
        while (block.isValid() && block != end) {
            ts.reindentLine(block, delta);
            block = block.next();
        }
    } else {
        indentBlock(doc, cursor.block(), QChar::Null, editor);
    }
}
Example #30
0
void CCodeEdit::mouseMoveLineNumEvent(QMouseEvent* event)
{
	QPoint pt(0, event->y());

	if( m_lineIconSize.width() < event->x() &&
		0 != (event->buttons()& Qt::LeftButton) )
	{
		QTextCursor cursor = textCursor();
		if( cursor.position() == cursor.selectionStart() )
		{
			// 選択開始位置より前を選択
			cursor.setPosition(cursor.selectionEnd());
			cursor.movePosition(QTextCursor::EndOfLine);
			cursor.setPosition(cursorForPosition(pt).position(), QTextCursor::KeepAnchor);
			cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
		}
		else
		{
			// 選択開始位置より後ろを選択
			cursor.setPosition(cursor.selectionStart());
			cursor.movePosition(QTextCursor::StartOfLine);
			cursor.setPosition(cursorForPosition(pt).position(), QTextCursor::KeepAnchor);
			cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
			cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor);
		}
		setTextCursor(cursor);
	}
}