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/18 00:24] – [Subscribing on onCompletion event] admin | scripting:examples:code-completion [2018/06/18 00:47] (current) – [Usage of CompletionSet and CompletionItem] admin | ||
|---|---|---|---|
| Line 45: | Line 45: | ||
| ==== Subscribing on onCompletion event ==== | ==== Subscribing on onCompletion event ==== | ||
| If you want to take full control over the completion process, you can subscribe to [[scripting: | If you want to take full control over the completion process, you can subscribe to [[scripting: | ||
| - | It can be display of pre-filled Completion List or maybe, some special dialog or other UI. | + | It can be a display of pre-filled Completion List or maybe, some special dialog or other UI. |
| Be aware, that [[scripting: | Be aware, that [[scripting: | ||
| Line 54: | Line 54: | ||
| if ( ActiveDocument.Syntax.IsInheritedFrom(" | if ( ActiveDocument.Syntax.IsInheritedFrom(" | ||
| // simplest, just pass a list of strings | // simplest, just pass a list of strings | ||
| - | var aSuggestions = [" | + | var aSuggestions = [" |
| this.ShowCompletionList(selection, | this.ShowCompletionList(selection, | ||
| return true; // event is handled | return true; // event is handled | ||
| Line 71: | Line 71: | ||
| // add as a string | // add as a string | ||
| completion_set.Add(" | completion_set.Add(" | ||
| - | // add as a completion item (to refer style you can use relative syntax e.g. " | + | // add as a completion item. To refer style you can use relative syntax e.g. " |
| - | // or at all pass Syntax object directly | + | // absolute " |
| completion_set.Add(CompletionItem(" | completion_set.Add(CompletionItem(" | ||
| this.ShowCompletionList(selection, | this.ShowCompletionList(selection, | ||
| Line 81: | Line 81: | ||
| };</ | };</ | ||
| + | ==== Usage of CompletionSet and CompletionItem | ||
| + | When you construct [[scripting: | ||
| <code javascript> | <code javascript> | ||
| - | //////////////////////////////////////////////////////////////////////// | + | var completion_set = CompletionSet([ |
| - | // Register Code Completion Handler | + | CompletionItem(" |
| - | Application.onCompletion = function(/*IRange*/ selection | + | CompletionItem(" |
| - | if ( ActiveDocument.Syntax.IsInheritedFrom("js") ) { | + | CompletionItem("Item3" |
| - | //var aSuggestions | + | ]); |
| - | //this.ShowCompletionList(selection, aSuggestions, | + | |
| - | + | // or | |
| - | var item5 = CompletionItem(" | + | var completion_set2 |
| - | item5.Pattern = " | + | </code> |
| - | item5.onDescription = function () { return this.Text + "() ..."; }; | + | |
| - | var item6 = CompletionItem(" | + | <note important> |
| - | item6.onExpand = onExpand; | + | |
| - | item6.SubMenu = true; | + | If you need more customization of your item displayed in CompletionList, there are several options you can use when constructing explicit [[scripting: |
| - | var aSuggestions = [CompletionItem(" | + | |
| - | + | <code javascript> | |
| - | var completion_set = CompletionSet(aSuggestions); | + | var item5 = CompletionItem(" |
| - | this.ShowCompletionList(selection, | + | // custom insetion pattern, with code template syntax. |
| - | return true; // processed | + | // %CurrentWord% - inserted text |
| - | } | + | item5.Pattern = " |
| - | + | // custom description tooltip, shown when user scrolls over items and stops on one of them | |
| - | return false; | + | 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]; | ||
| + | </ | ||