Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| scripting:examples:code-completion [2018/06/14 01:37] – [Methods] admin | scripting:examples:code-completion [2018/06/18 00:47] (current) – [Usage of CompletionSet and CompletionItem] admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Implementing Custom Code Completion ====== | ====== Implementing Custom Code Completion ====== | ||
| - | There is two ways of adding | + | There is two ways of adding |
| - | * Implementing/ | + | * Implementing/ |
| * Subscribing on [[scripting: | * Subscribing on [[scripting: | ||
| Line 24: | Line 24: | ||
| ===== Examples ===== | ===== Examples ===== | ||
| ==== Completion using Completion Provider ==== | ==== Completion using Completion Provider ==== | ||
| + | Implementing a custom code completion using Completion Providers is a preferred way. You can register to call back only for specific syntax (and all other syntaxes inherited from it) and been called exactly at the moment when items are necessary, with minimum service code from your side, to give items back. In addition to that, usage of completion providers, allows HippoEDIT to call collection from different providers in parallel, that speedups general processing. | ||
| + | |||
| <file javascript code-completion1.hejs> | <file javascript code-completion1.hejs> | ||
| function onCompletionCollect(view, | function onCompletionCollect(view, | ||
| // if we are not inside of comments or documentation (Style.Text == 1 ), add our items to the list | // if we are not inside of comments or documentation (Style.Text == 1 ), add our items to the list | ||
| if ( view.Document.GetStyleFromPos(selection.Start, | if ( view.Document.GetStyleFromPos(selection.Start, | ||
| - | // add items as strings, | + | // add item as string, ше would be displayed using default style |
| - | // they would be displayed using default style | + | |
| set.Add(" | set.Add(" | ||
| - | set.Add(" | + | // or explicitely as completion item, if you want to better item customizing options |
| + | // for example, assigning of special style, as below | ||
| + | set.Add(CompletionItem(" | ||
| } | } | ||
| return true; | return true; | ||
| Line 39: | Line 42: | ||
| Application.RegisterCompletionProvider(" | Application.RegisterCompletionProvider(" | ||
| </ | </ | ||
| + | |||
| + | ==== Subscribing on onCompletion event ==== | ||
| + | If you want to take full control over the completion process, you can subscribe to [[scripting: | ||
| + | It can be a display of pre-filled Completion List or maybe, some special dialog or other UI. | ||
| + | Be aware, that [[scripting: | ||
| <file javascript code-completion2.hejs> | <file javascript code-completion2.hejs> | ||
| - | function | + | //////////////////////////////////////////////////////////////////////// |
| - | for (var i = 0; i < 10000; i++) | + | // Register Code Completion Handler |
| - | set.Add(CompletionItem(" | + | Application.onCompletion = function(/ |
| - | return true; | + | if ( ActiveDocument.Syntax.IsInheritedFrom(" |
| - | } | + | // simplest, just pass a list of strings |
| + | var aSuggestions = [" | ||
| + | this.ShowCompletionList(selection, | ||
| + | return true; // event is handled | ||
| + | } | ||
| + | // event is not handled, editor shall execute default logic | ||
| + | return false; | ||
| + | };</ | ||
| - | Application.RegisterCompletionProvider(" | + | If for some reason, in your plugin you cannot create JS like array, to pass a list of strings, you can pass it as [[scripting: |
| - | </ | + | <file javascript code-completion2.hejs> |
| - | + | ||
| - | ==== Subscribing on onCompletion event ==== | + | |
| - | <file javascript code-completion3.hejs> | + | |
| //////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////// | ||
| // Register Code Completion Handler | // Register Code Completion Handler | ||
| Application.onCompletion = function(/ | Application.onCompletion = function(/ | ||
| if ( ActiveDocument.Syntax.IsInheritedFrom(" | if ( ActiveDocument.Syntax.IsInheritedFrom(" | ||
| - | //var aSuggestions | + | var completion_set |
| - | //this.ShowCompletionList(selection, | + | // add as a string |
| - | + | completion_set.Add("Variable1"); | |
| - | var item5 = CompletionItem("Item5"); | + | // add as a completion item. To refer style you can use relative syntax e.g. "variable", |
| - | item5.Pattern = " | + | // absolute |
| - | item5.onDescription = function () { return this.Text + "() ..."; }; | + | |
| - | var item6 = CompletionItem("Item6"); | + | |
| - | item6.onExpand = onExpand; | + | |
| - | item6.SubMenu = true; | + | |
| - | var aSuggestions = [CompletionItem(" | + | |
| - | + | ||
| - | var completion_set = CompletionSet(aSuggestions); | + | |
| this.ShowCompletionList(selection, | this.ShowCompletionList(selection, | ||
| - | return true; // processed | + | return true; // event is handled |
| } | } | ||
| - | + | // event is not handled, editor shall execute default logic | |
| return false; | return false; | ||
| };</ | };</ | ||
| + | ==== Usage of CompletionSet and CompletionItem | ||
| + | |||
| + | When you construct [[scripting: | ||
| + | <code javascript> | ||
| + | var completion_set = CompletionSet([ | ||
| + | CompletionItem(" | ||
| + | CompletionItem(" | ||
| + | CompletionItem(" | ||
| + | ]); | ||
| + | |||
| + | // or | ||
| + | var completion_set2 = CompletionSet([" | ||
| + | </ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | If you need more customization of your item displayed in CompletionList, | ||
| + | |||
| + | <code javascript> | ||
| + | var item5 = CompletionItem(" | ||
| + | // custom insetion pattern, with code template syntax. | ||
| + | // %CurrentWord% - inserted text | ||
| + | item5.Pattern = " | ||
| + | // custom description tooltip, shown when user scrolls over items and stops on one of them | ||
| + | item5.onDescription = function () { return this.Text + "() ..."; }; | ||
| + | |||
| + | function onExpand(view, | ||
| + | ActiveDocument.ReplaceText(selection, | ||
| + | } | ||
| + | |||
| + | var item6 = CompletionItem(" | ||
| + | // adding of custom handler, which is called when item is selected and inserted | ||
| + | // default, inserts item Text property | ||
| + | item6.onExpand = onExpand; | ||
| + | // add small arrow icon on the right of the item, indicating that items expands | ||
| + | item6.SubMenu = true; | ||
| + | |||
| + | var aSuggestions = [ | ||
| + | // add completion items with text and style, from which color/ | ||
| + | CompletionItem(" | ||
| + | CompletionItem(" | ||
| + | |||
| + | // use default style for item display | ||
| + | CompletionItem(" | ||
| + | // define an item witthout style, but with explicit image, subimage (public/ | ||
| + | // description and selection call-back | ||
| + | CompletionItem(" | ||
| + | item5, | ||
| + | item6]; | ||
| + | </ | ||