Skip to content

Commit

Permalink
Progress on splitting up EditorUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Jul 7, 2021
1 parent 427c41c commit 39c049e
Show file tree
Hide file tree
Showing 30 changed files with 157 additions and 739 deletions.
5 changes: 1 addition & 4 deletions Src/VimApp/VimComponentHost.cs
Expand Up @@ -6,7 +6,6 @@
using Microsoft.VisualStudio.Text;
using Vim;
using Vim.UI.Wpf;
using Vim.UnitTest.Exports;

namespace VimApp
{
Expand All @@ -32,10 +31,8 @@ internal IVim Vim

internal VimComponentHost()
{
var editorHostFactory = new VimEditorHostFactory(includeSelf: false, includeWpf: false);
var editorHostFactory = new VimEditorHostFactory();

editorHostFactory.Add(new AssemblyCatalog(typeof(IVim).Assembly));
editorHostFactory.Add(new AssemblyCatalog(typeof(VimComponentHost).Assembly));
_editorHost = editorHostFactory.CreateVimEditorHost();
_vim = _editorHost.CompositionContainer.GetExportedValue<IVim>();
}
Expand Down
2 changes: 1 addition & 1 deletion Src/VimCore/CoreInterfaces.fs
Expand Up @@ -1913,7 +1913,7 @@ type BlockSpan =
BlockSpan(x.VirtualStart, x.TabStop, x.SpacesLength, x.Height, endOfLine)

override x.ToString() =
sprintf "Point: %s Spaces: %d Height: %d EndOfLine: %b" (x.Start.ToString()) x._spaces x._height x._endOfLine
sprintf "Line Position: (%d, %d) Spaces: %d Height: %d EndOfLine: %b" (x.Start.LineNumber) (x.Start.Offset) x._spaces x._height x._endOfLine

static member op_Equality(this,other) = System.Collections.Generic.EqualityComparer<BlockSpan>.Default.Equals(this,other)
static member op_Inequality(this,other) = not (System.Collections.Generic.EqualityComparer<BlockSpan>.Default.Equals(this,other))
Expand Down
10 changes: 8 additions & 2 deletions Src/VimCore/EditorUtil.fs
Expand Up @@ -439,7 +439,10 @@ type SnapshotCodePoint =

/// Debugger display
override x.ToString() =
sprintf "CodePoint: %s Text: %s Line: %d Offset: %d" (x.CodePointText) (x.GetText()) (x.Line.LineNumber) (x.Offset)
if x.Length = 1 then
sprintf "Character: %s Position: %d" (x.CharacterText) (x.StartPosition)
else
sprintf "Character: %s Position: [%d, %d)" (x.CharacterText) (x.StartPosition) (x.EndPosition)

override x.GetHashCode() =
HashUtil.Combine2 x.Line.LineNumber x.Offset
Expand Down Expand Up @@ -1297,7 +1300,10 @@ type SnapshotOverlapColumnSpan =
new (startColumn: SnapshotOverlapColumn, endColumn: SnapshotOverlapColumn, tabStop: int) =
let startColumn = startColumn.WithTabStop tabStop
let endColumn = endColumn.WithTabStop tabStop
if startColumn.Column.StartPosition + startColumn.SpacesBefore > endColumn.Column.StartPosition + endColumn.SpacesBefore then
if startColumn.Column = endColumn.Column then
if startColumn.Column.StartPosition + startColumn.SpacesBefore > endColumn.Column.StartPosition + endColumn.SpacesBefore then
invalidArg "endColumn" "End cannot be before the start"
if startColumn.Column.StartPosition > endColumn.Column.StartPosition then
invalidArg "endColumn" "End cannot be before the start"
{ _start = startColumn; _end = endColumn }

Expand Down
Expand Up @@ -8,15 +8,13 @@
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;

namespace Vim.UnitTest.Exports
namespace Vim.EditorHost.Implementation.Misc
{
// https://github.com/VsVim/VsVim/issues/2905
// All of the exports here should move to VimEditorHost
[Export(typeof(ITaggerProvider))]
[ContentType("any")]
[TextViewRole(PredefinedTextViewRoles.Structured)]
[TagType(typeof(OutliningRegionTag))]
public sealed class OutlinerTaggerProvider : ITaggerProvider
internal sealed class OutlinerTaggerProvider : ITaggerProvider
{
ITagger<T> ITaggerProvider.CreateTagger<T>(ITextBuffer textBuffer)
{
Expand Down
@@ -1,6 +1,7 @@
using System.ComponentModel.Composition;
#if VS_UNIT_TEST_HOST
using System.ComponentModel.Composition;

namespace Vim.UnitTest.Exports
namespace Vim.UnitTest
{
[Export(typeof(IClipboardDevice))]
public sealed class TestableClipboardDevice : IClipboardDevice
Expand All @@ -9,3 +10,4 @@ public sealed class TestableClipboardDevice : IClipboardDevice
public string Text { get; set; }
}
}
#endif
@@ -1,6 +1,7 @@
using System.ComponentModel.Composition;
#if VS_UNIT_TEST_HOST
using System.ComponentModel.Composition;

namespace Vim.UnitTest.Exports
namespace Vim.UnitTest
{
[Export(typeof(IKeyboardDevice))]
public sealed class TestableKeyboardDevice : IKeyboardDevice
Expand All @@ -16,3 +17,4 @@ public VimKeyModifiers KeyModifiers
}
}
}
#endif
@@ -1,11 +1,12 @@
using Microsoft.FSharp.Core;
#if VS_UNIT_TEST_HOST
using Microsoft.FSharp.Core;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using System.ComponentModel.Composition;
using System.Windows;
using Vim;

namespace Vim.UnitTest.Exports
namespace Vim.UnitTest
{
[Export(typeof(IMouseDevice))]
public sealed class TestableMouseDevice : IMouseDevice
Expand Down Expand Up @@ -55,3 +56,4 @@ public bool InDragOperation(ITextView textView)
}
}
}
#endif
@@ -1,18 +1,20 @@
using System;
#if VS_UNIT_TEST_HOST
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Vim.UnitTest.Mock;

