mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Don't add parentheses to ANSI Scalar functions (#1487)
* Don't add parentheses to ANSI Scalar functions * fix test
This commit is contained in:
@@ -10,6 +10,7 @@ using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
|||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||||
using Microsoft.SqlTools.Utility;
|
using Microsoft.SqlTools.Utility;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
||||||
{
|
{
|
||||||
@@ -23,6 +24,16 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
|||||||
private static DelimitedIdentifier FunctionPostfix = new DelimitedIdentifier { Start = "", End = "()" };
|
private static DelimitedIdentifier FunctionPostfix = new DelimitedIdentifier { Start = "", End = "()" };
|
||||||
private static DelimitedIdentifier[] DelimitedIdentifiers =
|
private static DelimitedIdentifier[] DelimitedIdentifiers =
|
||||||
new DelimitedIdentifier[] { BracketedIdentifiers, new DelimitedIdentifier { Start = "\"", End = "\"" } };
|
new DelimitedIdentifier[] { BracketedIdentifiers, new DelimitedIdentifier { Start = "\"", End = "\"" } };
|
||||||
|
public static readonly IList<string> AnsiScalarFunctions = new List<string>()
|
||||||
|
{
|
||||||
|
"CURRENT_DATE",
|
||||||
|
"CURRENT_TIME",
|
||||||
|
"CURRENT_TIMESTAMP",
|
||||||
|
"CURRENT_USER",
|
||||||
|
"SESSION_USER",
|
||||||
|
"SYSTEM_USER",
|
||||||
|
"USER"
|
||||||
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create new instance given the SQL parser declaration
|
/// Create new instance given the SQL parser declaration
|
||||||
@@ -75,7 +86,8 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion
|
|||||||
case DeclarationType.ScalarValuedFunction:
|
case DeclarationType.ScalarValuedFunction:
|
||||||
case DeclarationType.TableValuedFunction:
|
case DeclarationType.TableValuedFunction:
|
||||||
// Add ()'s for all functions except global variable system functions (which all start with @@)
|
// Add ()'s for all functions except global variable system functions (which all start with @@)
|
||||||
if (!DeclarationTitle.StartsWith("@@"))
|
// and ANSI scalar functions (which don't include the parentheses to match the spec)
|
||||||
|
if (!DeclarationTitle.StartsWith("@@") && !AnsiScalarFunctions.Contains(DeclarationTitle.ToUpperInvariant()))
|
||||||
{
|
{
|
||||||
InsertText = WithDelimitedIdentifier(FunctionPostfix, DeclarationTitle);
|
InsertText = WithDelimitedIdentifier(FunctionPostfix, DeclarationTitle);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -248,6 +248,11 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.LanguageServer
|
|||||||
{
|
{
|
||||||
foreach (string word in AutoCompleteHelper.DefaultCompletionText)
|
foreach (string word in AutoCompleteHelper.DefaultCompletionText)
|
||||||
{
|
{
|
||||||
|
if (SqlCompletionItem.AnsiScalarFunctions.Contains(word.ToUpperInvariant()))
|
||||||
|
{
|
||||||
|
// Skip ANSI scalar functions, those don't have parentheses
|
||||||
|
continue;
|
||||||
|
}
|
||||||
string declarationTitle = word;
|
string declarationTitle = word;
|
||||||
string tokenText = "";
|
string tokenText = "";
|
||||||
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
|
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, declarationType, tokenText);
|
||||||
@@ -261,9 +266,16 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.LanguageServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GlobalVariableSystemFunctionsShouldNotHaveParenthesesAdded()
|
[TestCase("@@CONNECTIONS")]
|
||||||
|
[TestCase("CURRENT_DATE")]
|
||||||
|
[TestCase("CURRENT_TIME")]
|
||||||
|
[TestCase("CURRENT_TIMESTAMP")]
|
||||||
|
[TestCase("CURRENT_USER")]
|
||||||
|
[TestCase("SESSION_USER")]
|
||||||
|
[TestCase("SYSTEM_USER")]
|
||||||
|
[TestCase("USER")]
|
||||||
|
public void GlobalVariable_And_AnsiScalar_Functions_Should_Not_Have_Parentheses_Added(string declarationTitle)
|
||||||
{
|
{
|
||||||
string declarationTitle = "@@CONNECTIONS";
|
|
||||||
string tokenText = "";
|
string tokenText = "";
|
||||||
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, DeclarationType.BuiltInFunction, tokenText);
|
SqlCompletionItem item = new SqlCompletionItem(declarationTitle, DeclarationType.BuiltInFunction, tokenText);
|
||||||
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
|
CompletionItem completionItem = item.CreateCompletionItem(0, 1, 2);
|
||||||
@@ -271,7 +283,6 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.LanguageServer
|
|||||||
Assert.AreEqual(declarationTitle, completionItem.Label);
|
Assert.AreEqual(declarationTitle, completionItem.Label);
|
||||||
Assert.AreEqual($"{declarationTitle}", completionItem.InsertText);
|
Assert.AreEqual($"{declarationTitle}", completionItem.InsertText);
|
||||||
Assert.AreEqual(declarationTitle, completionItem.Detail);
|
Assert.AreEqual(declarationTitle, completionItem.Detail);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|||||||
Reference in New Issue
Block a user