WPF AvalonEdit SQL xhsd request

CoolStraw picture CoolStraw · Nov 23, 2010 · Viewed 8.6k times · Source

I've looked on the internet (over and over) and I couldn't find an SQL.vshd file for AvalonEdit, I've used the old format version but it makes the editor bug (it doesn't show anymore at all), So I'm wondering maybe someone has done such a file because I don't want to waste my time doing this if it's been already done.

If you could share yours with me that'd be appreciated :)

Thanks in advance

Answer

Edward picture Edward · Mar 10, 2015

Step-by-step:

  1. If not already done, use nuget to install AvalonEdit into your project
  2. Add the Avalon Editor to your window, it will look something like this:

                <avalonEdit:TextEditor
                    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"  
                    Name="MyAvalonEdit"
                    FontFamily="Consolas"
                    FontSize="10pt" 
                    ShowLineNumbers="True" 
                    LineNumbersForeground="#FF2B91AF" />
    
  3. Add a new xml file to your project (put it in 'resources' folder, or you can put it anywhere), and name it sql.xshd (xml syntax highlight definition) (or you can name it whatever you like)

  4. Paste in the syntax definition from one the answers here
  5. Change the build action to 'Embedded Resource'
  6. Make a note of the project's default namespace (right-click project -> properties -> application) ProjectProperties
  7. Add the following code in your window load to wire up the syntax hightlighting; replace DefaultNameSpace and Folder as approriate

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("DefaultNamespace.Folder.sql.xshd"))
        {
            using (var reader = new System.Xml.XmlTextReader(stream))
            {
                MyAvalonEdit.SyntaxHighlighting = 
                    ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(reader, 
                    ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
            }
        }
    }
    
  8. If you have trouble with the resource name, you can find the names of all resources in the assembly with this:

        // Get names of embedded resources
        var resourceNames = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); 
    

I found a good xshd for SQL from Dirk Bahle's open source editor project here: https://github.com/Dirkster99/Edi/blob/master/Edi/Edi/AvalonEdit/Highlighting/SQL.xshd

