使用AvalonEdit实现WPF的Lua编辑器
原文发布于:https://www.chenxublog.com/2019/07/14/use-avalonedit-make-wpf-lua-editor.html
由于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">
[?,.;()\[\]{}+\-/%*<>^+~!|&]+
</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看看就知道了嘛:

使用AvalonEdit实现WPF的Lua编辑器的更多相关文章
- 最好用的lua编辑器--------emmylua使用汇总
最好的lua编辑器Emmylua,欢迎打脸 官方文档 https://emmylua.github.io/zh_CN/ github https://github.com/EmmyLua ...
- 基于WPF&Prism&AvalonEdit的XAML轻量编辑器
1. 写在前面 一直从事WPF的相关开发工作,有时为了尝试或演示某些仅仅基于XAML的效果时,但又不想大动干戈打开VS去创建项目,所以一个轻便简单,集编辑与预览于一身的XAML编辑器就显得格外重要. ...
- Cocos2d-x游戏开发之lua编辑器 Sublime 搭建,集成cocos2dLuaApi和自有类
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wisdom605768292/article/details/34085969 Sublime Te ...
- debian上安装lua编辑器
Debian服务器上安装lua 1)下载压缩包 wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 2)解压文件 tar zxvf lua-5.1.4.tar. ...
- wpf 富文本编辑器richtextbox的简单用法
最近弄得一个小软件,需要用到富文本编辑器,richtextbox,一开始以为是和文本框一样的用法,但是实践起来碰壁之后才知道并不简单. richtextbox 类似于Word,是一个可编辑的控件.结构 ...
- WPF学习笔记(四):AvalonEdit 代码高亮编辑控件专题
AvalonEdit 是一个基于 WPF 的文本编辑器组件.它是由 Daniel Grunwald 为 SharpDevelop 编写的.从 5.0 版开始,AvalonEdit 根据MIT许可证发布 ...
- WPF开发时光之痕日记本(一)——富文本编辑器
本篇给大家推荐一个 WPF 版的富文本编辑器,SmithHtmlEditor,具体网址大家可以找一找,我在这个编辑器的基础上修改了界面,增加了一些功能,模仿了kindeditor 的界面,鉴于自己现在 ...
- Lua------------------改善Unity编辑器对Lua文件的支持
原创 2017年03月10日 18:44:22 标签: Unity / lua / 编辑器 952 当前版本的Unity(截至Unity5.5.x)中TextAsset类不支持后缀为lua的文件,将l ...
- .NET Core/.NET5/.NET6 开源项目汇总12:WPF组件库2
系列目录 [已更新最新开发文章,点击查看详细] WPF(Windows Presentation Foundation)是微软推出的基于Windows 的用户界面框架,属于.NET Frame ...
随机推荐
- 深入理解JVM,类加载器
虚拟机设计团队把类加载阶段中的“通过一个类的全限定名来获取描述此类的二进制字节流(即字节码)”这个动作放到Java虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个动作的代码模块称 ...
- Create an XAF Application 创建一个XAF应用程序
This topic describes how to use the Solution Wizard to create XAF applications and specify a connect ...
- 常见的Web源码泄露总结
常见的Web源码泄露总结 源码泄露方式分类 .hg源码泄露 漏洞成因: hg init 的时候会生成 .hg 漏洞利用: 工具: dvcs-ripper .git源码泄露 漏洞成因: 在运行git i ...
- Gradle使用的简单了解
Gradle 认识 参考博客:http://www.enjoytoday.cn/categorys/Gradle gradle是一个用于构建工程的工程配置脚本,它可以很便捷的帮助我们构建管理工程结构, ...
- Java低配版简单的随机点名系统
import java.util.*; public class Dome{ public static void addSname(String[] students){ Scanner sc = ...
- 元祖,range,字典,结构,fromkeys的使用
3.5.3 元祖 关键字 tuple tu=(1,2,3,'你好',True) print(tu) 元祖是有序,不可变数据,不能进行修改, 支持索引查看 存储一些比较重要的信息,在配置文件中使用 存放 ...
- 201871010116-祁英红《面向对象程序设计(java)》第二周学习总结
博文正文开头格式:(3分) 项目 内容 <面向对象程序设计(java)> https://home.cnblogs.com/u/nwnu-daizh/ 这个作业的要求在哪里 https:/ ...
- Pwn-level3
题目地址 https://dn.jarvisoj.com/challengefiles/level3.rar.2047525b05c499c9dd189ba212bba1f8 借鉴 https://w ...
- 鲜贝7.3--postman安装
Postman电脑客户端安装: Postman的安装非常简单,在windows系统只需要双击安装包,然后什么都不需要操作,它直接就自己完成了,如下图.如果是mac 也是跟普通软件的安装方法相同.在初次 ...
- 期望DP的一般思路
期望DP的一般思路 转载自_new2zy_ 期望\(dp\),也加概率\(dp\) 一般来说,期望\(dp\)找到正确的状态后,转移是比较容易想到的. 但一般情况下,状态一定是"可数&quo ...