{------------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is: SynEditAutoComplete.pas, released 2000-06-25. The Initial Author of the Original Code is Michael Hieke. Portions written by Michael Hieke are Copyright 2000 Michael Hieke. Portions written by Cyrille de Brebisson (from mwCompletionProposal.pas) are Copyright 1999 Cyrille de Brebisson. All Rights Reserved. Contributors to the SynEdit and mwEdit projects are listed in the Contributors.txt file. Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 2 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL and not to allow others to use your version of this file under the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under either the MPL or the GPL. $Id$ You may retrieve the latest version of this file at the SynEdit home page, located at http://SynEdit.SourceForge.net Known Issues: -------------------------------------------------------------------------------} unit SynEditAutoComplete; {$I synedit.inc} interface uses Classes, SysUtils, StrUtils, LCLIntf, LCLType, Controls, LCLProc, LazStringUtils, LazUTF8, SynEdit, SynEditKeyCmds, SynEditPlugins; const CodeTemplateMacroMagic = '$(EnableMakros)'; CodeTemplateEnableMacros = 'EnableMakros'; CodeTemplateKeepSubIndent = 'KeepSubIndent'; CodeTemplateAttributesStartMagic = '$(AttributesStart)'; CodeTemplateAttributesEndMagic = '$(AttributesEnd)'; type TCustomSynAutoComplete = class; TOnTokenNotFound = procedure(Sender: TObject; AToken: string; AEditor: TCustomSynEdit; var Index:integer) of object; TOnExecuteCompletion = procedure(ASynAutoComplete: TCustomSynAutoComplete; Index: integer) of object; { TCustomSynAutoComplete } TCustomSynAutoComplete = class(TLazSynMultiEditPlugin) private fOnTokenNotFound: TOnTokenNotFound; fIndentToTokenStart: boolean; FOnExecuteCompletion: TOnExecuteCompletion; fAttributes: TFPList;// list of TStrings function GetCompletionAttributes(Index: integer): TStrings; procedure ClearAttributes; protected fAutoCompleteList: TStrings; fCompletions: TStrings; fCompletionComments: TStrings; fCompletionValues: TStrings; fEditor: TCustomSynEdit; fEditors: TList; fEOTokenChars: string; fCaseSensitive: boolean; fParsed: boolean; procedure CompletionListChanged(Sender: TObject); function GetCompletions: TStrings; function GetCompletionComments: TStrings; function GetCompletionValues: TStrings; procedure ParseCompletionList; virtual; procedure SetAutoCompleteList(Value: TStrings); virtual; procedure SynEditCommandHandler(Sender: TObject; AfterProcessing: boolean; var Handled: boolean; var Command: TSynEditorCommand; var AChar: TUTF8Char; Data: pointer; HandlerData: pointer); procedure DoEditorAdded(AValue: TCustomSynEdit); override; procedure DoEditorRemoving(AValue: TCustomSynEdit); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure AddCompletion(AToken, AValue, AComment: string; TheAttributes: TStrings = nil); procedure DeleteCompletion(Index: integer); procedure Execute(AEditor: TCustomSynEdit); virtual; procedure ExecuteCompletion(AToken: string; AEditor: TCustomSynEdit); virtual; public property AutoCompleteList: TStrings read fAutoCompleteList write SetAutoCompleteList; property CaseSensitive: boolean read fCaseSensitive write fCaseSensitive; property Completions: TStrings read GetCompletions; property CompletionComments: TStrings read GetCompletionComments; property CompletionValues: TStrings read GetCompletionValues; property EndOfTokenChr: string read fEOTokenChars write fEOTokenChars; property CompletionAttributes[Index: integer]: TStrings read GetCompletionAttributes; property OnTokenNotFound: TOnTokenNotFound read fOnTokenNotFound write fOnTokenNotFound; property IndentToTokenStart: boolean read fIndentToTokenStart write fIndentToTokenStart; property OnExecuteCompletion: TOnExecuteCompletion read FOnExecuteCompletion write FOnExecuteCompletion; end; { TSynEditAutoComplete } TSynEditAutoComplete = class(TCustomSynAutoComplete) published property AutoCompleteList; property CaseSensitive; property Editor; property EndOfTokenChr; property OnTokenNotFound; property IndentToTokenStart; property OnExecuteCompletion; end; implementation { TCustomSynAutoComplete } procedure TCustomSynAutoComplete.AddCompletion(AToken, AValue, AComment: string; TheAttributes: TStrings); begin if AToken <> '' then begin if not fParsed then ParseCompletionList; fCompletions.Add(AToken); fCompletionComments.Add(AComment); fCompletionValues.Add(AValue); if TheAttributes=nil then TheAttributes:=TStringList.Create; fAttributes.Add(TheAttributes); end; end; procedure TCustomSynAutoComplete.CompletionListChanged(Sender: TObject); begin fParsed := FALSE; end; constructor TCustomSynAutoComplete.Create(AOwner: TComponent); begin inherited Create(AOwner); fAutoCompleteList := TStringList.Create; TStringList(fAutoCompleteList).OnChange := @CompletionListChanged; fCompletions := TStringListUTF8Fast.Create; fCompletionComments := TStringListUTF8Fast.Create; fCompletionValues := TStringListUTF8Fast.Create; fEditors := TList.Create; fEOTokenChars := '()[]{}.'; fAttributes:=TFPList.Create; end; function TCustomSynAutoComplete.GetCompletionAttributes(Index: integer): TStrings; begin Result:=TStrings(fAttributes[Index]); end; procedure TCustomSynAutoComplete.ClearAttributes; var i: Integer; begin for i:=0 to fAttributes.Count-1 do TObject(fAttributes[i]).Free; fAttributes.Clear; end; procedure TCustomSynAutoComplete.DeleteCompletion(Index: integer); begin fCompletions.Delete(Index); fCompletionComments.Delete(Index); fCompletionValues.Delete(Index); TObject(fAttributes[Index]).Free; fAttributes.Delete(Index); end; destructor TCustomSynAutoComplete.Destroy; begin fEditors.Free; fCompletions.Free; fCompletionComments.Free; fCompletionValues.Free; fAutoCompleteList.Free; ClearAttributes; fAttributes.Free; inherited Destroy; end; procedure TCustomSynAutoComplete.Execute(AEditor: TCustomSynEdit); var s: string; i, j: integer; begin if AEditor <> nil then begin // get token s := AEditor.LineText; j := AEditor.LogicalCaretXY.x; i := j - 1; if i <= Length(s) then begin while (i > 0) and (s[i] > ' ') and (Pos(s[i], fEOTokenChars) = 0) do Dec(i); Inc(i); s := Copy(s, i, j - i); ExecuteCompletion(s, AEditor); end else begin i:=-1; if Assigned(OnTokenNotFound) then OnTokenNotFound(Self,'',AEditor,i); if i>=0 then ExecuteCompletion(FCompletions[i], AEditor); end; end; end; procedure TCustomSynAutoComplete.ExecuteCompletion(AToken: string; AEditor: TCustomSynEdit); var i, j, Len, IndentLen, TokenStartX: integer; s: string; IdxMaybe, NumMaybe: integer; p, p2: TPoint; NewCaretPos: boolean; Temp: TStringList; begin if not fParsed then ParseCompletionList; Len := Length(AToken); if (Len=0) and Assigned(OnTokenNotFound) then begin i := 0; OnTokenNotFound(Self,AToken,AEditor,i); end; if (Len > 0) and (AEditor <> nil) and not AEditor.ReadOnly and (fCompletions.Count > 0) then begin // find completion for this token - not all chars necessary if unambiguous i := fCompletions.Count - 1; IdxMaybe := -1; NumMaybe := 0; if fCaseSensitive then begin while i > -1 do begin s := fCompletions[i]; if s = AToken then break else if LazStartsStr(AToken, s) then begin Inc(NumMaybe); IdxMaybe := i; end; Dec(i); end; end else begin while i > -1 do begin s := fCompletions[i]; if UTF8CompareLatinTextFast(s, AToken) = 0 then break else if UTF8CompareLatinTextFast(Copy(s, 1, Len), AToken) = 0 then begin Inc(NumMaybe); IdxMaybe := i; end; Dec(i); end; end; if (i = -1) and (NumMaybe = 1) then i := IdxMaybe; if (i < 0) and Assigned(fOnTokenNotFound) then fOnTokenNotFound(Self,AToken,AEditor,i); if i > -1 then begin // select token in editor p := AEditor.LogicalCaretXY; if Assigned(OnExecuteCompletion) then OnExecuteCompletion(Self,i) else begin AEditor.BeginUpdate; try TokenStartX:=p.x; s:=AEditor.Lines[p.y-1]; if TokenStartX>length(s) then TokenStartX:=length(s); while (TokenStartX > 1) and (s[TokenStartX-1] > ' ') and (Pos(s[TokenStartX-1], fEOTokenChars) = 0) do Dec(TokenStartX); AEditor.BlockBegin := Point(TokenStartX, p.y); AEditor.BlockEnd := p; // indent the completion string if necessary, determine the caret pos if IndentToTokenStart then begin IndentLen := p.x - Len - 1; end else begin // indent the same as the first line IndentLen:=1; if (p.y>0) and (p.y<=AEditor.Lines.Count) then begin s:=AEditor.Lines[p.y-1]; while (IndentLen