About Monkey 2 › Forums › Monkey 2 Development › Ted2: Experimental Plugin System
This topic contains 11 replies, has 5 voices, and was last updated by 
 cocon 2 years, 7 months ago.
- 
		AuthorPosts
 - 
		
			
				
August 19, 2016 at 10:24 am #3220
One of the latest commits to Ted2 was this experimental plugin system. It took me a few hours to wrap my head around this, so here is a micrography on how it works.
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#Import "<std>"Using std..Class PluginProperty Name:String() VirtualReturn "Untitled Plugin"EndFunction PluginsOfType<T>:T[]() Where T Extends PluginReturn Plugins<T>.Plugins().ToArray()EndFunction Display<T>() Where T Extends PluginFor Local p := Eachin PluginsOfType<T>()Print(p.Name)NextEndProtectedMethod New()AddPlugin(Self)EndMethod AddPlugin<T>( plugin:T ) Where T Extends PluginPlugins<T>.Plugins().Add( plugin )EndPrivateStruct Plugins<T>Global _plugins:Stack<T>Function Plugins:Stack<T>()If Not _plugins _plugins=New Stack<T>Return _pluginsEndEndEndClass DocumentType Extends PluginMethod New()AddPlugin(Self)EndEndClass ADocumentType Extends DocumentTypeProperty Name:String() OverrideReturn "ADocumentType"EndProtectedMethod New()EndPrivateGlobal _instance := New ADocumentTypeEndClass BDocumentType Extends DocumentTypeProperty Name:String() OverrideReturn "BDocumentType"EndProtectedMethod New()EndPrivateGlobal _instance := New BDocumentTypeEndClass TestPlugin Extends PluginProperty Name:String() OverrideReturn "GUIPlugin"EndProtectedMethod New()AddPlugin(Self)EndPrivateGlobal _instance := New TestPluginEndFunction Main()Print("Show All Plugins")Plugin.Display<Plugin>()Print("")Print("Show Any Plugin Derived From DocumentType")Plugin.Display<DocumentType>()Print("")EndAugust 25, 2016 at 9:40 pm #3451The last few days things are going very slow, I laid the groundwork however to create a bunch of Ted2 Plugins.
https://github.com/coconcode/monkey2/tree/experimental-plugins
The Plugins At This Moment:
12345678910111213141516PluginTextView_CursorMoveHomeName: "Toggle cursor position between the start of the line and the start of the first letter."Shortcut "Home"PluginTextView_CursorMoveWordName: "Move cursor to start or end of each the word."Shortcut: "Control(Shift) + Left or Right"PluginTextView_LineCopyCutName: "Copy or Cut a complete line when there is no text selection."Shortcut: "Control + C or X"PluginTextView_LineCommentName: "Toggle comment for lines."Shortcut: "Alt + C"However I made some code changes to the original design.
TextViewKeyEventFilterbecomes PluginTextView
This more of a naming convention since I thought that in the future there might be other plugins as well other than the text view. (e.g. PluginConsole, PluginExplorer, PluginAnything). This will create a nice mental grouping that will make sense, in comparison to rogue classes where each one has it’s own unique name and you can’t put them in a basket.Also I created the class TextViewFeatures (located in textviewplugin) to provide some shorthand methods for TextView objects, nothing more than a convenience wrapper class. It is also initialized as a private global so that it can be accessed easily.
August 26, 2016 at 2:00 am #3454Just a while ago I finished the LineComment Plugin, you can see it in action here:
https://1drv.ms/v/s!AquVr9J7xRcsgW6UpLVLBF-r4iT-
August 26, 2016 at 3:20 am #3456Imo plugins in Lua. Then you can re-use existing libraries. Actually, I might even write a plugin that will execute Lua in the editor.
August 26, 2016 at 6:02 am #3459August 28, 2016 at 8:05 am #3522One question.
I’m still learning mojo/mojox & co… and I would like to add some changes to TED2. Some of these changes are already implemented in my own MaxIDE version. (I’m a lazy person, I find quite tedious typing too much !)
I’d like (and I think others) to do something like this
Monkey1Local a,b,c>>Intto have a sort of ‘automatic conversion’ to obtain this
Monkey1Local a:Int,b:Int,c:IntThe next move should be the use of the old symbol syntax ($,#,% etc) – to handy sorry!
But (of course there’s always a but …) I’m asking where is the ‘correct place/way’ to insert this checking/altering thing in the current TED2 structure (due to the latest changes with ‘plugin’ support I’m a little lost to be honest!)
I’m still trying to understand how TextView is working – a change on a line is working without problems, but with more lines I get strange results!
Thanks
August 31, 2016 at 9:00 am #3563IMO, writing plugins for key processing isn’t usefull, because to use this plugins you must use PluginTextView, and so you will get working of all these keyfilters or nothing.
What should I do if I want to add support for cpp-coding and replace some filters, for example, replace monkey comments to cpp comments?
Thinking in this key, I plan to write base textview for all code langs (named CodeTextView). Inside this view I put specific key processing, codeformatter-plugin (capitalize and so on) – extendable for any langs, highlighter-plugin and keywords-plugin. These plugins will rely on the file type.
This interface:
Monkey1234Interface IDependsOnFileTypeMethod GetFileTypes:String[]()Method GetMainFileType:String()Endhelps us to check plugin for suitability for opened file.
Also using of plugins has some limitations – if you need to get data from file while initialize plugin – inside of method New() – you get “Memory access violation” error.
(in my case – I need to read keywords from json)
Anyway, plugin system is great!
(I’ll post my version when complete it)
August 31, 2016 at 9:03 am #3564@degac, you can write your logic inside of
ted2 / monkey2document.monkey2 / Monkey2DocumentView / OnKeyEvent()
August 31, 2016 at 12:31 pm #3570And how to control plugins collisions?
For documents – if I write my own plugin for catch .cpp files I see that it does’t work because of plaintext plugin which catch cpp too.
The same problem is facing if I want to write my own keys filter for keys which filters other plugin.
September 2, 2016 at 8:49 am #3604I’m asking where is the ‘correct place/way’ to insert this checking/altering thing in the current TED2 structure
There should be a ‘Ted2TextView’ in there which is currently responsible for calling the textview plugins – this is probably a good place to start tinkering as there’s not much in there.
Perhaps the TextViewKeyEventFilter plugin should be replaced with a simpler ‘TextViewPlugin’ that provides the same virtual methods as TextView, ie: OnKeyEvent, OnMouseEvent, OnRender? Perhaps not as easy to use as something more specific, but more flexbile (and you can always subclass it for a more specific superclass). TextViewPlugin could also perhaps provide a set of file types it’s applicable for (eg: there may be c++ specific textview plugins etc).
Also using of plugins has some limitations – if you need to get data from file while initialize plugin – inside of method New() – you get “Memory access violation” er
Will add an OnCreate() for this that gets called once everything’s up and running.
And how to control plugins collisions?
For document type plugins, there will need to be a way for the user to select the ‘default’ document type to be used when doing a plain ‘Open’. I can probably hack this into a menu somehow, although it probably belongs in a ‘real’ IDE options dialog – coming one day…
For the file tree view RMB menu, an ‘Open With’ menu item could also be used to open documents with a different plugin.
For textview plugins, you’re kind of boned aren’t you? Although it might also be an idea to think about plugin priorities?
September 2, 2016 at 2:40 pm #3611Thanks for OnCreate().
Sort by priority can be useful. But all users want to use them plugins as primary and we’ll got ride of priority.
I vote for adding priority.
September 6, 2016 at 2:57 pm #3704I would like to add some changes to TED2
You can code anything you want in a “dummy” class (that accepts a string parameter in an update method) and I will be able to convert it to a text view plugin. I had in plans for something like this to create a super lazy variable instantiation, but I had forgot it, I noted it down btw in a TODO list.
If you want to write the plugin write a new class as shown here:
https://github.com/blitz-research/monkey2/blob/master/src/ted2/textviewkeyeventfilter.monkey2In my branch everything is put here:
https://github.com/coconcode/monkey2/blob/experimental-plugins/src/ted2/plugintextview.monkey2What should I do if I want to add support for cpp-coding and replace some filters
At this moment the most easy way to solve it would be this, I consider later on to change the naming conventions back to the original.
Monkey12345678910111213141516171819202122232425262728Class PluginTextViewProtectedField fileTypeSupport:String()EndClass PluginTextView_PluginForCPPAndMonkey Extends PluginTextViewProtectedfileTypeSupport = New String[] ("cpp", "monkey")PublicMethod ValidateFileTypePlugin(fileType:String)'pseudocode 'Return fileTypeSupport.Contains(fileType)EndEnd......Function FilterKeyEvent(event:KeyEvent, textView:TextView)...For Local p := Eachin Plugin.PluginsOfType<PluginTextView>()If event.Eaten Exit' pseudocode 'If p.ValidateFileTypePlugin(MainWindowInstance.GetTheCurrentOpenedTypeFileType)p.OnFilterKeyEvent(event, textView)EndNext@Mark Sibly
Perhaps the TextViewKeyEventFilter plugin should be replaced with a simpler
This makes sense, since it would make plugins more powerful that way, can’t wait to try it.
Now about the status of my work, here are plugins so far.
12345678910111213141516171819202122232425262728PluginTextView_CursorMoveHomeName: "Toggle cursor between the the first word and the start of the current line."Shortcut: "Home"PluginTextView_CursorMoveWordName: "Move cursor to start or end of each the word."Shortcut: "Control(Shift) + Left or Right"PluginTextView_LineCopyCutName: "Copy or Cut a complete line when there is no text selection."Shortcut: "Control + C or X"PluginTextView_LineCommentName: "Toggle comment for lines."Shortcut: "Alt + C"PluginTextView_SelectWordName: "Select a word."Shortcut: "Ctrl + D"PluginTextView_MouseSelectName: "Select text while holding shift and clicking on the text."Shortcut: "Shift + Left Mouse Click"PluginTextView_MouseDoubleClickSelectName: "Select a word by double clicking."Shortcut: "Left Double Click"The only plugin that suffers a bit “PluginTextView_MouseSelect” since you might try to move the mouse a little or release the shift key to actually see the text get selected. (Later on I might try experiement with some async code, perhaps with fibers or timers, who knows. I need that part executed 500ms later after the last event fired so the effect is done. Alternatively, perhaps making the UpdateCursor method of the TextView public and call it might work.)
No more no less there are some other ongoing efforts for creation of cool plugins (if you know any other post it here), a good attempt for cross-contaminating codes.
https://github.com/StrangeIsMyName/monkey2
https://github.com/engor/Ted2Go
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.