diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs index e218de90fd..7e59765c14 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs @@ -370,6 +370,27 @@ namespace ICSharpCode.NRefactory.CSharp.Completion AddKeywords (dataList, linqKeywords); return dataList.Result; } + + var contextList = new CompletionDataWrapper (this); + if (identifierStart == null) { + var unit = ParseStub ("get; }", false); + var node = unit.GetNodeAt (location, cn => !(cn is CSharpTokenNode)); + if (node is Accessor) + node = node.Parent; + if (node is PropertyDeclaration) { + contextList.AddCustom ("get"); + contextList.AddCustom ("set"); + AddKeywords (contextList, accessorModifierKeywords); + } else if (node is CustomEventDeclaration) { + contextList.AddCustom ("add"); + contextList.AddCustom ("remove"); + } else { + AddContextCompletion (contextList, GetState (), null); + } + + return contextList.Result; + } + if (!(char.IsLetter (completionChar) || completionChar == '_') && (identifierStart == null || !(identifierStart.Item2 is ArrayInitializerExpression))) return controlSpace ? DefaultControlSpaceItems () : null; char prevCh = offset > 2 ? document.GetCharAt (offset - 2) : '\0'; @@ -387,11 +408,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion if (!char.IsLetterOrDigit (last) && last != '_') return null; } - var contextList = new CompletionDataWrapper (this); - if (identifierStart == null) { - AddContextCompletion (contextList, GetState (), null); - return contextList.Result; - } CSharpResolver csResolver; AstNode n = identifierStart.Item2; if (n is ArrayInitializerExpression) { @@ -554,7 +570,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion void AddContextCompletion (CompletionDataWrapper wrapper, CSharpResolver state, AstNode node) { - Console.WriteLine ("!!!!"); if (state == null) return; foreach (var variable in state.LocalVariables) { @@ -587,7 +602,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion } else if (state.CurrentTypeDefinition != null) { AddKeywords (wrapper, typeLevelKeywords); } else { - Console.WriteLine ("3"); AddKeywords (wrapper, globalLevelKeywords); } @@ -604,7 +618,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion { var n = node; while (n != null && !(n is MemberDeclaration)) { - Console.WriteLine (n.GetType ()); if (n is SwitchStatement) return true; if (n is BlockStatement) @@ -1482,8 +1495,6 @@ namespace ICSharpCode.NRefactory.CSharp.Completion } #region Parsing methods - - Tuple GetExpressionBeforeCursor () { CompilationUnit baseUnit; @@ -1877,6 +1888,10 @@ namespace ICSharpCode.NRefactory.CSharp.Completion "abstract", "sealed", "static", "unsafe", "partial" }; + static string[] accessorModifierKeywords = new string [] { + "public", "internal", "protected", "private" + }; + static string[] typeLevelKeywords = new string [] { "public", "internal", "protected", "private", "class", "interface", "struct", "enum", "delegate", diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs index e82aa307c3..acd5b539fd 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/KeywordTests.cs @@ -139,6 +139,121 @@ class Test Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); Assert.IsNull (provider.Find ("namespace"), "keyword 'namespace' found."); } + + [Test()] + public void GetSetKeywordTest () + { + var provider = CodeCompletionBugTests.CreateProvider ( +@"class Test +{ + public int MyProperty { + $g$ +} +"); + Assert.IsNotNull (provider, "provider == null"); + Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); + Assert.IsNotNull (provider.Find ("get"), "keyword 'get' not found."); + Assert.IsNotNull (provider.Find ("set"), "keyword 'set' not found."); + } + + + [Test()] + public void GetSetKeywordTestCtrlSpace () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@"class Test +{ + public int MyProperty { + $$ +} +"); + Assert.IsNotNull (provider, "provider == null"); + Assert.IsNotNull (provider.Find ("public"), "keyword 'public' not found."); + Assert.IsNotNull (provider.Find ("get"), "keyword 'get' not found."); + Assert.IsNotNull (provider.Find ("set"), "keyword 'set' not found."); + } + + + [Test()] + public void AddRemoveKeywordTest () + { + var provider = CodeCompletionBugTests.CreateProvider ( +@" +using System; +class Test +{ + public event EventHandler MyProperty { + $g$ +} +"); + Assert.IsNotNull (provider, "provider == null"); + Assert.AreEqual (2, provider.Count); + Assert.IsNotNull (provider.Find ("add"), "keyword 'add' not found."); + Assert.IsNotNull (provider.Find ("remove"), "keyword 'remove' not found."); + } + + [Test()] + public void AddRemoveKeywordTestCtrlSpace () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +using System; +class Test +{ + public event EventHandler MyProperty { + $g$ +} +"); + Assert.IsNotNull (provider, "provider == null"); + Assert.AreEqual (2, provider.Count); + Assert.IsNotNull (provider.Find ("add"), "keyword 'add' not found."); + Assert.IsNotNull (provider.Find ("remove"), "keyword 'remove' not found."); + } + + + [Test()] + public void IsAsKeywordTest () + { + var provider = CodeCompletionBugTests.CreateProvider ( +@" +using System; +class Test +{ + public void Method () + { + void TestMe (object o) + { + if (o $i$ + } + } +} +"); + Assert.IsNotNull (provider, "provider == null"); + Assert.IsNotNull (provider.Find ("is"), "keyword 'is' not found."); + Assert.IsNotNull (provider.Find ("as"), "keyword 'as' not found."); + } + + [Test()] + public void IsAsKeywordTestCtrlSpace () + { + var provider = CodeCompletionBugTests.CreateCtrlSpaceProvider ( +@" +using System; +class Test +{ + public void Method () + { + void TestMe (object o) + { + if (o i$$ + } + } +} +"); + Assert.IsNotNull (provider, "provider == null"); + Assert.IsNotNull (provider.Find ("is"), "keyword 'is' not found."); + Assert.IsNotNull (provider.Find ("as"), "keyword 'as' not found."); + } } }