namespace Vim.UnitTest.Exports
namespace Vim.UnitTest
{
[Export(typeof(IVimHost))]
public sealed class VimHost : MockVimHost
public sealed class TestableVimHost : MockVimHost
{
[ImportingConstructor]
public VimHost()
public TestableVimHost()
{
}
}
}
#endif
16 changes: 1 addition & 15 deletions Src/VimEditorHost/UnitTests/VimTestBase.cs
Expand Up @@ -12,7 +12,7 @@
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Operations;
using Vim.Interpreter;
using Vim.UnitTest.Exports;
using Vim.UnitTest;
using Vim.UnitTest.Mock;
using System.Windows;
using Microsoft.VisualStudio.Text.Projection;
Expand Down Expand Up @@ -552,20 +552,6 @@ private static VimEditorHost GetOrCreateVimEditorHost()
if (!s_cachedVimEditorHostMap.TryGetValue(key, out VimEditorHost host))
{
var editorHostFactory = new VimEditorHostFactory();

// Other Exports needed to construct VsVim
var types = new List<Type>()
{
typeof(TestableClipboardDevice),
typeof(TestableKeyboardDevice),
typeof(TestableMouseDevice),
typeof(global::Vim.UnitTest.Exports.VimHost),
typeof(AlternateKeyUtil),
typeof(OutlinerTaggerProvider)
};

editorHostFactory.Add(new TypeCatalog(types));

var compositionContainer = editorHostFactory.CreateCompositionContainer();
host = new VimEditorHost(compositionContainer);
s_cachedVimEditorHostMap[key] = host;
Expand Down
7 changes: 6 additions & 1 deletion Src/VimEditorHost/VimEditorHost.projitems
Expand Up @@ -9,6 +9,11 @@
<Import_RootNamespace>VimEditorHost</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Misc\OutlinerTaggerProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UnitTests\TestableClipboardDevice.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UnitTests\TestableKeyboardDevice.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UnitTests\TestableMouseDevice.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UnitTests\TestableVimHost.cs" />
<Compile Include="$(MSBuildThisFileDirectory)KeyboardInputSimulation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VimEditorHostFactory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VimEditorHostFactory.JoinableTaskContextExportProvider.cs" />
Expand All @@ -25,7 +30,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Implementation\Misc\VimErrorDetector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IVimErrorDetector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VimEditorHost.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VimTestBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)UnitTests\VimTestBase.cs" />
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)README.md" />
Expand Down
73 changes: 33 additions & 40 deletions Src/VimEditorHost/VimEditorHostFactory.cs
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Vim.UI.Wpf;

