📄 editor.pas
字号:
end;
procedure TEditorForm.NewSheet(const FileName:string);
// create new editor
var
PasEdit:TmwCustomEdit;
TabSheet:TTabSheet;
List:TStringList;
begin
TabSheet:=TTabSheet.Create(PageControl1);
with TabSheet do
begin
PageControl := PageControl1;
if ExtractFileExt(FileName)='.pas' then Caption:= GetNetFileName(FileName)
else Caption:= ExtractFileName(FileName);
PasEdit := TmwCustomEdit.Create(PageControl1);
PasEdit.Parent := TabSheet;
// RichEdit.Name := UniqueName(RichEdit);
PasEdit.Color := IniFile.ReadInteger('EDITOR','BackColor',clWhite);
PasEdit.Font.Name := IniFile.ReadString('EDITOR','FONTNAME','Arial');
PasEdit.Font.Size := IniFile.ReadInteger('EDITOR','FONTSIZE',10);
PasEdit.Align := alClient;
PasEdit.PopupMenu := EditorLocalMenu;
PasEdit.ScrollBars := ssBoth;
PasEdit.HighLighter:= PasSyn;
PasEdit.OnStatusChange := StatusChangeHandler;
end;
Caption:=FileName;
PageControl1.ActivePage:=TabSheet;
Show;
ActivePasEditor:= PasEdit;
end;
procedure TEditorForm.ecClosePageClick(Sender: TObject);
begin
FMainForm.CloseActiveFile;
end;
procedure TEditorForm.ecOpenFileAtCursorClick(Sender: TObject);
var
List:TStringList;
I:Integer;
begin
if OpenDialog1.Execute then
begin
List:=TStringList.Create;
List.LoadFromFile(OpenDialog1.FileName);
for I:=0 to List.Count-1 do
ActivePasEditor.Lines.Insert(1,List[I]);
end;
end;
procedure TEditorForm.ecReadOnlyItemClick(Sender: TObject);
begin
ecReadOnlyItem.Checked:=not ecReadOnlyItem.Checked;
ActivePasEditor.ReadOnly:= ecReadOnlyItem.Checked;
if ecReadOnlyItem.Checked then StatusBar.Panels[1].Text:='ReadOnly'
else StatusBar.Panels[1].Text:='';
end;
// 目辑 困摹俊 乐绰 窜绢甫 舅妨霖促.
function TEditorForm.GetCursorWord:string;
var
WordText:string;
I,LineLength,LinePos:Integer;
CurPos,StartPos,EndPos:Integer;
begin
Result:='';
WordText:= ActivePasEditor.LineText; // 目辑 困摹俊 乐绰 青狼 巩磊凯
if Length(WordText)<1 then Exit;
CurPos:=ActivePasEditor.CaretX;
LineLength:= Length(WordText);
for I:= CurPos downto 1 do
begin
if (WordText[I] ='') then Exit;
if WordText[I] in [' ',#0..#47,#58..#64,#91..#96,#123..#127] then Break;
end;
StartPos:= I+1; // 窜绢狼 矫累 困摹
for I:= CurPos to LineLength do begin
if WordText[I] ='' then Exit;
if WordText[I] in [' ',#0..#47,#58..#64,#91..#96,#123..#127] then Break;
end;
EndPos:= I; // 窜绢狼 付瘤阜 困摹
WordText:= Trim(Copy(WordText,StartPos, EndPos-StartPos));
Result:= WordText;
end;
procedure TEditorForm.CaretInfo;
var
Col,Row:integer;
begin
//泅犁 某返狼 困摹甫 炼荤茄促.
Col:= ActivePasEditor.CaretX;
Row:= ActivePasEditor.CaretY;
StatusBar.Panels[0].Text:=IntToStr(Row)+' :' + IntToStr(Col);
end;
procedure TEditorForm.Bookamrk01Click(Sender: TObject);
// Set BookMark
var
BookMark, X, Y: integer;
PasEdit: TmwCustomEdit;
begin
(Sender as TMenuItem).Checked:= not (Sender as TMenuItem).Checked;
if (Sender as TMenuItem).Checked then
begin
PasEdit:= ActivePasEditor;
if not PasEdit.Gutter.Visible then PasEdit.Gutter.Visible:= True;
BookMark:= (Sender as TMenuItem).Tag;
X:= PasEdit.CaretX;
Y:= PasEdit.CaretY;
PasEdit.SetBookMark(BookMark, X, Y);
end
else ActivePasEditor.ClearBookMark((Sender as TMenuItem).Tag);
end;
procedure TEditorForm.Bookmark01Click(Sender: TObject);
begin
ActivePasEditor.GotoBookMark((Sender as TMenuItem).Tag);
end;
procedure TEditorForm.ecTopicSearchClick(Sender: TObject);
begin
ShowKeywordHelp(GetCursorWord);
end;
function TEditorForm.GetLinkWithFile(const FileName:string): TmwCustomEdit;
var
I: Integer;
Name: string;
begin
Result:= ActivePasEditor;
for I:= Low(FMainForm.OpenUnitList) to High(FMainForm.OpenUnitList) do
begin
Name:= FMainForm.OpenUnitList[I].FileName;
if CompareText(Name, FileName) = 0 then
begin
Result:= FMainForm.OpenUnitList[I].Editor;
Break;
end;
end;
end;
function TEditorForm.IsIncludeUnit(UnitName:string): Boolean;
var
StartLine, EndLine, I: Integer;
begin
Result:= False;
ParserUnit.SetUnitSource(ActivePasEditor.Lines);
StartLine:= ParserUnit.GetStrPos('uses');
EndLine:= ParserUnit.GetStrPos('type');
for I:= StartLine to EndLine do
if Pos(UnitName, ParserUnit.UnitSource[I]) > 0 then
begin
Result:= True;
Break;
end;
end;
procedure TEditorForm.AddUseUnit(UnitName:string);
var
StartLine, EndLine, I, Pos1: Integer;
begin
if ParserUnit.UnitSource.Count = 0 then
ParserUnit.SetUnitSource(ActivePasEditor.Lines);
StartLine:= ParserUnit.GetStrPos('uses');
EndLine:= ParserUnit.GetStrPos('type');
for I:= StartLine to EndLine do
begin
Pos1:= Pos(';', ParserUnit.UnitSource[I]);
if Pos1 > 0 then
begin
ActivePasEditor.Lines[I]:= Copy(ActivePasEditor.Lines[I], 1, Pos1-1)
+' ,'+ UnitName + ';';
Break;
end;
end;
end;
procedure TEditorForm.CheckUseUnit(Component: TComponent);
// 家胶狼 uses巩俊 Component狼 unitname捞 器窃登绢 乐瘤 臼栏搁 器窃矫挪促.
var
Data: PTypeData;
Info: PTypeInfo;
UnitName: string;
begin
Info:= Component.ClassInfo;
Data:= GetTypeData(Info);
UnitName:= Data.UnitName;
if not IsIncludeUnit(UnitName) then AddUseUnit(UnitName);
end;
procedure TEditorForm.FormCreate(Sender: TObject);
begin
ParserUnit:= TParserUnit.Create('');
PasSyn.AsmAttri.Style := [];
PasSyn.CommentAttri.Style := [];
PasSyn.IdentifierAttri.Style:= [];
PasSyn.KeyAttri.Style := [];
PasSyn.NumberAttri.Style := [];
PasSyn.SpaceAttri.Style := [];
PasSyn.StringAttri.Style := [];
PasSyn.SymbolAttri.Style := [];
end;
procedure TEditorForm.FormDestroy(Sender: TObject);
begin
ParserUnit.Free;
end;
procedure TEditorForm.ecViewAsFormItemClick(Sender: TObject);
var
Input,Output:TMemoryStream;
FileName: string;
Form: TForm;
begin
FileName:= FilePath + TempFile;
ActivePasEditor.Lines.SaveToFile(FileName);
Input:= TMemoryStream.Create;
Input.LoadFromFile(FileName);
Output:= TMemoryStream.Create;
ObjectTextToResource(Input,Output);
Output.SaveToFile(FilePath+TempForm);
Output.LoadFromFile(FilePath+TempForm);
Form:= CreateProxy(TProxyForm,GetNetFileName(FileName)) as TForm;
Output.Position:=0;
// ReadClass;
try
Output.ReadComponentRes(Form);
finally
Input.Free;
OutPut.Free;
end;
end;
procedure TEditorForm.MwCompletionProposal1Execute(Sender: TObject);
// code completion procedure
// find and display object's properties, methods, events
// see object.dat file
var
Component: TComponent;
Idx: Integer;
begin
MwCompletionProposal1.ItemList.Clear;
Component:= FMainForm.ActiveForm.FindComponent(MwCompletionProposal1.CurrentString);
if Component <> nil then
begin
Idx:= CodeCompletionList.IndexOf('[' + Component.ClassName + ']');
if Idx <> -1 then
begin
Inc(Idx);
while (Idx < CodeCompletionList.Count) and
(CodeCompletionList[Idx][1] <> '[') do
begin
MwCompletionProposal1.ItemList.Add(CodeCompletionList[Idx]);
Inc(Idx);
end;
end;
end
else
begin // if not exists token, then active form's component list
for Idx:= 0 to FMainForm.ActiveForm.ComponentCount - 1 do
begin
MwCompletionProposal1.ItemList.Add(
Format('var %s:%s', [FMainForm.ActiveForm.Components[Idx].Name,
FMainForm.ActiveForm.Components[Idx].ClassName]));
end;
end;
end;
procedure TEditorForm.PasSynToken(Sender: TObject; TokenKind: Integer;
TokenText: String; LineNo: Integer);
var
_TokenKind : mwPasSyn.TtkTokenKind absolute TokenKind;
begin
if not FMainForm.ViewCodeExplorerItem.Checked then Exit;
if (_TokenKind = tkKey) and (Uppercase(TokenText) = 'CLASS') then
ParseState := psInClassDef;
if (_TokenKind = tkKey) and (Uppercase(TokenText) = 'END') and
(ParseState = psInClassDef) then
ParseState := psIdle;
if (_TokenKind = tkKey) and (Uppercase(TokenText) = 'IMPLEMENTATION') then
ObjectInspector.InsertIntoList(TokenText, LineNo, False);
if ParseState <> psInClassDef then
begin
if (ParseState = psIdle) and (_TokenKind = tkKey) and
((Uppercase(TokenText) = 'PROCEDURE') or (Uppercase(TokenText) = 'FUNCTION')) then
ParseState := psProcSeen;
if (ParseState = psProcSeen) and (_TokenKind = tkIdentifier) then
begin
ProcedureName := TokenText;
ParseState := psWaitForSymbol;
end;
if (ParseState = psWaitForSymbol) and (_TokenKind = tkSymbol) and
(TokenText = '.') then
ParseState := psProcSeen;
if (ParseState = psWaitForSymbol) and (_TokenKind = tkSymbol) and
(TokenText <> '.') then
begin
ObjectInspector.InsertIntoList(ProcedureName, LineNo, True);
ParseState := psIdle;
end;
end;
end;
procedure TEditorForm.ecViewExplorerItemClick(Sender: TObject);
begin
FMainForm.ViewCodeExplorerItemClick(self);
end;
procedure TEditorForm.CodeTemplateValidate(Sender: TObject);
var
Code, CodeStr: string;
Pos1: Integer;
begin
Code:= CompletionProposalForm.ItemList[CompletionProposalForm.Position];
Pos1:= Pos(' ', Code);
if Pos1 > 0 then Code:= Copy(Code, 1, Pos1 - 1);
CodeStr:= mwAutoComplete1.GetTokenValue(Code);
if CodeStr='' then Exit;
ActivePasEditor.SetSelStart(ActivePasEditor.GetSelStart - Length(Code));
ActivePasEditor.SetSelEnd(ActivePasEditor.GetSelStart + Length(Code));
ReplaceSelText(CodeStr);
ActivePasEditor.SetFocus;
end;
procedure TEditorForm.ecViewCodeTemplateClick(Sender: TObject);
var
p: TPoint;
begin
CompletionProposalForm:= TCompletionProposalForm.Create(self);
with CompletionProposalForm do
begin
ClSelect:= clAqua;
ItemList.Text:= mwAutoComplete1.GetTokenList;
with ActivePasEditor do
p := ClientToScreen(Point(CaretXPix, CaretYPix+LineHeight));
Left:= p.x;
Top:= p.y;
CompletionProposalForm.OnValidate:= CodeTemplateValidate;
Show;
end;
end;
procedure TEditorForm.ReplaceSelText(Replace:string);
begin
ActivePasEditor.SelText:= Replace;
end;
procedure TEditorForm.MwCompletionProposal1CodeCompletion(
var Value: String; var X: Integer);
var
i, j, k: Integer;
begin
i:= Pos(' ', Value);
if i > 0 then
begin
j:= Pos(':', Value); // property type position
k:= Pos('(', Value); // procedure parameters position
if k > 0 then j:= k;
if j > 0 then Value:= Trim(Copy(Value, i + 1, j - i - 1))
else Value:= Trim(Copy(Value, i + 1, Length(Value)));
end;
end;
procedure TEditorForm.StatusChangeHandler(Sender: TObject;
Changes: TmwStatusChanges);
const
ModifiedStrs: array[boolean] of string = ('', 'Modified');
InsertModeStrs: array[boolean] of string = ('Overwrite', 'Insert');
var
Edit: TmwCustomEdit;
begin
Edit:= GetLinkEditor;
// mwscAll can be used to update everything, see OnCreate handler
with StatusBar do begin
// statuspanel 0: caret position
if Changes * [mwscAll, mwscCaretX, mwscCaretY] <> [] then
begin
Panels[0].Text := Format('%6d:%3d', [Edit.CaretY, Edit.CaretX]);
end;
// statuspanel 1: Modified property
if Changes * [mwscAll, mwscModified] <> [] then
Panels[1].Text := ModifiedStrs[Edit.Modified];
// statuspanel 2: InsertMode property
if Changes * [mwscAll, mwscInsertMode] <> [] then
Panels[2].Text := InsertModeStrs[Edit.InsertMode];
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -