📄 completion.cpp
字号:
} if ( ke->text().length() && !( ke->state() & Qt::AltModifier ) && ( !ke->ascii() || ke->ascii() >= 32 ) || ( ke->text() == QString::fromLatin1("\t") && !( ke->state() & Qt::ControlModifier ) ) ) { if ( ke->key() == Qt::Key_Tab ) { if ( curEditor->textCursor()->index() == 0 && curEditor->textCursor()->paragraph()->isListItem() ) return false; if ( doCompletion() ) return true; } else if ( ke->key() == Qt::Key_Period && ( curEditor->textCursor()->index() == 0 || curEditor->textCursor()->paragraph()->at( curEditor->textCursor()->index() - 1 )->c != '.' ) || ke->key() == Qt::Key_Greater && curEditor->textCursor()->index() > 0 && curEditor->textCursor()->paragraph()->at( curEditor->textCursor()->index() - 1 )->c == '-' ) { doObjectCompletion(); } else { if ( !doArgumentHint( ke->text() == QString::fromLatin1("(") ) ) functionLabel->hide(); } } } else if ( o == completionPopup || o == completionListBox || o == completionListBox->viewport() ) { if ( e->type() == QEvent::KeyPress ) { QKeyEvent *ke = (QKeyEvent*)e; if ( ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Tab ) { if ( ke->key() == Qt::Key_Tab && completionListBox->count() > 1 && completionListBox->currentItem() < (int)completionListBox->count() - 1 ) { completionListBox->setCurrentItem( completionListBox->currentItem() + 1 ); return true; } completeCompletion(); return true; } else if ( ke->key() == Qt::Key_Left || ke->key() == Qt::Key_Right || ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down || ke->key() == Qt::Key_Home || ke->key() == Qt::Key_End || ke->key() == Qt::Key_PageUp || ke->key() == Qt::Key_PageDown ) { if (o == completionPopup) return QApplication::sendEvent(completionListBox, ke); else return false; } else if ( ke->key() != Qt::Key_Shift && ke->key() != Qt::Key_Control && ke->key() != Qt::Key_Alt ) { int l = searchString.length(); if ( ke->key() == Qt::Key_Backspace ) { searchString.remove( searchString.length() - 1, 1 ); } else { searchString += ke->text(); l = 1; } if ( !l || !continueComplete() ) { completionPopup->close(); curEditor->setFocus(); } QApplication::sendEvent( curEditor, e ); return true; } } else if ( e->type() == QEvent::MouseButtonDblClick ) { completeCompletion(); return true; } } if ( (o == functionLabel || qobject_cast<Editor*>(o)) && functionLabel->isVisible() ) { if ( e->type() == QEvent::KeyPress ) { QKeyEvent *ke = (QKeyEvent*)e; if ( ke->key() == Qt::Key_Escape ) { functionLabel->hide(); } else { if ( !doArgumentHint( ke->text() == QString::fromLatin1("(") ) ) functionLabel->hide(); if ( o == functionLabel ) { QApplication::sendEvent( curEditor, e ); return true; } } } } return false;}void EditorCompletion::completeCompletion(){ int idx = curEditor->textCursor()->index(); QString s = completionListBox->currentText().mid( searchString.length() ); curEditor->insert( s, (uint) ( Q3TextEdit::RedoIndentation | Q3TextEdit::CheckNewLines | Q3TextEdit::RemoveSelected ) ); int i = s.find( '(' ); completionPopup->close(); curEditor->setFocus(); if ( i != -1 && i < (int)s.length() ) { curEditor->setCursorPosition( curEditor->textCursor()->paragraph()->paragId(), idx + i + 1 ); doArgumentHint( false ); }}void EditorCompletion::setCurrentEdior( Editor *e ){ curEditor = e; curEditor->installEventFilter( this );}void EditorCompletion::addEditor( Editor *e ){ e->installEventFilter( this );}bool EditorCompletion::doObjectCompletion(){ searchString = ""; QString object; int i = curEditor->textCursor()->index(); i--; Q3TextParagraph *p = curEditor->textCursor()->paragraph(); for (;;) { if ( i < 0 ) break; if ( p->at( i )->c == ' ' || p->at( i )->c == '\t' ) break; object.prepend( p->at( i )->c ); i--; } if ( object[ (int)object.length() - 1 ] == '-' ) object.remove( object.length() - 1, 1 ); if ( object.isEmpty() ) return false; return doObjectCompletion( object );}bool EditorCompletion::doObjectCompletion( const QString & ){ return false;}static void strip( QString &txt ){ int i = txt.find( QString::fromLatin1("(") ); if ( i == -1 ) return; txt = txt.left( i );}bool EditorCompletion::continueComplete(){ if ( searchString.isEmpty() ) { completionListBox->clear(); for ( Q3ValueList<CompletionEntry>::ConstIterator it = cList.begin(); it != cList.end(); ++it ) (void)new CompletionItem( completionListBox, (*it).text, (*it).type, (*it).postfix, (*it).prefix, (*it).postfix2 ); completionListBox->setCurrentItem( 0 ); completionListBox->setSelected( completionListBox->currentItem(), true ); return true; } Q3ListBoxItem *i = completionListBox->findItem( searchString ); if ( !i ) return false; QString txt1 = i->text(); QString txt2 = searchString; strip( txt1 ); strip( txt2 ); if ( txt1 == txt2 && !i->next() ) return false; Q3ValueList<CompletionEntry> res; for ( Q3ValueList<CompletionEntry>::ConstIterator it = cList.begin(); it != cList.end(); ++it ) { if ( (*it).text.left( searchString.length() ) == searchString ) res << *it; } if ( res.isEmpty() ) return false; completionListBox->clear(); for ( Q3ValueList<CompletionEntry>::ConstIterator it2 = res.begin(); it2 != res.end(); ++it2 ) (void)new CompletionItem( completionListBox, (*it2).text, (*it2).type, (*it2).postfix, (*it2).prefix, (*it2).postfix2 ); completionListBox->setCurrentItem( 0 ); completionListBox->setSelected( completionListBox->currentItem(), true ); return true;}bool EditorCompletion::doArgumentHint( bool useIndex ){ Q3TextCursor *cursor = curEditor->textCursor(); int i = cursor->index() ; if ( !useIndex ) { bool foundParen = false; int closeParens = 0; while ( i >= 0 ) { if ( cursor->paragraph()->at( i )->c == ')' && i != cursor->index() ) closeParens++; if ( cursor->paragraph()->at( i )->c == '(' ) { closeParens--; if ( closeParens == -1 ) { foundParen = true; break; } } --i; } if ( !foundParen ) return false; } int j = i - 1; bool foundSpace = false; bool foundNonSpace = false; while ( j >= 0 ) { if ( foundNonSpace && ( cursor->paragraph()->at( j )->c == ' ' || cursor->paragraph()->at( j )->c == ',' ) ) { foundSpace = true; break; } if ( !foundNonSpace && ( cursor->paragraph()->at( j )->c != ' ' || cursor->paragraph()->at( j )->c != ',' ) ) foundNonSpace = true; --j; } if ( foundSpace ) ++j; j = QMAX( j, 0 ); QString function( cursor->paragraph()->string()->toString().mid( j, i - j + 1 ) ); QString part = cursor->paragraph()->string()->toString().mid( j, cursor->index() - j + 1 ); function = function.simplifyWhiteSpace(); for (;;) { if (function.isEmpty()) return false; if ( function[ (int)function.length() - 1 ] == '(' ) { function.remove( function.length() - 1, 1 ); function = function.simplifyWhiteSpace(); } else if ( function[ (int)function.length() - 1 ] == ')' ) { function.remove( function.length() - 1, 1 ); function = function.simplifyWhiteSpace(); } else { break; } } QChar sep; QString pre, post; Q3ValueList<QStringList> argl = functionParameters( function, sep, pre, post ); if ( argl.isEmpty() ) return false; QString label; int w = 0; int num = 0; if ( !functionLabel->isVisible() ) functionLabel->setNumFunctions( argl.count() ); for ( Q3ValueList<QStringList>::Iterator vit = argl.begin(); vit != argl.end(); ++vit, ++num ) { QStringList args = *vit; int argNum = 0; int inParen = 0; for ( int k = 0; k < (int)part.length(); ++k ) { if ( part[ k ] == sep && inParen < 2 ) argNum++; if ( part[ k ] == '(' ) inParen++; if ( part[ k ] == ')' ) inParen--; } QString func = function; int pnt = -1; pnt = func.findRev( '.' ); if ( pnt == -1 ) func.findRev( '>' ); if ( pnt != -1 ) func = func.mid( pnt + 1 ); QString s = func + "( "; if ( s[ 0 ] == '\"' ) s.remove( (uint)0, 1 ); i = 0; for ( QStringList::Iterator it = args.begin(); it != args.end(); ++it, ++i ) { if ( i == argNum ) s += "<b>" + *it + "</b>"; else s += *it; if ( i < (int)args.count() - 1 ) s += QString::fromLatin1(", "); else s += QString::fromLatin1(" "); } s += QString::fromLatin1(")"); s.prepend( pre ); s.append( post ); label += "<p>" + s + "</p>"; functionLabel->setFunctionText( num, s ); w = QMAX( w, functionLabel->fontMetrics().width( s ) + 10 ); } w += 16; if ( label.isEmpty() ) return false; if ( functionLabel->isVisible() ) { functionLabel->resize( w + 50, QMAX( functionLabel->fontMetrics().height(), 16 ) ); } else { Q3TextStringChar *chr = cursor->paragraph()->at( cursor->index() ); int h = cursor->paragraph()->lineHeightOfChar( cursor->index() ); int x = cursor->paragraph()->rect().x() + chr->x; int y, dummy; cursor->paragraph()->lineHeightOfChar( cursor->index(), &dummy, &y ); y += cursor->paragraph()->rect().y(); functionLabel->resize( w + 50, QMAX( functionLabel->fontMetrics().height(), 16 ) ); functionLabel->move( curEditor->mapToGlobal( curEditor->contentsToViewport( QPoint( x, y + h ) ) ) ); if ( functionLabel->x() + functionLabel->width() > QApplication::desktop()->width() ) functionLabel->move( QMAX( 0, QApplication::desktop()->width() - functionLabel->width() ), functionLabel->y() ); functionLabel->show(); curEditor->setFocus(); } QTimer::singleShot( 0, functionLabel, SLOT( relayout() ) ); return true;}Q3ValueList<QStringList> EditorCompletion::functionParameters( const QString &, QChar &, QString &, QString & ){ return Q3ValueList<QStringList>();}void EditorCompletion::setContext( QObject * ){}void EditorCompletion::showCompletion( const Q3ValueList<CompletionEntry> &lst ){ Q3TextCursor *cursor = curEditor->textCursor(); Q3TextStringChar *chr = cursor->paragraph()->at( cursor->index() ); int h = cursor->paragraph()->lineHeightOfChar( cursor->index() ); int x = cursor->paragraph()->rect().x() + chr->x; int y, dummy; cursor->paragraph()->lineHeightOfChar( cursor->index(), &dummy, &y ); y += cursor->paragraph()->rect().y(); completionListBox->clear(); for ( Q3ValueList<CompletionEntry>::ConstIterator it = lst.begin(); it != lst.end(); ++it ) (void)new CompletionItem( completionListBox, (*it).text, (*it).type, (*it).postfix, (*it).prefix, (*it).postfix2 ); cList = lst; completionPopup->resize( completionListBox->sizeHint() + QSize( completionListBox->verticalScrollBar()->width() + 4, completionListBox->horizontalScrollBar()->height() + 4 ) ); completionListBox->setCurrentItem( 0 ); if ( curEditor->mapToGlobal( QPoint( 0, y ) ).y() + h + completionPopup->height() < QApplication::desktop()->height() ) completionPopup->move( curEditor->mapToGlobal( curEditor-> contentsToViewport( QPoint( x, y + h ) ) ) ); else completionPopup->move( curEditor->mapToGlobal( curEditor-> contentsToViewport( QPoint( x, y - completionPopup->height() ) ) ) ); completionPopup->show();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -