Merlin32Language/Intellisense/CompletionSource.cs

105 lines
4.2 KiB
C#
Raw Permalink Normal View History

2016-12-31 00:46:52 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Utilities;
namespace VSMerlin32
{
[Export(typeof(ICompletionSourceProvider))]
[ContentType("Merlin32")]
[Name("Merlin32Completion")]
internal class Merlin32CompletionSourceProvider : ICompletionSourceProvider
{
[Import]
internal ITextStructureNavigatorSelectorService NavigatorService { get; set; }
public ICompletionSource TryCreateCompletionSource(ITextBuffer textBuffer)
{
return new Merlin32CompletionSource(this, textBuffer);
}
}
internal class Merlin32CompletionSource : ICompletionSource
{
2017-01-15 22:44:43 +00:00
private Merlin32CompletionSourceProvider _sourceprovider;
private ITextBuffer _buffer;
private List<Completion> _compList;
private bool _isDisposed;
2016-12-31 00:46:52 +00:00
public Merlin32CompletionSource(Merlin32CompletionSourceProvider sourceprovider, ITextBuffer buffer)
{
2017-01-15 22:44:43 +00:00
_sourceprovider = sourceprovider;
_buffer = buffer;
2016-12-31 00:46:52 +00:00
}
public void AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets)
{
2017-01-15 22:44:43 +00:00
if (_isDisposed)
2016-12-31 00:46:52 +00:00
throw new ObjectDisposedException("Merlin32CompletionSource");
List<string> strList = new List<string>();
char chTyped = ((session.TextView.Caret.Position.BufferPosition) - 1).GetChar();
// Testing for single and double quotes because these will be autocompleted...
if ((chTyped == '\'') || (chTyped == '"'))
{
strList.Add(chTyped.ToString() + chTyped.ToString());
}
else
{
// If the user has been typing lowercase, we'll present a lowercase list of keywords/opcodes...
foreach (Merlin32Opcodes token in Enum.GetValues(typeof(Merlin32Opcodes)))
2016-12-31 00:46:52 +00:00
{
strList.Add(char.IsLower(chTyped)? token.ToString().ToLower() : token.ToString());
2016-12-31 00:46:52 +00:00
}
foreach (Merlin32Directives token in Enum.GetValues(typeof(Merlin32Directives)))
2016-12-31 00:46:52 +00:00
{
if ((token.ToString().ToLower() == Merlin32Directives.ELUP.ToString().ToLower()) || (token.ToString() == Merlin32Directives.ELUP.ToString()))
2016-12-31 00:46:52 +00:00
{
strList.Add(Resources.directives.ELUPValue);
2016-12-31 00:46:52 +00:00
}
else
2016-12-31 00:46:52 +00:00
{
strList.Add(char.IsLower(chTyped)? token.ToString().ToLower() : token.ToString());
2016-12-31 00:46:52 +00:00
}
}
foreach (Merlin32DataDefines token in Enum.GetValues(typeof(Merlin32DataDefines)))
{
strList.Add(char.IsLower(chTyped)? token.ToString().ToLower() : token.ToString());
}
2016-12-31 00:46:52 +00:00
// OG We also need to replace "ELUP" with "--^"
// OG 2015/10/21
strList.Sort();
// strList[strList.IndexOf(Merlin32Directives.ELUP.ToString())] = "--^";
// OG
}
2017-01-15 22:44:43 +00:00
_compList = new List<Completion>();
2016-12-31 00:46:52 +00:00
foreach (string str in strList)
2017-01-15 22:44:43 +00:00
_compList.Add(new Completion(str, str, str, null, null));
2016-12-31 00:46:52 +00:00
2017-01-15 22:44:43 +00:00
completionSets.Add(new CompletionSet("All", "All", FindTokenSpanAtPosition(session), _compList, null));
2016-12-31 00:46:52 +00:00
}
2017-01-15 22:44:43 +00:00
private ITrackingSpan FindTokenSpanAtPosition(ICompletionSession session)
2016-12-31 00:46:52 +00:00
{
SnapshotPoint currentPoint = (session.TextView.Caret.Position.BufferPosition) - 1;
2017-01-15 22:44:43 +00:00
ITextStructureNavigator navigator = _sourceprovider.NavigatorService.GetTextStructureNavigator(_buffer);
2016-12-31 00:46:52 +00:00
TextExtent extent = navigator.GetExtentOfWord(currentPoint);
return currentPoint.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
}
public void Dispose()
{
2017-01-15 22:44:43 +00:00
if (!_isDisposed)
2016-12-31 00:46:52 +00:00
{
GC.SuppressFinalize(this);
2017-01-15 22:44:43 +00:00
_isDisposed = true;
2016-12-31 00:46:52 +00:00
}
}
}
}