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/13 15:43] – [Completion using Completion Provider] 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 10: | Line 10: | ||
| * [[scripting: | * [[scripting: | ||
| ==== Methods ==== | ==== Methods ==== | ||
| - | * [[scripting: | + | * [[scripting: |
| - | * [[scripting: | + | * [[scripting: |
| * [[scripting: | * [[scripting: | ||
| * [[scripting: | * [[scripting: | ||
| - | * [[scripting: | + | * [[scripting: |
| - | * [[scripting: | + | * [[scripting: |
| - | * [[scripting: | + | * [[scripting: |
| ==== Objects ==== | ==== Objects ==== | ||
| - | * [[scripting: | + | * [[scripting: |
| - | * [[scripting: | + | * [[scripting: |
| - | * [[scripting: | + | * [[scripting: |
| ===== Examples ===== | ===== Examples ===== | ||
| ==== Completion using Completion Provider ==== | ==== Completion using Completion Provider ==== | ||
| - | <file javascript | + | Implementing a custom |
| - | function onCompletionCollect(view, selection, set) { | + | |
| - | set.Add(" | + | |
| - | set.Add(" | + | |
| - | return true; | + | |
| - | } | + | |
| - | Application.RegisterCompletionProvider(" | + | <file javascript code-completion1.hejs> |
| - | </ | + | |
| - | + | ||
| - | <file javascript code-completion.hejs> | + | |
| function onCompletionCollect(view, | function onCompletionCollect(view, | ||
| - | for (var i = 0; i < 10000; i++) | + | // if we are not inside of comments or documentation |
| - | set.Add(CompletionItem(" | + | if ( view.Document.GetStyleFromPos(selection.Start, |
| + | // add item as string, ше would be displayed using default style | ||
| + | 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; | ||
| } | } | ||
| + | // create and register completion provider to be used for JavaScript (id=" | ||
| Application.RegisterCompletionProvider(" | Application.RegisterCompletionProvider(" | ||
| </ | </ | ||
| ==== Subscribing on onCompletion event ==== | ==== 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> | ||
| //////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////// | ||
| Line 50: | Line 53: | ||
| 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]; | ||
| + | </ | ||