feature request - OnSyntaxEditorAttach/Detach on Language

SyntaxEditor for Windows Forms Forum

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Version: 4.0.0252
Avatar
Perhaps there's already a way to do this, but I haven't found one yet.

There are several things I'd like to do when a syntax editor loads my language. For instance, I'd like to attach to the ContextMenuRequested event, and the SmartIndent and IntellipromptSmartTagClicked events. I'd also like to set the context menu strip on the control.

I know that some of these things sound like they wouldn't be good to have in my language (like context menu strip), but I am the only one using my language and I'd like to be able to put all my code for supporting the syntax editor within my language. My current approach to solving the problem is that I have most of my code in the language, with some code in a "Component" that I place on all of my forms and hook up to a syntax editor instance. This component does the attach / detach stuff and sets up / houses the code for the context menus.

Anyway, is there any chance I can get an event for when the language is 'attached' to a syntax editor, and when it is 'detached'?

I'd expect it to look something like:

event EventHandler SyntaxEditorAttached;
event EventHandler SyntaxEditorDetached;

and the code should work like:

private void FireSyntaxEditorAttached(SyntaxLanguage language)
{
  if(language != null && language.SyntaxEditorAttached != null)
    language.SyntaxEditorAttached(this, EventArgs.Empty);
}

private void FireSyntaxEditorDetached(SyntaxLanguage language)
{
  if(language != null && language.SyntaxEditorDetached != null)
    language.SyntaxEditorDetached(this, EventArgs.Empty);
}

//...

public SyntaxLanguage Language {
  get {
    //... same as before
  }
  set {
    if(_Language != value)
    {
      FireSyntaxEditorDetached(_Language);
      _Language = value;
      //... other stuff when language changes
      FireSyntaxEditorAttached(_Language);
    }
  }
}
I'm assuming all the other events that the syntax editor fires have a sender that is itself, right? That way I can figure out in my event handlers which syntax editor the event came from...

Thanks,

Kelly Leahy Software Architect Milliman, USA

Comments (11)

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kelly,

Hmm, I'm not sure this would be possible. The reason is that the language is separated from the UI layer. The language itself doesn't even know which Documents are using it, let alone any SyntaxEditors that might be using Documents that use it. While the language does have methods that can work on SyntaxEditor instances (like OnSyntaxEditorKeyTyped), note that you must pass the actual SyntaxEditor instance into it. I hope I explained this clearly.

The SyntaxEditor class has events for DocumentChanged and DocumentSyntaxLanguageLoaded though. Perhaps by using those you can perform your logic.


Actipro Software Support

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Hmm... I don't see why this is any different than OnSyntaxEditorKeyTyped...

I'm not planning on 'keeping' the syntax editor reference within the language (as I know that one language instance may be associated with many documents and/or syntax editors.

Perhaps instead of events, you could just make protected virtual methods that get called in these cases, that way nobody would use them by mistake?

I'll let you mull over this a bit.

The thing is, the only way I can put my code into the syntax language implementation itself is to require some 'external help' from the UI to call methods on the language whenever it is attached to a new syntax editor instance. It would be really nice if I didn't have to write this inconspicuous code everywhere in order to make things work well.

This is just a niceity, and I can certainly live without it, but it would really make my code a bit easier to maintain and I'd be thrilled if you could find a way to provide it.

BTW, I've finished my AST/lexer/parser framework and have been using it for a month or so now... would you like to see it to see if you get any ideas from it for later releases?

Kelly Leahy Software Architect Milliman, USA

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Oh... I just thought about it a little more and it might be more useful to have a 'SyntaxEditorAttached' (and Detached) event on the Document class, and a 'DocumentAttached' (and Detached) in the language. This is more representative of the way things work in the hierarchy of classes, and would likely be a bit easier for you to implement...

Surely, you must see the value in having such events, right? It allows you to not need to add things like 'OnSyntaxEditorKeyTyped' in the future, since users can 'add their own' by attaching events to the document and/or syntax editor...

Your turn... sorry, I went twice - but I'm a cheater that way! :)

Kelly Leahy Software Architect Milliman, USA

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Sure we'd be happy to look over anything you have or if you have specific recommendations for features for future versions, email them over.

As for the request, so would adding just the virtual methods to Document and SyntaxLanguage be enough (Document getting OnSyntaxEditorAttached/OnSyntaxEditorDetached, and SyntaxLanguage getting OnDocumentAttached/OnDocumentDetached)?


Actipro Software Support

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Quote:
As for the request, so would adding just the virtual methods to Document and SyntaxLanguage be enough (Document getting OnSyntaxEditorAttached/OnSyntaxEditorDetached, and SyntaxLanguage getting OnDocumentAttached/OnDocumentDetached)?


