本文整理汇总了C++中QTextCursor::positionInBlock方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextCursor::positionInBlock方法的具体用法?C++ QTextCursor::positionInBlock怎么用?C++ QTextCursor::positionInBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextCursor
的用法示例。
在下文中一共展示了QTextCursor::positionInBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: wt_findPairChar
/*
charPos is positive position
*/
int Wt_TextEdit::wt_findPairChar(int charPos, QChar pairChar, bool findBefore) {
QTextCursor textCursor = this->textCursor();
textCursor.setPosition(charPos, QTextCursor::KeepAnchor);
int charBlockPos = textCursor.positionInBlock();
QString line = textCursor.block().text();
QChar curChar = line[charBlockPos];
printf("wt_findPairChar char:%c | toChar:%c \n" , curChar.cell(), pairChar.cell());
int pair = 0;
//int findBlockPos = -1;
int findPositivePos = -1;
int curPos = -1;
QString curString = "";
QTextCursor::MoveOperation direction = findBefore == true ? QTextCursor::PreviousCharacter : QTextCursor::NextCharacter;
for (int i = 0 ; textCursor.movePosition(direction, QTextCursor::KeepAnchor) ; i++) {
curPos = textCursor.positionInBlock();
curString = textCursor.block().text();
if(curString[curPos] == curChar) {
pair += 1;
}
if(curString[curPos] == pairChar) {
pair -= 1;
}
if(pair < 0) {
//findBlockPos = curPos;
findPositivePos = textCursor.position();
return findPositivePos;
}
}
return -1;
}
示例2: wt_indentChar
/*
indent when there has nothing before the char in the block
charPos is positive position
*/
void Wt_TextEdit::wt_indentChar(int charPos, int toCharPos)
{
printf("wt_indentChar \n");
QTextCursor textCursor = this->textCursor();
textCursor.setPosition(toCharPos, QTextCursor::MoveAnchor);
QString toLine = textCursor.block().text();
textCursor.setPosition(charPos, QTextCursor::MoveAnchor);
int charBlockPos = textCursor.positionInBlock();
QString line = textCursor.block().text();
QChar mchar = line[charBlockPos];
// printf("mchar : %c | line : %s | charBlockPos: %d\n", mchar.cell(), line.toStdString().c_str(), charBlockPos);
if(line.trimmed()[0] != mchar) {
printf("mchar return : %c\n", mchar.cell());
return;
}
/*
textCursor.setPosition(charPos, QTextCursor::KeepAnchor);
pos between Anchor will be selected , delete will clear all selection
*/
for(int i = 0; i < charBlockPos ; i++) {
printf("deleteChar\n");
textCursor.deletePreviousChar();
}
for(int i = 0 ; i < toLine.length() ; i++) {
if(toLine[i] == ' ' || toLine[i] == '\t') {
textCursor.insertText(QString(toLine[i]));
} else {
break;
}
}
}
示例3: initialize
CompletionContextFinder::CompletionContextFinder(const QTextCursor &cursor)
: m_cursor(cursor)
, m_colonCount(-1)
, m_behaviorBinding(false)
, m_inStringLiteral(false)
, m_inImport(false)
{
QTextBlock lastBlock = cursor.block();
if (lastBlock.next().isValid())
lastBlock = lastBlock.next();
initialize(cursor.document()->begin(), lastBlock);
m_startTokenIndex = yyLinizerState.tokens.size() - 1;
// Initialize calls readLine - which skips empty lines. We should only adjust
// the start token index if the linizer still is in the same block as the cursor.
const int cursorPos = cursor.positionInBlock();
if (yyLinizerState.iter == cursor.block()) {
for (; m_startTokenIndex >= 0; --m_startTokenIndex) {
const Token &token = yyLinizerState.tokens.at(m_startTokenIndex);
if (token.end() <= cursorPos)
break;
if (token.begin() < cursorPos && token.is(Token::String))
m_inStringLiteral = true;
}
if (m_startTokenIndex == yyLinizerState.tokens.size() - 1 && yyLinizerState.insertedSemicolon)
--m_startTokenIndex;
}
getQmlObjectTypeName(m_startTokenIndex);
checkBinding();
checkImport();
}
示例4: contextAllowsElectricCharacters
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;
}
示例5: contextAllowsElectricCharacters
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;
}
示例6: handleKeyBackspace
void GenericCodeEditor::handleKeyBackspace(QKeyEvent * event, QTextCursor & textCursor, bool & updateCursor)
{
if (event->modifiers() & Qt::META) {
textCursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
textCursor.removeSelectedText();
} else {
if ( !overwriteMode()
|| (textCursor.positionInBlock() == 0)
|| textCursor.hasSelection() ) {
QPlainTextEdit::keyPressEvent(event);
} else {
// in overwrite mode, backspace should insert a space
textCursor.beginEditBlock();
textCursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
QString selectedText = textCursor.selectedText();
if (selectedText == QStringLiteral(" ") ||
selectedText == QStringLiteral("\t") ) {
textCursor.clearSelection();
} else {
textCursor.insertText(QString(QChar(' ')));
textCursor.movePosition(QTextCursor::PreviousCharacter);
}
textCursor.endEditBlock();
}
updateCursor = true;
}
}
示例7: wt_pressEnter
void Wt_TextEdit::wt_pressEnter()
{
printf("wt_pressEnter \n");
QTextCursor textCursor = this->textCursor();
int curPos = textCursor.positionInBlock();
QString curString = textCursor.block().text();
QChar curChar = curString[curPos];
textCursor.insertText("\n");
if(curString != NULL && curString.length() > 0) {
for(int i = 0 ;i < curString.length() && curString[i]=='\t' ; i++) {
textCursor.insertText("\t");
}
curString = curString.trimmed();
if(curString != NULL && curString.length() > 0 && curString[curString.length()-1] == '{') {
textCursor.insertText("\t");
}
//if(curString != NULL && curString.length() > 0 && curString[curString.length()-1] == '}') {
// wt_indentCurrentSymbol('{');
//}
if(curChar == '}') {
wt_indentCurrentSymbol('{');
}
}
//wt_indentSymbol('{', '}');
}
示例8: startLocalRenaming
void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback)
{
using CppTools::CompilerOptionsBuilder;
setRefactoringEngineAvailable(false);
m_client.setLocalRenamingCallback(std::move(renameSymbolsCallback));
QString filePath = data.filePath().toString();
QTextCursor textCursor = data.cursor();
CompilerOptionsBuilder optionsBuilder{*projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR};
Utils::SmallStringVector commandLine{optionsBuilder.build(
fileKindInProjectPart(projectPart, filePath),
CppTools::getPchUsage())};
commandLine.push_back(filePath);
RequestSourceLocationsForRenamingMessage message(ClangBackEnd::FilePath(filePath),
uint(textCursor.blockNumber() + 1),
uint(textCursor.positionInBlock() + 1),
textCursor.document()->toPlainText(),
std::move(commandLine),
textCursor.document()->revision());
m_server.requestSourceLocationsForRenamingMessage(std::move(message));
}
示例9: assignCommentOffset
void DoxygenGenerator::assignCommentOffset(QTextCursor cursor)
{
if (cursor.hasSelection()) {
if (cursor.anchor() < cursor.position())
cursor.setPosition(cursor.anchor());
}
m_commentOffset = cursor.positionInBlock();
}
示例10: insertParagraphSeparator
QString MatchingText::insertParagraphSeparator(const QTextCursor &tc) const
{
BackwardsScanner tk(tc, MAX_NUM_LINES);
int index = tk.startToken();
if (tk[index - 1].isNot(T_LBRACE))
return QString(); // nothing to do.
const QString textBlock = tc.block().text().mid(tc.positionInBlock()).trimmed();
if (! textBlock.isEmpty())
return QString();
--index; // consume the `{'
const Token &token = tk[index - 1];
if (token.is(T_STRING_LITERAL) && tk[index - 2].is(T_EXTERN)) {
// recognized extern "C"
return QLatin1String("}");
} else if (token.is(T_IDENTIFIER)) {
int i = index - 1;
forever {
const Token ¤t = tk[i - 1];
if (current.is(T_EOF_SYMBOL))
break;
else if (current.is(T_CLASS) || current.is(T_STRUCT) || current.is(T_UNION) || current.is(T_ENUM)) {
// found a class key.
QString str = QLatin1String("};");
if (shouldInsertNewline(tc))
str += QLatin1Char('\n');
return str;
}
else if (current.is(T_NAMESPACE))
return QLatin1String("}"); // found a namespace declaration
else if (current.is(T_SEMICOLON))
break; // found the `;' sync token
else if (current.is(T_LBRACE) || current.is(T_RBRACE))
break; // braces are considered sync tokens
else if (current.is(T_LPAREN) || current.is(T_RPAREN))
break; // sync token
else if (current.is(T_LBRACKET) || current.is(T_RBRACKET))
break; // sync token
--i;
}
}
示例11: blockAroundCursor
QTextCursor ScCodeEditor::blockAroundCursor( const QTextCursor & cursor )
{
TokenIterator left_bracket =
previousOpeningBracket(
TokenIterator::leftOf(cursor.block(), cursor.positionInBlock()) );
if (!left_bracket.isValid())
return QTextCursor();
TokenIterator right_bracket =
nextClosingBracket(
TokenIterator::rightOf(cursor.block(), cursor.positionInBlock()) );
if (!right_bracket.isValid())
return QTextCursor();
QTextCursor block_cursor = cursor;
block_cursor.setPosition( left_bracket.position() );
block_cursor.setPosition( right_bracket.position() + 1, QTextCursor::KeepAnchor );
return block_cursor;
}
示例12: symbolUnderCursor
QString GenericCodeEditor::symbolUnderCursor()
{
const QTextCursor cursor = textCursor();
if (cursor.hasSelection())
return cursor.selectedText();
const QString blockString = cursor.block().text();
const int position = cursor.positionInBlock();
return tokenInStringAt( position, blockString );
}
示例13: gotoNextBlock
void ScCodeEditor::gotoNextBlock()
{
QTextCursor cursor = textCursor();
TokenIterator tokenIt = TokenIterator::rightOf( cursor.block(), cursor.positionInBlock() );
if (tokenIt.type() == Token::OpeningBracket
&& tokenIt.block() == cursor.block()
&& tokenIt->positionInBlock == cursor.positionInBlock())
++tokenIt;
tokenIt = nextClosingBracket( tokenIt );
if (tokenIt.isValid())
setTextCursor( cursorAt(tokenIt, 1) );
else {
cursor.movePosition( QTextCursor::End );
setTextCursor( cursor );
}
}
示例14: gotoPreviousBlock
void ScCodeEditor::gotoPreviousBlock()
{
QTextCursor cursor = textCursor();
TokenIterator tokenIt = TokenIterator::leftOf(cursor.block(), cursor.positionInBlock());
if (tokenIt.type() == Token::ClosingBracket
&& tokenIt.block() == cursor.block()
&& tokenIt->positionInBlock == cursor.positionInBlock() - 1)
--tokenIt;
tokenIt = previousOpeningBracket( tokenIt );
if (tokenIt.isValid())
setTextCursor( cursorAt(tokenIt) );
else {
cursor.movePosition( QTextCursor::Start );
setTextCursor( cursor );
}
}
示例15: paragraphSeparatorAboutToBeInserted
int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor,
const TabSettings &tabSettings)
{
if (!m_autoParenthesesEnabled)
return 0;
QTextDocument *doc = cursor.document();
if (doc->characterAt(cursor.position() - 1) != QLatin1Char('{'))
return 0;
if (!contextAllowsAutoParentheses(cursor))
return 0;
// verify that we indeed do have an extra opening brace in the document
QTextBlock block = cursor.block();
const QString textFromCusror = block.text().mid(cursor.positionInBlock()).trimmed();
int braceDepth = TextDocumentLayout::braceDepth(doc->lastBlock());
if (braceDepth <= 0 && (textFromCusror.isEmpty() || textFromCusror.at(0) != QLatin1Char('}')))
return 0; // braces are all balanced or worse, no need to do anything and separator inserted not between '{' and '}'
// we have an extra brace , let's see if we should close it
/* verify that the next block is not further intended compared to the current block.
This covers the following case:
if (condition) {|
statement;
*/
int indentation = tabSettings.indentationColumn(block.text());
if (block.next().isValid()) { // not the last block
block = block.next();
//skip all empty blocks
while (block.isValid() && tabSettings.onlySpace(block.text()))
block = block.next();
if (block.isValid()
&& tabSettings.indentationColumn(block.text()) > indentation)
return 0;
}
const QString &textToInsert = insertParagraphSeparator(cursor);
int pos = cursor.position();
cursor.insertBlock();
cursor.insertText(textToInsert);
cursor.setPosition(pos);
// if we actually insert a separator, allow it to be overwritten if
// user types it
if (!textToInsert.isEmpty())
m_allowSkippingOfBlockEnd = true;
return 1;
}