diff --git a/include/eepp/ui/uicodeeditor.hpp b/include/eepp/ui/uicodeeditor.hpp index d13fa28ae..3e8378b38 100644 --- a/include/eepp/ui/uicodeeditor.hpp +++ b/include/eepp/ui/uicodeeditor.hpp @@ -363,7 +363,8 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client { void setColorPreview( bool colorPreview ); - void goToLine( const TextPosition& position ); + void goToLine( const TextPosition& position, bool centered = true ); + protected: struct LastXOffset { TextPosition position; @@ -488,7 +489,7 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client { int getVisibleLinesCount(); - void scrollToMakeVisible( const TextPosition& position ); + void scrollToMakeVisible( const TextPosition& position, bool centered = false ); void setScrollX( const Float& val, bool emmitEvent = true ); diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index 626378f2e..c5d6094d3 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -943,9 +943,9 @@ void UICodeEditor::updateScrollBar() { setScrollY( mScroll.y ); } -void UICodeEditor::goToLine( const TextPosition& position ) { +void UICodeEditor::goToLine( const TextPosition& position, bool centered ) { mDoc->setSelection( position ); - scrollToMakeVisible( mDoc->getSelection().start() ); + scrollToMakeVisible( mDoc->getSelection().start(), centered ); } void UICodeEditor::updateEditor() { @@ -1011,7 +1011,7 @@ int UICodeEditor::getVisibleLinesCount() { return lines.second - lines.first; } -void UICodeEditor::scrollToMakeVisible( const TextPosition& position ) { +void UICodeEditor::scrollToMakeVisible( const TextPosition& position, bool centered ) { auto lineRange = getVisibleLineRange(); Int64 minDistance = mHScrollBar->isVisible() ? 3 : 2; @@ -1021,10 +1021,22 @@ void UICodeEditor::scrollToMakeVisible( const TextPosition& position ) { Float lineHeight = getLineHeight(); Float min = eefloor( lineHeight * ( eemax( 0, position.line() - 1 ) ) ); Float max = eefloor( lineHeight * ( position.line() + minDistance ) - mSize.getHeight() ); - if ( min < mScroll.y ) + Float halfScreenLines = eefloor( mSize.getHeight() / lineHeight * 0.5f ); + if ( min < mScroll.y ) { + if ( centered ) { + if ( position.line() - 1 - halfScreenLines >= 0 ) + min = eefloor( lineHeight * + ( eemax( 0, position.line() - 1 - halfScreenLines ) ) ); + } setScrollY( min ); - else if ( max > mScroll.y ) + } else if ( max > mScroll.y ) { + if ( centered ) { + max = eefloor( lineHeight * ( position.line() + minDistance + halfScreenLines ) - + mSize.getHeight() ); + max = eemin( max, getMaxScroll().y ); + } setScrollY( max ); + } } // Horizontal Scroll diff --git a/src/tools/codeeditor/codeeditor.cpp b/src/tools/codeeditor/codeeditor.cpp index 46e2c680c..7e2dde217 100644 --- a/src/tools/codeeditor/codeeditor.cpp +++ b/src/tools/codeeditor/codeeditor.cpp @@ -759,8 +759,9 @@ void App::initGlobalSearchBar() { Model::Role::Custom ) ); if ( mEditorSplitter->getCurEditor() && lineNum.isValid() && colNum.isValid() && lineNum.is( Variant::Type::Int64 ) && colNum.is( Variant::Type::Int64 ) ) { - mEditorSplitter->getCurEditor()->getDocument().setSelection( - {lineNum.asInt64(), colNum.asInt64()} ); + TextPosition pos{lineNum.asInt64(), colNum.asInt64()}; + mEditorSplitter->getCurEditor()->getDocument().setSelection( pos ); + mEditorSplitter->getCurEditor()->goToLine( pos ); hideGlobalSearchBar(); } } @@ -1576,6 +1577,7 @@ void App::onCodeEditorCreated( UICodeEditor* editor, TextDocument& doc ) { doc.setCommand( "debug-draw-debug-data", [&] { mUISceneNode->setDrawDebugData( !mUISceneNode->getDrawDebugData() ); } ); doc.setCommand( "go-to-line", [&] { goToLine(); } ); + doc.setCommand( "load-current-dir", [&] { loadCurrentDirectory(); } ); editor->addEventListener( Event::OnSave, [&]( const Event* event ) { UICodeEditor* editor = event->getNode()->asType(); if ( editor->getDocument().getFilePath() == mKeybindingsPath ) { @@ -1614,21 +1616,38 @@ bool App::setAutoComplete( bool enable ) { return false; } +void App::loadCurrentDirectory() { + if ( !mEditorSplitter->getCurEditor() ) + return; + std::string path( mEditorSplitter->getCurEditor()->getDocument().getFilePath() ); + if ( path.empty() ) + return; + path = FileSystem::fileRemoveFileName( path ); + if ( !FileSystem::isDirectory( path ) ) + return; + loadFolder( path ); +} + UIPopUpMenu* App::createToolsMenu() { mToolsMenu = UIPopUpMenu::New(); mToolsMenu->add( "Locate...", findIcon( "search" ), getKeybind( "open-locatebar" ) ); mToolsMenu->add( "Project Find...", findIcon( "search" ), getKeybind( "open-global-search" ) ); mToolsMenu->add( "Go to line...", findIcon( "go-to-line" ), getKeybind( "go-to-line" ) ); + mToolsMenu->add( "Load current directory as folder", findIcon( "folder" ), + getKeybind( "load-current-dir" ) ); mToolsMenu->addEventListener( Event::OnItemClicked, [&]( const Event* event ) { if ( !event->getNode()->isType( UI_TYPE_MENUITEM ) ) return; UIMenuItem* item = event->getNode()->asType(); - if ( item->getText() == "Locate..." ) { + std::string txt( item->getText() ); + if ( txt == "Locate..." ) { showLocateBar(); - } else if ( item->getText() == "Project Find..." ) { + } else if ( txt == "Project Find..." ) { showGlobalSearch(); - } else if ( item->getText() == "Go to line..." ) { + } else if ( txt == "Go to line..." ) { goToLine(); + } else if ( txt == "Load current directory as folder" ) { + loadCurrentDirectory(); } } ); return mToolsMenu; diff --git a/src/tools/codeeditor/codeeditor.hpp b/src/tools/codeeditor/codeeditor.hpp index e8b2954b7..a89f9a7b5 100644 --- a/src/tools/codeeditor/codeeditor.hpp +++ b/src/tools/codeeditor/codeeditor.hpp @@ -359,6 +359,8 @@ class App : public UICodeEditorSplitter::Client { bool trySendUnlockedCmd( const KeyEvent& keyEvent ); void goToLine(); + + void loadCurrentDirectory(); }; #endif // EE_TOOLS_CODEEDITOR_HPP