Inital SMO autocomplete commit

This commit is contained in:
Karl Burtram
2016-08-24 23:03:43 -07:00
parent aac9f40ad2
commit 89ca0c1fde
6 changed files with 186 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlTools.EditorServices.Utility;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
@@ -291,5 +292,13 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection
}
return connectionBuilder.ToString();
}
public static ServerConnection GetServerConnection(ConnectionInfo connection)
{
string connectionString = BuildConnectionString(connection.ConnectionDetails);
var sqlConnection = new SqlConnection(connectionString);
return new ServerConnection(sqlConnection);
}
}
}

View File

@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
@@ -72,10 +73,22 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
{
List<CompletionItem> completions = new List<CompletionItem>();
// Take a reference to the list at a point in time in case we update and replace the list
//var suggestions = AutoCompleteList;
if (!LanguageService.Instance.ScriptParseInfoMap.ContainsKey(textDocumentPosition.Uri))
{
return completions;
}
var scriptParseInfo = LanguageService.Instance.ScriptParseInfoMap[textDocumentPosition.Uri];
var suggestions = Resolver.FindCompletions(
scriptParseInfo.ParseResult,
textDocumentPosition.Position.Line,
textDocumentPosition.Position.Character,
scriptParseInfo.MetadataDisplayInfoProvider);
int i = 0;
// Take a reference to the list at a point in time in case we update and replace the list
var suggestions = AutoCompleteList;
// the completion list will be null is user not connected to server
if (this.AutoCompleteList != null)
{
@@ -85,13 +98,13 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
// convert the completion item candidates into CompletionItems
completions.Add(new CompletionItem()
{
Label = autoCompleteItem,
Label = autoCompleteItem.Title,
Kind = CompletionItemKind.Keyword,
Detail = autoCompleteItem + " details",
Documentation = autoCompleteItem + " documentation",
Detail = autoCompleteItem.Title + " details",
Documentation = autoCompleteItem.Title + " documentation",
TextEdit = new TextEdit
{
NewText = autoCompleteItem,
NewText = autoCompleteItem.Title,
Range = new Range
{
Start = new Position

View File

@@ -19,6 +19,10 @@ using System.Linq;
using Microsoft.SqlServer.Management.SqlParser.Parser;
using Location = Microsoft.SqlTools.ServiceLayer.Workspace.Contracts.Location;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlServer.Management.SqlParser.Binder;
using Microsoft.SqlServer.Management.SmoMetadataProvider;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
{
@@ -33,6 +37,28 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
private static readonly Lazy<LanguageService> instance = new Lazy<LanguageService>(() => new LanguageService());
private Lazy<Dictionary<string, ScriptParseInfo>> scriptParseInfoMap
= new Lazy<Dictionary<string, ScriptParseInfo>>(() => new Dictionary<string, ScriptParseInfo>());
internal class ScriptParseInfo
{
public IBinder Binder { get; set; }
public ParseResult ParseResult { get; set; }
public SmoMetadataProvider MetadataProvider { get; set; }
public MetadataDisplayInfoProvider MetadataDisplayInfoProvider { get; set; }
}
internal Dictionary<string, ScriptParseInfo> ScriptParseInfoMap
{
get
{
return this.scriptParseInfoMap.Value;
}
}
public static LanguageService Instance
{
get { return instance.Value; }
@@ -125,6 +151,36 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
// save previous result for next incremental parse
this.prevParseResult = parseResult;
ConnectionInfo connInfo;
bool isConnected = ConnectionService.Instance.TryFindConnection(scriptFile.ClientFilePath, out connInfo);
if (isConnected)
{
if (!this.ScriptParseInfoMap.ContainsKey(scriptFile.ClientFilePath))
{
var srvConn = ConnectionService.GetServerConnection(connInfo);
var metadataProvider = SmoMetadataProvider.CreateConnectedProvider(srvConn);
var binder = BinderProvider.CreateBinder(metadataProvider);
var displayInfoProvider = new MetadataDisplayInfoProvider();
this.ScriptParseInfoMap.Add(scriptFile.ClientFilePath,
new ScriptParseInfo()
{
Binder = binder,
ParseResult = parseResult,
MetadataProvider = metadataProvider,
MetadataDisplayInfoProvider = displayInfoProvider
});
}
ScriptParseInfo parseInfo = this.ScriptParseInfoMap[scriptFile.ClientFilePath];
List<ParseResult> parseResults = new List<ParseResult>();
parseResults.Add(parseResult);
parseInfo.Binder.Bind(
parseResults,
connInfo.ConnectionDetails.DatabaseName,
BindMode.Batch);
}
// build a list of SQL script file markers from the errors
List<ScriptFileMarker> markers = new List<ScriptFileMarker>();
foreach (var error in parseResult.Errors)

View File

@@ -9,7 +9,11 @@
"Newtonsoft.Json": "9.0.1",
"Microsoft.SqlServer.SqlParser": "140.1.4",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.1.0"
"System.Data.SqlClient": "4.1.0",
"Microsoft.SqlServer.Smo": "140.1.2",
"System.Security.SecureString": "4.0.0",
"System.Collections.Specialized": "4.0.1",
"System.ComponentModel.TypeConverter": "4.1.0"
},
"frameworks": {
"netcoreapp1.0": {