From 3893d137b7d6c9f56ed2ed5afc68344a27a141fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 25 Jan 2024 00:40:13 -0300 Subject: [PATCH] Minor refactor --- src/tools/ecode/plugins/git/git.cpp | 326 +++++++++++----------- src/tools/ecode/plugins/git/git.hpp | 9 +- src/tools/ecode/plugins/git/gitplugin.cpp | 2 +- 3 files changed, 176 insertions(+), 161 deletions(-) diff --git a/src/tools/ecode/plugins/git/git.cpp b/src/tools/ecode/plugins/git/git.cpp index 158d74687..83b085936 100644 --- a/src/tools/ecode/plugins/git/git.cpp +++ b/src/tools/ecode/plugins/git/git.cpp @@ -445,7 +445,7 @@ std::string Git::repoName( const std::string& file, const std::string& projectDi return FileSystem::fileNameFromPath( !projectDir.empty() ? projectDir : mProjectPath ); } -std::string Git::repoPath( const std::string& file, const std::string& projectDir ) { +std::string Git::repoPath( const std::string& file ) { for ( const auto& subRepo : mSubModules ) { if ( String::startsWith( file, subRepo ) && file.size() != subRepo.size() ) return mProjectPath + subRepo; @@ -557,171 +557,38 @@ Git::Status Git::status( bool recurseSubmodules, const std::string& projectDir ) if ( statusStr.size() < 2 ) return; - Uint16 status = git_xy( statusStr[0], statusStr[1] ); - GitStatus gitStatus = GitStatus::NotSet; - GitStatusChar gitStatusChar = GitStatusChar::Unknown; - GitStatusType gitStatusType = GitStatusType::Untracked; + auto status = statusFromShortStatusStr( statusStr ); - switch ( status ) { - case StatusXY::DD: { - gitStatus = GitStatus::Unmerge_BothDeleted; - gitStatusChar = GitStatusChar::Deleted; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::AU: { - gitStatus = GitStatus::Unmerge_AddedByUs; - gitStatusChar = GitStatusChar::Added; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::UD: { - gitStatus = GitStatus::Unmerge_DeletedByThem; - gitStatusChar = GitStatusChar::Deleted; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::UA: { - gitStatus = GitStatus::Unmerge_AddedByThem; - gitStatusChar = GitStatusChar::Added; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::DU: { - gitStatus = GitStatus::Unmerge_DeletedByUs; - gitStatusChar = GitStatusChar::Deleted; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::AA: { - gitStatus = GitStatus::Unmerge_BothAdded; - gitStatusChar = GitStatusChar::Added; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::UU: { - gitStatus = GitStatus::Unmerge_BothModified; - gitStatusChar = GitStatusChar::Modified; - gitStatusType = GitStatusType::Unmerged; - break; - } - case StatusXY::QQ: { - gitStatus = GitStatus::Untracked; - gitStatusChar = GitStatusChar::Untracked; - gitStatusType = GitStatusType::Untracked; - break; - } - case StatusXY::II: { - gitStatus = GitStatus::Ignored; - gitStatusChar = GitStatusChar::Ignored; - gitStatusType = GitStatusType::Ignored; - break; - } - case StatusXY::m: { - gitStatus = GitStatus::WorkingTree_ModifiedSubmodule; - gitStatusChar = GitStatusChar::ModifiedSubmodule; - gitStatusType = GitStatusType::Changed; - break; - } - default: - break; + if ( status.gitStatus != GitStatus::NotSet ) + return; + + if ( status.gitStatusChar == GitStatusChar::ModifiedSubmodule ) { + modifiedSubmodule = true; + return; } - if ( gitStatus == GitStatus::NotSet ) { - char x = statusStr[0]; - - switch ( x ) { - case 'M': { - gitStatus = GitStatus::Index_Modified; - gitStatusChar = GitStatusChar::Modified; - gitStatusType = GitStatusType::Staged; - break; - } - case 'A': { - gitStatus = GitStatus::Index_Added; - gitStatusChar = GitStatusChar::Added; - gitStatusType = GitStatusType::Staged; - break; - } - case 'D': { - gitStatus = GitStatus::Index_Deleted; - gitStatusChar = GitStatusChar::Deleted; - gitStatusType = GitStatusType::Staged; - break; - } - case 'R': { - gitStatus = GitStatus::Index_Renamed; - gitStatusChar = GitStatusChar::Renamed; - gitStatusType = GitStatusType::Staged; - break; - } - case 'C': { - gitStatus = GitStatus::Index_Copied; - gitStatusChar = GitStatusChar::Copied; - gitStatusType = GitStatusType::Staged; - break; - } - case 'm': { - gitStatus = GitStatus::Index_ModifiedSubmodule; - gitStatusChar = GitStatusChar::ModifiedSubmodule; - gitStatusType = GitStatusType::Staged; + auto filePath = subModulePath + file; + auto repo = repoName( filePath, projectDir ); + auto repoIt = s.files.find( repo ); + if ( repoIt != s.files.end() ) { + bool found = false; + for ( auto& fileIt : repoIt->second ) { + if ( fileIt.file == filePath ) { + fileIt.statusChar = status.gitStatusChar; + fileIt.status = status.gitStatus; + fileIt.statusType = status.gitStatusType; + found = true; break; } } - } - - if ( gitStatus == GitStatus::NotSet ) { - char y = statusStr[1]; - switch ( y ) { - case 'M': { - gitStatus = GitStatus::WorkingTree_Modified; - gitStatusChar = GitStatusChar::Modified; - gitStatusType = GitStatusType::Changed; - break; - } - case 'D': { - gitStatus = GitStatus::WorkingTree_Deleted; - gitStatusChar = GitStatusChar::Deleted; - gitStatusType = GitStatusType::Changed; - break; - } - case 'A': { - gitStatus = GitStatus::WorkingTree_IntentToAdd; - gitStatusChar = GitStatusChar::Added; - gitStatusType = GitStatusType::Changed; - break; - } - } - } - - if ( gitStatus != GitStatus::NotSet ) { - if ( gitStatusChar == GitStatusChar::ModifiedSubmodule ) - modifiedSubmodule = true; - else { - auto filePath = subModulePath + file; - auto repo = repoName( filePath, projectDir ); - auto repoIt = s.files.find( repo ); - if ( repoIt != s.files.end() ) { - bool found = false; - for ( auto& fileIt : repoIt->second ) { - if ( fileIt.file == filePath ) { - fileIt.statusChar = gitStatusChar; - fileIt.status = gitStatus; - fileIt.statusType = gitStatusType; - found = true; - break; - } - } - if ( !found ) { - s.files[repo].push_back( { std::move( filePath ), 0, 0, gitStatus, - gitStatusType, gitStatusChar } ); - } - } else { - s.files.insert( { repo, - { { std::move( filePath ), 0, 0, gitStatus, - gitStatusType, gitStatusChar } } } ); - } + if ( !found ) { + s.files[repo].push_back( { std::move( filePath ), 0, 0, status.gitStatus, + status.gitStatusType, status.gitStatusChar } ); } + } else { + s.files.insert( { repo, + { { std::move( filePath ), 0, 0, status.gitStatus, + status.gitStatusType, status.gitStatusChar } } } ); } } } ); @@ -808,4 +675,145 @@ Git::Blame Git::blame( const std::string& filepath, std::size_t line ) const { line }; } +Git::GitStatusReport Git::statusFromShortStatusStr( const std::string_view& statusStr ) { + Uint16 status = git_xy( statusStr[0], statusStr[1] ); + GitStatus gitStatus = GitStatus::NotSet; + GitStatusChar gitStatusChar = GitStatusChar::Unknown; + GitStatusType gitStatusType = GitStatusType::Untracked; + + switch ( status ) { + case StatusXY::DD: { + gitStatus = GitStatus::Unmerge_BothDeleted; + gitStatusChar = GitStatusChar::Deleted; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::AU: { + gitStatus = GitStatus::Unmerge_AddedByUs; + gitStatusChar = GitStatusChar::Added; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::UD: { + gitStatus = GitStatus::Unmerge_DeletedByThem; + gitStatusChar = GitStatusChar::Deleted; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::UA: { + gitStatus = GitStatus::Unmerge_AddedByThem; + gitStatusChar = GitStatusChar::Added; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::DU: { + gitStatus = GitStatus::Unmerge_DeletedByUs; + gitStatusChar = GitStatusChar::Deleted; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::AA: { + gitStatus = GitStatus::Unmerge_BothAdded; + gitStatusChar = GitStatusChar::Added; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::UU: { + gitStatus = GitStatus::Unmerge_BothModified; + gitStatusChar = GitStatusChar::Modified; + gitStatusType = GitStatusType::Unmerged; + break; + } + case StatusXY::QQ: { + gitStatus = GitStatus::Untracked; + gitStatusChar = GitStatusChar::Untracked; + gitStatusType = GitStatusType::Untracked; + break; + } + case StatusXY::II: { + gitStatus = GitStatus::Ignored; + gitStatusChar = GitStatusChar::Ignored; + gitStatusType = GitStatusType::Ignored; + break; + } + case StatusXY::m: { + gitStatus = GitStatus::WorkingTree_ModifiedSubmodule; + gitStatusChar = GitStatusChar::ModifiedSubmodule; + gitStatusType = GitStatusType::Changed; + break; + } + default: + break; + } + + if ( gitStatus == GitStatus::NotSet ) { + char x = statusStr[0]; + + switch ( x ) { + case 'M': { + gitStatus = GitStatus::Index_Modified; + gitStatusChar = GitStatusChar::Modified; + gitStatusType = GitStatusType::Staged; + break; + } + case 'A': { + gitStatus = GitStatus::Index_Added; + gitStatusChar = GitStatusChar::Added; + gitStatusType = GitStatusType::Staged; + break; + } + case 'D': { + gitStatus = GitStatus::Index_Deleted; + gitStatusChar = GitStatusChar::Deleted; + gitStatusType = GitStatusType::Staged; + break; + } + case 'R': { + gitStatus = GitStatus::Index_Renamed; + gitStatusChar = GitStatusChar::Renamed; + gitStatusType = GitStatusType::Staged; + break; + } + case 'C': { + gitStatus = GitStatus::Index_Copied; + gitStatusChar = GitStatusChar::Copied; + gitStatusType = GitStatusType::Staged; + break; + } + case 'm': { + gitStatus = GitStatus::Index_ModifiedSubmodule; + gitStatusChar = GitStatusChar::ModifiedSubmodule; + gitStatusType = GitStatusType::Staged; + break; + } + } + } + + if ( gitStatus == GitStatus::NotSet ) { + char y = statusStr[1]; + switch ( y ) { + case 'M': { + gitStatus = GitStatus::WorkingTree_Modified; + gitStatusChar = GitStatusChar::Modified; + gitStatusType = GitStatusType::Changed; + break; + } + case 'D': { + gitStatus = GitStatus::WorkingTree_Deleted; + gitStatusChar = GitStatusChar::Deleted; + gitStatusType = GitStatusType::Changed; + break; + } + case 'A': { + gitStatus = GitStatus::WorkingTree_IntentToAdd; + gitStatusChar = GitStatusChar::Added; + gitStatusType = GitStatusType::Changed; + break; + } + } + } + + return { gitStatus, gitStatusChar, gitStatusType }; +} + } // namespace ecode diff --git a/src/tools/ecode/plugins/git/git.hpp b/src/tools/ecode/plugins/git/git.hpp index 09e27f83d..7690f2b22 100644 --- a/src/tools/ecode/plugins/git/git.hpp +++ b/src/tools/ecode/plugins/git/git.hpp @@ -273,7 +273,7 @@ class Git { std::string repoName( const std::string& file, const std::string& projectDir = "" ); - std::string repoPath( const std::string& file, const std::string& projectDir = "" ); + std::string repoPath( const std::string& file ); bool hasSubmodules( const std::string& projectDir ); @@ -285,6 +285,13 @@ class Git { std::string mGitFolder; std::vector mSubModules; bool mSubModulesUpdated{ false }; + + struct GitStatusReport { + GitStatus gitStatus = GitStatus::NotSet; + GitStatusChar gitStatusChar = GitStatusChar::Unknown; + GitStatusType gitStatusType = GitStatusType::Untracked; + }; + GitStatusReport statusFromShortStatusStr( const std::string_view& statusStr ); }; } // namespace ecode diff --git a/src/tools/ecode/plugins/git/gitplugin.cpp b/src/tools/ecode/plugins/git/gitplugin.cpp index 271150ada..3515dbe35 100644 --- a/src/tools/ecode/plugins/git/gitplugin.cpp +++ b/src/tools/ecode/plugins/git/gitplugin.cpp @@ -823,7 +823,7 @@ void GitPlugin::displayTooltip( UICodeEditor* editor, const Git::Blame& blame, patterns.emplace_back( SyntaxPattern( { "([%w:]+)%s(%x+)%s%((%x+)%)" }, { "normal", "keyword", "number", "number" } ) ); - patterns.emplace_back( SyntaxPattern( { "([%w:]+)%s(.*)%(([%w%.-]+@[%w-]+%.%w+)%)" }, + patterns.emplace_back( SyntaxPattern( { "([%w:]+)%s(.*)%(([%w%+%.-]+@[%w%.-]+%.%w+)%)" }, { "normal", "keyword", "function", "link" } ) ); patterns.emplace_back( SyntaxPattern( { "([%w:]+)%s(%d%d%d%d%-%d%d%-%d%d[%s%d%-+:]+)" }, { "normal", "keyword", "warning" } ) );