Skip to content

Commit

Permalink
Added "ft" locator for switch file type.
Browse files Browse the repository at this point in the history
Added "select-all-words" command for documents (default keybind is ctrl/cmd+shift+d).
Manage git plugin tab visibility when folder is closed, reopened, etc.
  • Loading branch information
SpartanJ committed Feb 18, 2024
1 parent a9e3b8a commit d7d5678
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 19 deletions.
4 changes: 4 additions & 0 deletions include/eepp/ui/doc/textdocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ class EE_API TextDocument {

void selectWord( bool withMulticursor = true );

void selectAllWords();

void selectLine();

void selectToNextWord();
Expand Down Expand Up @@ -530,6 +532,8 @@ class EE_API TextDocument {

TextRange addSelection( const TextPosition& selection );

TextRange addSelections( TextRanges&& selections );

void popSelection();

void deleteSelection( const size_t& cursorIdx );
Expand Down
2 changes: 2 additions & 0 deletions include/eepp/ui/doc/textrange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class EE_API TextRange {
return *this;
}

void reverse() { std::swap( mEnd, mStart ); }

TextRange reversed() const { return TextRange( mEnd, mStart ); }

void setStart( const TextPosition& position ) { mStart = position; }
Expand Down
32 changes: 32 additions & 0 deletions src/eepp/ui/doc/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,24 @@ TextRange TextDocument::addSelection( const TextPosition& selection ) {
return addSelection( { selection, selection } );
}

TextRange TextDocument::addSelections( TextRanges&& selections ) {
mSelection.reserve( mSelection.size() + selections.size() );
for ( auto& selection : selections ) {
if ( mSelection.exists( selection ) )
return {};
selection = sanitizeRange( selection );
if ( mSelection.exists( selection ) )
return {};
mSelection.push_back( selection );
}
mSelection.sort();
mergeSelection();
notifyCursorChanged( selections.back().start() );
notifySelectionChanged( selections.back() );
mLastSelection = mSelection.findIndex( selections.back() );
return selections.back();
}

TextRange TextDocument::addSelection( TextRange selection ) {
if ( mSelection.exists( selection ) )
return {};
Expand Down Expand Up @@ -1739,6 +1757,19 @@ void TextDocument::selectWord( bool withMulticursor ) {
}
}

void TextDocument::selectAllWords() {
if ( !hasSelection() )
selectWord( false );
String text( getSelectedText() );
auto res( findAll( text, true, false, FindReplaceType::Normal,
{ getBottomMostCursor().normalized().end(), endOfDoc() } ) );
if ( !res.empty() ) {
for ( auto& selection : res )
selection.reverse();
addSelections( std::move( res ) );
}
}

void TextDocument::selectLine() {
for ( size_t i = 0; i < mSelection.size(); ++i ) {
auto sel = getSelectionIndex( i );
Expand Down Expand Up @@ -2971,6 +3002,7 @@ void TextDocument::initializeCommands() {
mCommands["select-to-next-word"] = [this] { selectToNextWord(); };
mCommands["select-to-next-line"] = [this] { selectToNextLine(); };
mCommands["select-word"] = [this] { selectWord(); };
mCommands["select-all-words"] = [this] { selectAllWords(); };
mCommands["select-line"] = [this] { selectLine(); };
mCommands["select-to-start-of-line"] = [this] { selectToStartOfLine(); };
mCommands["select-to-end-of-line"] = [this] { selectToEndOfLine(); };
Expand Down
1 change: 1 addition & 0 deletions src/eepp/ui/uicodeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const std::map<KeyBindings::Shortcut, std::string> UICodeEditor::getDefaultKeybi
{ { KEY_DOWN, KEYMOD_CTRL | KEYMOD_LALT | KEYMOD_SHIFT }, "selection-to-lower" },
{ { KEY_F, KeyMod::getDefaultModifier() }, "find-replace" },
{ { KEY_D, KeyMod::getDefaultModifier() }, "select-word" },
{ { KEY_D, KeyMod::getDefaultModifier() | KEYMOD_SHIFT }, "select-all-words" },
{ { KEY_UP, KEYMOD_LALT }, "add-cursor-above" },
{ { KEY_DOWN, KEYMOD_LALT }, "add-cursor-below" },
{ { KEY_ESCAPE }, "reset-cursor" },
Expand Down
1 change: 1 addition & 0 deletions src/tools/ecode/ecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,7 @@ void App::closeFolder() {

mProjectViewEmptyCont->setVisible( true );
mFileSystemModel->setRootPath( "" );
mPluginManager->setWorkspaceFolder( "" );
updateOpenRecentFolderBtn();
UIWelcomeScreen::createWelcomeScreen( this );
mStatusBar->setVisible( false );
Expand Down
2 changes: 2 additions & 0 deletions src/tools/ecode/ecode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ class App : public UICodeEditorSplitter::Client {

void showBuildTab();

SettingsMenu* getSettingsMenu() const { return mSettings.get(); }

template <typename T> void registerUnlockedCommands( T& t ) {
t.setCommand( "keybindings", [this] { loadFileFromPath( mKeybindingsPath ); } );
t.setCommand( "debug-draw-boxes-toggle", [this] { debugDrawBoxesToggle(); } );
Expand Down
59 changes: 42 additions & 17 deletions src/tools/ecode/plugins/git/gitplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ GitPlugin::~GitPlugin() {
mShuttingDown = true;
if ( mStatusButton )
mStatusButton->close();

if ( mSidePanel && mTab )
mSidePanel->removeTab( mTab );

Expand All @@ -95,7 +96,7 @@ GitPlugin::~GitPlugin() {
{ Lock l( mRepoMutex ); }
{ Lock l( mReposMutex ); }

// TODO: Add a signal for this waits
// TODO: Add a signal for these waits
while ( mRunningUpdateStatus )
Sys::sleep( 1.f );

Expand Down Expand Up @@ -457,6 +458,14 @@ PluginRequestHandle GitPlugin::processMessage( const PluginMessage& msg ) {
mRepos.clear();
}

if ( getUISceneNode() && mSidePanel ) {
getUISceneNode()->runOnMainThread( [this] {
if ( mProjectPath.empty() ) {
hideSidePanel();
}
} );
}

updateUINow( true );
mInitialized = true;
}
Expand Down Expand Up @@ -1361,6 +1370,16 @@ void GitPlugin::updateBranchesUI( std::shared_ptr<GitBranchModel> model ) {
}

void GitPlugin::buildSidePanelTab() {
if ( mTabContents && !mTab ) {
if ( mProjectPath.empty() )
return;
UIIcon* icon = findIcon( "source-control" );
mTab = mSidePanel->add( i18n( "source_control", "Source Control" ), mTabContents,
icon ? icon->getSize( PixelDensity::dpToPx( 12 ) ) : nullptr );
mTab->setId( "source_control" );
mTab->setTextAsFallback( true );
return;
}
if ( mTab )
return;
if ( mSidePanel == nullptr )
Expand Down Expand Up @@ -1425,25 +1444,24 @@ void GitPlugin::buildSidePanelTab() {
!mHighlightStyleColor.empty() && Color::isColorString( mHighlightStyleColor )
? mHighlightStyleColor
: std::string{ DEFAULT_HIGHLIGHT_COLOR };
UIWidget* node =
getUISceneNode()->loadLayoutFromString( String::format( STYLE, color, color ) );
mTab = mSidePanel->add( i18n( "source_control", "Source Control" ), node,
mTabContents = getUISceneNode()->loadLayoutFromString( String::format( STYLE, color, color ) );
mTab = mSidePanel->add( i18n( "source_control", "Source Control" ), mTabContents,
icon ? icon->getSize( PixelDensity::dpToPx( 12 ) ) : nullptr );
mTab->setId( "source_control" );
mTab->setTextAsFallback( true );

node->bind( "git_panel_switcher", mPanelSwicher );
node->bind( "git_panel_stack", mStackWidget );
node->bind( "git_branches_tree", mBranchesTree );
node->bind( "git_status_tree", mStatusTree );
node->bind( "git_content", mGitContentView );
node->bind( "git_no_content", mGitNoContentView );
node->bind( "git_panel_loader", mLoader );
node->bind( "git_repo", mRepoDropDown );
mTabContents->bind( "git_panel_switcher", mPanelSwicher );
mTabContents->bind( "git_panel_stack", mStackWidget );
mTabContents->bind( "git_branches_tree", mBranchesTree );
mTabContents->bind( "git_status_tree", mStatusTree );
mTabContents->bind( "git_content", mGitContentView );
mTabContents->bind( "git_no_content", mGitNoContentView );
mTabContents->bind( "git_panel_loader", mLoader );
mTabContents->bind( "git_repo", mRepoDropDown );

node->find( "branch_pull" )->onClick( [this]( auto ) { pull( repoSelected() ); } );
node->find( "branch_push" )->onClick( [this]( auto ) { push( repoSelected() ); } );
node->find( "branch_add" )->onClick( [this]( auto ) { branchCreate(); } );
mTabContents->find( "branch_pull" )->onClick( [this]( auto ) { pull( repoSelected() ); } );
mTabContents->find( "branch_push" )->onClick( [this]( auto ) { push( repoSelected() ); } );
mTabContents->find( "branch_add" )->onClick( [this]( auto ) { branchCreate(); } );

mBranchesTree->setAutoExpandOnSingleColumn( true );
mBranchesTree->setHeadersVisible( false );
Expand Down Expand Up @@ -1504,8 +1522,8 @@ void GitPlugin::buildSidePanelTab() {
auto listBox = mPanelSwicher->getListBox();
listBox->addListBoxItems( { i18n( "branches", "Branches" ), i18n( "status", "Status" ) } );
mStackMap.resize( 2 );
mStackMap[0] = node->find<UIWidget>( "git_branches" );
mStackMap[1] = node->find<UIWidget>( "git_status" );
mStackMap[0] = mTabContents->find<UIWidget>( "git_branches" );
mStackMap[1] = mTabContents->find<UIWidget>( "git_status" );
listBox->setSelected( 0 );

mPanelSwicher->on( Event::OnItemSelected, [this, listBox]( const Event* ) {
Expand Down Expand Up @@ -1697,6 +1715,13 @@ void GitPlugin::buildSidePanelTab() {
} );
}

void GitPlugin::hideSidePanel() {
if ( mSidePanel && mTab ) {
mSidePanel->removeTab( mTab, false );
mTab = nullptr;
}
}

void GitPlugin::openBranchMenu( const Git::Branch& branch ) {
UIPopUpMenu* menu = UIPopUpMenu::New();
menu->setId( "git_branch_menu" );
Expand Down
3 changes: 3 additions & 0 deletions src/tools/ecode/plugins/git/gitplugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class GitPlugin : public PluginBase {
Color mOldBackgroundColor;
UITabWidget* mSidePanel{ nullptr };
UITab* mTab{ nullptr };
UIWidget* mTabContents{ nullptr };

UILinearLayout* mStatusBar{ nullptr };
UIPushButton* mStatusButton{ nullptr };
Expand Down Expand Up @@ -238,6 +239,8 @@ class GitPlugin : public PluginBase {
void initModelStyler();

void endModelStyler();

void hideSidePanel();
};

} // namespace ecode
Expand Down
63 changes: 61 additions & 2 deletions src/tools/ecode/universallocator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "universallocator.hpp"
#include "ecode.hpp"
#include "pathhelper.hpp"
#include "settingsmenu.hpp"

#include <algorithm>

namespace ecode {
Expand Down Expand Up @@ -221,7 +223,7 @@ void UniversalLocator::goToLine() {
static bool isCommand( const std::string& filename ) {
return !filename.empty() &&
( filename == "> " || filename == ": " || filename == "l " || filename == ". " ||
filename == "o " || filename == "sb " || filename == "sbt " );
filename == "o " || filename == "sb " || filename == "sbt " || filename == "ft " );
}

void UniversalLocator::initLocateBar( UILocateBar* locateBar, UITextInput* locateInput ) {
Expand Down Expand Up @@ -262,6 +264,8 @@ void UniversalLocator::initLocateBar( UILocateBar* locateBar, UITextInput* locat
showSwitchBuild();
} else if ( String::startsWith( inputTxt, "sbt " ) ) {
showSwitchBuildType();
} else if ( String::startsWith( inputTxt, "ft " ) ) {
showSwitchFileType();
} else {
showLocateTable();
}
Expand Down Expand Up @@ -374,6 +378,15 @@ void UniversalLocator::initLocateBar( UILocateBar* locateBar, UITextInput* locat
}
}
return;
} else if ( String::startsWith( mLocateInput->getText(), "ft " ) ) {
if ( mSplitter->getCurEditor() ) {
const auto& df = SyntaxDefinitionManager::instance()->getByLanguageName(
vName.toString() );
mSplitter->getCurEditor()->setSyntaxDefinition( df );
mApp->getSettingsMenu()->updateCurrentFileType();
mLocateBarLayout->execute( "close-locatebar" );
}
return;
}

Variant vPath( modelEvent->getModel()->data(
Expand Down Expand Up @@ -486,7 +499,8 @@ void UniversalLocator::showLocateBar() {
String::startsWith( mLocateInput->getText(), "l " ) ||
String::startsWith( mLocateInput->getText(), "o " ) ||
String::startsWith( mLocateInput->getText(), "sb " ) ||
String::startsWith( mLocateInput->getText(), "sbt " ) ) )
String::startsWith( mLocateInput->getText(), "sbt " ) ||
String::startsWith( mLocateInput->getText(), "ft " ) ) )
mLocateInput->setText( "" );

if ( mApp->getDirTree() && !mLocateTable->getModel() ) {
Expand Down Expand Up @@ -713,6 +727,50 @@ void UniversalLocator::updateSwitchBuildTypeTable() {
mLocateTable->setColumnsVisible( { 0 } );
}

void UniversalLocator::showSwitchFileType() {
showBar();

if ( mLocateInput->getText().empty() || !String::startsWith( mLocateInput->getText(), "ft " ) )
mLocateInput->setText( "ft " );

if ( mLocateInput->getText().size() >= 3 )
updateSwitchFileTypeTable();
updateLocateBar();
mApp->getStatusBar()->updateState();
}

std::shared_ptr<ItemListOwnerModel<std::string>>
UniversalLocator::openFileTypeModel( const std::string& match ) {
if ( nullptr == mApp->getSplitter()->getCurEditor() )
return ItemListOwnerModel<std::string>::create( {} );
const auto& defs = SyntaxDefinitionManager::instance()->getDefinitions();
std::vector<std::string> fileTypeNames;
fileTypeNames.reserve( defs.size() );
for ( const auto& def : defs ) {
if ( match.empty() || String::startsWith( String::toLower( def.getLanguageName() ),
String::toLower( match ) ) )
fileTypeNames.push_back( def.getLanguageName() );
}
std::sort( fileTypeNames.begin(), fileTypeNames.end() );
return ItemListOwnerModel<std::string>::create( fileTypeNames );
}

void UniversalLocator::updateSwitchFileTypeTable() {
mLocateTable->setModel(
openFileTypeModel( mLocateInput->getText().substr( 3 ).trim().toUtf8() ) );
if ( mLocateTable->getModel()->rowCount() > 0 && mApp->getSplitter()->getCurEditor() ) {
ModelIndex idx = mLocateTable->findRowWithText( mApp->getSplitter()
->getCurEditor()
->getDocumentRef()
->getSyntaxDefinition()
.getLanguageName() );
mLocateTable->getSelection().set( idx.isValid() ? idx
: mLocateTable->getModel()->index( 0 ) );
}
mLocateTable->scrollToTop();
mLocateTable->setColumnsVisible( { 0 } );
}

void UniversalLocator::onCodeEditorFocusChange( UICodeEditor* editor ) {
if ( !mLocateTable || !mLocateTable->isVisible() )
return;
Expand Down Expand Up @@ -927,6 +985,7 @@ std::vector<ProjectDirectoryTree::CommandInfo> UniversalLocator::getLocatorComma
vec.push_back( { "sb ", mUISceneNode->i18n( "switch_build", "Switch Build" ), icon } );
vec.push_back(
{ "sbt ", mUISceneNode->i18n( "switch_build_type", "Switch Build Type" ), icon } );
vec.push_back( { "ft ", mUISceneNode->i18n( "switch_file_type", "Switch File Type" ), icon } );
return vec;
}

Expand Down
6 changes: 6 additions & 0 deletions src/tools/ecode/universallocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class UniversalLocator {

void showSwitchBuildType();

void showSwitchFileType();

protected:
UILocateBar* mLocateBarLayout{ nullptr };
UITableView* mLocateTable{ nullptr };
Expand Down Expand Up @@ -81,6 +83,8 @@ class UniversalLocator {

void updateSwitchBuildTypeTable();

void updateSwitchFileTypeTable();

void requestWorkspaceSymbol();

void requestDocumentSymbol();
Expand All @@ -104,6 +108,8 @@ class UniversalLocator {

std::shared_ptr<ItemListOwnerModel<std::string>> openBuildTypeModel( const std::string& match );

std::shared_ptr<ItemListOwnerModel<std::string>> openFileTypeModel( const std::string& match );

bool findCapability( PluginCapability );

String getDefQueryText( PluginCapability );
Expand Down

0 comments on commit d7d5678

Please sign in to comment.