Code Completion implementation

Source: Internet
Author: User

In Delphi or many other ides, code-based functions are available. You can press the "." key to call up the members and methods of an object.
This greatly facilitates programming. If you want to develop your own IDE, code-based functions are essential.
The following code is used in my rarnu script and is now publicly available for you to learn.

ProcedureTformmain. codecompleteexecute (kind: syncompletiontype;
Sender: tobject;VaRCurrentinput: string;VaRX, Y: integer;
VaRCanexecute: 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
IfCV =Nil then
Begin
Cname: = getclassnamebyobjectname (w );
CV: = script. findclass (cname );
End;
IfCV =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.
IfCV <>Nil then
Begin
While(Cv. Ancestor <> emptystr)Or(Cv. Name = 'tobject ')Do
Begin
// Get parent class
An: = cv. ancestor;
// Loop for Members
ForI: = 0ToCv. memberscount-1Do
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.
CaseCv. members [I]. typOf
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
// 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
IfCv. members [I]. Count> 0Then
Begin
Insstr: = cv. members [I]. Name;
Insfull: = cv. members [I]. Name + '(';
ForJ: = 0ToCv. members [I]. Count-1Do
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
IfJ <cv. members [I]. Count-1Then
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
IfCodecomplete. insertlist. indexof (insstr) =-1Then
Begin
Codecomplete. insertlist. Add (insstr );
Codecomplete. itemlist. Add (coloredstr );
End;
End;
// Find parant class
CV: = script. findclass ();
IfCV =Nil then
Break;
End;
End;
// If popup list is not empty
// Allow popup else not allowed
IfCodecomplete. itemlist. Count = 0Then
Canexecute: = false
Else
Canexecute: = true;
End;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.