From ae1bb00af71eb5e7b6420c84635ac0f1e8ae5a5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Fri, 28 Aug 2020 01:03:00 -0300 Subject: [PATCH] ecode: Syntax highlighting in project search. --- .../eepp/ui/tools/uicodeeditorsplitter.hpp | 4 +- include/eepp/ui/uitextview.hpp | 2 + include/eepp/ui/uitreeview.hpp | 72 ++++++++++++++- src/eepp/ui/tools/uicodeeditorsplitter.cpp | 6 +- src/eepp/ui/uitextview.cpp | 7 ++ src/eepp/ui/uitreeview.cpp | 77 ++-------------- src/tools/codeeditor/codeeditor.cpp | 91 +++++++++++++++++-- 7 files changed, 180 insertions(+), 79 deletions(-) diff --git a/include/eepp/ui/tools/uicodeeditorsplitter.hpp b/include/eepp/ui/tools/uicodeeditorsplitter.hpp index a5539c864..69207e36c 100644 --- a/include/eepp/ui/tools/uicodeeditorsplitter.hpp +++ b/include/eepp/ui/tools/uicodeeditorsplitter.hpp @@ -101,7 +101,9 @@ class EE_API UICodeEditorSplitter { UICodeEditor* getCurEditor() const; - const std::string& getCurrentColorScheme() const; + const SyntaxColorScheme& getCurrentColorScheme() const; + + const std::string& getCurrentColorSchemeName() const; void setColorScheme( const std::string& name ); diff --git a/include/eepp/ui/uitextview.hpp b/include/eepp/ui/uitextview.hpp index 52b56dfb4..0b9dade9d 100644 --- a/include/eepp/ui/uitextview.hpp +++ b/include/eepp/ui/uitextview.hpp @@ -88,6 +88,8 @@ class EE_API UITextView : public UIWidget { void setTextAlign( const Uint32& align ); + UITextView* setFontFillColor( const Color& color, Uint32 from, Uint32 to ); + protected: Text* mTextCache; String mString; diff --git a/include/eepp/ui/uitreeview.hpp b/include/eepp/ui/uitreeview.hpp index c85887fbe..8c4c75ea8 100644 --- a/include/eepp/ui/uitreeview.hpp +++ b/include/eepp/ui/uitreeview.hpp @@ -13,6 +13,72 @@ namespace EE { namespace UI { class UITableRow; +class EE_API UITreeViewCell : public UITableCell { + public: + static UITreeViewCell* New() { return eeNew( UITreeViewCell, () ); } + + Uint32 getType() const { return UI_TYPE_TREEVIEW_CELL; } + + bool isType( const Uint32& type ) const { + return UITreeViewCell::getType() == type ? true : UITableCell::isType( type ); + } + + UIImage* getImage() const { return mImage; } + + Rectf calculatePadding() const { + Sizef size; + Rectf autoPadding; + if ( mFlags & UI_AUTO_PADDING ) { + autoPadding = makePadding( true, true, true, true ); + if ( autoPadding != Rectf() ) + autoPadding = PixelDensity::dpToPx( autoPadding ); + } + if ( mPaddingPx.Top > autoPadding.Top ) + autoPadding.Top = mPaddingPx.Top; + if ( mPaddingPx.Bottom > autoPadding.Bottom ) + autoPadding.Bottom = mPaddingPx.Bottom; + if ( mPaddingPx.Left > autoPadding.Left ) + autoPadding.Left = mPaddingPx.Left; + if ( mPaddingPx.Right > autoPadding.Right ) + autoPadding.Right = mPaddingPx.Right; + autoPadding.Left += mIndent; + return autoPadding; + } + + void setIndentation( const Float& indent ) { + if ( mIndent != indent ) { + mIndent = indent; + updateLayout(); + } + } + + const Float& getIndentation() const { return mIndent; } + + protected: + mutable UIImage* mImage{nullptr}; + Float mIndent{0}; + + UITreeViewCell() : UITableCell( "treeview::cell" ) { + mTextBox->setElementTag( mTag + "::text" ); + mIcon->setElementTag( mTag + "::icon" ); + mInnerWidgetOrientation = InnerWidgetOrientation::Left; + auto cb = [&]( const Event* ) { updateLayout(); }; + mImage = UIImage::NewWithTag( mTag + "::expander" ); + mImage->setScaleType( UIScaleType::FitInside ) + ->setLayoutSizePolicy( SizePolicy::Fixed, SizePolicy::Fixed ) + ->setFlags( UI_VALIGN_CENTER | UI_HALIGN_CENTER ) + ->setParent( const_cast( this ) ) + ->setVisible( false ) + ->setEnabled( false ); + mImage->addEventListener( Event::OnPaddingChange, cb ); + mImage->addEventListener( Event::OnMarginChange, cb ); + mImage->addEventListener( Event::OnSizeChange, cb ); + mImage->addEventListener( Event::OnVisibleChange, cb ); + } + + virtual UIWidget* getExtraInnerWidget() const { return mImage; } +}; + class EE_API UITreeView : public UIAbstractTableView { public: static UITreeView* New(); @@ -59,8 +125,7 @@ class EE_API UITreeView : public UIAbstractTableView { void setExpanderIconSize( const size_t& expanderSize ); - virtual ModelIndex findRowWithText( const std::string& text, - const bool& caseSensitive = false, + virtual ModelIndex findRowWithText( const std::string& text, const bool& caseSensitive = false, const bool& exactMatch = false ); protected: @@ -109,6 +174,9 @@ class EE_API UITreeView : public UIAbstractTableView { void updateContentSize(); void setAllExpanded( const ModelIndex& index = {}, bool expanded = true ); + + virtual UIWidget* setupCell( UITableCell* widget, UIWidget* rowWidget, + const ModelIndex& index ); }; }} // namespace EE::UI diff --git a/src/eepp/ui/tools/uicodeeditorsplitter.cpp b/src/eepp/ui/tools/uicodeeditorsplitter.cpp index de7041fe5..e01d2d049 100644 --- a/src/eepp/ui/tools/uicodeeditorsplitter.cpp +++ b/src/eepp/ui/tools/uicodeeditorsplitter.cpp @@ -262,10 +262,14 @@ UICodeEditor* UICodeEditorSplitter::createCodeEditor() { return editor; } -const std::string& UICodeEditorSplitter::getCurrentColorScheme() const { +const std::string& UICodeEditorSplitter::getCurrentColorSchemeName() const { return mCurrentColorScheme; } +const SyntaxColorScheme& UICodeEditorSplitter::getCurrentColorScheme() const { + return mColorSchemes.at( mCurrentColorScheme ); +} + void UICodeEditorSplitter::setColorScheme( const std::string& name ) { if ( name != mCurrentColorScheme ) { mCurrentColorScheme = name; diff --git a/src/eepp/ui/uitextview.cpp b/src/eepp/ui/uitextview.cpp index 621bde976..545d1cdc3 100644 --- a/src/eepp/ui/uitextview.cpp +++ b/src/eepp/ui/uitextview.cpp @@ -216,6 +216,13 @@ UITextView* UITextView::setFontColor( const Color& color ) { return this; } +UITextView* UITextView::setFontFillColor( const Color& color, Uint32 from, Uint32 to ) { + Color newColor( color.r, color.g, color.b, color.a * mAlpha / 255.f ); + mTextCache->setFillColor( newColor, from, to ); + invalidateDraw(); + return this; +} + const Color& UITextView::getFontShadowColor() const { return mFontStyleConfig.ShadowColor; } diff --git a/src/eepp/ui/uitreeview.cpp b/src/eepp/ui/uitreeview.cpp index 943722dee..9ae61cf8f 100644 --- a/src/eepp/ui/uitreeview.cpp +++ b/src/eepp/ui/uitreeview.cpp @@ -108,75 +108,8 @@ void UITreeView::updateContentSize() { onContentSizeChange(); } -class UITreeViewCell : public UITableCell { - public: - static UITreeViewCell* New() { return eeNew( UITreeViewCell, () ); } - - Uint32 getType() const { return UI_TYPE_TREEVIEW_CELL; } - - bool isType( const Uint32& type ) const { - return UITreeViewCell::getType() == type ? true : UITableCell::isType( type ); - } - - UIImage* getImage() const { return mImage; } - - Rectf calculatePadding() const { - Sizef size; - Rectf autoPadding; - if ( mFlags & UI_AUTO_PADDING ) { - autoPadding = makePadding( true, true, true, true ); - if ( autoPadding != Rectf() ) - autoPadding = PixelDensity::dpToPx( autoPadding ); - } - if ( mPaddingPx.Top > autoPadding.Top ) - autoPadding.Top = mPaddingPx.Top; - if ( mPaddingPx.Bottom > autoPadding.Bottom ) - autoPadding.Bottom = mPaddingPx.Bottom; - if ( mPaddingPx.Left > autoPadding.Left ) - autoPadding.Left = mPaddingPx.Left; - if ( mPaddingPx.Right > autoPadding.Right ) - autoPadding.Right = mPaddingPx.Right; - autoPadding.Left += mIndent; - return autoPadding; - } - - void setIndentation( const Float& indent ) { - if ( mIndent != indent ) { - mIndent = indent; - updateLayout(); - } - } - - const Float& getIndentation() const { return mIndent; } - - protected: - mutable UIImage* mImage{nullptr}; - Float mIndent{0}; - - UITreeViewCell() : UITableCell( "treeview::cell" ) { - mTextBox->setElementTag( mTag + "::text" ); - mIcon->setElementTag( mTag + "::icon" ); - mInnerWidgetOrientation = InnerWidgetOrientation::Left; - auto cb = [&]( const Event* ) { updateLayout(); }; - mImage = UIImage::NewWithTag( mTag + "::expander" ); - mImage->setScaleType( UIScaleType::FitInside ) - ->setLayoutSizePolicy( SizePolicy::Fixed, SizePolicy::Fixed ) - ->setFlags( UI_VALIGN_CENTER | UI_HALIGN_CENTER ) - ->setParent( const_cast( this ) ) - ->setVisible( false ) - ->setEnabled( false ); - mImage->addEventListener( Event::OnPaddingChange, cb ); - mImage->addEventListener( Event::OnMarginChange, cb ); - mImage->addEventListener( Event::OnSizeChange, cb ); - mImage->addEventListener( Event::OnVisibleChange, cb ); - } - - virtual UIWidget* getExtraInnerWidget() const { return mImage; } -}; - -UIWidget* UITreeView::createCell( UIWidget* rowWidget, const ModelIndex& index ) { - UITableCell* widget = index.column() == (Int64)getModel()->treeColumn() ? UITreeViewCell::New() - : UITableCell::New(); +UIWidget* UITreeView::setupCell( UITableCell* widget, UIWidget* rowWidget, + const ModelIndex& index ) { widget->setParent( rowWidget ); widget->unsetFlags( UI_AUTO_SIZE ); widget->clipEnable(); @@ -219,6 +152,12 @@ UIWidget* UITreeView::createCell( UIWidget* rowWidget, const ModelIndex& index ) return widget; } +UIWidget* UITreeView::createCell( UIWidget* rowWidget, const ModelIndex& index ) { + UITableCell* widget = index.column() == (Int64)getModel()->treeColumn() ? UITreeViewCell::New() + : UITableCell::New(); + return setupCell( widget, rowWidget, index ); +} + UIWidget* UITreeView::updateCell( const int& rowIndex, const ModelIndex& index, const size_t& indentLevel, const Float& yOffset ) { if ( rowIndex >= (int)mWidgets.size() ) diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index d5431af8d..104914bed 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -359,7 +359,7 @@ void App::loadConfig() { } void App::saveConfig() { - mConfig.editor.colorScheme = mEditorSplitter->getCurrentColorScheme(); + mConfig.editor.colorScheme = mEditorSplitter->getCurrentColorSchemeName(); mConfig.window.size = mWindow->getLastWindowedSize(); mConfig.window.maximized = mWindow->isMaximized(); mIni.setValue( "editor", "colorscheme", mConfig.editor.colorScheme ); @@ -683,6 +683,83 @@ void App::hideGlobalSearchBar() { mGlobalSearchTree->setVisible( false ); } +class UITreeViewCellGlobalSearch : public UITreeViewCell { + public: + static UITreeViewCellGlobalSearch* New() { return eeNew( UITreeViewCellGlobalSearch, () ); } + + UITreeViewCellGlobalSearch() : UITreeViewCell() {} + + UIPushButton* setText( const String& text ); + + UIPushButton* updateText( const String& text ); +}; + +class UITreeViewGlobalSearch : public UITreeView { + public: + static UITreeViewGlobalSearch* New( const SyntaxColorScheme& colorScheme ) { + return eeNew( UITreeViewGlobalSearch, ( colorScheme ) ); + } + + UITreeViewGlobalSearch( const SyntaxColorScheme& colorScheme ) : + UITreeView(), mColorScheme( colorScheme ) { + mLineNumColor = Color::fromString( + mUISceneNode->getRoot()->getUIStyle()->getVariable( "--font-hint" ).getValue() ); + } + + UIWidget* createCell( UIWidget* rowWidget, const ModelIndex& index ) { + UITableCell* widget = index.column() == (Int64)getModel()->treeColumn() + ? UITreeViewCellGlobalSearch::New() + : UITableCell::New(); + return setupCell( widget, rowWidget, index ); + } + + const SyntaxColorScheme& getColorScheme() const { return mColorScheme; } + + const Color& getLineNumColor() const { return mLineNumColor; } + + void updateColorScheme( const SyntaxColorScheme& colorScheme ) { mColorScheme = colorScheme; } + + protected: + Color mLineNumColor; + SyntaxColorScheme mColorScheme; +}; + +UIPushButton* UITreeViewCellGlobalSearch::updateText( const String& text ) { + if ( getCurIndex().internalId() != -1 ) { + UITreeViewGlobalSearch* pp = getParent()->getParent()->asType(); + + ProjectSearch::ResultData* res = (ProjectSearch::ResultData*)getCurIndex().parent().data(); + + auto styleDef = SyntaxDefinitionManager::instance()->getStyleByExtension( res->file ); + + auto tokens = + SyntaxTokenizer::tokenize( styleDef, text, SYNTAX_TOKENIZER_STATE_NONE ).first; + + size_t start = 0; + for ( auto& token : tokens ) { + mTextBox->setFontFillColor( pp->getColorScheme().getSyntaxStyle( token.type ).color, + start, start + token.text.size() ); + start += token.text.size(); + } + + Uint32 from = text.find_first_not_of( ' ' ); + if ( from != String::InvalidPos ) + mTextBox->setFontFillColor( pp->getLineNumColor(), from, + text.find_first_of( ' ', from ) ); + } + return this; +} + +UIPushButton* UITreeViewCellGlobalSearch::setText( const String& text ) { + if ( text != mTextBox->getText() ) { + mTextBox->setVisible( !text.empty() ); + mTextBox->setText( text ); + updateText( text ); + updateLayout(); + } + return this; +} + void App::initGlobalSearchBar() { auto addClickListener = [&]( UIWidget* widget, std::string cmd ) { widget->addEventListener( Event::MouseClick, [this, cmd]( const Event* event ) { @@ -758,7 +835,7 @@ void App::initGlobalSearchBar() { } ); addClickListener( searchButton, "search-in-files" ); addClickListener( searchBarClose, "close-global-searchbar" ); - mGlobalSearchTree = UITreeView::New(); + mGlobalSearchTree = UITreeViewGlobalSearch::New( mEditorSplitter->getCurrentColorScheme() ); mGlobalSearchTree->setId( "search_tree" ); mGlobalSearchTree->setParent( mUISceneNode->getRoot() ); mGlobalSearchTree->setExpanderIconSize( PixelDensity::dpToPx( 20 ) ); @@ -1558,6 +1635,9 @@ void App::onCodeEditorFocusChange( UICodeEditor* editor ) { void App::onColorSchemeChanged( const std::string& ) { updateColorSchemeMenu(); + + mGlobalSearchTree->asType()->updateColorScheme( + mEditorSplitter->getCurrentColorScheme() ); } void App::onDocumentLoaded( UICodeEditor* codeEditor, const std::string& path ) { @@ -1617,8 +1697,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { const CodeEditorConfig& config = mConfig.editor; editor->setFontSize( config.fontSize.asDp( 0, Sizef(), mUISceneNode->getDPI() ) ); editor->setEnableColorPickerOnSelection( true ); - editor->setColorScheme( - mEditorSplitter->getColorSchemes().at( mEditorSplitter->getCurrentColorScheme() ) ); + editor->setColorScheme( mEditorSplitter->getCurrentColorScheme() ); editor->setShowLineNumber( config.showLineNumbers ); editor->setShowWhitespaces( config.showWhiteSpaces ); editor->setHighlightMatchingBracket( config.highlightMatchingBracket ); @@ -1838,7 +1917,7 @@ void App::createSettingsMenu() { void App::updateColorSchemeMenu() { for ( size_t i = 0; i < mColorSchemeMenu->getCount(); i++ ) { UIMenuRadioButton* menuItem = mColorSchemeMenu->getItem( i )->asType(); - menuItem->setActive( mEditorSplitter->getCurrentColorScheme() == menuItem->getText() ); + menuItem->setActive( mEditorSplitter->getCurrentColorSchemeName() == menuItem->getText() ); } } @@ -1846,7 +1925,7 @@ UIMenu* App::createColorSchemeMenu() { mColorSchemeMenu = UIPopUpMenu::New(); for ( auto& colorScheme : mEditorSplitter->getColorSchemes() ) { mColorSchemeMenu->addRadioButton( - colorScheme.first, mEditorSplitter->getCurrentColorScheme() == colorScheme.first ); + colorScheme.first, mEditorSplitter->getCurrentColorSchemeName() == colorScheme.first ); } mColorSchemeMenu->addEventListener( Event::OnItemClicked, [&]( const Event* event ) { UIMenuItem* item = event->getNode()->asType();