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/17 23:32] – [Implementing Custom Code Completion] admin | scripting:examples:code-completion [2018/06/18 00:47] (current) – [Usage of CompletionSet and CompletionItem] admin | ||
|---|---|---|---|
| Line 44: | Line 44: | ||
| ==== Subscribing on onCompletion event ==== | ==== Subscribing on onCompletion event ==== | ||
| - | <file javascript code-completion3.hejs> | + | 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> | ||
| //////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////// | ||
| // Register Code Completion Handler | // Register Code Completion Handler | ||
| Application.onCompletion = function(/ | Application.onCompletion = function(/ | ||
| if ( ActiveDocument.Syntax.IsInheritedFrom(" | if ( ActiveDocument.Syntax.IsInheritedFrom(" | ||
| - | //var aSuggestions = [" | + | |
| - | //this.ShowCompletionList(selection, | + | var aSuggestions = [" |
| - | + | this.ShowCompletionList(selection, | |
| - | var item5 = CompletionItem(" | + | return true; // event is handled |
| - | item5.Pattern = " | + | } |
| - | item5.onDescription | + | // event is not handled, editor shall execute default logic |
| - | var item6 = CompletionItem(" | + | return false; |
| - | item6.onExpand = onExpand; | + | };</ |
| - | item6.SubMenu = true; | + | |
| - | var aSuggestions = [CompletionItem("Item1", "js:methods"), CompletionItem(" | + | 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> | |
| - | var completion_set = CompletionSet(aSuggestions); | + | //////////////////////////////////////////////////////////////////////// |
| + | // Register Code Completion Handler | ||
| + | Application.onCompletion | ||
| + | if ( ActiveDocument.Syntax.IsInheritedFrom("js") ) { | ||
| + | var completion_set | ||
| + | // add as a string | ||
| + | | ||
| + | // add as a completion item. To refer style you can use relative syntax e.g. "variable", | ||
| + | // absolute | ||
| + | completion_set.Add(CompletionItem(" | ||
| 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]; | ||
| + | </ | ||