<?xml version="1.0"?>
    <!-- Shades of Red-Brown: #a31515, #cf4315, #ffb96e, #ffdc95 -->
    <!-- Shades of Bright-Blue: #0077dc #008fe4 #8dbbdc #8de8ff -->
    <SyntaxDefinition name="SQL" extensions=".sql"
                      xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">

    <!-- T-SQL Reference: http://msdn.microsoft.com/de-de/library/ms189826%28v=sql.90%29.aspx -->

    <Color name="Digits"     foreground="DarkBlue" exampleText="3.1415f"/>
    <Color name="Comment" foreground="Green" exampleText="string text = &quot;Hello, World!&quot;"/>
    <Color name="Punctuation" foreground="Red" exampleText="string text = &quot;Hello, World!&quot;"/>

    <Color name="String" foreground="Olive" exampleText="string text = &quot;Hello, World!&quot;"/>
    <Color name="String2" foreground="#993" exampleText="string text = &quot;Hello, World!&quot;"/>

    <Color name="Keyword" fontWeight="bold" foreground="Blue" exampleText="SELECT"/>
    <Color name="Keyword1" fontWeight="normal" foreground="Blue" exampleText="NOCOUNT"/>
    <Color name="GoKeyword" fontWeight="bold" foreground="Red" exampleText="GO"/>

    <Color name="MethodCall" foreground="MidnightBlue" fontWeight="bold" />

    <Color name="Variable" foreground="Red"  exampleText="@Variable" />
    <Color name="Variable1" foreground="Red" exampleText="@@Variable" />

    <Color name="ObjectReference" foreground="Teal" exampleText="Customer.Name" />
    <Color name="ObjectReference1" foreground="Teal" exampleText="dbo.Customer.Name" />

    <Color name="ObjectReferenceInBrackets" foreground="Teal" exampleText="[Customer].[Name]" />
    <Color name="ObjectReferenceInBrackets1" foreground="Teal" exampleText="[dbo].[Customer].[Name]" />

    <Color name="CommentMarkerSetTodo"       foreground="Red"     fontWeight="bold" />
    <Color name="CommentMarkerSetHackUndone" foreground="#E0E000" fontWeight="bold" />

    <RuleSet name="CommentMarkerSet">
        <Keywords color="CommentMarkerSetTodo">
            <Word>TODO</Word>
            <Word>FIXME</Word>
        </Keywords>
        <Keywords color="CommentMarkerSetHackUndone">
            <Word>HACK</Word>
            <Word>UNDONE</Word>
        </Keywords>
    </RuleSet>

    <RuleSet  ignoreCase="true">
        <Span color="String" multiline="true" >
            <Begin>'</Begin>
            <End>'</End>
        </Span>

        <Span color="String2" multiline="true"  >
            <Begin>"</Begin>
            <End>"</End>
        </Span>

        <!-- span for escape sequences -->
        <Span  color="Comment" begin="--" end="\n" ruleSet="CommentMarkerSet"/>
        <Span color="Comment"  multiline="true" ruleSet="CommentMarkerSet">
            <Begin>/\*</Begin>
            <End>\*/</End>
        </Span>

        <Keywords color="Keyword" >
            <Word>ABORT</Word>
            <Word>BETWEEN</Word>
            <Word>CRASH</Word>
            <Word>DIGITS</Word>
            <Word>ACCEPT</Word>
            <Word>BINARY_INTEGER</Word>
            <Word>CREATE</Word>
            <Word>DISPOSE</Word>
            <Word>ACCESS</Word>
            <Word>BODY</Word>
            <Word>CURRENT</Word>
            <Word>DISTINCT</Word>
            <Word>ADD</Word>
            <Word>BOOLEAN</Word>
            <Word>CURRVAL</Word>
            <Word>DO</Word>
            <Word>ALL</Word>
            <Word>BY</Word>
            <Word>CURSOR</Word>
            <Word>DROP</Word>
            <Word>ALTER</Word>
            <Word>CASE</Word>
            <Word>DATABASE</Word>
            <Word>ELSE</Word>
            <Word>AND</Word>
            <Word>CHAR</Word>
            <Word>DATA_BASE</Word>
            <Word>ELSIF</Word>
            <Word>ANY</Word>
            <Word>CHAR_BASE</Word>
            <Word>DATE</Word>
            <Word>END</Word>
            <Word>ARRAY</Word>
            <Word>CHECK</Word>
            <Word>DBA</Word>
            <Word>ENTRY</Word>
            <Word>ARRAYLEN</Word>
            <Word>CLOSE</Word>
            <Word>DEBUGOFF</Word>
            <Word>EXCEPTION</Word>
            <Word>AS</Word>
            <Word>CLUSTER</Word>
            <Word>DEBUGON</Word>
            <Word>EXCEPTION_INIT</Word>
            <Word>ASC</Word>
            <Word>CLUSTERS</Word>
            <Word>DECLARE</Word>
            <Word>EXISTS</Word>
            <Word>ASSERT</Word>
            <Word>COLAUTH</Word>
            <Word>DECIMAL</Word>
            <Word>EXIT</Word>
            <Word>ASSIGN</Word>
            <Word>COLUMNS</Word>
            <Word>DEFAULT</Word>
            <Word>FALSE</Word>
            <Word>AT</Word>
            <Word>COMMIT</Word>
            <Word>DEFINITION</Word>
            <Word>FETCH</Word>
            <Word>AUTHORIZATION</Word>
            <Word>COMPRESS</Word>
            <Word>DELAY</Word>
            <Word>FLOAT</Word>
            <Word>AVG</Word>
            <Word>CONNECT</Word>
            <Word>DELETE</Word>
            <Word>FOR</Word>
            <Word>BASE_TABLE</Word>
            <Word>CONSTANT</Word>
            <Word>DELTA</Word>
            <Word>FORM</Word>
            <Word>BEGIN</Word>
            <Word>COUNT</Word>
            <Word>DESC</Word>
            <Word>FROM</Word>
            <Word>FUNCTION</Word>
            <Word>NEW</Word>
            <Word>RELEASE</Word>
            <Word>SUM</Word>
            <Word>GENERIC</Word>
            <Word>NEXTVAL</Word>
            <Word>REMR</Word>
            <Word>TABAUTH</Word>
            <Word>GOTO</Word>
            <Word>NOCOMPRESS</Word>
            <Word>RENAME</Word>
            <Word>TABLE</Word>
            <Word>GRANT</Word>
            <Word>NOT</Word>
            <Word>RESOURCE</Word>
            <Word>TABLES</Word>
            <Word>GROUP</Word>
            <Word>NULL</Word>
            <Word>RETURN</Word>
            <Word>TASK</Word>
            <Word>HAVING</Word>
            <Word>NUMBER</Word>
            <Word>REVERSE</Word>
            <Word>TERMINATE</Word>
            <Word>IDENTIFIED</Word>
            <Word>NUMBER_BASE</Word>
            <Word>REVOKE</Word>
            <Word>THEN</Word>
            <Word>IF</Word>
            <Word>OF</Word>
            <Word>ROLLBACK</Word>
            <Word>TO</Word>
            <Word>IN</Word>
            <Word>ON</Word>
            <Word>ROWID</Word>
            <Word>TRUE</Word>
            <Word>INDEX</Word>
            <Word>OPEN</Word>
            <Word>ROWLABEL</Word>
            <Word>TYPE</Word>
            <Word>INDEXES</Word>
            <Word>OPTION</Word>
            <Word>ROWNUM</Word>
            <Word>UNION</Word>
            <Word>INDICATOR</Word>
            <Word>OR</Word>
            <Word>ROWTYPE</Word>
            <Word>UNIQUE</Word>
            <Word>INSERT</Word>
            <Word>ORDER</Word>
            <Word>RUN</Word>
            <Word>UPDATE</Word>
            <Word>INTEGER</Word>
            <Word>OTHERS</Word>
            <Word>SAVEPOINT</Word>
            <Word>USE</Word>
            <Word>INTERSECT</Word>
            <Word>OUT</Word>
            <Word>SCHEMA</Word>
            <Word>VALUES</Word>
            <Word>INTO</Word>
            <Word>PACKAGE</Word>
            <Word>SELECT</Word>
            <Word>VARCHAR</Word>
            <Word>IS</Word>
            <Word>PARTITION</Word>
            <Word>SEPARATE</Word>
            <Word>VARCHAR2</Word>
            <Word>LEVEL</Word>
            <Word>PCTFREE</Word>
            <Word>SET</Word>
            <Word>VARIANCE</Word>
            <Word>LIKE</Word>
            <Word>POSITIVE</Word>
            <Word>SIZE</Word>
            <Word>VIEW</Word>
            <Word>LIMITED</Word>
            <Word>PRAGMA</Word>
            <Word>SMALLINT</Word>
            <Word>VIEWS</Word>
            <Word>LOOP</Word>
            <Word>PRIOR</Word>
            <Word>SPACE</Word>
            <Word>WHEN</Word>
            <Word>MAX</Word>
            <Word>PRIVATE</Word>
            <Word>SQL</Word>
            <Word>WHERE</Word>
            <Word>MIN</Word>
            <Word>PROCEDURE</Word>
            <Word>SQLCODE</Word>
            <Word>WHILE</Word>
            <Word>MINUS</Word>
            <Word>PUBLIC</Word>
            <Word>SQLERRM</Word>
            <Word>WITH</Word>
            <Word>MLSLABEL</Word>
            <Word>RAISE</Word>
            <Word>START</Word>
            <Word>WORK</Word>
            <Word>MOD</Word>
            <Word>RANGE</Word>
            <Word>STATEMENT</Word>
            <Word>XOR</Word>
            <Word>MODE</Word>
            <Word>REAL</Word>
            <Word>STDDEV</Word>
            <Word>NATURAL</Word>
            <Word>RECORD</Word>
            <Word>SUBTYPE</Word>
            <Word>TRUNCATE</Word>
        </Keywords>

        <Keywords color="Keyword1">
            <Word>NOCOUNT</Word>
        </Keywords>

        <Keywords color="GoKeyword" >
            <Word>GO</Word>
        </Keywords>

        <Rule color="ObjectReference1">([\d\w]+)\.([\d\w]+)\.([\d\w]+)</Rule>
        <Rule color="ObjectReference">([\d\w]+)\.([\d\w]+)</Rule>

        <Rule color="ObjectReferenceInBrackets1">([\d\w]+)\.([\d\w]+)\.([\d\w]+)</Rule>
        <Rule color="ObjectReferenceInBrackets">\[([\d\w]+)\]\.\[([\d\w]+)\]\.\[([\d\w]+)\]</Rule>
        <Rule color="ObjectReferenceInBrackets">\[([\d\w]+)\]\.\[([\d\w]+)\]</Rule>

        <Rule color="Punctuation">
      [?,.;()\[\]{}+\-/%*&lt;&gt;^+~!|&amp;]+
        </Rule>

        <Rule color="MethodCall">[\d\w_]+(?=(\s*\())</Rule>
        <Rule color="Variable1">@@([\w]+)</Rule>
        <Rule color="Variable">@([\w]+)</Rule>

        <!-- Digits -->
        <Rule color="Digits">
      \b0[xX][0-9a-fA-F]+  # hex number
      |
      (    \b\d+(\.[0-9]+)?   #number with optional floating point
      |    \.[0-9]+           #or just starting with floating point
      )
      ([eE][+-]?[0-9]+)? # optional exponent
        </Rule>

    </RuleSet>

</SyntaxDefinition>