Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 129bf84

Browse files
committed
Update to NRefactory commit '2a46efacdad982a6fcf07303f57a917585356462'
2a46efa CSharpResolver: return ErrorResolveResult instead of crashing when given an invalid operator type. 2371920 Merge pull request icsharpcode/NRefactory#388 from mono-soc-2013/MateY-IndentEngine dec33c5 Fixed Bug 18463 9719b41 Added failing unit test. 9d2209e Updated mcs 98f042d Fix #404: Crash in CSharpCompletionEngine.GetLineIndent() when line has no indentation. 39e17c9 Documentation for IAmbience 01bf167 Fix icsharpcode/NRefactory#386: A using statement referring to a missing nested type causes infinite recursion in resolver 2cd1a17 Re-enable NameLookupTests.NamespaceDefinitionOverwritesTypeName 0c4a81e Fixed bug in using declaration namespace name. 09531ca Fixed bug in in IndentBlocksInsideExpressions option. 484160b Added 'IndentBlocksInsideExpressions' formatting option. a35eafb Improved event method naming. fd9e073 Removed duplicate event creation data. 81436d0 Added new 'bold' display flag for code completion.
1 parent 56b3d62 commit 129bf84

40 files changed

Lines changed: 732 additions & 473 deletions

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/NamespaceDeclaration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public AstType NamespaceName {
5555

5656
public string Name {
5757
get {
58-
return NamespaceName.ToString();
58+
return UsingDeclaration.ConstructNamespace(NamespaceName);
5959
}
6060
set {
6161
var arr = value.Split('.');

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/GeneralScope/UsingDeclaration.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System;
2828
using System.Linq;
2929
using System.Text;
30+
using System.Collections.Generic;
3031

3132
namespace ICSharpCode.NRefactory.CSharp
3233
{
@@ -54,7 +55,29 @@ public AstType Import {
5455
}
5556

5657
public string Namespace {
57-
get { return this.Import.ToString(); }
58+
get { return ConstructNamespace (Import); }
59+
}
60+
61+
internal static string ConstructNamespace (AstType type)
62+
{
63+
var stack = new Stack<string>();
64+
while (type is MemberType) {
65+
var mt = (MemberType)type;
66+
stack.Push(mt.MemberName);
67+
type = mt.Target;
68+
if (mt.IsDoubleColon) {
69+
stack.Push("::");
70+
} else {
71+
stack.Push(".");
72+
}
73+
}
74+
if (type is SimpleType)
75+
stack.Push(((SimpleType)type).Identifier);
76+
77+
var result = new StringBuilder();
78+
while (stack.Count > 0)
79+
result.Append(stack.Pop());
80+
return result.ToString();
5881
}
5982

6083
public CSharpTokenNode SemicolonToken {

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngine.cs

Lines changed: 23 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ IEnumerable<ICompletionData> HandleObjectInitializer(SyntaxTree unit, AstNode n)
350350
}
351351
var lookup = new MemberLookup(ctx.CurrentTypeDefinition, Compilation.MainAssembly);
352352
bool isProtectedAllowed = ctx.CurrentTypeDefinition != null && initializerType.GetDefinition() != null ?
353-
ctx.CurrentTypeDefinition.IsDerivedFrom(initializerType.GetDefinition()) :
354-
false;
353+
ctx.CurrentTypeDefinition.IsDerivedFrom(initializerType.GetDefinition()) :
354+
false;
355355
foreach (var m in initializerType.GetMembers (m => m.SymbolKind == SymbolKind.Field)) {
356356
var f = m as IField;
357357
if (f != null && (f.IsReadOnly || f.IsConst))
@@ -844,6 +844,7 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
844844
return null;
845845
case "+=":
846846
case "-=":
847+
var curTokenIndex = tokenIndex;
847848
GetPreviousToken(ref tokenIndex, false);
848849

849850
expressionOrVariableDeclaration = GetExpressionAt(tokenIndex);
@@ -881,17 +882,8 @@ IEnumerable<ICompletionData> MagicKeyCompletion(char completionChar, bool contro
881882
if (token == "+=") {
882883
string parameterDefinition = AddDelegateHandlers(
883884
wrapper,
884-
delegateType
885-
);
886-
string varName = GetPreviousMemberReferenceExpression(tokenIndex);
887-
wrapper.Result.Add(
888-
factory.CreateEventCreationCompletionData(
889-
varName,
890-
delegateType,
891-
evt,
892-
parameterDefinition,
893-
currentMember,
894-
currentType)
885+
delegateType,
886+
optDelegateName: GuessEventHandlerMethodName(curTokenIndex)
895887
);
896888
}
897889

@@ -2316,7 +2308,7 @@ string GetLineIndent(int lineNr)
23162308
for (int j = line.Offset; j < line.EndOffset; j++) {
23172309
char ch = document.GetCharAt(j);
23182310
if (!char.IsWhiteSpace(ch)) {
2319-
return document.GetText(line.Offset, j - line.Offset - 1);
2311+
return document.GetText(line.Offset, j - line.Offset);
23202312
}
23212313
}
23222314
return "";
@@ -2364,7 +2356,7 @@ IEnumerable<ICompletionData> CreateConstructorCompletionData(IType hintType)
23642356
// check for valid constructors
23652357
if (t.GetConstructors().Count() > 0) {
23662358
bool isProtectedAllowed = currentType != null ?
2367-
currentType.Resolve(ctx).GetDefinition().IsDerivedFrom(t.GetDefinition()) : false;
2359+
currentType.Resolve(ctx).GetDefinition().IsDerivedFrom(t.GetDefinition()) : false;
23682360
if (!t.GetConstructors().Any(m => lookup.IsAccessible(m, isProtectedAllowed))) {
23692361
return null;
23702362
}
@@ -2589,36 +2581,10 @@ void AddKeywords(CompletionDataWrapper wrapper, IEnumerable<string> keywords)
25892581
}
25902582
}
25912583

2592-
public string GetPreviousMemberReferenceExpression(int tokenIndex)
2584+
public string GuessEventHandlerMethodName(int tokenIndex)
25932585
{
25942586
string result = GetPreviousToken(ref tokenIndex, false);
2595-
result = GetPreviousToken(ref tokenIndex, false);
2596-
if (result != ".") {
2597-
result = null;
2598-
} else {
2599-
var names = new List<string>();
2600-
while (result == ".") {
2601-
result = GetPreviousToken(ref tokenIndex, false);
2602-
if (result == "this") {
2603-
names.Add("handle");
2604-
} else if (result != null) {
2605-
string trimmedName = result.Trim();
2606-
if (trimmedName.Length == 0) {
2607-
break;
2608-
}
2609-
names.Insert(0, trimmedName);
2610-
}
2611-
result = GetPreviousToken(ref tokenIndex, false);
2612-
}
2613-
result = String.Join("", names.ToArray());
2614-
foreach (char ch in result) {
2615-
if (!char.IsLetterOrDigit(ch) && ch != '_') {
2616-
result = "";
2617-
break;
2618-
}
2619-
}
2620-
}
2621-
return result;
2587+
return "Handle" + result;
26222588
}
26232589

26242590
bool MatchDelegate(IType delegateType, IMethod method)
@@ -2654,13 +2620,13 @@ string AddDelegateHandlers(CompletionDataWrapper completionList, IType delegateT
26542620
"delegate",
26552621
"Creates anonymous delegate.",
26562622
"delegate {" + EolMarker + thisLineIndent + IndentString + "|" + delegateEndString
2657-
);
2623+
).DisplayFlags |= DisplayFlags.MarkedBold;
26582624
if (LanguageVersion.Major >= 5) {
26592625
completionList.AddCustom(
26602626
"async delegate",
26612627
"Creates anonymous async delegate.",
26622628
"async delegate {" + EolMarker + thisLineIndent + IndentString + "|" + delegateEndString
2663-
);
2629+
).DisplayFlags |= DisplayFlags.MarkedBold;
26642630
}
26652631
}
26662632
var sb = new StringBuilder("(");
@@ -2691,60 +2657,52 @@ string AddDelegateHandlers(CompletionDataWrapper completionList, IType delegateT
26912657
"delegate" + signature,
26922658
"Creates anonymous delegate.",
26932659
"delegate" + signature + " {" + EolMarker + thisLineIndent + IndentString + "|" + delegateEndString
2694-
);
2660+
).DisplayFlags |= DisplayFlags.MarkedBold;
26952661
if (LanguageVersion.Major >= 5) {
26962662
completionList.AddCustom(
26972663
"async delegate" + signature,
26982664
"Creates anonymous async delegate.",
26992665
"async delegate" + signature + " {" + EolMarker + thisLineIndent + IndentString + "|" + delegateEndString
2700-
);
2666+
).DisplayFlags |= DisplayFlags.MarkedBold;
27012667
}
27022668
if (!completionList.Result.Any(data => data.DisplayText == sb.ToString())) {
27032669
completionList.AddCustom(
27042670
signature,
27052671
"Creates typed lambda expression.",
27062672
signature + " => |" + (addSemicolon ? ";" : "")
2707-
);
2673+
).DisplayFlags |= DisplayFlags.MarkedBold;
27082674
if (LanguageVersion.Major >= 5) {
27092675
completionList.AddCustom(
27102676
"async " + signature,
27112677
"Creates typed async lambda expression.",
27122678
"async " + signature + " => |" + (addSemicolon ? ";" : "")
2713-
);
2679+
).DisplayFlags |= DisplayFlags.MarkedBold;
27142680
}
27152681

27162682
if (!delegateMethod.Parameters.Any(p => p.IsOut || p.IsRef) && !completionList.Result.Any(data => data.DisplayText == sbWithoutTypes.ToString())) {
27172683
completionList.AddCustom(
27182684
sbWithoutTypes.ToString(),
27192685
"Creates lambda expression.",
27202686
sbWithoutTypes + " => |" + (addSemicolon ? ";" : "")
2721-
);
2687+
).DisplayFlags |= DisplayFlags.MarkedBold;
27222688
if (LanguageVersion.Major >= 5) {
27232689
completionList.AddCustom(
27242690
"async " + sbWithoutTypes,
27252691
"Creates async lambda expression.",
27262692
"async " + sbWithoutTypes + " => |" + (addSemicolon ? ";" : "")
2727-
);
2693+
).DisplayFlags |= DisplayFlags.MarkedBold;
27282694
}
27292695
}
27302696
}
27312697

27322698
}
27332699

2734-
string varName = "Handle" + delegateType.Name + optDelegateName;
2735-
completionList.Add(factory.CreateEventCreationCompletionData(varName, delegateType, null, signature, currentMember, currentType));
2700+
string varName = optDelegateName ?? "Handle" + delegateType.Name;
27362701

2702+
var ecd = factory.CreateEventCreationCompletionData(varName, delegateType, null, signature, currentMember, currentType);
2703+
ecd.DisplayFlags |= DisplayFlags.MarkedBold;
2704+
completionList.Add(ecd);
27372705

