<commit_before>unit ce_mru;

{$I ce_defines.inc}

interface

uses
  Classes, SysUtils, ce_interfaces, ce_observer,
  ce_project, ce_synmemo;

type

  (**
   * 'Most Recently Used' list for strings.
   *)
  TCEMruList = class(TStringList)
  private
    fMaxCount: Integer;
    fObj: TObject;
  protected
    fChecking: boolean;
    procedure clearOutOfRange;
    procedure setMaxCount(aValue: Integer);
    function checkItem(const S: string): boolean; virtual;
    procedure Put(Index: Integer; const S: string); override;
    procedure InsertItem(Index: Integer; const S: string); override;
  published
    property maxCount: Integer read fMaxCount write setMaxCount;
  public
    constructor Create; virtual;
    procedure Insert(Index: Integer; const S: string); override;
    property objectTag: TObject read fObj write fObj;
  end;

  (**
   * MRU list for filenames.
   *)
  TCEMRUFileList = class(TCEMruList)
  protected
    function checkItem(const S: string): boolean; override;
  public
    procedure assign(src: TPersistent); override;
  end;

  (**
   * MRU list for D/text files.
   * Insertion is automatic (ICEMultiDocObserver).
   *)
  TCEMRUDocumentList = class(TCEMRUFileList, ICEMultiDocObserver)
  private
    procedure docNew(aDoc: TCESynMemo);
    procedure docFocused(aDoc: TCESynMemo);
    procedure docChanged(aDoc: TCESynMemo);
    procedure docClosing(aDoc: TCESynMemo);
  public
    constructor create; override;
  end;

  (**
   * MRU list for the ceodit projects.
   * Insertion is automatic (ICEProjectObserver).
   *)
  TCEMRUProjectList = class(TCEMRUFileList, ICEProjectObserver)
  private
    procedure projNew(aProject: TCEProject);
    procedure projChanged(aProject: TCEProject);
    procedure projClosing(aProject: TCEProject);
    procedure projFocused(aProject: TCEProject);
    procedure projCompiling(aProject: TCEProject);
  public
    constructor create; override;
  end;

implementation

constructor TCEMruList.Create;
begin
  fMaxCount := 10;
end;

procedure TCEMruList.clearOutOfRange;
begin
  while Count > fMaxCount do
    delete(Count-1);
end;

procedure TCEMruList.setMaxCount(aValue: Integer);
begin
  if aValue < 0 then
    aValue := 0;
  if fMaxCount = aValue then
    exit;
  fMaxCount := aValue;
  clearOutOfRange;
end;

function TCEMruList.checkItem(const S: string): boolean;
var
  i: NativeInt;
begin
  i := indexOf(S);
  if i = -1 then
    exit(true);
  if i = 0 then
    exit(false);
  if Count < 2 then
    exit(false);
  exchange(i, i-1);
  exit( false);
end;

procedure TCEMruList.Put(Index: Integer; const S: string);
begin
  if not (checkItem(S)) then
    exit;
  inherited;
  clearOutOfRange;
end;

procedure TCEMruList.InsertItem(Index: Integer; const S: string);
begin
  if not (checkItem(S)) then
    exit;
  inherited;
  clearOutOfRange;
end;

procedure TCEMruList.Insert(Index: Integer; const S: string);
begin
  if not (checkItem(S)) then
    exit;
  inherited;
  clearOutOfRange;
end;

procedure TCEMRUFileList.assign(src: TPersistent);
var
  i: Integer;
begin
  inherited;
  for i := Count-1 downto 0 do
    if not fileExists(Strings[i]) then
      Delete(i);
end;

function TCEMRUFileList.checkItem(const S: string): boolean;
begin
  exit( inherited checkItem(S) and fileExists(S));
end;

constructor TCEMRUDocumentList.create;
begin
  inherited;
  EntitiesConnector.addObserver(self);
end;

procedure TCEMRUDocumentList.docNew(aDoc: TCESynMemo);
begin
end;

procedure TCEMRUDocumentList.docFocused(aDoc: TCESynMemo);
begin
end;

procedure TCEMRUDocumentList.docChanged(aDoc: TCESynMemo);
begin
end;

procedure TCEMRUDocumentList.docClosing(aDoc: TCESynMemo);
begin
  if FileExists(aDoc.fileName) and (aDoc.fileName <> aDoc.tempFilename) then
    Insert(0, aDoc.fileName);
end;

constructor TCEMRUProjectList.create;
begin
  inherited;
  EntitiesConnector.addObserver(self);
end;

procedure TCEMRUProjectList.projNew(aProject: TCEProject);
begin
end;

procedure TCEMRUProjectList.projFocused(aProject: TCEProject);
begin
end;

procedure TCEMRUProjectList.projChanged(aProject: TCEProject);
begin
end;

procedure TCEMRUProjectList.projCompiling(aProject: TCEProject);<commit_msg>fix, hidden AV on quit<commit_after>unit ce_mru;

{$I ce_defines.inc}

interface

uses
  Classes, SysUtils, ce_interfaces, ce_observer,
  ce_project, ce_synmemo;

