This is an old revision of the document!
Implementing Custom Code Completion
There is two ways of adding cusomt code completion to HippoEDIT:
- Implementing/registering own Completion provider. Recommended for most of use cases.
- Subscribing on Application.onCompletion event, and showing completion list by yourself. Can be used if you want to use different UI for completion.
API
There are several APIs and scripting object that helps implementing the code completion logic.
Events
Methods
- View.ShowCompletionList - Show Completion list
- View.HideCompletionList - Hide Completion list
- Applicaiton.RegisterCompletionProvider - Register code completion provider for syntax
- Applicaiton.UnRegisterCompletionProvider - UnRegister code completion provider for syntax by instance
- Applicaiton.CompletionItem - CompletionItem object constructor
- Applicaiton.CompletionSet - CompletionSet object constructor
- Applicaiton.CompletionProvider - CompletionProvider object constructor
Objects
- CompletionItem - to be added to CompletionSet
- CompletionSet - used for displaing completion list
- CompletionProvider - implement to be called on building completion list
Examples
Completion using Completion Provider
- code-completion1.hejs
function onCompletionCollect(view, selection, set) { // if we are not inside of comments or documentation (Style.Text == 1 ), add our items to the list if ( view.Document.GetStyleFromPos(selection.Start, true).Style.Text != 1 ) { // add items as strings, // they would be displayed using default style set.Add("Variable1"); set.Add("Variable2"); } return true; } // create and register completion provider to be used for JavaScript (id="js") and all syntaxes inherited from it Application.RegisterCompletionProvider("js", Application.CompletionProvider(onCompletionCollect));
- code-completion2.hejs
function onCompletionCollect(view, selection, set) { for (var i = 0; i < 10000; i++) set.Add(CompletionItem("Item2" + i, "variable")); return true; } Application.RegisterCompletionProvider("js", Application.CompletionProvider(onCompletionCollect));
Subscribing on onCompletion event
- code-completion3.hejs
//////////////////////////////////////////////////////////////////////// // Register Code Completion Handler Application.onCompletion = function(/*IRange*/ selection ) { if ( ActiveDocument.Syntax.IsInheritedFrom("js") ) { //var aSuggestions = ["Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10"]; //this.ShowCompletionList(selection, aSuggestions, false); var item5 = CompletionItem("Item5"); item5.Pattern = "%CurrentWord%(%|%)"; item5.onDescription = function () { return this.Text + "() ..."; }; var item6 = CompletionItem("Item6"); item6.onExpand = onExpand; item6.SubMenu = true; var aSuggestions = [CompletionItem("Item1", "js:methods"), CompletionItem("Item2", "variable"), CompletionItem("Item3"), CompletionItem("Item4", null, 11, 1, "Item4 Description", onExpand), item5, item6]; var completion_set = CompletionSet(aSuggestions); this.ShowCompletionList(selection, completion_set, true); return true; // processed } return false; };