Files
FlaxEngine/Source/Editor/Modules/SourceCodeEditing/DefaultSourceCodeEditor.cs
2020-12-07 23:40:54 +01:00

109 lines
2.9 KiB
C#

// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
using FlaxEngine;
namespace FlaxEditor.Modules.SourceCodeEditing
{
/// <summary>
/// Default source code editor. Picks the best available editor on the current the platform.
/// </summary>
/// <seealso cref="FlaxEditor.Modules.SourceCodeEditing.ISourceCodeEditor" />
internal class DefaultSourceCodeEditor : ISourceCodeEditor
{
private ISourceCodeEditor _currentEditor;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultSourceCodeEditor"/> class.
/// </summary>
public DefaultSourceCodeEditor()
{
}
private void OnEditorAdded(ISourceCodeEditor editor)
{
if (editor == this)
return;
UpdateCurrentEditor();
}
private void OnEditorRemoved(ISourceCodeEditor editor)
{
if (editor != _currentEditor)
return;
UpdateCurrentEditor();
}
private void UpdateCurrentEditor()
{
var codeEditing = Editor.Instance.CodeEditing;
// Favor the newest Visual Studio
for (int i = (int)CodeEditorTypes.VS2019; i >= (int)CodeEditorTypes.VS2008; i--)
{
var visualStudio = codeEditing.GetInBuildEditor((CodeEditorTypes)i);
if (visualStudio != null)
{
_currentEditor = visualStudio;
return;
}
}
// Fallback default editor (always valid)
_currentEditor = codeEditing.GetInBuildEditor(CodeEditorTypes.SystemDefault);
}
/// <inheritdoc />
public string Name => "Default";
/// <inheritdoc />
public string GenerateProjectCustomArgs => null;
/// <inheritdoc />
public void OpenSolution()
{
_currentEditor?.OpenSolution();
}
/// <inheritdoc />
public void OpenFile(string path, int line)
{
_currentEditor?.OpenFile(path, line);
}
/// <inheritdoc />
public void OnFileAdded(string path)
{
}
/// <inheritdoc />
public void OnSelected(Editor editor)
{
}
/// <inheritdoc />
public void OnDeselected(Editor editor)
{
}
/// <inheritdoc />
public void OnAdded(Editor editor)
{
editor.CodeEditing.EditorAdded += OnEditorAdded;
editor.CodeEditing.EditorRemoved += OnEditorRemoved;
UpdateCurrentEditor();
}
/// <inheritdoc />
public void OnRemoved(Editor editor)
{
_currentEditor = null;
editor.CodeEditing.EditorAdded -= OnEditorAdded;
editor.CodeEditing.EditorRemoved -= OnEditorRemoved;
}
}
}