Skip to content

Instantly share code, notes, and snippets.

@weltkante
Created March 30, 2015 17:09
Show Gist options
  • Save weltkante/758a66a310df96699028 to your computer and use it in GitHub Desktop.
Save weltkante/758a66a310df96699028 to your computer and use it in GitHub Desktop.
private static async Task<CompletionResult> GetCompletionData(ScriptEditorControl parent, CancellationToken cancellationToken = default(CancellationToken))
{
if (!parent.ContainsScript)
return null;
var doc = parent.GetScriptDocument();
var model = await doc.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var syntax = await doc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var text = parent.GetScriptView().CurrentText;
var position = parent.txtEditor.SelectionStart;
var token = syntax.FindToken(position);
var completionSpan = token.Span;
if (position < completionSpan.Start)
completionSpan = new TextSpan(position, 0);
if (completionSpan.Length > 0 && position == completionSpan.End && !IsCompletionWordCharacter(text[position - 1]))
completionSpan = new TextSpan(position, 0);
var filterText = text.ToString(TextSpan.FromBounds(completionSpan.Start, position));
var data = Recommender.GetRecommendedSymbolsAtPosition(model, completionSpan.Start, parent.GetScriptWorkspace(), null, cancellationToken)
.Where(symbol => symbol.Name.IndexOf(filterText, StringComparison.InvariantCultureIgnoreCase) >= 0)
.GroupBy(x => new SymbolGroupKey(x))
.OrderByDescending(x => x.Key.Name.StartsWith(filterText, StringComparison.InvariantCultureIgnoreCase))
.ThenBy(x => x.Key.Name)
.ToImmutableArray();
if (data.Length == 0)
return null;
return new CompletionResult(completionSpan, data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment