SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Adding File Extensions to the XML Editor

Last post 08-22-2016 11:00 AM by vengeance. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 08-26-2006 12:02 AM

    • GoMa
    • Top 50 Contributor
    • Joined on 04-12-2006
    • Posts 80

    Adding File Extensions to the XML Editor

    Hi all,

    In my quest to extend SharpDevelop to support a non-.NET language (you probably heard me rumble enough already :D ), I also wanted to make the great built-in XML editor open my file type, which has a unique extension (.dscanvas).

    (The question is at the end of the post, this is some background. ;-)  ) 

    I tried searching for the method to do this, but it seemed too hard-coded into the editor, so what I eventually did was create a primary display binding for my filetype and use the XmlView to display the file.
    I supplied the XmlView with my custom schema for code-completion using some hacking:

    //Get schema from embedded resource.
    Stream schemaResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(GetType(), "dscanvas.xsd");
    //Setup an XML reader for the schema document and pass that to the editor.
    XmlTextReader xmlReader = new XmlTextReader(schemaResourceStream);
    canvasSchema = new XmlSchemaCompletionData(xmlReader);
    canvasSchemas = new XmlSchemaCompletionDataCollection(new XmlSchemaCompletionData[] { canvasSchema });
    //Add the schema to the collection and make it the default.
    xmlEditor.SchemaCompletionDataItems = canvasSchemas;
    xmlEditor.DefaultSchemaCompletionData = canvasSchema;

    I ran into two problems:

    1. Code completion would not work after save. I found out this is because the Save method sets the FileName property which resets the DefaultSchemaCompletionData property. To solve this I hooked an event handler for XmlView.Saved that set the default schema to my schema again.
    2. There is no syntax highlighting. I've tried:
      xmlEditor.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(XmlView.Language);
      But it doesn't help. Any ideas, Matt or anyone?

    Thanks in advance,

        GoMa
     

     

     

  • 08-26-2006 1:09 AM In reply to

    Re: Adding File Extensions to the XML Editor

    I think you're trying to do it the hard way. How to get a new extension in to the XML Editor depends on which version you're using

    If you're using the XML Editor in SharpDevelop 2.1 you just need to add your extension to the XmlEditor.addin file, the important part is the XmlFoldingParser section:

        <Path name = "/Workspace/Parser">
            <Parser id = "XmlFoldingParser"
                    supportedextensions  = ".xml;.xsl;.xslt;.xsd;.manifest;.config;....[snip]
                    class = "ICSharpCode.XmlEditor.Parser"/>
        </Path>

    You can also add it to the ExtraXmlFileIcon path, just below the parser one, so it gets the XML icon in the project browser.

    In SharpDevelop 2.0 the process is more painful. You'll need to update the XmlEditor.addin file as explained above. Just doing that however only gives you folding, you don't get any syntax highlighting because of the poor way I implemented it originally. The syntax highlighting is controlled by the ICSharpCode.TextEditor library and only the extensions it defines as XML are syntax highlighted. You'll need to add your extension to the files SyntaxModes.xml and XML-Mode.xshd. Then you'll need to recompile the text editor library. Then it should work. You'll XML file may still be shown without syntax highlighting since SharpDevelop stores the file type in files in C:\Documents and Settings\[username]\Application Data\.ICSharpCode\SharpDevelop2.1\temp. Either find your file in here and delete it or simply right click your file and change the File Mode to XML.

    When I get a spare few minutes I may implement an easier way of adding a new extension to the XML editor, probably somewhere in the Tools Options page, or maybe I'll implement something similar to Visual Studio where you can choose to view a file in different editors. The Tools Options page is probably easier. 

  • 08-26-2006 9:50 PM In reply to

    • GoMa
    • Top 50 Contributor
    • Joined on 04-12-2006
    • Posts 80

    Re: Adding File Extensions to the XML Editor

    Thanks for the detailed explanation Matt. I was searching for a way to do this without modifying/recompiling any core SharpDevelop code.
    Anyway, after setting a breakpoint on the DefaultDocument.HighlightingStrategy set accessor, I found out that the HighlightingStrategy was reset to the default after I had set it to the XML one because of the memento. That and your advice on the temp file brought to a quick solution - delete all the xml files in the temp folder.
    Too bad I hadn't tried creating a new project before. :)
    So, my problem is now solved. Thank you very much!

  • 09-25-2006 9:05 AM In reply to

    • wildbill
    • Top 500 Contributor
    • Joined on 09-14-2006
    • Habichtswald, Germany
    • Posts 10

    Re: Adding File Extensions to the XML Editor

    I recently had a question about syntax definitions that you (MattWard) answered. I accidentally read this question as well and decided to implement folding AND syntax highlighting for my simple NPL-Addin. I added NPL to the syntax definition file SyntaxModes.xml in the TextEditor Project and ran the ReleaseBuild.bat file in the SharpDevelop source code project that I downloaded (Version 2.0). I also put the NPL syntax definition file NPL-Mode.xshd in the modes directory (NOT in the TextEditor project, it worked fine without the definition). Everything else is in the NPL-Editor Addin in the Addin directory (DisplayBinding, FormattingStrategy, FoldingStrategy, View and Pad).

    It works great! It does pay to look around a bit.

    I do have one problem though (maybe two). If I try to open a file I get a complaint about not having permission. If I include the file in a project or solution first, then I can open the file.

    NPL is not a .net language and wont likely be one in the future. In fact the files are stored in binary archives that contain whole directories of NPL program files that are interpreted at runtime. Up to now I have done all testing with simple text files. I can use the NPL interpreter to get the code from a file in text format and want to load it in the TextEditor of SharpDevelop. I'm not able to do this. I found the FileService method for loading files from a file name. I don't have a file name, however, but instead already have the code in a text string. I implemented an NPLView from AbstractViewContent where I was forced to implement the LoadContent method which accepts text in a string as a parameter. It loads text from files just fine. How do I get it to load content from a string?

    I also have a Pad where I show all program files from a binary archive in a tree. A DoubleClick on an entry in the tree calls a method where I extract the content into a string. How do I get the TextEditor to open a new Tab with the string as content?

     

  • 09-25-2006 6:27 PM In reply to

    Re: Adding File Extensions to the XML Editor

    If you want to load text into your view then you will have to do it yourself. SharpDevelop either opens a view giving it the filename to load, or it will create a view and give it the text content if you are creating a new file.

    I am not totally sure how you want your addin to work, but on the DoubleClick event in the tree you say you get the text content. Then do something like this:

    Create a new NPLView:

        NPLView view = new NPLView();
        view.LoadContent(content);
        WorkbenchSingleton.Workbench.ShowView(view);

    Loading text into the text editor:

        void LoadContent(string content)
        {
            textEditor.Document.TextContent = content;

            // Optional bit here - you have set this up when creating the NPLView class.   
            textEditor.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("NPL");

            // Forces the folds to update.
            textEditor.Document.FoldingManager.UpdateFoldings(String.Empty, null);
            textEditor.ActiveTextAreaControl.TextArea.Refresh(textEditor.ActiveTextAreaControl.TextArea.FoldMargin);
        }

    I am assuming that your NPLView class contains a TextEditorControl.
     

  • 09-25-2006 8:52 PM In reply to

    • wildbill
    • Top 500 Contributor
    • Joined on 09-14-2006
    • Habichtswald, Germany
    • Posts 10

    Re: Adding File Extensions to the XML Editor

    You might not believe this, but ...

    After writing the last question, I tried this very same thing. In fact my code looks quite the same! It worked too!

    The folding didn't want to work anymore though and the syntax highlighting didn't work either. But I did get a View that I had implemented just the way you suggested. I think there is probably some little glitch in my code that keeps it from completely working. I usually stumble over things like that later or after much searching.

    I still have the problem of not being able to open any files because of permission problems. My colleague thinks it has something to do with the .net security concept. If I run ReleaseBuild.bat in the source code archive fresh out of the box, then I have no problems. If I add NPL to the systax_modes.xml file in the source directory of TextEditor and then run ReleaseBuild.bat again, then it starts complaining about permissions and refusing to cooperate.
     

  • 09-26-2006 2:41 PM In reply to

    Re: Adding File Extensions to the XML Editor

    You don't need to modify the text editor to add syntax modes. See the ILAsmBinding project in the SharpDevelop source code, it comes with a syntax mode that is registered with the AddIn tree.
  • 09-26-2006 8:16 PM In reply to

    • GoMa
    • Top 50 Contributor
    • Joined on 04-12-2006
    • Posts 80

    Re: Adding File Extensions to the XML Editor

    In addition to what Daniel said, whenever you get a permission problem when loading a file, it's probably some other error - click Show Exception to see the real problem. SharpDevelop just wraps it in a nice message box for users.

  • 08-22-2016 11:00 AM In reply to

    Re: Adding File Extensions to the XML Editor

    Hello, tell me how to add a file with the extension .saml that he would be recognized as XML, including what would in the text editor had a form as an XML document

Page 1 of 1 (9 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.