namespace Vim.EditorHost
{
Expand Down Expand Up @@ -38,9 +39,9 @@ public sealed partial class VimEditorHostFactory
private readonly List<ComposablePartCatalog> _composablePartCatalogList = new List<ComposablePartCatalog>();
private readonly List<ExportProvider> _exportProviderList = new List<ExportProvider>();

public VimEditorHostFactory(bool includeSelf = true, bool includeWpf = true)
public VimEditorHostFactory(Func<Type, bool> typeFilter = null)
{
BuildCatalog(includeSelf, includeWpf);
BuildCatalog(typeFilter ?? (_ => true));
}

public void Add(ComposablePartCatalog composablePartCatalog)
Expand Down Expand Up @@ -90,8 +91,23 @@ public VimEditorHost CreateVimEditorHost()
return new VimEditorHost(CreateCompositionContainer());
}

private void BuildCatalog(bool includeSelf, bool includeWpf)
private void BuildCatalog(Func<Type, bool> typeFilter)
{
#if VS_UNIT_TEST_HOST
// The unit test host exports several replacement components to facilitate
// better testing. For example it exports TestableClipboard which doesn't use
// the real clipboard (would make testing flaky). Have to exclude the real components
// here to avoid export conflicts
Func<Type, bool> unitTestTypeFilter = type =>
type != typeof(Vim.UI.Wpf.Implementation.Misc.ClipboardDevice) &&
type != typeof(Vim.UI.Wpf.Implementation.Misc.KeyboardDeviceImpl) &&
type != typeof(Vim.UI.Wpf.Implementation.Misc.MouseDeviceImpl) &&
type.FullName != "Vim.VisualStudio.VsVimHost";
var originalTypeFilter = typeFilter;

typeFilter = type => originalTypeFilter(type) && unitTestTypeFilter(type);
#endif

// https://github.com/VsVim/VsVim/issues/2905
// Once VimEditorUtils is broken up correctly the composition code here should be
// reconsidered: particularly all of the ad-hoc exports below. Really need to move
Expand All @@ -106,49 +122,26 @@ private void BuildCatalog(bool includeSelf, bool includeWpf)
AppendEditorAssembly("Microsoft.VisualStudio.Threading", VisualStudioThreadingVersion);
_exportProviderList.Add(new JoinableTaskContextExportProvider());

if (includeSelf)
{
// Other Exports needed to construct VsVim
var types = new List<Type>()
{
typeof(Implementation.BasicUndo.BasicTextUndoHistoryRegistry),
typeof(Implementation.Misc.VimErrorDetector),
#if VS_SPECIFIC_2019
typeof(Implementation.Misc.BasicExperimentationServiceInternal),
typeof(Implementation.Misc.BasicLoggingServiceInternal),
typeof(Implementation.Misc.BasicObscuringTipManager),
#elif VS_SPECIFIC_2017
typeof(Implementation.Misc.BasicLoggingServiceInternal),
typeof(Implementation.Misc.BasicObscuringTipManager),
#else
#error Unsupported configuration
#endif

};
AddAssembly(typeof(IVim).Assembly);

_composablePartCatalogList.Add(new TypeCatalog(types));
}
var wpfAssembly = typeof(IBlockCaret).Assembly;
AddAssembly(wpfAssembly);

if (includeWpf)
var hostAssembly = typeof(VimEditorHostFactory).Assembly;
if (hostAssembly != wpfAssembly)
{
var types = new List<Type>()
{
#if VS_SPECIFIC_2019
typeof(Vim.UI.Wpf.Implementation.WordCompletion.Async.WordAsyncCompletionSourceProvider),
#elif !VS_SPECIFIC_MAC
typeof(Vim.UI.Wpf.Implementation.WordCompletion.Legacy.WordLegacyCompletionPresenterProvider),
#endif
typeof(Vim.UI.Wpf.Implementation.WordCompletion.Legacy.WordLegacyCompletionSourceProvider),
typeof(Vim.UI.Wpf.Implementation.WordCompletion.VimWordCompletionUtil),
#if VS_SPECIFIC_2017
#else
typeof(Vim.UI.Wpf.Implementation.MultiSelection.MultiSelectionUtilFactory),
#endif
};
AddAssembly(hostAssembly);
}

void AddAssembly(Assembly assembly)
{
var types = assembly
.GetTypes()
.Where(typeFilter)
.OrderBy(x => x.Name)
.ToList();
_composablePartCatalogList.Add(new TypeCatalog(types));
}

}

private void AppendEditorAssemblies(Version editorAssemblyVersion)
Expand Down

0 comments on commit 39c049e

Please sign in to comment.