hmmm... I thought about this a bit more... I'm not sure it would work.

the problem is, I need some way to be notified (in the syntax language) by the syntax editor when my syntax language is added to a document that's already attached to the syntax editor. As you said, this is where the 'detachment' of the UI and document model becomes a problem - the Document doesn't know which syntax editors it's attached to, does it?

Ok... I guess we should table this for the moment, unless you've got an idea how to make this happen.

Essentially, what I need is a sort of 'initializeEditor' and 'finalizeEditor' method on the syntax language that is called by the editor any time the document property changes (the reference, not the contents), or the document's language property changes (again, the reference).

We could still have the pair of events I referred to, but there's some code that would have to be added to the editor to support this - namely adding an event handler to watch when Document.Language changes, and some logic to the 'set' accessor for Document.

Sorry, I thought I'd thought this through sufficiently before asking for it, but obviously I was wrong.

Kelly Leahy Software Architect Milliman, USA

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Well the SyntaxEditor class already has events for DocumentChanged and DocumentSyntaxLanguageLoaded. Those tell SyntaxEditor when the Document property is set and when the Document.Language is set respectively. Perhaps handle those events and add code in there to register the SyntaxEditor with your language.


Actipro Software Support

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Well... the point of my request was to alleviate the need for me to write those event handlers in every form of my application that uses a syntax editor. I guess that's what I'll have to do.

Thanks,

Kelly Leahy Software Architect Milliman, USA

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Why not just make a class that inherits SyntaxEditor and override the related On methods (instead of handling the events themselves). Then as long as you use your custom SyntaxEditor class, it will work without external code.


Actipro Software Support

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Yep that was my next step. I would have preferred that you wrote that code, and built the plumbing between SyntaxEditor and the Language, but in the absense of a change to the editor/language plumbing, I'll have to do it myself :(

I just hate having to use derived versions of third party controls, but I was starting to see the need for it anyway, since there are several 'defaults' that I'd like to change in the editor when I use it - and it would save me the trouble if my derived class already changed them for me by the time it made it to the form.

Thanks,

Kelly Leahy Software Architect Milliman, USA

Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
I'll add it to the TODO list to add OnSyntaxEditorAttached/OnSyntaxEditorDetached and OnDocumentAttached/OnDocumentDetached methods to SyntaxEditor but it will probably be a lower priority item. That is the correct description of what you need, correct?


Actipro Software Support

Posted 17 years ago by Kelly Leahy - Software Architect, Milliman
Avatar
Yes... that's what I need.

I'm VERY ok with it being a 'low priority' item, as I understand that there aren't a lot of people (namely just me) asking for this.

The correct semantic description for what I need is this:

1) OnDocumentAttached/Detached added to SyntaxLanguage.
2) code to call #1 when Document.Language is changed for a given document.
3) OnSyntaxEditorAttached/Detached added to SyntaxLanguage.
4) code to call #3 when SyntaxEditor.Document.Language is changed, or when SyntaxEditor.Document is changed (as you mentioned - this can be supported by handling the document's LanguageChanged event in the syntax editor, and adding a small bit of code to the SyntaxEditor.Document 'set' accessor).

the reason #3 needs to be on 'SyntaxLanguage' is that there is no way for the document to give the language a reference to the syntax editor when 'language' is changed on the already attached document. Basically, I need to be notified when the default document that was attached to syntax editor when it was created has it's language changed to my language - so that I can do my language-specific initializations to that editor.

Strictly speaking, I really don't need #1 or #2, but I just included them for completeness, as I'm pretty sure that if you add #3 and #4, someone will ask for #1 and #2.

The precise semantics should be:
on a change to Document.Language:
Document should call Language.OnDocumentDetached for the old language,
Document should call Language.OnDocumentAttached for the new language,
Document should fire LanguageChanged event (which happens to be handled by SE)
event handler for LanguageChanged in syntaxEditor:
SyntaxEditor should call Language.OnSyntaxEditorDetached for the old language,
SyntaxEditor should call Language.OnSyntaxEditorAttached for the new language,
SyntaxEditor should do whatever else is necessary when handling this event.
on a change to SyntaxEditor.Document:
SyntaxEditor should remove event handler for LanguageChanged from old document,
SyntaxEditor should add event handler for LanguageChanged to new document,
SyntaxEditor should call Language.OnSyntaxEditorDetached for old document's language
SyntaxEditor should call Language.OnSyntaxEditorAttached for new document's language

Let me know if any of this is unclear and I'll be happy to clarify.

Thanks,

Kelly Leahy Software Architect Milliman, USA

The latest build of this product (v24.1.1) was released 29 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.