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> /// <returns></returns>
public IEnumerable<string> GetServerObjects() 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(); SqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT name FROM sys.objects"; command.CommandText = "SELECT name FROM sys.tables";
command.CommandTimeout = 15; command.CommandTimeout = 15;
command.CommandType = CommandType.Text; command.CommandType = CommandType.Text;
var reader = command.ExecuteReader(); var reader = command.ExecuteReader();

View File

@@ -4,6 +4,7 @@
// //
using Microsoft.SqlTools.EditorServices.Connection; using Microsoft.SqlTools.EditorServices.Connection;
using Microsoft.SqlTools.EditorServices.Protocol.LanguageServer;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -20,8 +21,14 @@ namespace Microsoft.SqlTools.LanguageSupport
private static Lazy<AutoCompleteService> instance private static Lazy<AutoCompleteService> instance
= new Lazy<AutoCompleteService>(() => new AutoCompleteService()); = new Lazy<AutoCompleteService>(() => new AutoCompleteService());
/// <summary>
/// The current autocomplete candidate list
/// </summary>
private IEnumerable<string> autoCompleteList; private IEnumerable<string> autoCompleteList;
/// <summary>
/// Gets the current autocomplete candidate list
/// </summary>
public IEnumerable<string> AutoCompleteList public IEnumerable<string> AutoCompleteList
{ {
get 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) public void UpdateAutoCompleteCache(ISqlConnection connection)
{ {
this.autoCompleteList = connection.GetServerObjects(); 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); await Task.FromResult(true);
} }
/// <summary>
/// Handle the file open notification
/// </summary>
/// <param name="openParams"></param>
/// <param name="eventContext"></param>
protected Task HandleDidOpenTextDocumentNotification( protected Task HandleDidOpenTextDocumentNotification(
DidOpenTextDocumentNotification openParams, DidOpenTextDocumentNotification openParams,
EventContext eventContext) EventContext eventContext)
{ {
Logger.Write(LogLevel.Verbose, "HandleDidOpenTextDocumentNotification"); 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); 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, TextDocumentIdentifier closeParams,
EventContext eventContext) EventContext eventContext)
{ {
@@ -257,62 +280,20 @@ namespace Microsoft.SqlTools.EditorServices.Protocol.Server
await Task.FromResult(true); await Task.FromResult(true);
} }
/// <summary>
/// Handles the completion list request
/// </summary>
/// <param name="textDocumentPosition"></param>
/// <param name="requestContext"></param>
protected async Task HandleCompletionRequest( protected async Task HandleCompletionRequest(
TextDocumentPosition textDocumentPosition, TextDocumentPosition textDocumentPosition,
RequestContext<CompletionItem[]> requestContext) RequestContext<CompletionItem[]> requestContext)
{ {
Logger.Write(LogLevel.Verbose, "HandleCompletionRequest"); Logger.Write(LogLevel.Verbose, "HandleCompletionRequest");
var connectionService = ConnectionService.Instance; // get teh current list of completion items and return to client
if (connectionService.ActiveConnections.Count > 0) var completionItems = AutoCompleteService.Instance.GetCompletionItems(textDocumentPosition);
{ await requestContext.SendResult(completionItems);
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());
} }
protected async Task HandleCompletionResolveRequest( protected async Task HandleCompletionResolveRequest(