Switch to using sys.tables for autocomplete

Move some code into a better class
This commit is contained in:
Karl Burtram
2016-07-24 13:37:10 -07:00
parent a7d634a014
commit 4cebc196ff
3 changed files with 98 additions and 52 deletions

View File

@@ -52,8 +52,10 @@ namespace Microsoft.SqlTools.EditorServices.Connection
/// <returns></returns>
public IEnumerable<string> GetServerObjects()
{
// Select the values from sys.tables to give a super basic
// autocomplete experience. This will be replaced by SMO.
SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT name FROM sys.objects";
command.CommandText = "SELECT name FROM sys.tables";
command.CommandTimeout = 15;
command.CommandType = CommandType.Text;
var reader = command.ExecuteReader();

View File

@@ -4,6 +4,7 @@
//
using Microsoft.SqlTools.EditorServices.Connection;
using Microsoft.SqlTools.EditorServices.Protocol.LanguageServer;
using System;
using System.Collections.Generic;
@@ -20,8 +21,14 @@ namespace Microsoft.SqlTools.LanguageSupport
private static Lazy<AutoCompleteService> instance
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
/// <summary>
/// The current autocomplete candidate list
/// </summary>
private IEnumerable<string> autoCompleteList;
/// <summary>
/// Gets the current autocomplete candidate list
/// </summary>
public IEnumerable<string> AutoCompleteList
{
get
@@ -41,9 +48,65 @@ namespace Microsoft.SqlTools.LanguageSupport
}
}
/// <summary>
/// Update the cached autocomplete candidate list when the user connects to a database
/// </summary>
/// <param name="connection"></param>
public void UpdateAutoCompleteCache(ISqlConnection connection)
{
this.autoCompleteList = connection.GetServerObjects();
}
/// <summary>
/// Return the completion item list for the current text position
/// </summary>
/// <param name="textDocumentPosition"></param>
public CompletionItem[] GetCompletionItems(TextDocumentPosition textDocumentPosition)
{
var completions = new List<CompletionItem>();
int i = 0;
// the completion list will be null is user not connected to server
if (this.AutoCompleteList != null)
{
foreach (var autoCompleteItem in this.AutoCompleteList)
{
// convert the completion item candidates into CompletionItems
completions.Add(new CompletionItem()
{
Label = autoCompleteItem,
Kind = CompletionItemKind.Keyword,
Detail = autoCompleteItem + " details",
Documentation = autoCompleteItem + " documentation",
TextEdit = new TextEdit
{
NewText = autoCompleteItem,
Range = new Range
{
Start = new Position
{
Line = textDocumentPosition.Position.Line,
Character = textDocumentPosition.Position.Character
},
End = new Position
{
Line = textDocumentPosition.Position.Line,
Character = textDocumentPosition.Position.Character + 5
}
}
}
});
// only show 50 items
if (++i == 50)
{
break;
}
}
}
return completions.ToArray();
}
}
}

View File

@@ -175,15 +175,38 @@ namespace Microsoft.SqlTools.EditorServices.Protocol.Server
await Task.FromResult(true);
}
/// <summary>
/// Handle the file open notification
/// </summary>
/// <param name="openParams"></param>
/// <param name="eventContext"></param>
protected Task HandleDidOpenTextDocumentNotification(
DidOpenTextDocumentNotification openParams,
EventContext eventContext)
{
Logger.Write(LogLevel.Verbose, "HandleDidOpenTextDocumentNotification");
// read the SQL file contents into the ScriptFile
ScriptFile openedFile =
editorSession.Workspace.GetFileBuffer(
openParams.Uri,
openParams.Text);
// run diagnostics on the opened file
this.RunScriptDiagnostics(
new ScriptFile[] { openedFile },
editorSession,
eventContext);
return Task.FromResult(true);
}
protected Task HandleDidCloseTextDocumentNotification(
/// <summary>
/// Handle the close document notication
/// </summary>
/// <param name="closeParams"></param>
/// <param name="eventContext"></param>
protected Task HandleDidCloseTextDocumentNotification(
TextDocumentIdentifier closeParams,
EventContext eventContext)
{
@@ -257,62 +280,20 @@ namespace Microsoft.SqlTools.EditorServices.Protocol.Server
await Task.FromResult(true);
}
/// <summary>
/// Handles the completion list request
/// </summary>
/// <param name="textDocumentPosition"></param>
/// <param name="requestContext"></param>
protected async Task HandleCompletionRequest(
TextDocumentPosition textDocumentPosition,
RequestContext<CompletionItem[]> requestContext)
{
Logger.Write(LogLevel.Verbose, "HandleCompletionRequest");
var connectionService = ConnectionService.Instance;
if (connectionService.ActiveConnections.Count > 0)
{
AutoCompleteService.Instance.UpdateAutoCompleteCache(
connectionService.ActiveConnections.First().Value);
}
var autoCompleteList = AutoCompleteService.Instance.AutoCompleteList;
var completions = new List<CompletionItem>();
int i = 0;
if (autoCompleteList != null)
{
foreach (var autoCompleteItem in autoCompleteList)
{
completions.Add(new CompletionItem()
{
Label = autoCompleteItem,
Kind = CompletionItemKind.Keyword,
Detail = autoCompleteItem + " details",
Documentation = autoCompleteItem + " documentation",
//SortText = "SortText",
TextEdit = new TextEdit
{
NewText = "New Text",
Range = new Range
{
Start = new Position
{
Line = textDocumentPosition.Position.Line,
Character = textDocumentPosition.Position.Character
},
End = new Position
{
Line = textDocumentPosition.Position.Line,
Character = textDocumentPosition.Position.Character + 5
}
}
}
});
// only show 50 items
if (++i == 50)
{
break;
}
}
}
await requestContext.SendResult(completions.ToArray());
// get teh current list of completion items and return to client
var completionItems = AutoCompleteService.Instance.GetCompletionItems(textDocumentPosition);
await requestContext.SendResult(completionItems);
}
protected async Task HandleCompletionResolveRequest(