Skip to content

Commit

Permalink
Add comments and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Takoooooo committed Oct 29, 2023
1 parent bdf5152 commit f58b6d6
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 139 deletions.
2 changes: 2 additions & 0 deletions AvaloniaVS.Shared/AvaloniaVS.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Services\PreviewerProcess.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\SolutionService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Services\Throttle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SuggestedActions\Actions\Base\BaseSuggestedAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SuggestedActions\Actions\MissingAliasSuggestedAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SuggestedActions\Actions\MissingNamespaceAndAliasSuggestedAction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SuggestedActions\Actions\MissingNamespaceSuggestedAction.cs" />
Expand Down Expand Up @@ -101,5 +102,6 @@
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Commands\" />
<Folder Include="$(MSBuildThisFileDirectory)SuggestedActions\Actions\Base\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Language.Intellisense;

namespace AvaloniaVS.Shared.SuggestedActions.Actions.Base
{
internal class BaseSuggestedAction
{
public bool HasActionSets { get; }

public ImageMoniker IconMoniker { get; }

public string IconAutomationText { get; }

public string InputGestureText { get; }

public bool HasPreview => true;

public void Dispose()
{
}

public bool TryGetTelemetryId(out Guid telemetryId)
{
telemetryId = Guid.Empty;
return false;
}

public Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
{
return Task.FromResult<IEnumerable<SuggestedActionSet>>(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,85 +1,56 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AvaloniaVS.Shared.SuggestedActions.Actions.Base;
using AvaloniaVS.Shared.SuggestedActions.Helpers;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Differencing;
using Microsoft.VisualStudio.Text.Editor;

namespace AvaloniaVS.Shared.SuggestedActions.Actions
{
internal class MissingAliasSuggestedAction : ISuggestedAction
internal class MissingAliasSuggestedAction : BaseSuggestedAction, ISuggestedAction
{
private readonly ITrackingSpan _span;
private readonly ITextSnapshot _snapshot;
private readonly string _targetClassName;
private readonly KeyValuePair<string, string> _targetClassMetadata;
private readonly string _namespaceAlias;
private readonly IWpfDifferenceViewerFactoryService _diffFactory;
private readonly IDifferenceBufferFactoryService _diffBufferFactory;
private readonly ITextBufferFactoryService _bufferFactory;
private readonly IReadOnlyDictionary<string, string> _inverseNamespaces;
private readonly ITextViewRoleSet _previewRoleSet;

public MissingAliasSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerFactoryService diffFactory, IDifferenceBufferFactoryService diffBufferFactory, ITextBufferFactoryService bufferFactory, ITextEditorFactoryService textEditorFactoryService, IReadOnlyDictionary<string, string> inverseNamespaces)
{
_inverseNamespaces = inverseNamespaces;
_span = span;
_snapshot = span.TextBuffer.CurrentSnapshot;
_snapshot = _span.TextBuffer.CurrentSnapshot;
_targetClassName = _span.GetText(_snapshot);
_targetClassMetadata = _inverseNamespaces.FirstOrDefault(x => x.Key.Split('.').Last() == _targetClassName);
_namespaceAlias = _targetClassMetadata.Value.Split(':').Last().Split('.').Last();
var targetClassMetadata = inverseNamespaces.FirstOrDefault(x => x.Key.Split('.').Last() == _targetClassName);
_namespaceAlias = targetClassMetadata.Value.Split(':').Last().Split('.').Last();
_diffFactory = diffFactory;
_diffBufferFactory = diffBufferFactory;
_bufferFactory = bufferFactory;
_previewRoleSet = textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Analyzable);
DisplayText = $"Use {_namespaceAlias.ToLower()} ({_targetClassMetadata.Value})";
DisplayText = $"Use {_namespaceAlias.ToLower()} ({targetClassMetadata.Value})";
}

public bool HasActionSets { get; }

public string DisplayText { get; }

public ImageMoniker IconMoniker { get; }

public string IconAutomationText { get; }

public string InputGestureText { get; }

public bool HasPreview => true;

public void Dispose()
{
}

public Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
{
return Task.FromResult<IEnumerable<SuggestedActionSet>>(null);
}

public Task<object> GetPreviewAsync(CancellationToken cancellationToken)
{
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplyNamespaceSuggestion));
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplySuggestion));
}

