Files
sqltoolsservice/test/CompletionExtSample/CompletionExt.cs
Shengyu Fu e1b9890f5c Adding completion extension loading and execution logic (#833)
* Adding ICompletionExtension interface

* Adding extension loading and execution logic

* Fixing compilation error in VS 2017

* Using MEF for completion extension discovery

* using await on GetCompletionItems

* Adding an integration test for completion extension and update the completion extension interface

* Update the completion extension test

* Fix issues based on review comments

* Remove try/cache based on review comments, fix a integration test.

* More changes based on review comments

* Fixing SendResult logic for completion extension loading

* Only load completion extension from the assembly passed in, add more comments in the test

* Adding right assert messages in the test.

* More fixes based on review comments

* Dropping ICompletionExtensionProvider, load assembly only if it's loaded at the first time or updated since last load.

* Fix based on the latest review comments

* Adding missing TSQL functions in default completion list

* Update jsonrpc documentation for completion/extLoad
2019-07-19 12:04:03 -07:00

82 lines
2.8 KiB
C#

//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion.Extension;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
namespace Microsoft.SqlTools.Test.CompletionExtension
{
[Export(typeof(ICompletionExtension))]
public class CompletionExt : ICompletionExtension
{
public string Name => "CompletionExt";
private string modelPath;
public CompletionExt()
{
}
void IDisposable.Dispose()
{
}
async Task<CompletionItem[]> ICompletionExtension.HandleCompletionAsync(ConnectionInfo connInfo, ScriptDocumentInfo scriptDocumentInfo, CompletionItem[] completions, CancellationToken token)
{
if (completions == null || completions == null || completions.Length == 0)
{
return completions;
}
return await Run(completions, token);
}
async Task ICompletionExtension.Initialize(IReadOnlyDictionary<string, object> properties, CancellationToken token)
{
modelPath = (string)properties["modelPath"];
await LoadModel(token).ConfigureAwait(false);
return;
}
private async Task LoadModel(CancellationToken token)
{
//loading model logic here
await Task.Delay(2000).ConfigureAwait(false); //for testing
token.ThrowIfCancellationRequested();
Console.WriteLine("Model loaded from: " + modelPath);
}
private async Task<CompletionItem[]> Run(CompletionItem[] completions, CancellationToken token)
{
Console.WriteLine("Enter ExecuteAsync");
var sortedItems = completions.OrderBy(item => item.SortText);
sortedItems.First().Preselect = true;
foreach(var item in sortedItems)
{
item.Command = new Command
{
CommandStr = "vsintellicode.completionItemSelected",
Arguments = new object[] { new Dictionary<string, string> { { "IsCommit", "True" } } }
};
}
//code to augment the default completion list
await Task.Delay(20); // for testing
token.ThrowIfCancellationRequested();
Console.WriteLine("Exit ExecuteAsync");
return sortedItems.ToArray();
}
}
}