SharpDevelop Community

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

Applying Custom Highlighting in AddIn

Last post 02-10-2014 3:36 PM by MHO. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 01-24-2014 4:37 PM

    • MHO
    • Not Ranked
    • Joined on 01-24-2014
    • Posts 7

    Applying Custom Highlighting in AddIn

    I'm trying to create a SharpDevelop addin that applies a custom highlighting strategy to all *.data files opened in SharpDevelop. I have created the xshd file that defines the strategy and successfully loaded it as an embedded resource:

    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyAddin.Resources.MyHighlighting.xshd"))
                    using (XmlTextReader reader = new XmlTextReader(stream))
                        highlightingDef = HighlightingLoader.Load(reader, HighlightingManager.Instance);

    (The code above works.)

    How do I apply the highlighting strategy to the appropriate files? I have tried for a couple of hours but can't find a way.

    Thanks for your help in advance!

  • 01-24-2014 5:06 PM In reply to

    Re: Applying Custom Highlighting in AddIn

    You can directly register an embedded .xshd in the AddIn-Tree, for example:

        <Path name="/SharpDevelop/ViewContent/AvalonEdit/SyntaxModes">
            <SyntaxMode id="Ruby.SyntaxMode"
                        extensions=".rb"
                        name="Ruby"
                        resource="ICSharpCode.RubyBinding.Resources.Ruby.xshd"/>
        </Path>

    Alternatively, if (for some reason) you need to register highlightings by code, you can use the HighlightingManager.Instance.RegisterHighlighting() method.

  • 01-29-2014 11:15 AM In reply to

    • MHO
    • Not Ranked
    • Joined on 01-24-2014
    • Posts 7

    Re: Applying Custom Highlighting in AddIn

    Thank you very much! Works like a charm!

  • 01-31-2014 9:56 AM In reply to

    • MHO
    • Not Ranked
    • Joined on 01-24-2014
    • Posts 7

    Re: Applying Custom Highlighting in AddIn

    I ran into another problem.

    I try to get the SharpDevelop's Comment Region tool bar button to work and added the following to the .xshd file:

        <Property name="LineComment" value="@@" />

    However, nothing happens when I click the button. Can you help me?

  • 02-01-2014 3:20 PM In reply to

    Re: Applying Custom Highlighting in AddIn

    To get the comment region toolbar to work requires a language binding and formatting strategy to be defined for the file. The syntax highlighting definition file is only used to colour and format the text.

    I am assuming you are working with SharpDevelop 4 so you will need the language binding defined in the addin file, for example:

    <Path name="/SharpDevelop/Workbench/LanguageBindings">
    <LanguageBinding id="Python"
    class="ICSharpCode.PythonBinding.PythonLanguageBinding"
    extensions=".py" />
    </Path>

    Then you will need to implement the language binding and a formatting strategy.

  • 02-07-2014 12:04 PM In reply to

    • MHO
    • Not Ranked
    • Joined on 01-24-2014
    • Posts 7

    Re: Applying Custom Highlighting in AddIn

    Thank you for your fast response! Got it working.

  • 02-10-2014 1:15 PM In reply to

    • MHO
    • Not Ranked
    • Joined on 01-24-2014
    • Posts 7

    Re: Applying Custom Highlighting in AddIn

    I ran into another problem. I made successfully a file template for the files of the custom language. However, the encoding of the files has to be be US-ASCII. I see that it's possible to change the encoding of each file manually, but it's not practical in this case. I'm searching for a way to automate this.

    Is there a way to specify the encoding of new files in the xft-file?

     

    My plan B was to save the file with new encoding with the following code:

    using ICSharpCode.AvalonEdit.AddIn;

    IViewContent vc = WorkbenchSingleton.Workbench.ActiveViewContent;
    ICodeEditorProvider cep = vc as ICodeEditorProvider;
    cep.CodeEditor.PrimaryTextEditor.Encoding = System.Text.Encoding.GetEncoding(1252);

    vc.PrimaryFile.SaveToDisk();

    However, it turns out that I would need .NET Framework 4.0 for that. I'm running Windows 8 so that's not possible.

     

    Do you have any ideas how to solve the problem?

  • 02-10-2014 3:36 PM In reply to

    • MHO
    • Not Ranked
    • Joined on 01-24-2014
    • Posts 7

    Re: Applying Custom Highlighting in AddIn

    Okay, I found a workaround for this using a FileTemplateAction. It launches the following command when a new file is created:

    string activeItemPath = WorkbenchSingleton.Workbench.ActiveViewContent.PrimaryFileName.ToString()

    ITextEditorProvider editorProvider = WorkbenchSingleton.Workbench.ActiveContent as ITextEditorProvider;
    string allText = editorProvider.TextEditor.Document.Text;
    File.WriteAllText(activeItemPath, allText, Encoding.GetEncoding(1252));
    activeViewContent.PrimaryFile.ReloadFromDisk();

    Well, it works but it isn't very convenient.

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