private void ApplyNamespaceSuggestion(ITextBuffer buffer)
private void ApplySuggestion(ITextBuffer buffer)
{
buffer.Replace(_span.GetSpan(_snapshot), $"{_namespaceAlias.ToLower()}:{_targetClassName}");
}

public void Invoke(CancellationToken cancellationToken)
{
ApplyNamespaceSuggestion(_span.TextBuffer);
}

public bool TryGetTelemetryId(out Guid telemetryId)
{
telemetryId = Guid.Empty;
return false;
ApplySuggestion(_span.TextBuffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AvaloniaVS.Shared.SuggestedActions.Actions.Base;
using AvaloniaVS.Shared.SuggestedActions.Helpers;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Differencing;
using Microsoft.VisualStudio.Text.Editor;

namespace AvaloniaVS.Shared.SuggestedActions.Actions
{
internal class MissingNamespaceAndAliasSuggestedAction
: ISuggestedAction
internal class MissingNamespaceAndAliasSuggestedAction : BaseSuggestedAction, ISuggestedAction
{
private readonly ITrackingSpan _span;
private readonly ITextSnapshot _snapshot;
Expand All @@ -23,18 +21,22 @@ internal class MissingNamespaceAndAliasSuggestedAction
private readonly IWpfDifferenceViewerFactoryService _diffFactory;
private readonly IDifferenceBufferFactoryService _diffBufferFactory;
private readonly ITextBufferFactoryService _bufferFactory;
private readonly IReadOnlyDictionary<string, string> _inverseNamespaces;
private readonly Dictionary<string, string> _aliases;
private readonly ITextViewRoleSet _previewRoleSet;

public MissingNamespaceAndAliasSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerFactoryService diffFactory, IDifferenceBufferFactoryService diffBufferFactory, ITextBufferFactoryService bufferFactory, ITextEditorFactoryService textEditorFactoryService, IReadOnlyDictionary<string, string> inverseNamespaces,
Dictionary<string, string> aliases)
public MissingNamespaceAndAliasSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerFactoryService diffFactory,
IDifferenceBufferFactoryService diffBufferFactory, ITextBufferFactoryService bufferFactory, ITextEditorFactoryService textEditorFactoryService,
IReadOnlyDictionary<string, string> inverseNamespaces, Dictionary<string, string> aliases)
{
_span = span;
_inverseNamespaces = inverseNamespaces;
_snapshot = span.TextBuffer.CurrentSnapshot;
_snapshot = _span.TextBuffer.CurrentSnapshot;
_targetClassName = _span.GetText(_snapshot);
_targetClassMetadata = _inverseNamespaces.FirstOrDefault(x => x.Key.Split('.').Last() == _targetClassName);
_targetClassMetadata = inverseNamespaces.FirstOrDefault(x => x.Key.Split('.').Last() == _targetClassName);

// _targetClassMetadata.Value is the namespace of the control we are trying to add the namespace to.
// It is usually in the format using:MyNamespace.Something.
// So to get the prefix for the control we are splitting it by ':'
// Then taking the MyNamespace.Something part and splitting it by '.' and getting Something.
_namespaceAlias = _targetClassMetadata.Value.Split(':').Last().Split('.').Last();
DisplayText = $"Add xmlns {_namespaceAlias}";
_diffFactory = diffFactory;
Expand All @@ -46,28 +48,10 @@ public MissingNamespaceAndAliasSuggestedAction(ITrackingSpan span, IWpfDifferenc

public string DisplayText { get; }

public string IconAutomationText { get; }

ImageMoniker ISuggestedAction.IconMoniker { get; }

public string InputGestureText { get; }

public bool HasActionSets { get; }

public Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
{
return Task.FromResult<IEnumerable<SuggestedActionSet>>(null);
}

public bool HasPreview => true;

public Task<object> GetPreviewAsync(CancellationToken cancellationToken)
{
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplyNamespaceSuggestion));
}

public void Dispose()
{
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplySuggestion));
}

public void Invoke(CancellationToken cancellationToken)
Expand All @@ -76,20 +60,17 @@ public void Invoke(CancellationToken cancellationToken)
{
return;
}
ApplyNamespaceSuggestion(_span.TextBuffer);
ApplySuggestion(_span.TextBuffer);
}

public bool TryGetTelemetryId(out Guid telemetryId)
{
telemetryId = Guid.Empty;
return false;
}

