⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expl.pas.svn-base

📁 支持自定义语法高亮显示的编辑器控件
💻 SVN-BASE
字号:
unit expl;

interface

{$I Easy.inc}

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ImgList, ExtCtrls, Menus;

type
  TExplorerFrm = class(TForm)
    TreeView: TTreeView;
    ExplorerImages: TImageList;
    PopupMenu1: TPopupMenu;
    ShowCode1: TMenuItem;
    procedure TreeViewDblClick(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure ShowCode1Click(Sender: TObject);
  private
    { Private declarations }
    FFileName : string;
    procedure FillTree;
    procedure BeforeRefresh(List : TStrings);
    procedure AfterRefresh(List : TStrings);
    function  GetNodeText(Node : TTreeNode) : string;
    procedure ExpandWithParent(Node : TTreeNode);
  public
    procedure UpdateExplorer(const FileName : string);
    procedure FileNameChanged(const OldName, NewName : string);
    { Public declarations }
  end;

var
  ExplorerFrm: TExplorerFrm;

implementation
uses
  jparser, editunit, EasyUtils, EasyParser, EasyEditSource, EasyKeyMap,
  main;
{$R *.DFM}
const
  sPublicStr = 'public';
  sConstants = 'constants';
  sVariables = 'variables';
  sFunctions = 'functions';
  sJsExt = '.js';

function TExplorerFrm.GetNodeText(Node : TTreeNode) : string;
begin
  result := '';
  while Node <> nil do
  begin
    if result = '' then
      result := Node.Text
    else
      result := result + '.' + Node.Text;
    Node := Node.Parent;
  end;
end;

procedure TExplorerFrm.ExpandWithParent(Node : TTreeNode);
begin
  while Node <> nil do
  begin
    Node.Expand(False);
    Node := Node.Parent;
  end;
end;

procedure TExplorerFrm.BeforeRefresh(List : TStrings);
var
  Node  : TTreeNode;
begin
  with TreeView, Items do
  begin
    Node := GetFirstNode;
    while Node <> nil do
    begin
      if Node.Expanded then
        if (TObject(Node.Data) is TElementInfo) then
           List.Add(TElementInfo(Node.Data).Name)
        else
        begin
          List.Add(GetNodeText(Node));
        end;
      Node := Node.GetNextVisible;
    end;
  end;
end;

procedure TExplorerFrm.AfterRefresh(List : TStrings);
var
  Node : TTreeNode;
  s    : string;
begin
  with TreeView, Items do
  begin
    Node := GetFirstNode;
    while Node <> nil do
    begin
      if TObject(Node.Data) is TElementInfo then
        s := TElementInfo(Node.Data).Name
      else
        s := GetNodeText(Node);
      if (List.IndexOf(s) >= 0) then
        ExpandWithParent(Node);
      Node := Node.GetNext;
    end;
  end;
end;

procedure TExplorerFrm.UpdateExplorer(const FileName : string);
var
  List     : TStrings;
  F        : TCustomForm;
  {$IFDEF EASY_WIDESTRINGS}
  i        : integer;
  AStrings : TStrings;
  {$ENDIF}

begin
  if MainForm.InDestroying then
    Exit;
  FFileName := FileName;
  with TreeView, Items do
  begin
    BeginUpdate;
    try
      List := CreateSortedStrings;
      try
        BeforeRefresh(List);
        Clear;
        F := MainForm.GetEditForm(FileName);
        if F is TEditForm then
          with TEditForm(F).EasyEdit do
          begin
            UnitInfo.Parser := TEasyEditorParser(Parser);
            {$IFDEF EASY_WIDESTRINGS}
            AStrings := TStringList.Create;
            try
              for i := 0 to Lines.Count - 1 do
                AStrings.Add(Lines[i]);
              UnitInfo.ReparseStrings(AStrings);
            finally
              AStrings.Free;
            end;
            {$ELSE}
            UnitInfo.ReparseStrings(Lines);
            {$ENDIF}
          end
        else
          UnitInfo.ReparseStrings(nil);
        FillTree;
        AfterRefresh(List);
      finally
        List.Free;
      end;
    finally
      EndUpdate;
    end;
  end;
end;

procedure TExplorerFrm.FileNameChanged(const OldName, NewName : string);
begin
  //
end;

procedure TExplorerFrm.FillTree;
var
  Node : TTreeNode;

  function _AddNode(Node : TTreeNode; const s : string; ImageIndex : integer; Data : Pointer) : TTreeNode;
  begin
    result := TreeView.Items.AddChildObject(Node, s, Data);
    result.ImageIndex := ImageIndex;
    result.SelectedIndex := ImageIndex;
  end;

  procedure ProcessScope(const ACaption : string; ImageIndex : integer; Node : TTreeNode; AScope : TElementScope; Strings : TStrings);
  var
    i     : integer;
    Info  : TElementInfo;
    ANode : TTreeNode;
  begin
    ANode := nil;
    with TreeView.Items, Strings do
      for i := 0 to Count - 1 do
      begin
        Info := TElementInfo(Objects[i]);
        if (Info <> nil) and (Info.Scope = AScope) then
        begin
          if ANode = nil then
            ANode := _AddNode(Node, ACaption, 0, nil);
          _AddNode(ANode, Info.Name, ImageIndex, Info);
        end;
      end;
  end;

  procedure ProcessStrings(const ACaption : string; ImageIndex : integer; Strings : TStrings);
  begin
    with TreeView do
      ProcessScope(ACaption, ImageIndex, Node, sPublic, Strings);
  end;

begin
  with TreeView, Items do
  begin
    BeginUpdate;
    try
      Node := _AddNode(nil, sPublicStr, 3, nil);
      with UnitInfo do
      begin
        ProcessStrings(sConstants, 4, Constants);
        ProcessStrings(sVariables, 2, Variables);
        ProcessStrings(sFunctions, 1, Functions);
      end;
    finally
      EndUpdate;
    end;
  end;
end;

procedure TExplorerFrm.TreeViewDblClick(Sender: TObject);
var
  F : TCustomForm;
begin
  with TreeView do
    if (Selected <> nil) and (FFileName <> '') and (TObject(Selected.Data) is TElementInfo) then
    begin
      F := MainForm.GetEditForm(FFileName);
      if F is TEditForm then
        with TEditForm(F).EasyEdit do
        begin
          EditSource.BeginSourceUpdate(oprNavigate);
          try
            EditSource.JumpToLine(TElementInfo(Selected.Data).LineNo);
            Navigate(cCenterLine);
            SendMessage(MainForm.ClientHandle, WM_MDIACTIVATE, F.Handle, 0);
            SetFocus;
            Windows.SetFocus(Handle);
          finally
            EditSource.EndSourceUpdate;
          end;
        end;
    end;
end;

procedure TExplorerFrm.FormHide(Sender: TObject);
begin
  if Parent is TPanel then
  begin
    Parent.Width := 1;
    if MainForm <> nil then
      MainForm.LeftSplitter.Hide;
  end;
end;

procedure TExplorerFrm.ShowCode1Click(Sender: TObject);
var
  Sl       : TStrings;
  Info     : TElementInfo;
  StartPos : integer;
  EndPos   : integer;
  i        : integer;
  F        : TCustomForm;
  s        : string;
begin
  with TreeView do
    if (Selected <> nil) and (FFileName <> '') and (TObject(Selected.Data) is TElementInfo) then
    begin
      Info := Selected.Data;
      StartPos := Info.LineNo;
      if Info is TFunctionInfo then
        EndPos := TFunctionInfo(Info).EndPos
      else
        EndPos :=  StartPos;
      F := MainForm.GetEditForm(FFileName);
      if F is TEditForm then
        with TEditForm(F).EasyEdit do
        begin
          Sl := TStringList.Create;
          try
            for i := StartPos to EndPos do
              Sl.Add(Lines[i]);
            s := ChangeFileExt(Info.Name, sJsExt);
            with MainForm do
              if OpenEditor(s) = nil then
              begin
                F := CreateEditor(s);
                if F is TEditForm then
                  TEditForm(F).EasyEdit.Lines.Assign(Sl);
              end;
          finally
            Sl.Free;
          end;
        end;
    end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -