Skip to content

Commit 7af72ad

Browse files
authored
don't suggest fields that are already specified (#2240)
Fixes #2126
1 parent 2c5e636 commit 7af72ad

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/libraries/Microsoft.PowerFx.Core/Texl/Intellisense/SuggestionHandlers/FunctionRecordNameSuggestionHandler.cs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
67
using System.Linq;
78
using Microsoft.PowerFx.Core.Binding.BindInfo;
89
using Microsoft.PowerFx.Core.Functions;
@@ -152,9 +153,17 @@ private static bool TryGetParentRecordFieldType(DType aggregateType, TexlNode cu
152153
internal static bool AddAggregateSuggestions(DType aggregateType, IntellisenseData.IntellisenseData intellisenseData, int cursorPos)
153154
{
154155
var suggestionsAdded = false;
156+
var parentRecordNode = intellisenseData.CurNode.Parent as RecordNode;
157+
var alreadyUsedFields = parentRecordNode?.Ids.Select(id => id.Name).ToImmutableHashSet() ?? Enumerable.Empty<DName>().ToImmutableHashSet();
155158
foreach (var tName in aggregateType.GetNames(DPath.Root).Where(param => !param.Type.IsError))
156159
{
157160
var usedName = tName.Name;
161+
162+
if (alreadyUsedFields.Contains(usedName))
163+
{
164+
continue;
165+
}
166+
158167
if (DType.TryGetDisplayNameForColumn(aggregateType, usedName, out var maybeDisplayName))
159168
{
160169
usedName = new DName(maybeDisplayName);

src/tests/Microsoft.PowerFx.Core.Tests/IntellisenseTests/SuggestTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ private string[] SuggestStrings(string expression, PowerFxConfig config, Culture
168168
[InlineData("[@In|]", "ErrorKind")]
169169

170170
// FunctionRecordNameSuggestionHandler
171-
[InlineData("Error({Kin|d:0})", "Kind:")]
172-
[InlineData("Error({|Kind:0, Test:\"\"})", "Kind:", "Test:")]
171+
[InlineData("Error({Kin|d:0})")]
172+
[InlineData("Error({|Kind:0, Test:\"\"})")]
173173

174174
// ErrorNodeSuggestionHandler
175175
[InlineData("ForAll([0],`|", "ThisRecord", "Value")]
@@ -493,7 +493,7 @@ public void SuggestUser(string expression, params string[] expected)
493493
[Theory]
494494
[InlineData("{|", "output1:", "output2:")]
495495

496-
[InlineData("{output1: 1, |", "output1:", "output2:")]
496+
[InlineData("{output1: 1, |", "output2:")]
497497

498498
// We do not suggest nested type, as this can explode if type is DV.
499499
[InlineData("{output1: {|")]

src/tests/Microsoft.PowerFx.Interpreter.Tests/InterpreterSuggestTests.cs

+26-5
Original file line numberDiff line numberDiff line change
@@ -274,21 +274,42 @@ public void TestArgSuggestion(string expression, params string[] expectedSuggest
274274
"field1:")]
275275
[InlineData(
276276
"RecordInputTest( {field1 : 1}, \"test\", {|",
277-
"id:",
277+
"id:",
278+
"name:")]
279+
280+
// do not repeat already used fields.
281+
[InlineData(
282+
"RecordInputTest( {field1 : 2}, \"test\", { id: 1, |",
278283
"name:")]
284+
285+
[InlineData(
286+
"RecordInputTest( {field1 : 2}, \"test\", { name: \"test\", |",
287+
"id:")]
288+
279289
[InlineData(
280-
"RecordInputTest( {field1 : 2}, \"test\", { id: 1, name: \"test\"}, {|",
290+
"RecordInputTest( {field1 : 2}, \"test\", { id: 1, name:\"test name\", |}")]
291+
292+
[InlineData(
293+
"RecordInputTest( {field1 : 2}, \"test\", { id: 1, name: \"test\"}, {|",
281294
"nested:",
282295
"nested2:")]
283296

284297
// nested record field.
285298
[InlineData(
286-
"RecordInputTest( {field1 : 3}, \"test\", { id: 1, name: \"test\"}, { nested:{|",
299+
"RecordInputTest( {field1 : 3}, \"test\", { id: 1, name: \"test\"}, { nested:{|",
287300
"field1:")]
288301
[InlineData(
289-
"RecordInputTest( {field1 : 4}, \"test\", { id: 1, name: \"test\"}, { nested2:{|",
290-
"id:",
302+
"RecordInputTest( {field1 : 4}, \"test\", { id: 1, name: \"test\"}, { nested2:{|",
303+
"id:",
291304
"name:")]
305+
306+
// do not repeat already used fields.
307+
[InlineData(
308+
"RecordInputTest( {field1 : 4}, \"test\", { id: 1, name: \"test\"}, { nested2:{ id: 2, |",
309+
"name:")]
310+
[InlineData(
311+
"RecordInputTest( {field1 : 4}, \"test\", { id: 1, name: \"test\"}, { nested2:{ id: 2, name: \"test\", |")]
312+
292313
[InlineData(
293314
"RecordInputTest( {field1 : 3}, \"test\", { id: 1, name: \"test\"}, { nested:{ field1: 1}, nested2: {|",
294315
"id:",

0 commit comments

Comments
 (0)