Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
181 ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs
@@ -397,7 +397,13 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
}
methodGroup = invocationResult.Item1 as MethodGroupResolveResult;
if (methodGroup != null) {
- return CreateParameterCompletion(methodGroup, invocationResult.Item2, invoke.Node, invoke.Unit, currentParameter, controlSpace);
+ return CreateParameterCompletion(
+ methodGroup,
+ invocationResult.Item2,
+ invoke.Node,
+ invoke.Unit,
+ currentParameter,
+ controlSpace);
}
return null;
case "=":
@@ -415,7 +421,11 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
}
if (resolveResult.Item1.Type.Kind == TypeKind.Enum) {
var wrapper = new CompletionDataWrapper(this);
- AddContextCompletion(wrapper, resolveResult.Item2, expressionOrVariableDeclaration.Node, expressionOrVariableDeclaration.Unit);
+ AddContextCompletion(
+ wrapper,
+ resolveResult.Item2,
+ expressionOrVariableDeclaration.Node,
+ expressionOrVariableDeclaration.Unit);
AddEnumMembers(wrapper, resolveResult.Item1.Type, resolveResult.Item2);
AutoCompleteEmptyMatch = false;
return wrapper.Result;
@@ -484,7 +494,14 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
if (token == "+=") {
string parameterDefinition = AddDelegateHandlers(wrapper, delegateType);
string varName = GetPreviousMemberReferenceExpression(tokenIndex);
- wrapper.Result.Add(factory.CreateEventCreationCompletionData(varName, delegateType, evt, parameterDefinition, currentMember, currentType));
+ wrapper.Result.Add(
+ factory.CreateEventCreationCompletionData(
+ varName,
+ delegateType,
+ evt,
+ parameterDefinition,
+ currentMember,
+ currentType));
}
return wrapper.Result;
@@ -498,7 +515,11 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
return HandleEnumContext();
var wrapper = new CompletionDataWrapper(this);
- AddTypesAndNamespaces(wrapper, GetState(), null, t => currentType != null && !currentType.ReflectionName.Equals(t.ReflectionName) ? t : null);
+ AddTypesAndNamespaces(
+ wrapper,
+ GetState(),
+ null,
+ t => currentType != null && !currentType.ReflectionName.Equals(t.ReflectionName) ? t : null);
return wrapper.Result;
}
return null;
@@ -549,7 +570,8 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
}
if (identifierStart.Node is MemberReferenceExpression) {
- return HandleMemberReferenceCompletion(new ExpressionResult(((MemberReferenceExpression)identifierStart.Node).Target, identifierStart.Unit));
+ return HandleMemberReferenceCompletion(
+ new ExpressionResult(((MemberReferenceExpression)identifierStart.Node).Target, identifierStart.Unit));
}
if (identifierStart.Node is Identifier) {
@@ -631,6 +653,16 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
}
return null;
}
+
+ var astResolver = new CSharpAstResolver(GetState(), identifierStart.Unit, CSharpParsedFile);
+
+ foreach (var type in CreateFieldAction.GetValidTypes(astResolver, (Expression)n)) {
+ if (type.Kind == TypeKind.Delegate) {
+ AddDelegateHandlers(contextList, type, false, false);
+ AutoSelect = false;
+ AutoCompleteEmptyMatch = false;
+ }
+ }
}
// Handle object/enumerable initialzer expressions: "new O () { P$"
@@ -669,12 +701,11 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
AutoSelect = false;
AutoCompleteEmptyMatch = false;
}
-
}
}
}
}
-
+
if (n != null && n.Parent is ObjectCreateExpression) {
var invokeResult = ResolveExpression(n.Parent, identifierStart.Unit);
var mgr = invokeResult != null ? invokeResult.Item1 as ResolveResult : null;
@@ -763,16 +794,21 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
}
if (n is MemberType) {
resolveResult = ResolveExpression(((MemberType)n).Target, identifierStart.Unit);
- return CreateTypeAndNamespaceCompletionData(location, resolveResult.Item1, ((MemberType)n).Target, resolveResult.Item2);
+ return CreateTypeAndNamespaceCompletionData(
+ location,
+ resolveResult.Item1,
+ ((MemberType)n).Target,
+ resolveResult.Item2
+ );
}
if (n != null/* && !(identifierStart.Item2 is TypeDeclaration)*/) {
- csResolver = new CSharpResolver (ctx);
- var nodes = new List<AstNode> ();
+ csResolver = new CSharpResolver(ctx);
+ var nodes = new List<AstNode>();
nodes.Add(n);
if (n.Parent is ICSharpCode.NRefactory.CSharp.Attribute) {
nodes.Add(n.Parent);
}
- var astResolver = new CSharpAstResolver (csResolver, identifierStart.Unit, CSharpParsedFile);
+ var astResolver = new CSharpAstResolver(csResolver, identifierStart.Unit, CSharpParsedFile);
astResolver.ApplyNavigator(new NodeListResolveVisitorNavigator (nodes));
try {
csResolver = astResolver.GetResolverStateBefore(n);
@@ -1328,7 +1364,12 @@ IEnumerable<ICompletionData> HandleKeywordCompletion(int wordStart, string word)
}
var isAsWrapper = new CompletionDataWrapper(this);
var def = isAsType != null ? isAsType.GetDefinition() : null;
- AddTypesAndNamespaces(isAsWrapper, GetState(), null, t => t.GetDefinition() == null || def == null || t.GetDefinition().IsDerivedFrom(def) ? t : null, m => false);
+ AddTypesAndNamespaces(
+ isAsWrapper,
+ GetState(),
+ null,
+ t => t.GetDefinition() == null || def == null || t.GetDefinition().IsDerivedFrom(def) ? t : null,
+ m => false);
return isAsWrapper.Result;
// {
// CompletionDataList completionList = new ProjectDomCompletionDataList ();
@@ -1421,7 +1462,7 @@ IEnumerable<ICompletionData> HandleKeywordCompletion(int wordStart, string word)
return null;
}
var state = GetState();
-
+
if (state.CurrentTypeDefinition != null && (state.CurrentTypeDefinition.Kind == TypeKind.Class || state.CurrentTypeDefinition.Kind == TypeKind.Struct)) {
string modifiers = document.GetText(firstMod, wordStart - firstMod);
return GetPartialCompletionData(state.CurrentTypeDefinition, modifiers);
@@ -1452,88 +1493,11 @@ IEnumerable<ICompletionData> HandleKeywordCompletion(int wordStart, string word)
IType hintType = null;
var expressionOrVariableDeclaration = GetNewExpressionAt(j);
- AstNode newParentNode = null;
- AstType hintTypeAst = null;
- if (expressionOrVariableDeclaration != null) {
- newParentNode = expressionOrVariableDeclaration.Node.Parent;
- if (newParentNode is VariableInitializer) {
- newParentNode = newParentNode.Parent;
- }
- }
- if (newParentNode is InvocationExpression) {
- var invoke = (InvocationExpression)newParentNode;
- var resolved = ResolveExpression(invoke, expressionOrVariableDeclaration.Unit);
- if (resolved != null) {
- var mgr = resolved.Item1 as CSharpInvocationResolveResult;
- if (mgr != null) {
- int i1 = 0;
- foreach (var a in invoke.Arguments) {
- if (a == expressionOrVariableDeclaration.Node) {
- if (mgr.Member.Parameters.Count > i1) {
- hintType = mgr.Member.Parameters [i1].Type;
- }
- break;
- }
- i1++;
- }
- }
- }
- }
-
- if (newParentNode is ObjectCreateExpression) {
- var invoke = (ObjectCreateExpression)newParentNode;
- var resolved = ResolveExpression(invoke, expressionOrVariableDeclaration.Unit);
- if (resolved != null) {
- var mgr = resolved.Item1 as CSharpInvocationResolveResult;
- if (mgr != null) {
- int i1 = 0;
- foreach (var a in invoke.Arguments) {
- if (a == expressionOrVariableDeclaration.Node) {
- if (mgr.Member.Parameters.Count > i1) {
- hintType = mgr.Member.Parameters [i1].Type;
- }
- break;
- }
- i1++;
- }
- }
- }
- }
-
- if (newParentNode is AssignmentExpression) {
- var assign = (AssignmentExpression)newParentNode;
- var resolved = ResolveExpression(assign.Left, expressionOrVariableDeclaration.Unit);
- if (resolved != null) {
- hintType = resolved.Item1.Type;
- }
- }
-
- if (newParentNode is VariableDeclarationStatement) {
- var varDecl = (VariableDeclarationStatement)newParentNode;
- hintTypeAst = varDecl.Type;
- var resolved = ResolveExpression(varDecl.Type, expressionOrVariableDeclaration.Unit);
- if (resolved != null) {
- hintType = resolved.Item1.Type;
- }
- }
-
- if (newParentNode is FieldDeclaration) {
- var varDecl = (FieldDeclaration)newParentNode;
- hintTypeAst = varDecl.ReturnType;
- var resolved = ResolveExpression(varDecl.ReturnType, expressionOrVariableDeclaration.Unit);
- if (resolved != null) {
- hintType = resolved.Item1.Type;
- }
- }
-
- if (newParentNode is ReturnStatement) {
- //var varDecl = (ReturnStatement)newParentNode;
- if (ctx.CurrentMember != null) {
- hintType = ctx.CurrentMember.ReturnType;
- }
- }
+
+ var astResolver = new CSharpAstResolver(GetState(), expressionOrVariableDeclaration.Unit, CSharpParsedFile);
+ hintType = CreateFieldAction.GetValidTypes(astResolver, expressionOrVariableDeclaration.Node as Expression).FirstOrDefault ();
- return CreateTypeCompletionData(hintType, hintTypeAst);
+ return CreateTypeCompletionData(hintType);
case "yield":
var yieldDataList = new CompletionDataWrapper(this);
DefaultCompletionString = "return";
@@ -1589,7 +1553,7 @@ public override int CompareTo (CompletionCategory other)
}
}
- IEnumerable<ICompletionData> CreateTypeCompletionData(IType hintType, AstType hintTypeAst)
+ IEnumerable<ICompletionData> CreateTypeCompletionData(IType hintType)
{
var wrapper = new CompletionDataWrapper(this);
var state = GetState();
@@ -1648,9 +1612,9 @@ IEnumerable<ICompletionData> CreateTypeCompletionData(IType hintType, AstType hi
wrapper.AddType(array, amb.ConvertType(array));
}
} else {
- DefaultCompletionString = hintTypeAst.ToString();
var hint = wrapper.AddType(hintType, DefaultCompletionString);
if (hint != null) {
+ DefaultCompletionString = hint.DisplayText;
hint.CompletionCategory = derivedTypesCategory;
}
}
@@ -1944,22 +1908,18 @@ static bool IsAttributeContext(AstNode node)
return n is Attribute;
}
- IType GuessHintType(AstNode resolvedNode)
- {
- ObjectCreateExpression oce = resolvedNode.Parent as ObjectCreateExpression ?? (ObjectCreateExpression)resolvedNode.Ancestors.FirstOrDefault(n => n is ObjectCreateExpression);
- if (oce != null && oce.Parent is ReturnStatement) {
- return ctx.CurrentMember != null ? ctx.CurrentMember.ReturnType : null;
- }
- return null;
- }
-
IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData(TextLocation location, ResolveResult resolveResult, AstNode resolvedNode, CSharpResolver state)
{
if (resolveResult == null || resolveResult.IsError) {
return null;
}
+ var exprParent = resolvedNode.GetParent<Expression>();
+ var unit = exprParent != null ? exprParent.GetParent<CompilationUnit>() : null;
- var hintType = GuessHintType(resolvedNode);
+ var astResolver = unit != null ? new CSharpAstResolver(state, unit, CSharpParsedFile) : null;
+ IType hintType = exprParent != null && astResolver != null ?
+ CreateFieldAction.GetValidTypes(astResolver, exprParent) .FirstOrDefault() :
+ null;
var result = new CompletionDataWrapper (this);
if (resolveResult is NamespaceResolveResult) {
var nr = (NamespaceResolveResult)resolveResult;
@@ -2023,7 +1983,14 @@ IEnumerable<ICompletionData> CreateParameterCompletion(MethodGroupResolveResult
continue;
string parameterDefinition = AddDelegateHandlers(result, resolvedType);
string varName = "Handle" + method.Parameters [parameter].Type.Name + method.Parameters [parameter].Name;
- result.Result.Add(factory.CreateEventCreationCompletionData(varName, resolvedType, null, parameterDefinition, currentMember, currentType));
+ result.Result.Add(
+ factory.CreateEventCreationCompletionData(
+ varName,
+ resolvedType,
+ null,
+ parameterDefinition,
+ currentMember,
+ currentType));
}
}
if (!controlSpace) {
View
32 ICSharpCode.NRefactory.CSharp/Formatter/AstFormattingVisitor.cs
@@ -863,17 +863,21 @@ void FormatParameters(AstNode node)
}
} else {
foreach (var arg in parameters) {
- if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
- ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
- } else {
- ForceSpacesBefore(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
+ if (arg.PrevSibling != null) {
+ if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
+ ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
+ } else {
+ ForceSpacesBefore(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
+ }
}
arg.AcceptVisitor(this);
}
ForceSpacesBeforeRemoveNewLines(rParToken, spaceWithinMethodCallParentheses);
}
- foreach (CSharpTokenNode comma in rParToken.Parent.Children.Where(n => n.Role == Roles.Comma)) {
- ForceSpacesBefore(comma, spaceBeforeMethodCallParameterComma);
+ if (!rParToken.IsNull) {
+ foreach (CSharpTokenNode comma in rParToken.Parent.Children.Where(n => n.Role == Roles.Comma)) {
+ ForceSpacesBefore(comma, spaceBeforeMethodCallParameterComma);
+ }
}
}
@@ -1764,17 +1768,21 @@ void FormatArguments(AstNode node)
}
} else {
foreach (var arg in arguments) {
- if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
- ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
- } else {
- ForceSpacesBefore(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
+ if (arg.PrevSibling != null) {
+ if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
+ ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
+ } else {
+ ForceSpacesBefore(arg, spaceAfterMethodCallParameterComma && arg.PrevSibling.Role == Roles.Comma);
+ }
}
arg.AcceptVisitor(this);
}
ForceSpacesBeforeRemoveNewLines(rParToken, spaceWithinMethodCallParentheses);
}
- foreach (CSharpTokenNode comma in rParToken.Parent.Children.Where(n => n.Role == Roles.Comma)) {
- ForceSpacesBefore(comma, spaceBeforeMethodCallParameterComma);
+ if (!rParToken.IsNull) {
+ foreach (CSharpTokenNode comma in rParToken.Parent.Children.Where(n => n.Role == Roles.Comma)) {
+ ForceSpacesBefore(comma, spaceBeforeMethodCallParameterComma);
+ }
}
}
View
10 ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs
@@ -149,6 +149,13 @@ internal static IEnumerable<IType> GetValidTypes(CSharpAstResolver resolver, Exp
}
}
+ if (expr.Parent is ArrayCreateExpression) {
+ var ace = (ArrayCreateExpression)expr.Parent;
+ if (!ace.Type.IsNull) {
+ return new [] { resolver.Resolve(ace.Type).Type };
+ }
+ }
+
if (expr.Parent is InvocationExpression) {
var parent = expr.Parent;
if (parent is InvocationExpression) {
@@ -159,6 +166,9 @@ internal static IEnumerable<IType> GetValidTypes(CSharpAstResolver resolver, Exp
if (expr.Parent is VariableInitializer) {
var initializer = (VariableInitializer)expr.Parent;
+ var field = initializer.GetParent<FieldDeclaration>();
+ if (field != null)
+ return new [] { resolver.Resolve(field.ReturnType).Type };
return new [] { resolver.Resolve(initializer).Type };
}
View
9 ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs
@@ -4164,12 +4164,13 @@ public class Test
/// Bug 1747 - [New Resolver] Code completion issues when declaring a generic dictionary
/// </summary>
[Test()]
- public void Test1747 ()
+ public void Test1747()
{
- var provider = CreateProvider (
-@"public class Test
+ var provider = CreateProvider(
+@"using System.Collections.Generic;
+public class Test
{
- $Dictionary<int, string> field = new $
+ $Dictionary<int,string> field = new $
}
");
Assert.IsNotNull (provider, "provider not found.");
View
77 ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/DelegateContextTests.cs
@@ -0,0 +1,77 @@
+//
+// DelegateContextTests.cs
+//
+// Author:
+// Mike Krüger <mkrueger@xamarin.com>
+//
+// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
+//
+// 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
+{
+ [TestFixture()]
+ public class DelegateContextTests : TestBase
+ {
+ /// <summary>
+ /// Bug 4483 - Completion too aggressive for anonymous methods
+ /// </summary>
+ [Test()]
+ public void TestBug4483()
+ {
+ // note 'string bar = new Test ().ToString ()' would be valid.
+ CodeCompletionBugTests.CombinedProviderTest(
+@"using System;
+
+public class Test
+{
+ void TestFoo()
+ {
+ $Action act = a$
+ }
+}
+", provider => {
+ Assert.IsFalse(provider.AutoSelect);
+ });
+ }
+
+ [Test()]
+ public void TestParameterContext()
+ {
+ // note 'string bar = new Test ().ToString ()' would be valid.
+ CodeCompletionBugTests.CombinedProviderTest(
+@"using System;
+
+public class Test
+{
+ void TestFoo(Action act)
+ {
+ $TestFoo(a$
+ }
+}
+", provider => {
+ Assert.IsFalse(provider.AutoSelect);
+ Assert.IsNotNull(provider.Find("delegate"));
+ });
+ }
+ }
+}
+
View
21 ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/ObjectInitializerTests.cs
@@ -90,7 +90,7 @@ public void Test ()
/// <summary>
/// Bug 487236 - Object initializer completion uses wrong type
/// </summary>
- [Test(), Ignore ("FIXME!")]
+ [Test()]
public void TestBug487236 ()
{
CompletionDataList provider = CodeCompletionBugTests.CreateCtrlSpaceProvider (
@@ -458,6 +458,25 @@ void Method ()
");
Assert.IsNotNull(provider.Find("Foo"), "'Foo' not found.");
}
+
+ [Test()]
+ public void TestArrayInitializerObjectCreation()
+ {
+ CodeCompletionBugTests.CombinedProviderTest(
+@"using System;
+using System.Collections.Generic;
+
+class MyTest
+{
+ public void Test ()
+ {
+ $new IEnumerable<string>[] { new L$
+ }
+}
+", provider => {
+ Assert.IsNotNull(provider.Find("List<string>"), "class 'List<string>' not found.");
+ });
+ }
}
}
View
3 ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
@@ -265,6 +265,7 @@
<Compile Include="CSharp\Parser\Bugs\ParserBugTests.cs" />
<Compile Include="FormattingTests\TestKeepReformattingRules.cs" />
<Compile Include="FormattingTests\TestWrapping.cs" />
+ <Compile Include="CSharp\CodeCompletion\DelegateContextTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">
@@ -301,7 +302,7 @@
<Policies>
<TextStylePolicy TabWidth="4" EolMarker="Unix" inheritsSet="Mono" inheritsScope="text/plain" scope="text/plain" />
<TextStylePolicy FileWidth="120" TabWidth="4" EolMarker="Unix" inheritsSet="Mono" inheritsScope="text/plain" scope="text/x-csharp" />
- <CSharpFormattingPolicy inheritsSet="1TBS" inheritsScope="text/x-csharp" scope="text/x-csharp" />
+ <CSharpFormattingPolicy IndentSwitchBody="True" IfElseBraceForcement="AddBraces" ForBraceForcement="AddBraces" ForEachBraceForcement="AddBraces" WhileBraceForcement="AddBraces" UsingBraceForcement="AddBraces" FixedBraceForcement="AddBraces" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
</Policies>
</Properties>
</MonoDevelop>

No commit comments for this range

Something went wrong with that request. Please try again.