<commit_before>package UI::DBPicker;
use base qw/Wx::BoxSizer/;
use Wx;
use Wx qw/:sizer wxDefaultPosition wxDefaultSize/;
use Core::PluginManager;
use Wx::Event qw/EVT_BUTTON EVT_KILL_FOCUS EVT_COMBOBOX EVT_TEXT_ENTER EVT_SET_FOCUS/;
use UI::Components::PluginConfig;

use warnings;
use strict;

sub new {
    my( $class, $parent, $model ) = @_;
    my $self = $class->SUPER::new( &Wx::wxHORIZONTAL );

    my $label = Wx::StaticText->new($parent, -1, 'Database Engine', &Wx::wxDefaultPosition, [-1, -1], 0);
    $self->Add($label, 1, &Wx::wxALL | &Wx::wxALIGN_CENTER_VERTICAL, 5);

    my @initial = (keys %Core::PluginManager::dplugins) or ();
    my $combo = Wx::ComboBox->new($parent, -1, $model->{dbCfg}->{plugin}, &Wx::wxDefaultPosition, [-1, -1], 
        \@initial,
        &Wx::wxTE_PROCESS_ENTER);
    $self->Add($combo, 1, &Wx::wxALL, 5);

    my $cfgBtn = Wx::Button->new($parent, -1, '?', &Wx::wxDefaultPosition, [25, 25], 0);
    $self->Add($cfgBtn, 0, &Wx::wxALL, 5);

    my $cb = sub {
        my ($tf, $evt) = @_;
        my $text = $tf->GetValue();
        if(defined $Core::PluginManager::dplugins{$text}) {
            $model->{dbCfg}->{plugin} = $text;
            my $oldCfg = $model->{dbCfg}->{config};
            my $somewhatNewCfg = $text->default_parameters();
            # if cfg's are compatible, DO NOT REMOVE
            foreach (keys %{ $somewhatNewCfg }) {
                if(!defined $oldCfg->{$_}) {
                    %{ $model->{dbCfg}->{config} } = %{ $somewhatNewCfg };
                }
            }
        } elsif($text ne '') {
            $tf->SetValue('');
        }
	Wx::Event::Skip($_[1]);
    };
    Wx::Event::EVT_TEXT_ENTER($combo, -1, $cb);
    Wx::Event::EVT_KILL_FOCUS($combo, $cb);
    Wx::Event::EVT_COMBOBOX($combo, -1, $cb);

    Wx::Event::EVT_BUTTON($cfgBtn, -1, sub {
        my $data = $model->{dbCfg}->{config};
        my $pluginConfiger = UI::Components::PluginConfig->new($parent, $data);
        $pluginConfiger->ShowModal();
        $pluginConfiger->Destroy();
    });

    Wx::Event::EVT_SET_FOCUS($combo, sub {
        my %la = map { ($_ => 1) } $combo->GetStrings();
        if(%la ~~ %Core::PluginManager::dplugins) {
            Wx::Event::Skip($_[1]);
            return;
        }
        $combo->Clear();
        foreach (keys %Core::PluginManager::dplugins) {
            $combo->Append($_);
        }
        Wx::Event::Skip($_[1]);
    });

    return $self;
}

1;<commit_msg>don't always modify the list<commit_after>package UI::DBPicker;
use base qw/Wx::BoxSizer/;
use Wx;
use Wx qw/:sizer wxDefaultPosition wxDefaultSize/;
use Core::PluginManager;
use Wx::Event qw/EVT_BUTTON EVT_KILL_FOCUS EVT_COMBOBOX EVT_TEXT_ENTER EVT_SET_FOCUS/;
use UI::Components::PluginConfig;

use warnings;
use strict;

sub new {
    my( $class, $parent, $model ) = @_;
    my $self = $class->SUPER::new( &Wx::wxHORIZONTAL );

    my $label = Wx::StaticText->new($parent, -1, 'Database Engine', &Wx::wxDefaultPosition, [-1, -1], 0);
    $self->Add($label, 1, &Wx::wxALL | &Wx::wxALIGN_CENTER_VERTICAL, 5);

    my @initial = (keys %Core::PluginManager::dplugins) or ();
    my $combo = Wx::ComboBox->new($parent, -1, $model->{dbCfg}->{plugin}, &Wx::wxDefaultPosition, [-1, -1], 
        \@initial,
        &Wx::wxTE_PROCESS_ENTER);
    $self->Add($combo, 1, &Wx::wxALL, 5);

    my $cfgBtn = Wx::Button->new($parent, -1, '?', &Wx::wxDefaultPosition, [25, 25], 0);
    $self->Add($cfgBtn, 0, &Wx::wxALL, 5);

    my $cb = sub {
        my ($tf, $evt) = @_;
        my $text = $tf->GetValue();
        if(defined $Core::PluginManager::dplugins{$text}) {
            $model->{dbCfg}->{plugin} = $text;
            my $oldCfg = $model->{dbCfg}->{config};
            my $somewhatNewCfg = $text->default_parameters();
            # if cfg's are compatible, DO NOT REMOVE
            foreach (keys %{ $somewhatNewCfg }) {
                if(!defined $oldCfg->{$_}) {
                    %{ $model->{dbCfg}->{config} } = %{ $somewhatNewCfg };
                }
            }
        } elsif($text ne '') {
            $tf->SetValue('');
        }
	Wx::Event::Skip($_[1]);
    };
    Wx::Event::EVT_TEXT_ENTER($combo, -1, $cb);
    Wx::Event::EVT_KILL_FOCUS($combo, $cb);
    Wx::Event::EVT_COMBOBOX($combo, -1, $cb);

    Wx::Event::EVT_BUTTON($cfgBtn, -1, sub {
        my $data = $model->{dbCfg}->{config};
        my $pluginConfiger = UI::Components::PluginConfig->new($parent, $data);
        $pluginConfiger->ShowModal();
        $pluginConfiger->Destroy();
    });

    Wx::Event::EVT_SET_FOCUS($combo, sub {
        update_list($combo);
        Wx::Event::Skip($_[1]);
    });

    return $self;
}

sub update_list {
    my $combo = shift;
    my %la = map { ($_ => 1) } $combo->GetStrings();
    if(smatch(\%la, \%Core::PluginManager::dplugins)) { return }
    $combo->Clear();
    foreach (keys %Core::PluginManager::dplugins) {
        $combo->Append($_);
    }
}

# apparently ~~ is experimental AND deprecated
sub smatch {
    my ($b, $a) = @_;
    foreach (keys %{$a}) {
        unless($b->{$_}) { return undef }
    }
    1;
}

1;
{'fraction_non_alphanumeric': 0.2027450980392157, 'fraction_numerical': 0.010588235294117647, 'mean_word_length': 2.7785185185185184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2523058', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1884, 'n_words': 345}