From 31567eb8a44527b1645f000636d20ffbe99f3aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Wed, 10 May 2023 08:01:16 -0300 Subject: [PATCH] Added an option to treat .h files as C++ code/syntax. --- .../eepp/ui/doc/syntaxdefinitionmanager.hpp | 8 ++++--- include/eepp/ui/doc/textdocument.hpp | 5 +++++ src/eepp/ui/doc/syntaxdefinitionmanager.cpp | 21 ++++++++++++------- src/eepp/ui/doc/textdocument.cpp | 10 ++++++++- src/tools/ecode/appconfig.cpp | 2 ++ src/tools/ecode/appconfig.hpp | 1 + src/tools/ecode/ecode.cpp | 1 + 7 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/eepp/ui/doc/syntaxdefinitionmanager.hpp b/include/eepp/ui/doc/syntaxdefinitionmanager.hpp index 55e73f8e6..9e6b74db1 100644 --- a/include/eepp/ui/doc/syntaxdefinitionmanager.hpp +++ b/include/eepp/ui/doc/syntaxdefinitionmanager.hpp @@ -22,11 +22,13 @@ class EE_API SyntaxDefinitionManager { const SyntaxDefinition& getPlainStyle() const; - const SyntaxDefinition& getByExtension( const std::string& filePath ) const; + const SyntaxDefinition& getByExtension( const std::string& filePath, + bool hFileAsCPP = false ) const; - const SyntaxDefinition& getByHeader( const std::string& header ) const; + const SyntaxDefinition& getByHeader( const std::string& header, bool hFileAsCPP = false ) const; - const SyntaxDefinition& find( const std::string& filePath, const std::string& header ); + const SyntaxDefinition& find( const std::string& filePath, const std::string& header, + bool hFileAsCPP = false ); SyntaxDefinition& getByExtensionRef( const std::string& filePath ); diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index 5b9ab5dc4..6663d0675 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -563,6 +563,10 @@ class EE_API TextDocument { TextRange getActiveClientVisibleRange() const; + bool hAsCpp() const; + + void setHAsCpp( bool hAsCpp ); + protected: friend class UndoStack; @@ -590,6 +594,7 @@ class EE_API TextDocument { bool mSaving{ false }; bool mDeleteOnClose{ false }; bool mMightBeBinary{ false }; + bool mHAsCpp{ false }; std::vector> mAutoCloseBracketsPairs; Uint32 mIndentWidth{ 4 }; IndentType mIndentType{ IndentType::IndentTabs }; diff --git a/src/eepp/ui/doc/syntaxdefinitionmanager.cpp b/src/eepp/ui/doc/syntaxdefinitionmanager.cpp index 2e9f0bb5b..9b2c9f4a4 100644 --- a/src/eepp/ui/doc/syntaxdefinitionmanager.cpp +++ b/src/eepp/ui/doc/syntaxdefinitionmanager.cpp @@ -2672,7 +2672,7 @@ static void addCMake() { { { "#", "[^\\]\n" }, "comment" }, { { "\"", "\"", "\\" }, "string" }, { { "'", "'", "\\" }, "string" }, - { { "[%a_][%w_]*%f[(]" }, "function" }, + { { "[%a_][%w_]*%s?%f[(]" }, "function" }, { { "CMAKE_[%w%d_]+" }, "keyword" }, { { "CTEST_[%w%d_]+" }, "keyword" }, { { "%u[%u%d_]*_[%u%d_]+" }, "keyword" }, @@ -4611,8 +4611,8 @@ void SyntaxDefinitionManager::loadFromFolder( const std::string& folderPath ) { } } -const SyntaxDefinition& -SyntaxDefinitionManager::getByExtension( const std::string& filePath ) const { +const SyntaxDefinition& SyntaxDefinitionManager::getByExtension( const std::string& filePath, + bool hFileAsCPP ) const { std::string extension( FileSystem::fileExtension( filePath ) ); std::string fileName( FileSystem::fileNameFromPath( filePath ) ); @@ -4627,9 +4627,14 @@ SyntaxDefinitionManager::getByExtension( const std::string& filePath ) const { String::endsWith( ext, "$" ) ) { LuaPattern words( ext ); int start, end; - if ( words.find( fileName, start, end ) ) + if ( words.find( fileName, start, end ) ) { + if ( hFileAsCPP && style->getLSPName() == "c" && ext == "%.h$" ) + return getByLSPName( "cpp" ); return *style; + } } else if ( extension == ext ) { + if ( hFileAsCPP && style->getLSPName() == "c" && ext == ".h" ) + return getByLSPName( "cpp" ); return *style; } } @@ -4638,7 +4643,8 @@ SyntaxDefinitionManager::getByExtension( const std::string& filePath ) const { return mDefinitions[0]; } -const SyntaxDefinition& SyntaxDefinitionManager::getByHeader( const std::string& header ) const { +const SyntaxDefinition& SyntaxDefinitionManager::getByHeader( const std::string& header, + bool /*hFileAsCPP*/ ) const { if ( !header.empty() ) { for ( auto style = mDefinitions.rbegin(); style != mDefinitions.rend(); ++style ) { for ( const auto& hdr : style->getHeaders() ) { @@ -4654,10 +4660,11 @@ const SyntaxDefinition& SyntaxDefinitionManager::getByHeader( const std::string& } const SyntaxDefinition& SyntaxDefinitionManager::find( const std::string& filePath, - const std::string& header ) { + const std::string& header, + bool hFileAsCPP ) { const SyntaxDefinition& def = getByHeader( header ); if ( def.getLanguageName() == mDefinitions[0].getLanguageName() ) - return getByExtension( filePath ); + return getByExtension( filePath, hFileAsCPP ); return def; } diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index e006ee001..7dbdbc8ec 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -266,7 +266,7 @@ bool TextDocument::hasSyntaxDefinition() const { void TextDocument::resetSyntax() { String header( getText( { { 0, 0 }, positionOffset( { 0, 0 }, 128 ) } ) ); std::string oldDef = mSyntaxDefinition.getLSPName(); - mSyntaxDefinition = SyntaxDefinitionManager::instance()->find( mFilePath, header ); + mSyntaxDefinition = SyntaxDefinitionManager::instance()->find( mFilePath, header, mHAsCpp ); if ( mSyntaxDefinition.getLSPName() != oldDef ) notifySyntaxDefinitionChange(); } @@ -1623,6 +1623,14 @@ TextRange TextDocument::getActiveClientVisibleRange() const { return {}; } +bool TextDocument::hAsCpp() const { + return mHAsCpp; +} + +void TextDocument::setHAsCpp( bool hAsCpp ) { + mHAsCpp = hAsCpp; +} + void TextDocument::selectWord( bool withMulticursor ) { if ( !hasSelection() ) { setSelection( { nextWordBoundary( getSelection().start(), false ), diff --git a/src/tools/ecode/appconfig.cpp b/src/tools/ecode/appconfig.cpp index d66a9f3cb..7f479da83 100644 --- a/src/tools/ecode/appconfig.cpp +++ b/src/tools/ecode/appconfig.cpp @@ -365,6 +365,7 @@ void AppConfig::saveProject( std::string projectFolder, UICodeEditorSplitter* ed IniFile ini( projectCfgPath, false ); ini.setValue( "path", "folder_path", projectFolder ); ini.setValueB( "document", "use_global_settings", docConfig.useGlobalSettings ); + ini.setValueB( "document", "h_as_cpp", docConfig.hAsCPP ); ini.setValueB( "document", "trim_trailing_whitespaces", docConfig.doc.trimTrailingWhitespaces ); ini.setValueB( "document", "force_new_line_at_end_of_file", docConfig.doc.forceNewLineAtEndOfFile ); @@ -496,6 +497,7 @@ void AppConfig::loadProject( std::string projectFolder, UICodeEditorSplitter* ed IniFile ini( projectCfgPath ); docConfig.useGlobalSettings = ini.getValueB( "document", "use_global_settings", true ); + docConfig.hAsCPP = ini.getValueB( "document", "h_as_cpp", false ); docConfig.doc.trimTrailingWhitespaces = ini.getValueB( "document", "trim_trailing_whitespaces", false ); docConfig.doc.forceNewLineAtEndOfFile = diff --git a/src/tools/ecode/appconfig.hpp b/src/tools/ecode/appconfig.hpp index 63d652591..9653e88e1 100644 --- a/src/tools/ecode/appconfig.hpp +++ b/src/tools/ecode/appconfig.hpp @@ -104,6 +104,7 @@ struct GlobalSearchBarConfig { struct ProjectDocumentConfig { bool useGlobalSettings{ true }; + bool hAsCPP{ false }; DocumentConfig doc; ProjectDocumentConfig() {} ProjectDocumentConfig( const DocumentConfig& doc ) { this->doc = doc; } diff --git a/src/tools/ecode/ecode.cpp b/src/tools/ecode/ecode.cpp index 4c22ecc9b..aef885a5c 100644 --- a/src/tools/ecode/ecode.cpp +++ b/src/tools/ecode/ecode.cpp @@ -2193,6 +2193,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { UITab* tab = (UITab*)editor->getData(); tab->setIcon( icon->getSize( mMenuIconSize ) ); } + editor->getDocument().setHAsCpp( mProjectDocConfig.hAsCPP ); }; auto docLoaded = [this, editor, docChanged]( const Event* event ) {