diff --git a/ide/text/fpcodcmp.pas b/ide/text/fpcodcmp.pas new file mode 100644 index 0000000000..2082b53787 --- /dev/null +++ b/ide/text/fpcodcmp.pas @@ -0,0 +1,59 @@ +unit FPCodCmp; { CodeComplete } + +interface + +uses Objects, + WUtils; + +const CodeCompleteWords : PTextCollection = nil; + +function FPCompleteCodeWord(const WordS: string; var Text: string): boolean; + +procedure InitCodeComplete; +procedure DoneCodeComplete; + +implementation + +uses WEditor, + FPViews; + +function FPCompleteCodeWord(const WordS: string; var Text: string): boolean; +var OK: boolean; + Index: sw_integer; +begin + OK:=Assigned(CodeCompleteWords); + if OK then + begin + Text:=CodeCompleteWords^.Lookup(WordS,Index); + OK:=(Index<>-1); + end; + if OK=false then Text:=''; + FPCompleteCodeWord:=OK; +end; + +procedure InitCodeComplete; +var I:integer; + S: string; +begin + if Assigned(CodeCompleteWords) then Exit; + + New(CodeCompleteWords, Init(10,10)); + for I:=0 to GetReservedWordCount-1 do + begin + S:=LowCaseStr(GetReservedWord(I)); + if length(S)>=CodeCompleteMinLen then + CodeCompleteWords^.Insert(NewStr(S)); + end; + { + there should be also a user front-end for customizing CodeComplete ! + any volunteers to implement? ;) - Gabor + } +end; + +procedure DoneCodeComplete; +begin + if Assigned(CodeCompleteWords) then Dispose(CodeCompleteWords, Done); + CodeCompleteWords:=nil; +end; + +END. \ No newline at end of file diff --git a/ide/text/fpcodtmp.pas b/ide/text/fpcodtmp.pas new file mode 100644 index 0000000000..ff98395926 --- /dev/null +++ b/ide/text/fpcodtmp.pas @@ -0,0 +1,148 @@ +unit FPCodTmp; { Code Templates } + +interface + +uses Objects, + WUtils; + +type + PCodeTemplate = ^TCodeTemplate; + TCodeTemplate = object(TObject) + constructor Init(const AShortCut: string; AText: PUnsortedStringCollection); + function GetShortCut: string; + procedure GetText(AList: PUnsortedStringCollection); + destructor Done; virtual; + private + ShortCut: PString; + Text: PUnsortedStringCollection; + end; + + PCodeTemplateCollection = ^TCodeTemplateCollection; + TCodeTemplateCollection = object(TSortedCollection) + function Compare(Key1, Key2: Pointer): sw_Integer; virtual; + function SearchByShortCut(const ShortCut: string): PCodeTemplate; virtual; + end; + +const CodeTemplates : PCodeTemplateCollection = nil; + +function FPTranslateCodeTemplate(const Shortcut: string; ALines: PUnsortedStringCollection): boolean; + +procedure InitCodeTemplates; +procedure DoneCodeTemplates; + +implementation + +constructor TCodeTemplate.Init(const AShortCut: string; AText: PUnsortedStringCollection); +procedure CopyIt(P: PString); {$ifndef FPC}far;{$endif} +begin + Text^.Insert(NewStr(GetStr(P))); +end; +begin + inherited Init; + ShortCut:=NewStr(AShortCut); + New(Text, Init(10,10)); + if Assigned(AText) then + AText^.ForEach(@CopyIt); +end; + +function TCodeTemplate.GetShortCut: string; +begin + GetShortCut:=GetStr(ShortCut); +end; + +procedure TCodeTemplate.GetText(AList: PUnsortedStringCollection); +procedure CopyIt(P: PString); {$ifndef FPC}far;{$endif} +begin + AList^.Insert(NewStr(GetStr(P))); +end; +begin + if Assigned(AList) then + Text^.ForEach(@CopyIt); +end; + +destructor TCodeTemplate.Done; +begin + if Assigned(ShortCut) then DisposeStr(ShortCut); ShortCut:=nil; + if Assigned(Text) then Dispose(Text, Done); Text:=nil; + inherited Done; +end; + +function TCodeTemplateCollection.Compare(Key1, Key2: Pointer): sw_Integer; +var K1: PCodeTemplate absolute Key1; + K2: PCodeTemplate absolute Key2; + R: Sw_integer; + S1,S2: string; +begin + S1:=UpCaseStr(K1^.GetShortCut); + S2:=UpCaseStr(K2^.GetShortCut); + if S1S2 then R:=1 else + R:=0; + Compare:=R; +end; + +function TCodeTemplateCollection.SearchByShortCut(const ShortCut: string): PCodeTemplate; +var T: TCodeTemplate; + Index: sw_integer; + P: PCodeTemplate; +begin + T.Init(ShortCut,nil); + if Search(@T,Index)=false then P:=nil else + P:=At(Index); + T.Done; + SearchByShortCut:=P; +end; + +function FPTranslateCodeTemplate(const Shortcut: string; ALines: PUnsortedStringCollection): boolean; +var OK: boolean; + P: PCodeTemplate; +begin + OK:=Assigned(CodeTemplates); + if OK then + begin + P:=CodeTemplates^.SearchByShortCut(ShortCut); + OK:=Assigned(P); + if OK then + P^.GetText(ALines); + end; +{$ifdef GABOR} + { + this is for testing purposes only. once the user front-end for defining + CodeTemplates is implemented, this can be removed - Gabor + } + if (OK=false) and (UpCaseStr(ShortCut)='CASES') then + begin + OK:=true; + ALines^.Insert(NewStr('case of')); + ALines^.Insert(NewStr(' : ;')); + ALines^.Insert(NewStr(' : ;')); + ALines^.Insert(NewStr('end;')); + end else + if (OK=false) and (UpCaseStr(ShortCut)='CASEE') then + begin + OK:=true; + ALines^.Insert(NewStr('case of')); + ALines^.Insert(NewStr(' : ;')); + ALines^.Insert(NewStr(' : ;')); + ALines^.Insert(NewStr('else ;')); + ALines^.Insert(NewStr('end;')); + end else + ; +{$endif} + FPTranslateCodeTemplate:=OK; +end; + +procedure InitCodeTemplates; +begin + if Assigned(CodeTemplates) then Exit; + + New(CodeTemplates, Init(10,10)); +end; + +procedure DoneCodeTemplates; +begin + if Assigned(CodeTemplates) then Dispose(CodeTemplates, Done); + CodeTemplates:=nil; +end; + +END. \ No newline at end of file