-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #408 from AvaloniaUI/namespace-suggestions
Add support for Namespace suggestions
- Loading branch information
Showing
14 changed files
with
557 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.ComponentModel.Composition; | ||
using Avalonia.Ide.CompletionEngine; | ||
|
||
namespace AvaloniaVS.Shared.IntelliSense | ||
{ | ||
[Export] | ||
public class CompletionEngineSource | ||
{ | ||
public CompletionEngineSource() | ||
{ | ||
CompletionEngine = new CompletionEngine(); | ||
} | ||
public CompletionEngine CompletionEngine { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
AvaloniaVS.Shared/SuggestedActions/Actions/Base/BaseSuggestedAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
AvaloniaVS.Shared/SuggestedActions/Actions/MissingAliasSuggestedAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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.Language.Intellisense; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Differencing; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
|
||
namespace AvaloniaVS.Shared.SuggestedActions.Actions | ||
{ | ||
internal class MissingAliasSuggestedAction : BaseSuggestedAction, ISuggestedAction | ||
{ | ||
private readonly ITrackingSpan _span; | ||
private readonly ITextSnapshot _snapshot; | ||
private readonly string _targetClassName; | ||
private readonly string _namespaceAlias; | ||
private readonly IWpfDifferenceViewerFactoryService _diffFactory; | ||
private readonly IDifferenceBufferFactoryService _diffBufferFactory; | ||
private readonly ITextBufferFactoryService _bufferFactory; | ||
private readonly ITextViewRoleSet _previewRoleSet; | ||
|
||
public MissingAliasSuggestedAction(ITrackingSpan span, IWpfDifferenceViewerFactoryService diffFactory, IDifferenceBufferFactoryService diffBufferFactory, ITextBufferFactoryService bufferFactory, ITextEditorFactoryService textEditorFactoryService, IReadOnlyDictionary<string, string> inverseNamespaces) | ||
{ | ||
_span = span; | ||
_snapshot = _span.TextBuffer.CurrentSnapshot; | ||
_targetClassName = _span.GetText(_snapshot); | ||
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})"; | ||
} | ||
|
||
public string DisplayText { get; } | ||
|
||
public Task<object> GetPreviewAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplySuggestion)); | ||
} | ||
|
||
private void ApplySuggestion(ITextBuffer buffer) | ||
{ | ||
buffer.Replace(_span.GetSpan(_snapshot), $"{_namespaceAlias.ToLower()}:{_targetClassName}"); | ||
} | ||
|
||
public void Invoke(CancellationToken cancellationToken) | ||
{ | ||
ApplySuggestion(_span.TextBuffer); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
AvaloniaVS.Shared/SuggestedActions/Actions/MissingNamespaceAndAliasSuggestedAction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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.Language.Intellisense; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Differencing; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
|
||
namespace AvaloniaVS.Shared.SuggestedActions.Actions | ||
{ | ||
internal class MissingNamespaceAndAliasSuggestedAction : BaseSuggestedAction, ISuggestedAction | ||
{ | ||
private readonly ITrackingSpan _span; | ||
private readonly ITextSnapshot _snapshot; | ||
private readonly string _namespaceAlias; | ||
private readonly string _targetClassName; | ||
private readonly KeyValuePair<string, string> _targetClassMetadata; | ||
private readonly IWpfDifferenceViewerFactoryService _diffFactory; | ||
private readonly IDifferenceBufferFactoryService _diffBufferFactory; | ||
private readonly ITextBufferFactoryService _bufferFactory; | ||
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) | ||
{ | ||
_span = span; | ||
_snapshot = _span.TextBuffer.CurrentSnapshot; | ||
_targetClassName = _span.GetText(_snapshot); | ||
_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; | ||
_diffBufferFactory = diffBufferFactory; | ||
_bufferFactory = bufferFactory; | ||
_aliases = aliases; | ||
_previewRoleSet = textEditorFactoryService.CreateTextViewRoleSet(PredefinedTextViewRoles.Analyzable); | ||
} | ||
|
||
public string DisplayText { get; } | ||
|
||
|
||
public Task<object> GetPreviewAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult<object>(PreviewProvider.GetPreview(_bufferFactory, _span, _diffBufferFactory, _diffFactory, _previewRoleSet, ApplySuggestion)); | ||
} | ||
|
||
public void Invoke(CancellationToken cancellationToken) | ||
{ | ||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
return; | ||
} | ||
ApplySuggestion(_span.TextBuffer); | ||
} | ||
|
||
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}\""); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.