From a34a76e574772508cb42c2ed6a1497ae9a47b823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 28 Jan 2023 02:10:19 -0300 Subject: [PATCH] SyntaxTokenizer: Added an option to skip the subsyntax separators when tokenizing and also trimming the text in the tokenizeText function. ecode: Improved LSP mouse hover tooltips. --- include/eepp/ui/doc/syntaxtokenizer.hpp | 11 +-- include/eepp/ui/uitooltip.hpp | 2 + include/eepp/version.hpp | 2 +- src/eepp/ui/doc/syntaxtokenizer.cpp | 79 ++++++++++++++----- src/eepp/ui/uitooltip.cpp | 10 +++ .../ecode/plugins/lsp/lspclientplugin.cpp | 5 +- src/tools/ecode/version.hpp | 2 +- 7 files changed, 82 insertions(+), 29 deletions(-) diff --git a/include/eepp/ui/doc/syntaxtokenizer.hpp b/include/eepp/ui/doc/syntaxtokenizer.hpp index 740d2e701..afb36d15b 100644 --- a/include/eepp/ui/doc/syntaxtokenizer.hpp +++ b/include/eepp/ui/doc/syntaxtokenizer.hpp @@ -28,14 +28,15 @@ struct SyntaxState { class EE_API SyntaxTokenizer { public: - static std::pair, Uint32> tokenize( const SyntaxDefinition& syntax, - const std::string& text, - const Uint32& state, - const size_t& startIndex = 0 ); + static std::pair, Uint32> + tokenize( const SyntaxDefinition& syntax, const std::string& text, const Uint32& state, + const size_t& startIndex = 0, bool skipSubSyntaxSeparator = false ); static Text& tokenizeText( const SyntaxDefinition& syntax, const SyntaxColorScheme& colorScheme, Text& text, const size_t& startIndex = 0, - const size_t& endIndex = 0xFFFFFFFF ); + const size_t& endIndex = 0xFFFFFFFF, + bool skipSubSyntaxSeparator = false, + const std::string& trimChars = "" ); static SyntaxState retrieveSyntaxState( const SyntaxDefinition& syntax, const Uint32& state ); }; diff --git a/include/eepp/ui/uitooltip.hpp b/include/eepp/ui/uitooltip.hpp index 1c4324920..dcb6c0222 100644 --- a/include/eepp/ui/uitooltip.hpp +++ b/include/eepp/ui/uitooltip.hpp @@ -119,6 +119,8 @@ class EE_API UITooltip : public UIWidget { const Vector2f& getFontShadowOffset() const; + void notifyTextChangedFromTextCache(); + protected: Text* mTextCache; UIFontStyleConfig mStyleConfig; diff --git a/include/eepp/version.hpp b/include/eepp/version.hpp index fa917fc36..498755e57 100644 --- a/include/eepp/version.hpp +++ b/include/eepp/version.hpp @@ -6,7 +6,7 @@ #define EEPP_MAJOR_VERSION 2 #define EEPP_MINOR_VERSION 5 -#define EEPP_PATCH_LEVEL 4 +#define EEPP_PATCH_LEVEL 5 #define EEPP_CODENAME "Bindu" /** The compiled version of the library */ diff --git a/src/eepp/ui/doc/syntaxtokenizer.cpp b/src/eepp/ui/doc/syntaxtokenizer.cpp index 3151c6345..a33b2f8b7 100644 --- a/src/eepp/ui/doc/syntaxtokenizer.cpp +++ b/src/eepp/ui/doc/syntaxtokenizer.cpp @@ -115,7 +115,8 @@ SyntaxState SyntaxTokenizer::retrieveSyntaxState( const SyntaxDefinition& syntax std::pair, Uint32> SyntaxTokenizer::tokenize( const SyntaxDefinition& syntax, const std::string& text, - const Uint32& state, const size_t& startIndex ) { + const Uint32& state, const size_t& startIndex, + bool skipSubSyntaxSeparator ) { std::vector tokens; LuaPattern::Range matches[12]; int start, end; @@ -172,8 +173,10 @@ SyntaxTokenizer::tokenize( const SyntaxDefinition& syntax, const std::string& te if ( rangeSubsyntax.first != -1 && ( range.first == -1 || rangeSubsyntax.first < range.first ) ) { - pushToken( tokens, curState.subsyntaxInfo->types[0], - text.substr( i, rangeSubsyntax.second - i ) ); + if ( !skipSubSyntaxSeparator ) { + pushToken( tokens, curState.subsyntaxInfo->types[0], + text.substr( i, rangeSubsyntax.second - i ) ); + } popSubsyntax(); i = rangeSubsyntax.second; skip = true; @@ -199,8 +202,10 @@ SyntaxTokenizer::tokenize( const SyntaxDefinition& syntax, const std::string& te : "" ); if ( rangeSubsyntax.first != -1 ) { - pushToken( tokens, curState.subsyntaxInfo->types[0], - text.substr( i, rangeSubsyntax.second - i ) ); + if ( !skipSubSyntaxSeparator ) { + pushToken( tokens, curState.subsyntaxInfo->types[0], + text.substr( i, rangeSubsyntax.second - i ) ); + } popSubsyntax(); i = rangeSubsyntax.second; } @@ -243,12 +248,14 @@ SyntaxTokenizer::tokenize( const SyntaxDefinition& syntax, const std::string& te std::string patternText( text.substr( start, end - start ) ); std::string type = curState.currentSyntax->getSymbol( patternText ); - pushToken( tokens, - type.empty() - ? ( curMatch < pattern.types.size() ? pattern.types[curMatch] - : pattern.types[0] ) - : type, - patternText ); + if ( !skipSubSyntaxSeparator || pattern.syntax.empty() ) { + pushToken( tokens, + type.empty() ? ( curMatch < pattern.types.size() + ? pattern.types[curMatch] + : pattern.types[0] ) + : type, + patternText ); + } if ( !pattern.syntax.empty() ) { pushSubsyntax( pattern, patternIndex + 1 ); @@ -278,12 +285,14 @@ SyntaxTokenizer::tokenize( const SyntaxDefinition& syntax, const std::string& te continue; std::string patternText( text.substr( start, end - start ) ); std::string type = curState.currentSyntax->getSymbol( patternText ); - pushToken( tokens, - type.empty() - ? ( curMatch < pattern.types.size() ? pattern.types[curMatch] - : pattern.types[0] ) - : type, - patternText ); + if ( !skipSubSyntaxSeparator || pattern.syntax.empty() ) { + pushToken( tokens, + type.empty() ? ( curMatch < pattern.types.size() + ? pattern.types[curMatch] + : pattern.types[0] ) + : type, + patternText ); + } if ( !pattern.syntax.empty() ) { pushSubsyntax( pattern, patternIndex + 1 ); } else if ( pattern.patterns.size() > 1 ) { @@ -308,18 +317,46 @@ SyntaxTokenizer::tokenize( const SyntaxDefinition& syntax, const std::string& te Text& SyntaxTokenizer::tokenizeText( const SyntaxDefinition& syntax, const SyntaxColorScheme& colorScheme, Text& text, - const size_t& startIndex, const size_t& endIndex ) { + const size_t& startIndex, const size_t& endIndex, + bool skipSubSyntaxSeparator, const std::string& trimChars ) { auto tokens = SyntaxTokenizer::tokenize( syntax, text.getString(), SYNTAX_TOKENIZER_STATE_NONE, - startIndex ) + startIndex, skipSubSyntaxSeparator ) .first; + if ( skipSubSyntaxSeparator || !trimChars.empty() ) { + String txt; + size_t c = 0; + for ( auto& token : tokens ) { + if ( c == 0 ) { + auto f = token.text.find_first_not_of( trimChars ); + if ( f == std::string::npos ) { + token.text.clear(); + } else if ( f > 0 ) { + token.text = token.text.substr( f ); + } + } else if ( c == tokens.size() - 1 ) { + auto f = token.text.find_last_not_of( trimChars ); + if ( f == std::string::npos ) { + token.text.clear(); + } else if ( f >= 0 && f + 1 <= token.text.size() ) { + token.text = token.text.substr( 0, f + 1 ); + } + } + if ( token.text.size() ) + txt += token.text; + ++c; + } + text.setString( txt ); + } + size_t start = startIndex; for ( auto& token : tokens ) { if ( start < endIndex ) { size_t strSize = String::utf8Length( token.text ); - text.setFillColor( colorScheme.getSyntaxStyle( token.type ).color, start, - std::min( start + strSize, endIndex ) ); + if ( strSize > 0 ) + text.setFillColor( colorScheme.getSyntaxStyle( token.type ).color, start, + std::min( start + strSize, endIndex ) ); start += strSize; } else { break; diff --git a/src/eepp/ui/uitooltip.cpp b/src/eepp/ui/uitooltip.cpp index d909a89f2..b3e3cb192 100644 --- a/src/eepp/ui/uitooltip.cpp +++ b/src/eepp/ui/uitooltip.cpp @@ -52,6 +52,10 @@ Vector2f UITooltip::getTooltipPosition( UITooltip* toolip, const Vector2f& reque pos.y = 0; } + pos.x = eeclamp( pos.x, 0.f, + eemax( 0.f, uiSceneNode->getPixelsSize().getWidth() - + toolip->getPixelsSize().getWidth() ) ); + return pos; } @@ -218,6 +222,12 @@ const Vector2f& UITooltip::getFontShadowOffset() const { return mStyleConfig.ShadowOffset; } +void UITooltip::notifyTextChangedFromTextCache() { + autoPadding(); + onAutoSize(); + autoAlign(); +} + void UITooltip::setFontShadowOffset( const Vector2f& offset ) { if ( mStyleConfig.ShadowOffset != offset ) { mStyleConfig.ShadowOffset = offset; diff --git a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp index 20a5bd905..d14914414 100644 --- a/src/tools/ecode/plugins/lsp/lspclientplugin.cpp +++ b/src/tools/ecode/plugins/lsp/lspclientplugin.cpp @@ -535,7 +535,10 @@ void LSPClientPlugin::displayTooltip( UICodeEditor* editor, const LSPHover& resp ? SyntaxDefinitionManager::instance()->getByLSPName( "markdown" ) : editor->getSyntaxDefinition(); - SyntaxTokenizer::tokenizeText( syntaxDef, editor->getColorScheme(), *tooltip->getTextCache() ); + SyntaxTokenizer::tokenizeText( syntaxDef, editor->getColorScheme(), *tooltip->getTextCache(), 0, + 0xFFFFFFFF, true, "\n\t " ); + + tooltip->notifyTextChangedFromTextCache(); if ( editor->hasFocus() && !tooltip->isVisible() ) tooltip->show(); diff --git a/src/tools/ecode/version.hpp b/src/tools/ecode/version.hpp index 12d955f71..bcce49853 100644 --- a/src/tools/ecode/version.hpp +++ b/src/tools/ecode/version.hpp @@ -8,7 +8,7 @@ using namespace EE; #define ECODE_MAJOR_VERSION 0 #define ECODE_MINOR_VERSION 4 -#define ECODE_PATCH_LEVEL 2 +#define ECODE_PATCH_LEVEL 3 #define ECODE_CODENAME "Vajra" /** The compiled version of the library */