Intellisense for when the text starts with bracket (#164)

* Intellisense for when the text starts with bracket
This commit is contained in:
Leila Lali
2016-12-06 10:09:50 -08:00
committed by GitHub
parent 439845eb0a
commit 379c6170b3
5 changed files with 426 additions and 82 deletions

View File

@@ -157,7 +157,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
CompletionProvider = new CompletionOptions
{
ResolveProvider = true,
TriggerCharacters = new string[] { ".", "-", ":", "\\" }
TriggerCharacters = new string[] { ".", "-", ":", "\\", "[" }
},
SignatureHelpProvider = new SignatureHelpOptions
{

View File

@@ -31,8 +31,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
private static WorkspaceService<SqlToolsSettings> workspaceServiceInstance;
private static Regex ValidSqlNameRegex = new Regex(@"^[\p{L}_@][\p{L}\p{N}@$#_]{0,127}$");
private static CompletionItem[] emptyCompletionList = new CompletionItem[0];
private static readonly string[] DefaultCompletionText = new string[]
@@ -433,7 +431,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
int startColumn,
int endColumn)
{
return CreateCompletionItem(label, label + " keyword", label, CompletionItemKind.Keyword, row, startColumn, endColumn);
return SqlCompletionItem.CreateCompletionItem(label, label + " keyword", label, CompletionItemKind.Keyword, row, startColumn, endColumn);
}
internal static CompletionItem[] AddTokenToItems(CompletionItem[] currentList, Token token, int row,
@@ -447,49 +445,12 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
))
{
var list = currentList.ToList();
list.Insert(0, CreateCompletionItem(token.Text, token.Text, token.Text, CompletionItemKind.Text, row, startColumn, endColumn));
list.Insert(0, SqlCompletionItem.CreateCompletionItem(token.Text, token.Text, token.Text, CompletionItemKind.Text, row, startColumn, endColumn));
return list.ToArray();
}
return currentList;
}
private static CompletionItem CreateCompletionItem(
string label,
string detail,
string insertText,
CompletionItemKind kind,
int row,
int startColumn,
int endColumn)
{
CompletionItem item = new CompletionItem()
{
Label = label,
Kind = kind,
Detail = detail,
InsertText = insertText,
TextEdit = new TextEdit
{
NewText = insertText,
Range = new Range
{
Start = new Position
{
Line = row,
Character = startColumn
},
End = new Position
{
Line = row,
Character = endColumn
}
}
}
};
return item;
}
/// <summary>
/// Converts a list of Declaration objects to CompletionItem objects
/// since VS Code expects CompletionItems but SQL Parser works with Declarations
@@ -502,56 +463,22 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
IEnumerable<Declaration> suggestions,
int row,
int startColumn,
int endColumn)
int endColumn,
string tokenText = null)
{
List<CompletionItem> completions = new List<CompletionItem>();
foreach (var autoCompleteItem in suggestions)
{
string insertText = GetCompletionItemInsertName(autoCompleteItem);
CompletionItemKind kind = CompletionItemKind.Variable;
switch (autoCompleteItem.Type)
{
case DeclarationType.Schema:
kind = CompletionItemKind.Module;
break;
case DeclarationType.Column:
kind = CompletionItemKind.Field;
break;
case DeclarationType.Table:
case DeclarationType.View:
kind = CompletionItemKind.File;
break;
case DeclarationType.Database:
kind = CompletionItemKind.Method;
break;
case DeclarationType.ScalarValuedFunction:
case DeclarationType.TableValuedFunction:
case DeclarationType.BuiltInFunction:
kind = CompletionItemKind.Value;
break;
default:
kind = CompletionItemKind.Unit;
break;
}
SqlCompletionItem sqlCompletionItem = new SqlCompletionItem(autoCompleteItem, tokenText);
// convert the completion item candidates into CompletionItems
completions.Add(CreateCompletionItem(autoCompleteItem.Title, autoCompleteItem.Title, insertText, kind, row, startColumn, endColumn));
completions.Add(sqlCompletionItem.CreateCompletionItem(row, startColumn, endColumn));
}
return completions.ToArray();
}
private static string GetCompletionItemInsertName(Declaration autoCompleteItem)
{
string insertText = autoCompleteItem.Title;
if (!string.IsNullOrEmpty(autoCompleteItem.Title) && !ValidSqlNameRegex.IsMatch(autoCompleteItem.Title))
{
insertText = string.Format(CultureInfo.InvariantCulture, "[{0}]", autoCompleteItem.Title);
}
return insertText;
}
/// <summary>
/// Preinitialize the parser and binder with common metadata.
/// This should front load the long binding wait to the time the