2738-
/* TODO:Make factory method out of it.
2739-
// It's needed to temporarly disable inserting auto matching bracket because the anonymous delegates are selectable with '('
2740-
// otherwise we would end up with () => )
2741-
if (!containsDelegateData) {
2742-
var savedValue = MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket;
2743-
MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket = false;
2744-
completionList.Result.CompletionListClosed += delegate {
2745-
MonoDevelop.SourceEditor.DefaultSourceEditorOptions.Instance.AutoInsertMatchingBracket = savedValue;
2746-
};
2747-
}*/
27482706
return sb.ToString();
27492707
}
27502708

@@ -2835,7 +2793,7 @@ IEnumerable<ICompletionData> CreateTypeAndNamespaceCompletionData(TextLocation l
28352793
var astResolver = unit != null ? CompletionContextProvider.GetResolver(state, unit) : null;
28362794
IType hintType = exprParent != null && astResolver != null ?
28372795
TypeGuessing.GetValidTypes(astResolver, exprParent).FirstOrDefault() :
2838-
null;
2796+
null;
28392797
var result = new CompletionDataWrapper(this);
28402798
var lookup = new MemberLookup(
28412799
ctx.CurrentTypeDefinition,
@@ -2900,7 +2858,7 @@ void CreateParameterForInvocation(CompletionDataWrapper result, IMethod method,
29002858
if (resolvedType.Kind == TypeKind.Delegate) {
29012859
if (addedDelegates.Contains(resolvedType.ReflectionName))
29022860
return;
2903-
AddDelegateHandlers(result, resolvedType, false, true, method.Parameters [parameter].Name);
2861+
AddDelegateHandlers(result, resolvedType, false, true, "Handle" + method.Parameters [parameter].Type.Name + method.Parameters [parameter].Name);
29042862
}
29052863
}
29062864

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Completion/CompletionDataWrapper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ public void Add (ICompletionData data)
6565
}
6666

6767

68-
public void AddCustom (string displayText, string description = null, string completionText = null)
68+
public ICompletionData AddCustom (string displayText, string description = null, string completionText = null)
6969
{
70-
result.Add (Factory.CreateLiteralCompletionData (displayText, description, completionText));
70+
var literalCompletionData = Factory.CreateLiteralCompletionData(displayText, description, completionText);
71+
result.Add(literalCompletionData);
72+
return literalCompletionData;
7173
}
7274

7375
HashSet<string> usedNamespaces = new HashSet<string> ();

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Completion/ICompletionDataFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public interface ICompletionDataFactory
6464

6565
ICompletionData CreateVariableCompletionData (ITypeParameter parameter);
6666

67-
ICompletionData CreateEventCreationCompletionData (string varName, IType delegateType, IEvent evt, string parameterDefinition, IUnresolvedMember currentMember, IUnresolvedTypeDefinition currentType);
67+
ICompletionData CreateEventCreationCompletionData (string delegateMethodName, IType delegateType, IEvent evt, string parameterDefinition, IUnresolvedMember currentMember, IUnresolvedTypeDefinition currentType);
6868

6969
ICompletionData CreateNewOverrideCompletionData (int declarationBegin, IUnresolvedTypeDefinition type, IMember m);
7070
ICompletionData CreateNewPartialCompletionData (int declarationBegin, IUnresolvedTypeDefinition type, IUnresolvedMember m);

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Formatter/CSharpFormattingOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ public bool AlignToMemberReferenceDot { // TODO!
186186
get;
187187
set;
188188
}
189+
190+
public bool IndentBlocksInsideExpressions {
191+
get;
192+
set;
193+
}
189194
#endregion
190195

191196
#region Braces

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Formatter/FormattingOptionsFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static CSharpFormattingOptions CreateMono()
5959
IndentCaseBody = true,
6060
IndentBreakStatements = true,
6161
IndentPreprocessorDirectives = true,
62+
IndentBlocksInsideExpressions = false,
6263
NamespaceBraceStyle = BraceStyle.NextLine,
6364
ClassBraceStyle = BraceStyle.NextLine,
6465
InterfaceBraceStyle = BraceStyle.NextLine,
@@ -390,6 +391,7 @@ public static CSharpFormattingOptions CreateAllman()
390391
baseOptions.FinallyNewLinePlacement = NewLinePlacement.NewLine;
391392
baseOptions.WhileNewLinePlacement = NewLinePlacement.DoNotCare;
392393
baseOptions.ArrayInitializerWrapping = Wrapping.DoNotChange;
394+
baseOptions.IndentBlocksInsideExpressions = true;
393395

394396
return baseOptions;
395397
}
@@ -418,6 +420,7 @@ public static CSharpFormattingOptions CreateWhitesmiths()
418420
baseOptions.EventAddBraceStyle = BraceStyle.NextLineShifted;
419421
baseOptions.EventRemoveBraceStyle = BraceStyle.NextLineShifted;
420422
baseOptions.StatementBraceStyle = BraceStyle.NextLineShifted;
423+
baseOptions.IndentBlocksInsideExpressions = true;
421424
return baseOptions;
422425
}
423426

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/Formatter/FormattingVisitor_Expressions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,21 @@ void FormatArguments(AstNode node)
389389
} else {
390390

391391
foreach (var arg in arguments.Take (argumentStart)) {
392+
if (policy.IndentBlocksInsideExpressions)
393+
curIndent.Push(IndentType.Continuation);
392394
arg.AcceptVisitor(this);
395+
if (policy.IndentBlocksInsideExpressions)
396+
curIndent.Pop();
393397
}
394398
foreach (var arg in arguments.Skip(argumentStart)) {
395399
if (arg.GetPrevSibling(NoWhitespacePredicate) != null) {
396400
if (methodCallArgumentWrapping == Wrapping.DoNotWrap) {
397401
ForceSpacesBeforeRemoveNewLines(arg, spaceAfterMethodCallParameterComma && arg.GetPrevSibling(NoWhitespacePredicate).Role == Roles.Comma);
402+
if (policy.IndentBlocksInsideExpressions)
403+
curIndent.Push(IndentType.Continuation);
398404
arg.AcceptVisitor(this);
405+
if (policy.IndentBlocksInsideExpressions)
406+
curIndent.Pop();
399407
} else {
400408
if (!doAlignToFirstArgument && arg.PrevSibling.Role == Roles.NewLine) {
401409
curIndent.Push(IndentType.Continuation);
@@ -405,7 +413,11 @@ void FormatArguments(AstNode node)
405413
} else {
406414
if (arg.PrevSibling.StartLocation.Line == arg.StartLocation.Line) {
407415
ForceSpacesBefore(arg, spaceAfterMethodCallParameterComma && arg.GetPrevSibling(NoWhitespacePredicate).Role == Roles.Comma);
416+
if (policy.IndentBlocksInsideExpressions)
417+
curIndent.Push(IndentType.Continuation);
408418
arg.AcceptVisitor(this);
419+
if (policy.IndentBlocksInsideExpressions)
420+
curIndent.Pop();
409421
} else {
410422
int extraSpaces = Math.Max(0, arguments.First().StartLocation.Column - 1 - curIndent.IndentString.Length);
411423
curIndent.ExtraSpaces += extraSpaces;

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/IndentEngine/IndentState.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,6 @@ public override void Push(char ch)
485485
ThisLineIndent.RemoveAlignment();
486486
while (ThisLineIndent.CurIndent > PreviousLineIndent &&
487487
ThisLineIndent.PopIf(IndentType.Continuation)) ;
488-
489488
ThisLineIndent.Push(IndentType.Continuation);
490489
NextLineIndent = ThisLineIndent.Clone();
491490
}
@@ -517,8 +516,10 @@ public override void InitializeState()
517516
var parent = Parent as BracesBodyState;
518517
if (parent == null || parent.LastBlockIndent == null || !Engine.EnableCustomIndentLevels)
519518
{
520-
NextLineIndent.RemoveAlignment();
521-
NextLineIndent.PopIf(IndentType.Continuation);
519+
if (!Engine.formattingOptions.IndentBlocksInsideExpressions) {
520+
NextLineIndent.RemoveAlignment();
521+
NextLineIndent.PopIf(IndentType.Continuation);
522+
}
522523
}
523524
else
524525
{
@@ -1032,7 +1033,7 @@ public override void CheckKeyword(string keyword)
10321033

10331034
public override void OnExit()
10341035
{
1035-
// override the base.OnExit() logic
1036+
Parent.OnExit();
10361037
}
10371038

10381039
public override IndentState Clone(CSharpIndentEngine engine)

src/Libraries/NRefactory/ICSharpCode.NRefactory.CSharp/OutputVisitor/CSharpAmbience.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
namespace ICSharpCode.NRefactory.CSharp
2525
{
2626
/// <summary>
27-
/// C# ambience.
27+
/// C# ambience. Used to convert type system symbols to text (usually for displaying the symbol to the user; e.g. in editor tooltips)
2828
/// </summary>
2929
public class CSharpAmbience : IAmbience
3030
{

0 commit comments

Comments
 (0)