diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs index f2e58b9a2e..5438df8c44 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs @@ -84,6 +84,65 @@ namespace ICSharpCode.NRefactory.CSharp.Completion } return Enumerable.Empty (); } + + IEnumerable GenerateNameProposals (AstType type) + { + if (type is PrimitiveType) { + var pt = (PrimitiveType)type; + Console.WriteLine (pt.Keyword); + switch (pt.Keyword) { + case "object": + yield return "o"; + yield return "obj"; + break; + case "bool": + yield return "b"; + yield return "pred"; + break; + case "double": + case "float": + case "decimal": + yield return "d"; + yield return "f"; + yield return "m"; + break; + default: + yield return "i"; + yield return "j"; + yield return "k"; + break; + } + yield break; + } + + var names = new List (); + int offset1 = document.GetOffset (type.StartLocation); + int offset2 = document.GetOffset (type.EndLocation); + + string name = document.GetText (offset1, offset2 - offset1); + int lastNameStart = 0; + for (int i = 1; i < name.Length; i++) { + if (Char.IsUpper (name [i])) { + names.Add (name.Substring (lastNameStart, i - lastNameStart)); + lastNameStart = i; + } + } + + names.Add (name.Substring (lastNameStart, name.Length - lastNameStart)); + + var possibleName = new StringBuilder (); + for (int i = 0; i < names.Count; i++) { + possibleName.Length = 0; + for (int j = i; j < names.Count; j++) { + if (string.IsNullOrEmpty (names [j])) + continue; + if (j == i) + names [j] = Char.ToLower (names [j] [0]) + names [j].Substring (1); + possibleName.Append (names [j]); + } + yield return possibleName.ToString (); + } + } IEnumerable MagicKeyCompletion (char completionChar, bool controlSpace) { @@ -189,30 +248,9 @@ namespace ICSharpCode.NRefactory.CSharp.Completion var isAsExpression = GetExpressionAt (offset); if (controlSpace && isAsExpression != null && isAsExpression.Item2 is VariableDeclarationStatement && token != "new") { var parent = isAsExpression.Item2 as VariableDeclarationStatement; - int offset1 = document.GetOffset (parent.Type.StartLocation); - int offset2 = document.GetOffset (parent.Type.EndLocation); - - string name = document.GetText (offset1, offset2 - offset1); - var names = new List (); - int lastNameStart = 0; - for (int i = 1; i < name.Length; i++) { - if (Char.IsUpper (name [i])) { - names.Add (name.Substring (lastNameStart, i - lastNameStart)); - lastNameStart = i; - } - } - names.Add (name.Substring (lastNameStart, name.Length - lastNameStart)); var proposeNameList = new CompletionDataWrapper (this); - var possibleName = new StringBuilder (); - for (int i = 0; i < names.Count; i++) { - possibleName.Length = 0; - for (int j = i; j < names.Count; j++) { - if (string.IsNullOrEmpty (names [j])) - continue; - if (j == i) - names [j] = Char.ToLower (names [j] [0]) + names [j].Substring (1); - possibleName.Append (names [j]); - } + + foreach (var possibleName in GenerateNameProposals (parent.Type)) { if (possibleName.Length > 0) proposeNameList.Result.Add (factory.CreateLiteralCompletionData (possibleName.ToString ())); } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs index efd1a90c4a..1dd0639505 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs @@ -1580,31 +1580,7 @@ public class SomeControl : Control Assert.IsNull (provider.Find ("Right"), "enum 'Right' found"); } - /// - /// Bug 487236 - Object initializer completion uses wrong type - /// - [Test()] - public void TestBug487236B () - { - CompletionDataList provider = CreateCtrlSpaceProvider ( -@" -public class A -{ - public string Name { get; set; } -} - -class MyTest -{ - public void Test () - { - $A x = new NotExists () { $ - } -} -"); - Assert.IsNotNull (provider, "provider not found."); - - Assert.IsNull (provider.Find ("Name"), "property 'Name' found, but shouldn't'."); - } + /// /// Bug 487228 - No intellisense for implicit arrays diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs index 25b4be94d3..44be84d300 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs @@ -57,6 +57,32 @@ class MyTest Assert.IsNotNull (provider.Find ("Name"), "property 'Name' not found."); } + /// + /// Bug 487236 - Object initializer completion uses wrong type + /// + [Test()] + public void TestBug487236B () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +public class A +{ + public string Name { get; set; } +} + +class MyTest +{ + public void Test () + { + $A x = new NotExists () { $ + } +} +"); + Assert.IsNotNull (provider, "provider not found."); + + Assert.IsNull (provider.Find ("Name"), "property 'Name' found, but shouldn't'."); + } + [Test()] public void TestField () { diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/VariableDeclarationStatementTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/VariableDeclarationStatementTests.cs new file mode 100644 index 0000000000..75fac48074 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/VariableDeclarationStatementTests.cs @@ -0,0 +1,85 @@ +// +// VariableDeclarationStatementTests.cs +// +// Author: +// Mike Krüger +// +// Copyright (c) 2011 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; + +namespace ICSharpCode.NRefactory.CSharp.CodeCompletion +{ + public class VariableDeclarationStatementTests : TestBase + { + [Test()] + public void TestDefaultBehavior () + { + var provider = CodeCompletionBugTests.CreateProvider ( +@" +class MyTest +{ + public void Test () + { + $var v$ + } +} +"); + Assert.IsTrue (provider == null || provider.Count == 0, "provider not found."); + } + + [Test()] + public void TestIntNameProposal () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +class MyTest +{ + public void Test () + { + $int $ + } +} +"); + Assert.IsNotNull (provider.Find ("i"), "name proposal 'i' not found."); + Assert.IsNotNull (provider.Find ("j"), "name proposal 'j' not found."); + Assert.IsNotNull (provider.Find ("k"), "name proposal 'k' not found."); + } + + [Test()] + public void TestNameProposal () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +class MyTest +{ + public void Test () + { + $MyTest $ + } +} +"); + Assert.IsNotNull (provider.Find ("myTest"), "name proposal 'myTest' not found."); + Assert.IsNotNull (provider.Find ("test"), "name proposal 'test' not found."); + } + } +} + diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index ac60dcd333..834aeccd85 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -197,6 +197,7 @@ +