diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs index 805e241a03..fdce3045cf 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs @@ -388,7 +388,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion if (!char.IsLetterOrDigit (last) && last != '_') return null; } - if (identifierStart == null) return null; @@ -532,9 +531,22 @@ namespace ICSharpCode.NRefactory.CSharp.Completion IEnumerable DefaultControlSpaceItems () { var wrapper = new CompletionDataWrapper (this); + while (offset > 0 && char.IsWhiteSpace (document.GetCharAt (offset))) { + offset--; + } + location = document.GetLocation (offset); + var xp = GetExpressionAtCursor (); - var node = Unit.GetNodeAt (location); - var rr = ResolveExpression (CSharpParsedFile, node, Unit); + AstNode node; + Tuple rr; + if (xp != null) { + node = xp.Item2; + rr = ResolveExpression (xp.Item1, node, xp.Item3); + } else { + node = Unit.GetNodeAt (location); + rr = ResolveExpression (CSharpParsedFile, node, Unit); + } + AddContextCompletion (wrapper, rr != null ? rr.Item2 : GetState (), node); return wrapper.Result; @@ -1571,6 +1583,18 @@ namespace ICSharpCode.NRefactory.CSharp.Completion // try statement if (expr == null) { expr = tmpUnit.GetNodeAt (location.Line, location.Column - 1); + baseUnit = tmpUnit; + } + + if (expr == null) { + var forStmt = tmpUnit.GetNodeAt (location.Line, location.Column - 3); + expr = forStmt; + if (forStmt != null && forStmt.EmbeddedStatement.IsNull) { + var id = new IdentifierExpression ("stub"); + forStmt.EmbeddedStatement = new BlockStatement () { Statements = { new ExpressionStatement (id) }}; + expr = id; + } + baseUnit = tmpUnit; } if (expr == null) diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs index c0142da470..8daa2f2fd3 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs @@ -3217,6 +3217,24 @@ class Foo Assert.IsNotNull (provider.Find ("i"), "variable 'i' not found."); } + /// + /// Bug 675956 - Completion in for loops is broken + /// + [Test()] + public void TestBug675956Case2 () + { + CompletionDataList provider = CreateProvider ( +@"class Test +{ + public static void Main (string[] args) + { + $for (int i = 0; i$ + } +} +"); + Assert.IsNotNull (provider.Find ("i"), "variable 'i' not found."); + } + /// /// Bug 676311 - auto completion too few proposals in fluent API (Moq) ///