View File

@@ -977,7 +977,8 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
scriptParseInfo.CurrentSuggestions,
startLine,
startColumn,
endColumn);
endColumn,
tokenText);
},
timeoutOperation: (bindingContext) =>
{

View File

@@ -0,0 +1,207 @@
//
// 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.Globalization;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
{
/// <summary>
/// Creates a completion item from SQL parser declaration item
/// </summary>
public class SqlCompletionItem
{
private static Regex ValidSqlNameRegex = new Regex(@"^[\p{L}_@][\p{L}\p{N}@$#_]{0,127}$");
/// <summary>
/// Create new instance given the SQL parser declaration
/// </summary>
public SqlCompletionItem(Declaration declaration, string tokenText) :
this(declaration == null ? null : declaration.Title, declaration == null ? DeclarationType.Table : declaration.Type, tokenText)
{
}
/// <summary>
/// Creates new instance given declaration title and type
/// </summary>
public SqlCompletionItem(string declarationTitle, DeclarationType declarationType, string tokenText)
{
Validate.IsNotNullOrEmptyString("declarationTitle", declarationTitle);
DeclarationTitle = declarationTitle;
DeclarationType = declarationType;
TokenText = tokenText;
Init();
}
private void Init()
{
InsertText = GetCompletionItemInsertName();
Label = DeclarationTitle;
if (StartsWithBracket(TokenText))
{
Label = WithBracket(Label);
InsertText = WithBracket(InsertText);
}
Detail = Label;
Kind = CreateCompletionItemKind();
}
private CompletionItemKind CreateCompletionItemKind()
{
CompletionItemKind kind = CompletionItemKind.Variable;
switch (DeclarationType)
{
case DeclarationType.Schema:
kind = CompletionItemKind.Module;
break;
case DeclarationType.Column:
kind = CompletionItemKind.Field;
break;
case DeclarationType.Table:
case DeclarationType.View:
kind = CompletionItemKind.File;
break;
case DeclarationType.Database:
kind = CompletionItemKind.Method;
break;
case DeclarationType.ScalarValuedFunction:
case DeclarationType.TableValuedFunction:
case DeclarationType.BuiltInFunction:
kind = CompletionItemKind.Value;
break;
default:
kind = CompletionItemKind.Unit;
break;
}
return kind;
}
/// <summary>
/// Declaration Title
/// </summary>
public string DeclarationTitle { get; private set; }
/// <summary>
/// Token text from the editor
/// </summary>
public string TokenText { get; private set; }
/// <summary>
/// SQL declaration type
/// </summary>
public DeclarationType DeclarationType { get; private set; }
/// <summary>
/// Completion item label
/// </summary>
public string Label { get; private set; }
/// <summary>
/// Completion item kind
/// </summary>
public CompletionItemKind Kind { get; private set; }
/// <summary>
/// Completion insert text
/// </summary>
public string InsertText { get; private set; }
/// <summary>
/// Completion item detail
/// </summary>
public string Detail { get; private set; }
/// <summary>
/// Creates a completion item given the editor info
/// </summary>
public CompletionItem CreateCompletionItem(
int row,
int startColumn,
int endColumn)
{
return CreateCompletionItem(Label, Detail, InsertText, Kind, row, startColumn, endColumn);
}
/// <summary>
/// Creates a completion item
/// </summary>
public static CompletionItem CreateCompletionItem(
string label,
string detail,
string insertText,
CompletionItemKind kind,
int row,
int startColumn,
int endColumn)
{
CompletionItem item = new CompletionItem()
{
Label = label,
Kind = kind,
Detail = detail,
InsertText = insertText,
TextEdit = new TextEdit
{
NewText = insertText,
Range = new Range
{
Start = new Position
{
Line = row,
Character = startColumn
},
End = new Position
{
Line = row,
Character = endColumn
}
}
}
};
return item;
}
private string GetCompletionItemInsertName()
{
string insertText = DeclarationTitle;
if (!string.IsNullOrEmpty(DeclarationTitle) && !ValidSqlNameRegex.IsMatch(DeclarationTitle))
{
insertText = WithBracket(DeclarationTitle);
}
return insertText;
}
private bool HasBrackets(string text)
{
return text != null && text.StartsWith("[") && text.EndsWith("]");
}
private bool StartsWithBracket(string text)
{
return text != null && text.StartsWith("[");
}
private string WithBracket(string text)
{
if (!HasBrackets(text))
{
return string.Format(CultureInfo.InvariantCulture, "[{0}]", text);
}
else
{
return text;
}
}
}
}

View File

@@ -0,0 +1,209 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServer
{
public class SqlCompletionItemTests
{
[Fact]
public void InsertTextShouldIncludeBracketGivenNameWithSpace()
{
string declarationTitle = "name with space";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.True(completionItem.InsertText.StartsWith("[") && completionItem.InsertText.EndsWith("]"));
}
[Fact]
public void InsertTextShouldIncludeBracketGivenNameWithSpecialCharacter()
{
string declarationTitle = "name @";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, declarationTitle);
Assert.Equal(completionItem.Label, declarationTitle);
}
[Fact]
public void LabelShouldIncludeBracketGivenTokenWithBracket()
{
string declarationTitle = "name";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "[";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Label, expected);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, expected);
}
[Fact]
public void LabelShouldIncludeBracketGivenTokenWithBrackets()
{
string declarationTitle = "name";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "[]";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Label, expected);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, expected);
}
[Fact]
public void LabelShouldIncludeBracketGivenSqlObjectNameWithBracket()
{
string declarationTitle = @"Bracket\[";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Label, declarationTitle);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, declarationTitle);
}
[Fact]
public void LabelShouldIncludeBracketGivenSqlObjectNameWithBracketAndTokenWithBracket()
{
string declarationTitle = @"Bracket\[";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "[]";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Label, expected);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, expected);
}
[Fact]
public void LabelShouldNotIncludeBracketGivenNameWithBrackets()
{
string declarationTitle = "[name]";
string expected = declarationTitle;
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "[]";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Label, expected);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, expected);
}
[Fact]
public void LabelShouldIncludeBracketGivenNameWithOneBracket()
{
string declarationTitle = "[name";
string expected = "[" + declarationTitle + "]";
DeclarationType declarationType = DeclarationType.Table;
string tokenText = "[]";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Label, expected);
Assert.Equal(completionItem.InsertText, expected);
Assert.Equal(completionItem.Detail, expected);
}
[Fact]
public void KindShouldBeModuleGivenSchemaDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.Module;
DeclarationType declarationType = DeclarationType.Schema;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeFieldGivenColumnDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.Field;
DeclarationType declarationType = DeclarationType.Column;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeFileGivenTableDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.File;
DeclarationType declarationType = DeclarationType.Table;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeFileGivenViewDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.File;
DeclarationType declarationType = DeclarationType.View;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeMethodGivenDatabaseDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.Method;
DeclarationType declarationType = DeclarationType.Database;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeValueGivenScalarValuedFunctionDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.Value;
DeclarationType declarationType = DeclarationType.ScalarValuedFunction;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeValueGivenTableValuedFunctionDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.Value;
DeclarationType declarationType = DeclarationType.TableValuedFunction;
ValidateDeclarationType(declarationType, expectedType);
}
[Fact]
public void KindShouldBeUnitGivenUnknownDeclarationType()
{
CompletionItemKind expectedType = CompletionItemKind.Unit;
DeclarationType declarationType = DeclarationType.XmlIndex;
ValidateDeclarationType(declarationType, expectedType);
}
private void ValidateDeclarationType(DeclarationType declarationType, CompletionItemKind expectedType)
{
string declarationTitle = "name";
string tokenText = "";
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
Assert.Equal(completionItem.Kind, expectedType);
}
}
}