Implemented a basic Spell Checker plugin using typos-cli. Still needs some improvements but it's working (SpartanJ/ecode#515).

This commit is contained in:
Martín Lucas Golini
2025-08-20 20:45:36 -03:00
parent 8f3911b3a3
commit 66e793436c
11 changed files with 715 additions and 85 deletions

View File

@@ -1,6 +1,9 @@
#include "plugin.hpp"
#include "pluginmanager.hpp"
#include <eepp/system/filesystem.hpp>
#include <eepp/ui/uieventdispatcher.hpp>
#include <eepp/ui/uilistview.hpp>
#include <eepp/ui/uiscrollbar.hpp>
namespace ecode {
@@ -118,6 +121,79 @@ void Plugin::setReady( Time loadTime ) {
}
}
bool Plugin::editorExists( UICodeEditor* editor ) {
return mManager->getSplitter() && mManager->getSplitter()->editorExists( editor );
}
void Plugin::createListView( UICodeEditor* editor, std::shared_ptr<Model> model,
const ModelEventCallback& onModelEventCb,
const std::function<void( UIListView* )> onCreateCb ) {
UICodeEditorSplitter* splitter = getManager()->getSplitter();
if ( nullptr == splitter || !editorExists( editor ) )
return;
editor->runOnMainThread( [model, editor, splitter, onModelEventCb, onCreateCb] {
auto lvs = editor->findAllByClass( "editor_listview" );
for ( auto* ilv : lvs )
ilv->close();
UIListView* lv = UIListView::New();
lv->setParent( editor );
lv->addClass( "editor_listview" );
auto pos =
editor->getRelativeScreenPosition( editor->getDocumentRef()->getSelection().start() );
lv->setPixelsPosition( { pos.x, pos.y + editor->getLineHeight() } );
if ( !lv->getParent()->getLocalBounds().contains(
lv->getLocalBounds().setPosition( lv->getPixelsPosition() ) ) ) {
lv->setPixelsPosition( { pos.x, pos.y - lv->getPixelsSize().getHeight() } );
}
lv->setVisible( true );
lv->getVerticalScrollBar()->reloadStyle( true, true, true );
lv->setAutoExpandOnSingleColumn( false );
lv->setModel( model );
Float height = std::min( lv->getContentSize().y, lv->getRowHeight() * 8 );
Float colWidth = lv->getMaxColumnContentWidth( 0 ) + PixelDensity::dpToPx( 4 );
bool needsVScroll = lv->getContentSize().y > lv->getRowHeight() * 8;
Float width = colWidth + lv->getPixelsPadding().getWidth() +
( needsVScroll ? lv->getVerticalScrollBar()->getPixelsSize().getWidth() : 0 );
lv->setPixelsSize( { width, height } );
lv->setColumnWidth( 0, colWidth );
lv->setScrollMode( needsVScroll ? ScrollBarMode::Auto : ScrollBarMode::AlwaysOff,
ScrollBarMode::AlwaysOff );
if ( onCreateCb )
onCreateCb( lv );
lv->setSelection( model->index( 0 ) );
lv->setFocus();
Uint32 focusCb = lv->getUISceneNode()->getUIEventDispatcher()->addFocusEventCallback(
[lv]( const auto&, Node* focus, Node* ) {
if ( !lv->inParentTreeOf( focus ) && !lv->isClosing() )
lv->close();
} );
Uint32 cursorCb =
editor->on( Event::OnCursorPosChange, [lv, editor, splitter]( const Event* ) {
if ( !lv->isClosing() ) {
lv->close();
if ( splitter->editorExists( editor ) )
editor->setFocus();
}
} );
lv->on( Event::KeyDown, [lv, splitter, editor]( const Event* event ) {
if ( event->asKeyEvent()->getKeyCode() == EE::Window::KEY_ESCAPE && !lv->isClosing() )
lv->close();
if ( splitter->editorExists( editor ) )
editor->setFocus();
} );
lv->on( Event::OnModelEvent, [onModelEventCb]( const Event* event ) {
const ModelEvent* modelEvent = static_cast<const ModelEvent*>( event );
if ( onModelEventCb )
onModelEventCb( modelEvent );
} );
lv->on( Event::OnClose, [lv, editor, cursorCb, focusCb]( const Event* ) {
lv->getUISceneNode()->getUIEventDispatcher()->removeFocusEventCallback( focusCb );
editor->removeEventListener( cursorCb );
} );
} );
}
PluginBase::~PluginBase() {
mShuttingDown = true;
unsubscribeFileSystemListener();