使用AvalonEdit实现WPF的Lua编辑器
本文最后更新于 2050 天前,其中的信息可能已经有所发展或是发生改变。

由于LLCOM里面内置了Lua代码的编辑器,所以我就使用了AvalonEdit这个轮子,不过一开始的Lua语言支持让我一顿好找

不过好在找到了网上的资料,我就把整个实现过程贴在下面

准备

先去nuget安装一下AvalonEdit,以备后面使用:

接着把下面的文件内容,保存为Lua.xshd文件名的文件:

<?xml version="1.0"?>
<SyntaxDefinition name="SharpLua" extensions=".slua;.lua" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
  <!-- The named colors 'Comment' and 'String' are used in SharpDevelop to detect if a line is inside a multiline string/comment -->
  <Color name="Comment" foreground="#ff999999" exampleText="-- comment" />
  <Color name="String" foreground="#fff99157" />
  <Color name="Punctuation" />
  <Color name="MethodCall" foreground="#ffffcc66" fontWeight="bold"/>
  <Color name="NumberLiteral" foreground="#ff99cc99"/>
  <Color name="NilKeyword" fontWeight="bold"/>
  <Color name="Keywords" fontWeight="bold" foreground="#ff6699cc" />
  <Color name="GotoKeywords" foreground="#ffcc99cc" />
  <Color name="Visibility" fontWeight="bold" foreground="#fff99157"/>
  <Color name="TrueFalse" fontWeight="bold" foreground="#ff66cccc" />

  <RuleSet name="CommentMarkerSet">
    <Keywords fontWeight="bold" foreground="#fff2777a">
      <Word>TODO</Word>
      <Word>FIXME</Word>
    </Keywords>
    <Keywords fontWeight="bold" foreground="#fff2777a">
      <Word>HACK</Word>
      <Word>UNDONE</Word>
    </Keywords>
  </RuleSet>

  <!-- This is the main ruleset. -->
  <RuleSet>

    <Span color="Comment">
      <Begin color="XmlDoc/DocComment">---</Begin>
      <RuleSet>
        <Import ruleSet="XmlDoc/DocCommentSet"/>
        <Import ruleSet="CommentMarkerSet"/>
      </RuleSet>
    </Span>


    <Span color="Comment" ruleSet="CommentMarkerSet" multiline="true">
      <Begin>--\[[=]*\[</Begin>
      <End>\][=]*]</End>
    </Span>


    <Span color="Comment" ruleSet="CommentMarkerSet">
      <Begin>--</Begin>
    </Span>

    <Span color="String">
      <Begin>"</Begin>
      <End>"</End>
      <RuleSet>
        <!-- span for escape sequences -->
        <Span begin="\\" end="."/>
      </RuleSet>
    </Span>

    <Span color="String">
      <Begin>'</Begin>
      <End>'</End>
      <RuleSet>
        <!-- span for escape sequences -->
        <Span begin="\\" end="."/>
      </RuleSet>
    </Span>

    <Span color="String" multiline="true">
      <Begin color="String">\[[=]*\[</Begin>
      <End>\][=]*]</End>
    </Span>

    <Keywords color="TrueFalse">
      <Word>true</Word>
      <Word>false</Word>
    </Keywords>

    <Keywords color="Keywords">
      <Word>and</Word>
      <Word>break</Word>
      <Word>do</Word>
      <Word>else</Word>
      <Word>elseif</Word>
      <Word>end</Word>
      <Word>false</Word>
      <Word>for</Word>
      <Word>function</Word>
      <Word>if</Word>
      <Word>in</Word>
      <Word>local</Word>
      <!--<Word>nil</Word>-->
      <Word>not</Word>
      <Word>or</Word>
      <Word>repeat</Word>
      <Word>return</Word>
      <Word>then</Word>
      <Word>true</Word>
      <Word>until</Word>
      <Word>while</Word>
      <Word>using</Word>
      <Word>continue</Word>
    </Keywords>

    <Keywords color="GotoKeywords">
      <Word>break</Word>
      <Word>return</Word>
    </Keywords>

    <Keywords color="Visibility">
      <Word>local</Word>
    </Keywords>

    <Keywords color="NilKeyword">
      <Word>nil</Word>
    </Keywords>

    <!-- Mark previous rule-->
    <Rule color="MethodCall">
      \b
      [\d\w_]+  # an identifier
      (?=\s*\() # followed by (
    </Rule>
    <Rule color="MethodCall">
      \b
      [\d\w_]+  # an identifier
      (?=\s*\") # followed by "
    </Rule>
    <Rule color="MethodCall">
      \b
      [\d\w_]+  # an identifier
      (?=\s*\') # followed by '
    </Rule>
    <Rule color="MethodCall">
      \b
      [\d\w_]+  # an identifier
      (?=\s*\{) # followed by {
    </Rule>
    <Rule color="MethodCall">
      \b
      [\d\w_]+  # an identifier
      (?=\s*\[) # followed by [
    </Rule>

    <!-- Digits -->
    <Rule color="NumberLiteral">
      \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>

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

Lua.xshd放到解决方案资源管理器中,生成操作改为嵌入的资源

使用

xaml里的代码如下:

<avalonEdit:TextEditor
 Grid.Row="2"
 xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
 Name="textEditor"
 FontFamily="Consolas"
 FontSize="10pt"
 ShowLineNumbers="True"
 LostFocus="TextEditor_LostFocus"/>

然后在窗体的loaded事件中运行下面的代码即可:

//快速搜索功能
SearchPanel.Install(textEditor.TextArea);
//设置语法规则
string name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".Lua.xshd";
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
using (System.IO.Stream s = assembly.GetManifestResourceStream(name))
{
    using (XmlTextReader reader = new XmlTextReader(s))
    {
        var xshd = HighlightingLoader.LoadXshd(reader);
        textEditor.SyntaxHighlighting = HighlightingLoader.Load(xshd, HighlightingManager.Instance);
    }
}

效果

效果什么的。。。自己去下载一个LLCOM看看就知道了嘛:

评论

  1. 乙戌子
    2 年前
    2023-7-23 16:51:45
    Microsoft Edge 114.0.1823.79 Microsoft Edge 114.0.1823.79 Windows 10 x64 Edition Windows 10 x64 Edition

    请问这个SearchPanel是哪个

    • 博主
      乙戌子
      2 年前
      2023-7-25 10:15:59
      Firefox 115.0 Firefox 115.0 Windows 10 x64 Edition Windows 10 x64 Edition

      框架自带的吧,不用管

  2. 无聊啊
    11 月前
    2024-4-08 2:05:43
    Google Chrome 123.0.0.0 Google Chrome 123.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition

    其中的 SearchPanel.Install(textEditor.TextArea); 可能会报错
    换成完整的 ICSharpCode.AvalonEdit.Search.SearchPanel.Install(textEditor.TextArea); 就好了

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