Skip to content

Commit 1b2434e

Browse files
committed
Correctly detect optional parameters and anonymous structure fields in DisambiguateComa.
1 parent e797a9c commit 1b2434e

File tree

2 files changed

+68
-18
lines changed

2 files changed

+68
-18
lines changed

External/Plugins/ASCompletion/Completion/ASComplete.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3582,7 +3582,17 @@ internal static ComaExpression DisambiguateComa(ScintillaControl Sci, int positi
35823582
if (":,(=".IndexOf(c) >= 0)
35833583
{
35843584
string line = Sci.GetLine(Sci.LineFromPosition(position));
3585+
//TODO: Very limited check, the case|default could be in a previous line, or it could be something else in the same line
35853586
if (Regex.IsMatch(line, @"\b(case|default)\b.*:")) break; // case: code block
3587+
if (c == ':' && Sci.ConfigurationLanguage == "haxe")
3588+
{
3589+
// Anonymous structures
3590+
ComaExpression coma = DisambiguateComa(Sci, position, minPos);
3591+
if (coma == ComaExpression.FunctionDeclaration || coma == ComaExpression.VarDeclaration)
3592+
{
3593+
return ComaExpression.VarDeclaration;
3594+
}
3595+
}
35863596
return ComaExpression.AnonymousObjectParam;
35873597
}
35883598
else if (c != ')' && c != '}' && !Char.IsLetterOrDigit(c)) return ComaExpression.AnonymousObject;
@@ -3593,7 +3603,28 @@ internal static ComaExpression DisambiguateComa(ScintillaControl Sci, int positi
35933603
{
35943604
braceCount++;
35953605
}
3596-
else if (c == '?') return ComaExpression.AnonymousObject;
3606+
else if (c == '?')
3607+
{
3608+
//TODO: Change to ASContext.Context.CurrentModel
3609+
if (Sci.ConfigurationLanguage == "haxe") // Haxe optional fields
3610+
{
3611+
ComaExpression coma = DisambiguateComa(Sci, position - 1, minPos);
3612+
if (coma == ComaExpression.FunctionDeclaration)
3613+
{
3614+
// Function optional argument
3615+
return coma;
3616+
}
3617+
else if (coma == ComaExpression.VarDeclaration)
3618+
{
3619+
// Possible anonymous structure optional field. Check we are not in a ternary operator
3620+
position--;
3621+
string word1 = GetWordLeft(Sci, ref position);
3622+
c = (word1.Length > 0) ? word1[word1.Length - 1] : (char) Sci.CharAt(position);
3623+
if (c == ',' || c == '{') return coma;
3624+
}
3625+
}
3626+
return ComaExpression.AnonymousObject;
3627+
}
35973628
position--;
35983629
}
35993630
return ComaExpression.None;

Tests/External/Plugins/ASCompletion.Tests/Completion/ASCompleteTests.cs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,25 @@ public void VariableType()
280280
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
281281
}
282282

283+
[Test]
284+
public void VariableMultiple()
285+
{
286+
var pluginMain = Substitute.For<PluginMain>();
287+
var pluginUiMock = new PluginUIMock(pluginMain);
288+
pluginMain.MenuItems.Returns(new List<System.Windows.Forms.ToolStripItem>());
289+
pluginMain.Settings.Returns(new GeneralSettings());
290+
pluginMain.Panel.Returns(pluginUiMock);
291+
ASContext.GlobalInit(pluginMain);
292+
ASContext.Context = new AS3Context.Context(new AS3Settings());
293+
294+
var sci = GetBaseScintillaControl();
295+
sci.Text = "var obj:Object, ";
296+
sci.ConfigurationLanguage = "as3";
297+
298+
var coma = ASComplete.DisambiguateComa(sci, 20, 0);
299+
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
300+
}
301+
283302
[Test]
284303
public void FunctionCallSimple()
285304
{
@@ -489,7 +508,7 @@ public void GenericTypeParameterInDeclarationWithFullyQualifiedClass()
489508
Assert.AreNotEqual(ComaExpression.None, coma);
490509
}
491510

492-
[Test(Description = "Not supported for now")]
511+
[Test]
493512
public void HaxeAnonymousStructureParameterType()
494513
{
495514
var pluginMain = Substitute.For<PluginMain>();
@@ -506,10 +525,10 @@ public void HaxeAnonymousStructureParameterType()
506525

507526
var coma = ASComplete.DisambiguateComa(sci, 9, 0);
508527

509-
Assert.AreEqual(ComaExpression.FunctionDeclaration, coma);
528+
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
510529
}
511530

