目录
一. AvalonEdit文本器
1.功能实现
2. 高亮
3. 代码提示
二. 运行效果展示
三. 源码链接
四. 参考资料
一. AvalonEdit文本器
1.功能实现
直接用Github上的源码进行实现,icsharpcode/AvalonEdit:The WPF-based text editor component used in SharpDevelop,源码地址见参考资料1,以下是文本器的使用方法
/// <summary>
/// 脚本显示初始化
/// </summary>
private void ScriptInit()
{
//文本编辑器定义
_editor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
_editor.FontSize = 12;
_editor.Completion = MyScriptProvider.Instance(new ScriptProvider(), ScriptProvider.GetRelativeAssemblies())._csharpCompletion;
_editor.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
_editor.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
if (!string.IsNullOrWhiteSpace(_moduleObj.InCode))
{
_editor.Text = _moduleObj.InCode;
}
string fileName = "TEST";
_editor.FileName = fileName;
_editor.Document.FileName = fileName;
elementHost.Child = _editor;
elementHost.Dock = DockStyle.Fill;
this.splitContainer4.Panel1.Controls.Add(elementHost);
//设置高亮
_editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
}
注意:
1)文本器CodeTextEditor是基于Control写的,如果是Form窗体需要用ElementHost容器包一下才能使用
2)关闭编译器窗体对象时,需要主动销毁CodeTextEditor和ElementHost,否则即使是托管内存,C#自动销毁特别缓慢,容易造成软件系统因为占用内存较大而卡顿(个人不建议定时器GC回收,原因是对高速项目,GC很容易引起系统短时间卡顿,而带来不可预知的问题,因此对占用较大的托管资源要主动销毁)
3)如果是Wpf的Window调用CodeTextEditor,如果在xaml语言里面写的,就不需要主动释放控件资源
主动释放控件资源代码:
private void button_Cancel_Click(object sender, EventArgs e)
{
//主动释放托管内存
_editor.Clear();
_editor = null;
elementHost.Controls.Clear();
elementHost.Dispose();
elementHost = null;
GC.Collect();
this.Close();
}
2. 高亮
editor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
3. 代码提示
源码中代码提示功能有限,需要结合NRefactory包,实现功能更强大的代码提示,具体参考代码见参考资料2,不过参考资料2中的代码提示,每次输入字符/数字都需要重新查询所有的依赖项内容,造成较大的资源开销,关键代码修改如下(这样,只有输入点.时才会重新查询方法项目)
/// <summary>
/// 文本器提示结果,当result为空时才获取结果
/// </summary>
CodeCompletionResult resultsWhole = null;
private void ShowCompletion(string enteredText, bool controlSpace)
{
if (!controlSpace)
Debug.WriteLine("Code Completion: TextEntered: " + enteredText);
else
Debug.WriteLine("Code Completion: Ctrl+Space");
only process csharp files and if there is a code completion engine available
//if (String.IsNullOrEmpty(Document.FileName)) {
// Debug.WriteLine("No document file name, cannot run code completion");
// return;
//}
if (Completion == null) {
Debug.WriteLine("Code completion is null, cannot run code completion");
return;
}
//var fileExtension = Path.GetExtension(Document.FileName);
//fileExtension = fileExtension != null ? fileExtension.ToLower() : null;
check file extension to be a c# file (.cs, .csx, etc.)
//if (fileExtension == null || (!fileExtension.StartsWith(".cs"))) {
// Debug.WriteLine("Wrong file extension, cannot run code completion");
// return;
//}
if (completionWindow == null) {
CodeCompletionResult results = null;
try {
var offset = 0;
var doc = GetCompletionDocument(out offset);
if (results == null || results.CompletionData.Count == 0 || enteredText == ".")
{
results = Completion.GetCompletions(doc, offset, controlSpace);
}
else
{
results = resultsWhole;
}
} catch (Exception exception) {
Debug.WriteLine("Error in getting completion: " + exception);
}
if (results == null)
return;
if (completionWindow == null && results != null && results.CompletionData.Any()) {
// Open code completion after the user has pressed dot:
completionWindow = new CompletionWindow(TextArea);
completionWindow.CloseWhenCaretAtBeginning = controlSpace;
completionWindow.StartOffset -= results.TriggerWordLength;
//completionWindow.EndOffset -= results.TriggerWordLength;
IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
foreach (var completion in results.CompletionData.OrderBy(item => item.Text)) {
data.Add(completion);
}
if (results.TriggerWordLength > 0) {
//completionWindow.CompletionList.IsFiltering = false;
completionWindow.CompletionList.SelectItem(results.TriggerWord);
}
completionWindow.Show();
completionWindow.Closed += (o, args) => completionWindow = null;
}
}//end if
}//end method
二. 运行效果展示
实现功能:
1. 编辑脚本内容
2. 编译脚本
3.运行脚本
三. 源码链接
CSDN资源
(9条消息) AvalonEdit文本器+NRefactory代码提示+Roslyn动态编译-C#文档类资源-CSDN文库
---------------------------------------------------------------------------------------------------------------------
四. 参考资料
动态编译器的两个github源码,可以学习下:
1. AvalonEdit源码:
GitHub - icsharpcode/AvalonEdit: The WPF-based text editor component used in SharpDevelop
2. AvalonEdit(源码的代码提示部分用NRefactory进行修改)+Roslyn实现动态编译器的demo
GitHub - lukebuehler/NRefactory-Completion-Sample: A small but full featured prototype how to do code completion with NRefactory