private void ApplyNamespaceSuggestion(ITextBuffer buffer)
private void ApplySuggestion(ITextBuffer buffer)
{
var lastNs = _aliases.Last().Value;

buffer.Replace(_span.GetSpan(_snapshot), $"{_namespaceAlias.ToLower()}:{_targetClassName}");

// We get the index of the last namespace in the list and add the last namespace length without quotes and add 2.
// One for qutation mark and one to place the new namespace in an empty space.
buffer.Insert(buffer.CurrentSnapshot.GetText().IndexOf(lastNs) + lastNs.Length + 2, $"xmlns:{_namespaceAlias.ToLower()}=\"{_targetClassMetadata.Value}\"");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using AvaloniaVS.Shared.SuggestedActions.Actions.Base;
using AvaloniaVS.Shared.SuggestedActions.Helpers;
using Microsoft.VisualStudio.Imaging.Interop;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Differencing;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text;
using System.Threading.Tasks;
using System.Threading;

namespace AvaloniaVS.Shared.SuggestedActions.Actions
{
internal class MissingNamespaceSuggestedAction : ISuggestedAction
internal class MissingNamespaceSuggestedAction : BaseSuggestedAction, ISuggestedAction
{
private readonly ITrackingSpan _span;
private readonly ITextSnapshot _snapshot;
private readonly string _targetClassName;
private readonly KeyValuePair<string, string> _targetClassMetadata;
private readonly IWpfDifferenceViewerFactoryService _diffFactory;
private readonly IDifferenceBufferFactoryService _diffBufferFactory;
private readonly ITextBufferFactoryService _bufferFactory;
private readonly IReadOnlyDictionary<string, string> _inverseNamespaces;
private readonly Dictionary<string, string> _aliases;
private readonly string _alias;
private readonly ITextViewRoleSet _previewRoleSet;

public MissingNamespaceSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerFactoryService diffFactory, IDifferenceBufferFactoryService diffBufferFactory, ITextBufferFactoryService bufferFactory, ITextEditorFactoryService textEditorFactoryService, IReadOnlyDictionary<string, string> inverseNamespaces,
public MissingNamespaceSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerFactoryService diffFactory, IDifferenceBufferFactoryService diffBufferFactory,
ITextBufferFactoryService bufferFactory, ITextEditorFactoryService textEditorFactoryService, IReadOnlyDictionary<string, string> inverseNamespaces,
Dictionary<string, string> aliases, string alias)
{
_span = span;
_inverseNamespaces = inverseNamespaces;
_snapshot = span.TextBuffer.CurrentSnapshot;
_targetClassName = _span.GetText(_snapshot);
_targetClassMetadata = _inverseNamespaces.FirstOrDefault(x => x.Key.Split('.').Last() == _targetClassName);
_targetClassMetadata = inverseNamespaces.FirstOrDefault(x => x.Key.Split('.').Last() == _span.GetText(_span.TextBuffer.CurrentSnapshot));
DisplayText = $"Add xmlns {alias}";
_diffFactory = diffFactory;
_diffBufferFactory = diffBufferFactory;
Expand All @@ -46,28 +39,9 @@ public MissingNamespaceSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerF

public string DisplayText { get; }

public string IconAutomationText { get; }

ImageMoniker ISuggestedAction.IconMoniker { get; }

public string InputGestureText { get; }

public bool HasActionSets { get; }

public Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
{
return Task.FromResult<IEnumerable<SuggestedActionSet>>(null);
}

public bool HasPreview => true;

public Task<object> GetPreviewAsync(CancellationToken cancellationToken)
{
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplyNamespaceSuggestion));
}

public void Dispose()
{
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplySuggestion));
}

public void Invoke(CancellationToken cancellationToken)
Expand All @@ -76,18 +50,13 @@ public void Invoke(CancellationToken cancellationToken)
{
return;
}
ApplyNamespaceSuggestion(_span.TextBuffer);
ApplySuggestion(_span.TextBuffer);
}

public bool TryGetTelemetryId(out Guid telemetryId)
{
telemetryId = Guid.Empty;
return false;
}

private void ApplyNamespaceSuggestion(ITextBuffer buffer)
private void ApplySuggestion(ITextBuffer buffer)
{
var lastNs = _aliases.Last().Value;

buffer.Insert(buffer.CurrentSnapshot.GetText().IndexOf(lastNs) + lastNs.Length + 2, $"xmlns:{_alias}=\"{_targetClassMetadata.Value}\"");
}

Expand Down
Loading

0 comments on commit f58b6d6

Please sign in to comment.