512-
[Test(Description = "Not supported for now")]
531+
[Test]
513532
public void HaxeAnonymousStructureOptionalParameterType()
514533
{
515534
var pluginMain = Substitute.For<PluginMain>();
@@ -526,10 +545,10 @@ public void HaxeAnonymousStructureOptionalParameterType()
526545

527546
var coma = ASComplete.DisambiguateComa(sci, 10, 0);
528547

529-
Assert.AreEqual(ComaExpression.FunctionDeclaration, coma);
548+
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
530549
}
531550

532-
[Test(Description = "Not supported for now")]
551+
[Test]
533552
public void HaxeAnonymousStructureParameterTypeAsFunctionArg()
534553
{
535554
var pluginMain = Substitute.For<PluginMain>();
@@ -546,10 +565,10 @@ public void HaxeAnonymousStructureParameterTypeAsFunctionArg()
546565

547566
var coma = ASComplete.DisambiguateComa(sci, 18, 0);
548567

549-
Assert.AreEqual(ComaExpression.FunctionDeclaration, coma);
568+
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
550569
}
551570

552-
[Test(Description = "Not supported for now")]
571+
[Test]
553572
public void HaxeAnonymousStructureOptionalParameterTypeAsFunctionArg()
554573
{
555574
var pluginMain = Substitute.For<PluginMain>();
@@ -566,10 +585,10 @@ public void HaxeAnonymousStructureOptionalParameterTypeAsFunctionArg()
566585

567586
var coma = ASComplete.DisambiguateComa(sci, 19, 0);
568587

569-
Assert.AreEqual(ComaExpression.FunctionDeclaration, coma);
588+
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
570589
}
571590

572-
[Test(Description = "Not supported for now")]
591+
[Test]
573592
public void HaxeAnonymousStructureOptionalParameterTypeAsFunctionOptionalArg()
574593
{
575594
var pluginMain = Substitute.For<PluginMain>();
@@ -586,10 +605,10 @@ public void HaxeAnonymousStructureOptionalParameterTypeAsFunctionOptionalArg()
586605

587606
var coma = ASComplete.DisambiguateComa(sci, 20, 0);
588607

589-
Assert.AreEqual(ComaExpression.FunctionDeclaration, coma);
608+
Assert.AreEqual(ComaExpression.VarDeclaration, coma);
590609
}
591610

592-
[Test(Description = "Not supported for now")]
611+
[Test]
593612
public void HaxeFunctionOptionalArgument()
594613
{
595614
var pluginMain = Substitute.For<PluginMain>();
@@ -609,8 +628,8 @@ public void HaxeFunctionOptionalArgument()
609628
Assert.AreEqual(ComaExpression.FunctionDeclaration, coma);
610629
}
611630

612-
[Test(Description = "Not supported for now")]
613-
public void HaxeoArgumentType()
631+
[Test]
632+
public void HaxeFunctionOptionalArgumentType()
614633
{
615634
var pluginMain = Substitute.For<PluginMain>();
616635
var pluginUiMock = new PluginUIMock(pluginMain);
@@ -641,10 +660,10 @@ public void HaxeTernaryOperatorTruePart()
641660
ASContext.Context = new HaXeContext.Context(new HaXeSettings());
642661

643662
var sci = GetBaseScintillaControl();
644-
sci.Text = "var a = (2 > 3) ?";
663+
sci.Text = "var a:String = (2 > 3) ?";
645664
sci.ConfigurationLanguage = "haxe";
646665

647-
var coma = ASComplete.DisambiguateComa(sci, 17, 0);
666+
var coma = ASComplete.DisambiguateComa(sci, 28, 0);
648667

649668
Assert.AreEqual(ComaExpression.AnonymousObject, coma);
650669
}
@@ -661,10 +680,10 @@ public void HaxeTernaryOperatorFalsePart()
661680
ASContext.Context = new HaXeContext.Context(new HaXeSettings());
662681

663682
var sci = GetBaseScintillaControl();
664-
sci.Text = "var a = (2 > 3) ? 'Hah' :";
683+
sci.Text = "var a:String = (2 > 3) ? 'Hah' :";
665684
sci.ConfigurationLanguage = "haxe";
666685

667-
var coma = ASComplete.DisambiguateComa(sci, 25, 0);
686+
var coma = ASComplete.DisambiguateComa(sci, 36, 0);
668687

669688
Assert.AreEqual(ComaExpression.AnonymousObject, coma);
670689
}

0 commit comments

Comments
 (0)