mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-29 17:24:34 -05:00
added support for QUOTED_IDENTIFIER in auto complete list (#190)
* added support for QUOTED_IDENTIFIER in auto complete list
This commit is contained in:
@@ -27,6 +27,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
|
||||
/// prior to the process shutting down.
|
||||
/// </summary>
|
||||
private const int ShutdownTimeoutInSeconds = 120;
|
||||
public static readonly string[] CompletionTriggerCharacters = new string[] { ".", "-", ":", "\\", "[", "\"" };
|
||||
|
||||
#region Singleton Instance Code
|
||||
|
||||
@@ -135,7 +136,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
|
||||
/// <param name="initializeParams"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
private async Task HandleInitializeRequest(InitializeRequest initializeParams, RequestContext<InitializeResult> requestContext)
|
||||
internal async Task HandleInitializeRequest(InitializeRequest initializeParams, RequestContext<InitializeResult> requestContext)
|
||||
{
|
||||
// Call all tasks that registered on the initialize request
|
||||
var initializeTasks = initializeCallbacks.Select(t => t(initializeParams, requestContext));
|
||||
@@ -157,7 +158,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Hosting
|
||||
CompletionProvider = new CompletionOptions
|
||||
{
|
||||
ResolveProvider = true,
|
||||
TriggerCharacters = new string[] { ".", "-", ":", "\\", "[" }
|
||||
TriggerCharacters = CompletionTriggerCharacters
|
||||
},
|
||||
SignatureHelpProvider = new SignatureHelpOptions
|
||||
{
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||
@@ -19,6 +19,9 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
||||
public class SqlCompletionItem
|
||||
{
|
||||
private static Regex ValidSqlNameRegex = new Regex(@"^[\p{L}_@][\p{L}\p{N}@$#_]{0,127}$");
|
||||
private static DelimitedIdentifier BracketedIdentifiers = new DelimitedIdentifier { Start = "[", End = "]"};
|
||||
private static DelimitedIdentifier[] DelimitedIdentifiers =
|
||||
new DelimitedIdentifier[] { BracketedIdentifiers, new DelimitedIdentifier {Start = "\"", End = "\"" } };
|
||||
|
||||
/// <summary>
|
||||
/// Create new instance given the SQL parser declaration
|
||||
@@ -44,12 +47,19 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
||||
|
||||
private void Init()
|
||||
{
|
||||
InsertText = GetCompletionItemInsertName();
|
||||
InsertText = DeclarationTitle;
|
||||
DelimitedIdentifier delimitedIdentifier = GetDelimitedIdentifier(TokenText);
|
||||
Label = DeclarationTitle;
|
||||
if (StartsWithBracket(TokenText) || AutoCompleteHelper.IsReservedWord(InsertText))
|
||||
|
||||
if (delimitedIdentifier == null && !string.IsNullOrEmpty(DeclarationTitle) &&
|
||||
(!ValidSqlNameRegex.IsMatch(DeclarationTitle) || AutoCompleteHelper.IsReservedWord(InsertText)))
|
||||
{
|
||||
Label = WithBracket(Label);
|
||||
InsertText = WithBracket(InsertText);
|
||||
InsertText = WithDelimitedIdentifier(BracketedIdentifiers, DeclarationTitle);
|
||||
}
|
||||
if (delimitedIdentifier != null)
|
||||
{
|
||||
Label = WithDelimitedIdentifier(delimitedIdentifier, Label);
|
||||
InsertText = WithDelimitedIdentifier(delimitedIdentifier, InsertText);
|
||||
}
|
||||
Detail = Label;
|
||||
Kind = CreateCompletionItemKind();
|
||||
@@ -143,7 +153,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
||||
int row,
|
||||
int startColumn,
|
||||
int endColumn)
|
||||
{
|
||||
{
|
||||
CompletionItem item = new CompletionItem()
|
||||
{
|
||||
Label = label,
|
||||
@@ -172,31 +182,22 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
||||
return item;
|
||||
}
|
||||
|
||||
private string GetCompletionItemInsertName()
|
||||
private bool HasDelimitedIdentifier(DelimitedIdentifier delimiteIidentifier, string text)
|
||||
{
|
||||
string insertText = DeclarationTitle;
|
||||
if (!string.IsNullOrEmpty(DeclarationTitle) && !ValidSqlNameRegex.IsMatch(DeclarationTitle))
|
||||
return text != null && delimiteIidentifier != null && text.StartsWith(delimiteIidentifier.Start)
|
||||
&& text.EndsWith(delimiteIidentifier.End);
|
||||
}
|
||||
|
||||
private DelimitedIdentifier GetDelimitedIdentifier(string text)
|
||||
{
|
||||
return text != null ? DelimitedIdentifiers.FirstOrDefault(x => text.StartsWith(x.Start)) : null;
|
||||
}
|
||||
|
||||
private string WithDelimitedIdentifier(DelimitedIdentifier delimiteIidentifier, string text)
|
||||
{
|
||||
if (!HasDelimitedIdentifier(delimiteIidentifier, text))
|
||||
{
|
||||
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);
|
||||
return string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", delimiteIidentifier.Start, text, delimiteIidentifier.End);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -204,4 +205,10 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class DelimitedIdentifier
|
||||
{
|
||||
public string Start { get; set; }
|
||||
public string End { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user