type

  (**
   * 'Most Recently Used' list for strings.
   *)
  TCEMruList = class(TStringList)
  private
    fMaxCount: Integer;
    fObj: TObject;
  protected
    fChecking: boolean;
    procedure clearOutOfRange;
    procedure setMaxCount(aValue: Integer);
    function checkItem(const S: string): boolean; virtual;
    procedure Put(Index: Integer; const S: string); override;
    procedure InsertItem(Index: Integer; const S: string); override;
  published
    property maxCount: Integer read fMaxCount write setMaxCount;
  public
    constructor Create; virtual;
    procedure Insert(Index: Integer; const S: string); override;
    property objectTag: TObject read fObj write fObj;
  end;

  (**
   * MRU list for filenames.
   *)
  TCEMRUFileList = class(TCEMruList)
  protected
    function checkItem(const S: string): boolean; override;
  public
    procedure assign(src: TPersistent); override;
  end;

  (**
   * MRU list for D/text files.
   * Insertion is automatic (ICEMultiDocObserver).
   *)
  TCEMRUDocumentList = class(TCEMRUFileList, ICEMultiDocObserver)
  private
    procedure docNew(aDoc: TCESynMemo);
    procedure docFocused(aDoc: TCESynMemo);
    procedure docChanged(aDoc: TCESynMemo);
    procedure docClosing(aDoc: TCESynMemo);
  public
    constructor create; override;
    destructor destroy; override;
  end;

  (**
   * MRU list for the ceodit projects.
   * Insertion is automatic (ICEProjectObserver).
   *)
  TCEMRUProjectList = class(TCEMRUFileList, ICEProjectObserver)
  private
    procedure projNew(aProject: TCEProject);
    procedure projChanged(aProject: TCEProject);
    procedure projClosing(aProject: TCEProject);
    procedure projFocused(aProject: TCEProject);
    procedure projCompiling(aProject: TCEProject);
  public
    constructor create; override;
    destructor destroy; override;
  end;

implementation

constructor TCEMruList.Create;
begin
  fMaxCount := 10;
end;

procedure TCEMruList.clearOutOfRange;
begin
  while Count > fMaxCount do
    delete(Count-1);
end;

procedure TCEMruList.setMaxCount(aValue: Integer);
begin
  if aValue < 0 then
    aValue := 0;
  if fMaxCount = aValue then
    exit;
  fMaxCount := aValue;
  clearOutOfRange;
end;

function TCEMruList.checkItem(const S: string): boolean;
var
  i: NativeInt;
begin
  i := indexOf(S);
  if i = -1 then
    exit(true);
  if i = 0 then
    exit(false);
  if Count < 2 then
    exit(false);
  exchange(i, i-1);
  exit( false);
end;

procedure TCEMruList.Put(Index: Integer; const S: string);
begin
  if not (checkItem(S)) then
    exit;
  inherited;
  clearOutOfRange;
end;

procedure TCEMruList.InsertItem(Index: Integer; const S: string);
begin
  if not (checkItem(S)) then
    exit;
  inherited;
  clearOutOfRange;
end;

procedure TCEMruList.Insert(Index: Integer; const S: string);
begin
  if not (checkItem(S)) then
    exit;
  inherited;
  clearOutOfRange;
end;

procedure TCEMRUFileList.assign(src: TPersistent);
var
  i: Integer;
begin
  inherited;
  for i := Count-1 downto 0 do
    if not fileExists(Strings[i]) then
      Delete(i);
end;

function TCEMRUFileList.checkItem(const S: string): boolean;
begin
  exit( inherited checkItem(S) and fileExists(S));
end;

constructor TCEMRUDocumentList.create;
begin
  inherited;
  EntitiesConnector.addObserver(self);
end;

destructor TCEMRUDocumentList.destroy;
begin
  EntitiesConnector.removeObserver(self);
  inherited;
end;

procedure TCEMRUDocumentList.docNew(aDoc: TCESynMemo);
begin
end;

procedure TCEMRUDocumentList.docFocused(aDoc: TCESynMemo);
begin
end;

procedure TCEMRUDocumentList.docChanged(aDoc: TCESynMemo);
begin
end;

procedure TCEMRUDocumentList.docClosing(aDoc: TCESynMemo);
begin
  if FileExists(aDoc.fileName) and (aDoc.fileName <> aDoc.tempFilename) then
    Insert(0, aDoc.fileName);
end;

constructor TCEMRUProjectList.create;
begin
  inherited;
  EntitiesConnector.addObserver(self);
end;

destructor TCEMRUProjectList.destroy;
begin
  EntitiesConnector.removeObserver(self);
  inherited;
end;

procedure TCEMRUProjectList.projNew(aProject: TCEProject);
begin
end;

procedure TCEMRUProjectList.projFocused(aProject: TCEProject);
begin
end;

procedure TCEMRUProjectList.projChanged(aProject: TCEProject);
begin
end;

procedure TCEMRUProjectList.projCompiling(aProject: TCEProject);
{'fraction_non_alphanumeric': 0.09223075133752036, 'fraction_numerical': 0.0027913468248429866, 'mean_word_length': 4.572909915748542, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '4246063', 'n_tokens_mistral': 2978, 'n_tokens_neox': 2880, 'n_words': 751}