程式碼完成的實現

來源:互聯網
上載者:User

在Delphi或是許多其他的IDE中,都擁有程式碼完成的功能,按下“.”鍵就能調出某個對象的成員,方法等供選擇
這麼做極大的方便了程式的編寫。如果要自己開發IDE,程式碼完成的功能是必不可少的
下面這段代碼是我的 Rarnu Script 中所使用的,現在公開供各位學習之用。

procedure TFormMain.CodeCompleteExecute(Kind: SynCompletionType;
Sender: TObject; var CurrentInput: string; var x, y: integer;
var CanExecute: boolean);
var
cv:         TfsClassVariable;
i, j:       integer;
w:          string;
p:          TBufferCoord;
pg:         TCodeSheet;
An:         string;
insStr:     string;
insFull:    string;
coloredStr: string;
cName:      string;
begin
pg := TCodeSheet(ppMain.ActivePage);
// set script compile environment
// this setting is to make all class which loaded usable.
// if has not Global Unit
// the script will not work normally.
Script.Clear;
Script.Lines.Text := pg.Memo.Text;
Script.Parent     := fsGlobalUnit;
// get the word at cursor position
p      := pg.Memo.CaretXY;
p.char := p.char - 2;
w      := pg.Memo.GetWordAtRowCol(p);
// find pointed class
cv := Script.FindClass(w);
// if class not found
// it may be a variant object
// use the object name to get class name
// and then try to get class name again
if cv = nil then
begin
    cName := GetClassNameByObjectName(w);
    cv    := Script.FindClass(cName);
end;
if cv = nil then
begin
    cName := GetClassTypeByLine;
    cv    := Script.FindClass(cName);
end;
// clear popup member list
CodeComplete.ItemList.Clear;
CodeComplete.InsertList.Clear;
// get members
// Ancestor is the parent class
// if it is not empty or TObject
// do the loop as well
// for the class RTTI only listed the published
// and special members, not contains parents
// so must loop to top class
// and get all member of them.
if cv <> nil then
begin
    while (cv.Ancestor <> EmptyStr) or (cv.Name = 'TObject') do
    begin
      // get parent class
      An := cv.Ancestor;
      // loop for members
      for i := 0 to cv.MembersCount - 1 do
      begin
        insStr     := EmptyStr;
        insFull    := EmptyStr;
        coloredStr := EmptyStr;
        // use regular expression to create colors
        // the colors are used to pointer the type of member
        // use Members.Typ to get type.
        case cv.Members[i].Typ of
          fvtClass:
            coloredStr := '/color{clblack}/style{+B}';
          fvtArray:
            coloredStr := '/color{clgreen}';
          fvtEnum:
            coloredStr := '/color{clHighlight}';
          fvtConstructor:
            coloredStr := '/color{clred}/style{+B}';
          else
            coloredStr := '/color{clblack}';
        end;
        // if not in upon
        // check need result or not
        // the needed member set to navy
        if (coloredStr = '/color{clblack}') and (cv.Members[i].NeedResult) then
          coloredStr := '/color{clnavy}';
        // if contains params
        if cv.Members[i].Count > 0 then
        begin
          insStr := cv.Members[i].Name;
          insFull := cv.Members[i].Name + '(';
          for j := 0 to cv.Members[i].Count - 1 do
          begin
            // add param name and type to member line.
            insFull := insFull + cv.Members[i].Params[j].Name + ': ' +
              cv.Members[i].Params[j].TypeName;
            // if is not the last param
            // add comma behind
            if j < cv.Members[i].Count - 1 then
            begin
              insFull := insFull + '; ';
            end;
          end;
          insFull    := insFull + ')';
          // use colored member line
          coloredStr := coloredStr + insFull;
        end
        else
        begin

          // if has no param
          insStr     := cv.Members[i].Name;
          coloredStr := coloredStr + insStr;
        end;
        // if member line not exists
        // add it to popup list
        if CodeComplete.InsertList.IndexOf(insStr) = -1 then
        begin
          CodeComplete.InsertList.Add(insStr);
          CodeComplete.ItemList.Add(coloredStr);
        end;
      end;
      // find parant class
      cv := Script.FindClass(an);
      if cv = nil then
        Break;
    end;
end;
// if popup list is not empty
// allow popup else not allowed
if CodeComplete.ItemList.Count = 0 then
    CanExecute := False
else
    CanExecute := True;
end;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.