📄 texteditordisplaybinding.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Kr黦er" email="mike@icsharpcode.net"/>
// <version value="$version"/>
// </file>
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using SharpDevelop.Internal.Parser;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Gui.Pads;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.SharpDevelop.Internal.Project;
using ICSharpCode.SharpDevelop.Internal.Undo;
using ICSharpCode.Core.Properties;
using ICSharpCode.Core.AddIns;
using ICSharpCode.Core.Services;
using ICSharpCode.SharpDevelop.Services;
using ICSharpCode.Core.AddIns.Codons;
namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor
{
public class TextEditorDisplayBinding : IDisplayBinding
{
// load #D-specific syntax highlighting files here
// don't know if this could be solved better by new codons,
// but this will do
static TextEditorDisplayBinding()
{
PropertyService propertyService = (PropertyService)ServiceManager.Services.GetService(typeof(PropertyService));
string modeDir = Path.Combine(propertyService.ConfigDirectory, "modes");
if (!Directory.Exists(modeDir)) {
Directory.CreateDirectory(modeDir);
}
HighlightingManager.Manager.AddSyntaxModeFileProvider(new FileSyntaxModeProvider(modeDir));
HighlightingManager.Manager.AddSyntaxModeFileProvider(new FileSyntaxModeProvider(Path.Combine(propertyService.DataDirectory, "modes")));
}
public virtual bool CanCreateContentForFile(string fileName)
{
return true;
}
public virtual bool CanCreateContentForLanguage(string language)
{
return true;
}
public virtual IViewContent CreateContentForFile(string fileName)
{
TextEditorDisplayBindingWrapper b2 = new TextEditorDisplayBindingWrapper();
b2.textAreaControl.Dock = DockStyle.Fill;
b2.Load(fileName);
// b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategyForFile(fileName);
b2.textAreaControl.InitializeFormatter();
b2.ForceFoldingUpdate(null);
b2.textAreaControl.ActivateQuickClassBrowserOnDemand();
return b2;
}
public virtual IViewContent CreateContentForLanguage(string language, string content)
{
TextEditorDisplayBindingWrapper b2 = new TextEditorDisplayBindingWrapper();
StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
b2.textAreaControl.Document.TextContent = stringParserService.Parse(content);
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(language);
b2.textAreaControl.InitializeFormatter();
b2.ForceFoldingUpdate(language);
b2.textAreaControl.ActivateQuickClassBrowserOnDemand();
return b2;
}
}
public class TextEditorDisplayBindingWrapper : AbstractViewContent, IMementoCapable, IPrintable, IEditable, IPositionable, ITextEditorControlProvider, IParseInformationListener, IClipboardHandler, IHelpProvider
{
public SharpDevelopTextAreaControl textAreaControl = null;
public TextEditorControl TextEditorControl {
get {
return textAreaControl;
}
}
// KSL Start, New lines
FileSystemWatcher watcher;
bool wasChangedExternally = false;
// KSL End
public bool EnableUndo {
get {
return textAreaControl.EnableUndo;
}
}
public bool EnableRedo {
get {
return textAreaControl.EnableRedo;
}
}
public string Text {
get {
return textAreaControl.Document.TextContent;
}
set {
textAreaControl.Document.TextContent = value;
}
}
public PrintDocument PrintDocument {
get {
return textAreaControl.PrintDocument;
}
}
public IClipboardHandler ClipboardHandler {
get {
return this;
}
}
public override Control Control {
get {
return textAreaControl;
}
}
public override string TabPageText {
get {
return "${res:FormsDesigner.DesignTabPages.SourceTabPage}";
}
}
public override string UntitledName {
get {
return base.UntitledName;
}
set {
base.UntitledName = value;
textAreaControl.FileName = value;
}
}
protected override void OnFileNameChanged(System.EventArgs e)
{
base.OnFileNameChanged(e);
textAreaControl.FileName = base.FileName;
}
public void Undo()
{
this.textAreaControl.Undo();
}
public void Redo()
{
this.textAreaControl.Redo();
}
protected virtual SharpDevelopTextAreaControl CreateSharpDevelopTextAreaControl()
{
return new SharpDevelopTextAreaControl();
}
public TextEditorDisplayBindingWrapper()
{
textAreaControl = CreateSharpDevelopTextAreaControl();
textAreaControl.Document.DocumentChanged += new DocumentEventHandler(TextAreaChangedEvent);
textAreaControl.ActiveTextAreaControl.Caret.CaretModeChanged += new EventHandler(CaretModeChanged);
textAreaControl.ActiveTextAreaControl.Enter += new EventHandler(CaretUpdate);
// KSL Start, New lines
// textAreaControl.FileNameChanged += new EventHandler(FileNameChangedEvent);
((Form)ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench).Activated += new EventHandler(GotFocusEvent);
// KSL End
}
// KSL Start, new event handlers
#region ICSharpCode.SharpDevelop.Gui.IHelpProvider interface implementation
public void ShowHelp()
{
string word = TextUtilities.GetWordAt(textAreaControl.Document, textAreaControl.ActiveTextAreaControl.Caret.Offset);
HelpBrowser helpBrowser = (HelpBrowser)WorkbenchSingleton.Workbench.GetPad(typeof(HelpBrowser));
IClass c = textAreaControl.QuickClassBrowserPanel != null ? textAreaControl.QuickClassBrowserPanel.GetCurrentSelectedClass() : null;
IParserService parserService = (IParserService)ICSharpCode.Core.Services.ServiceManager.Services.GetService(typeof(IParserService));
if (c != null) {
IClass cl = parserService.SearchType(word, c, textAreaControl.ActiveTextAreaControl.Caret.Line, textAreaControl.ActiveTextAreaControl.Caret.Column);
if (cl != null) {
helpBrowser.ShowHelpFromType(cl.FullyQualifiedName);
return;
}
}
}
#endregion
void SetWatcher()
{
try {
if (this.watcher == null) {
this.watcher = new FileSystemWatcher();
this.watcher.Changed += new FileSystemEventHandler(this.OnFileChangedEvent);
} else {
this.watcher.EnableRaisingEvents = false;
}
this.watcher.Path = Path.GetDirectoryName(textAreaControl.FileName);
this.watcher.Filter = Path.GetFileName(textAreaControl.FileName);
this.watcher.NotifyFilter = NotifyFilters.LastWrite;
this.watcher.EnableRaisingEvents = true;
} catch (Exception) {
watcher = null;
}
}
void GotFocusEvent(object sender, EventArgs e)
{
lock (this) {
if (wasChangedExternally) {
wasChangedExternally = false;
StringParserService stringParserService = (StringParserService)ServiceManager.Services.GetService(typeof(StringParserService));
string message = stringParserService.Parse("${res:ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.TextEditorDisplayBinding.FileAlteredMessage}", new string[,] {{"File", Path.GetFullPath(textAreaControl.FileName)}});
if (MessageBox.Show(message,
stringParserService.Parse("${res:MainWindow.DialogName}"),
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes) {
Load(textAreaControl.FileName);
} else {
IsDirty = true;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -