From ff6f01ba28f491c36a4104442c57bb4f264187b2 Mon Sep 17 00:00:00 2001 From: Mark Carrington <31017244+MarkMpn@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:11:15 +0000 Subject: [PATCH] Fixed intellisense with trailing comments Fixes #569 --- .../Autocomplete/Autocomplete.cs | 3 +++ MarkMpn.Sql4Cds.Tests/AutocompleteTests.cs | 12 ++++++++++++ MarkMpn.Sql4Cds.XTB/Autocomplete.cs | 3 +++ 3 files changed, 18 insertions(+) diff --git a/MarkMpn.Sql4Cds.LanguageServer/Autocomplete/Autocomplete.cs b/MarkMpn.Sql4Cds.LanguageServer/Autocomplete/Autocomplete.cs index e23134fa..19884ec9 100644 --- a/MarkMpn.Sql4Cds.LanguageServer/Autocomplete/Autocomplete.cs +++ b/MarkMpn.Sql4Cds.LanguageServer/Autocomplete/Autocomplete.cs @@ -1023,6 +1023,9 @@ private bool InCommentOrStringLiteral(string text, int pos) while ((i = text.IndexOfAny(new[] { '\n', '-', '\'', '/' }, i + 1)) != -1) { + if (i > pos) + break; + if (text[i] == '\n') inSingleLineComment = false; else if (i > 0 && !inQuotes && text[i - 1] == '-' && text[i] == '-') diff --git a/MarkMpn.Sql4Cds.Tests/AutocompleteTests.cs b/MarkMpn.Sql4Cds.Tests/AutocompleteTests.cs index 7ae70dc2..fb3b4735 100644 --- a/MarkMpn.Sql4Cds.Tests/AutocompleteTests.cs +++ b/MarkMpn.Sql4Cds.Tests/AutocompleteTests.cs @@ -405,5 +405,17 @@ DECLARE @x varchar CollectionAssert.IsSubsetOf(new[] { "@i", "@x", "@@ROWCOUNT", "@@IDENTITY", "@@SERVERNAME", "@@VERSION" }, suggestions); } + + [TestMethod] + public void TrailingComment() + { + // https://github.com/MarkMpn/Sql4Cds/issues/569 + var prefix = "SELECT * FROM a"; + var suffix = "\r\n-- comment"; + var sql = prefix + suffix; + var suggestions = _autocomplete.GetSuggestions(sql, prefix.Length - 1).Where(s => s.ImageIndex == 4).Select(s => s.Text).ToList(); + + CollectionAssert.AreEqual(new[] { "account" }, suggestions); + } } } diff --git a/MarkMpn.Sql4Cds.XTB/Autocomplete.cs b/MarkMpn.Sql4Cds.XTB/Autocomplete.cs index c7d50346..b2ee3ef2 100644 --- a/MarkMpn.Sql4Cds.XTB/Autocomplete.cs +++ b/MarkMpn.Sql4Cds.XTB/Autocomplete.cs @@ -1022,6 +1022,9 @@ private bool InCommentOrStringLiteral(string text, int pos) while ((i = text.IndexOfAny(new[] { '\n', '-', '\'', '/' }, i + 1)) != -1) { + if (i > pos) + break; + if (text[i] == '\n') inSingleLineComment = false; else if (i > 0 && !inQuotes && text[i - 1] == '-' && text[i] == '-')