diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CompletionDataWrapper.cs b/ICSharpCode.NRefactory.CSharp/Completion/CompletionDataWrapper.cs index 87cfa33fb8..ea8352ab67 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CompletionDataWrapper.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CompletionDataWrapper.cs @@ -84,7 +84,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion return null; var def = type.GetDefinition (); - if (def != null && !def.IsBrowsable ()) + if (def != null && def.ParentAssembly != completion.ctx.CurrentAssembly && !def.IsBrowsable ()) return null; usedTypes.Add(shortType); @@ -131,7 +131,7 @@ namespace ICSharpCode.NRefactory.CSharp.Completion { var newData = Factory.CreateEntityCompletionData (member); - if (!member.IsBrowsable ()) + if (member.ParentAssembly != completion.ctx.CurrentAssembly && !member.IsBrowsable ()) return null; string memberKey = newData.DisplayText; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/BrowsableAttributeTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/BrowsableAttributeTests.cs index e8f3b1f00f..5631ad5a49 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/BrowsableAttributeTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/BrowsableAttributeTests.cs @@ -35,17 +35,44 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion [Test()] public void TestEditorBrowsableAttributeClasses () { - + int cp; + var engine1 = CodeCompletionBugTests.CreateEngine ( + @" +using System; +using System.ComponentModel; + +[EditorBrowsable(EditorBrowsableState.Always)] +public class BrowsableTest {} + +[EditorBrowsable(EditorBrowsableState.Never)] +public class NotBrowsableTest {} +", out cp); + CompletionDataList provider = CodeCompletionBugTests.CreateProvider ( + @"class Test +{ + void Test () + { + $B$ + } +}", false, engine1.ctx.CurrentAssembly.UnresolvedAssembly); + Assert.IsNotNull (provider, "provider == null"); + Assert.IsNotNull (provider.Find ("BrowsableTest"), "'BrowsableTest' not found."); + Assert.IsNull (provider.Find ("NotBrowsableTest"), "'NotBrowsableTest' found."); + } + + [Test()] + public void TestEditorBrowsableAttributeClassesSameAssembly () + { CompletionDataList provider = CodeCompletionBugTests.CreateProvider ( @" using System; using System.ComponentModel; [EditorBrowsable(EditorBrowsableState.Always)] -class BrowsableTest {} +public class BrowsableTest {} [EditorBrowsable(EditorBrowsableState.Never)] -class NotBrowsableTest {} +public class NotBrowsableTest {} class Test { @@ -56,12 +83,42 @@ class Test }"); Assert.IsNotNull (provider, "provider == null"); Assert.IsNotNull (provider.Find ("BrowsableTest"), "'BrowsableTest' not found."); - Assert.IsNull (provider.Find ("NotBrowsableTest"), "'NotBrowsableTest' found."); + Assert.IsNotNull (provider.Find ("NotBrowsableTest"), "'NotBrowsableTest' not found."); } - [Test()] public void TestEditorBrowsableAttributeMembers () + { + int cp; + var engine1 = CodeCompletionBugTests.CreateEngine ( + @" +using System; +using System.ComponentModel; +public class FooBar +{ + [EditorBrowsable(EditorBrowsableState.Always)] + public int BrowsableTest { get; set; } + + [EditorBrowsable(EditorBrowsableState.Never)] + public int NotBrowsableTest { get; set; } +} +", out cp); + CompletionDataList provider = CodeCompletionBugTests.CreateProvider ( + @"class Test : FooBar +{ + void Test () + { + $B$ + } +}", false, engine1.ctx.CurrentAssembly.UnresolvedAssembly); + Assert.IsNotNull (provider, "provider == null"); + Assert.IsNotNull (provider.Find ("BrowsableTest"), "'BrowsableTest' not found."); + Assert.IsNull (provider.Find ("NotBrowsableTest"), "'NotBrowsableTest' found."); + } + + + [Test()] + public void TestEditorBrowsableAttributeMembersSameAssembly () { CompletionDataList provider = CodeCompletionBugTests.CreateProvider ( @@ -84,7 +141,7 @@ class Test }"); Assert.IsNotNull (provider, "provider == null"); Assert.IsNotNull (provider.Find ("BrowsableTest"), "'BrowsableTest' not found."); - Assert.IsNull (provider.Find ("NotBrowsableTest"), "'NotBrowsableTest' found."); + Assert.IsNotNull (provider.Find ("NotBrowsableTest"), "'NotBrowsableTest' not found."); } } } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs index 5222b556d9..f4d995ef2f 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs @@ -196,52 +196,65 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion return new CecilLoader().LoadAssemblyFile(typeof(System.ComponentModel.BrowsableAttribute).Assembly.Location); }); - static CompletionDataList CreateProvider(string text, bool isCtrlSpace) + public static CSharpCompletionEngine CreateEngine(string text, out int cursorPosition, params IUnresolvedAssembly[] references) { string parsedText; string editorText; - int cursorPosition = text.IndexOf('$'); + cursorPosition = text.IndexOf('$'); int endPos = text.IndexOf('$', cursorPosition + 1); if (endPos == -1) { - parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1); + if (cursorPosition < 0) { + parsedText = editorText = text; + } else { + parsedText = editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1); + } } else { - parsedText = text.Substring(0, cursorPosition) + new string(' ', endPos - cursorPosition) + text.Substring(endPos + 1); - editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1); - cursorPosition = endPos - 1; + parsedText = text.Substring(0, cursorPosition) + new string(' ', endPos - cursorPosition) + text.Substring(endPos + 1); + editorText = text.Substring(0, cursorPosition) + text.Substring(cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring(endPos + 1); + cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument(editorText); - + IProjectContent pctx = new CSharpProjectContent(); - pctx = pctx.AddAssemblyReferences(new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore, SystemAssembly }); + var refs = new List { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore, SystemAssembly }; + if (references != null) + refs.AddRange (references); + + pctx = pctx.AddAssemblyReferences(refs); var compilationUnit = new CSharpParser().Parse(parsedText, "program.cs"); compilationUnit.Freeze(); - + var parsedFile = compilationUnit.ToTypeSystem(); pctx = pctx.UpdateProjectContent(null, parsedFile); - + var cmp = pctx.CreateCompilation(); - var loc = doc.GetLocation(cursorPosition); - + var loc = cursorPosition > 0 ? doc.GetLocation(cursorPosition) : new TextLocation (1, 1); + var rctx = new CSharpTypeResolveContext(cmp.MainAssembly); rctx = rctx.WithUsingScope(parsedFile.GetUsingScope(loc).Resolve(cmp)); - var curDef = parsedFile.GetInnermostTypeDefinition(loc); if (curDef != null) { - var resolvedDef = curDef.Resolve(rctx).GetDefinition(); - rctx = rctx.WithCurrentTypeDefinition(resolvedDef); - var curMember = resolvedDef.Members.FirstOrDefault(m => m.Region.Begin <= loc && loc < m.BodyRegion.End); - if (curMember != null) { - rctx = rctx.WithCurrentMember(curMember); - } + var resolvedDef = curDef.Resolve(rctx).GetDefinition(); + rctx = rctx.WithCurrentTypeDefinition(resolvedDef); + var curMember = resolvedDef.Members.FirstOrDefault(m => m.Region.Begin <= loc && loc < m.BodyRegion.End); + if (curMember != null) { + rctx = rctx.WithCurrentMember(curMember); + } } var mb = new DefaultCompletionContextProvider(doc, parsedFile); - var engine = new CSharpCompletionEngine (doc, mb, new TestFactory (), pctx, rctx); - + var engine = new CSharpCompletionEngine(doc, mb, new TestFactory(), pctx, rctx); + engine.EolMarker = Environment.NewLine; - engine.FormattingPolicy = FormattingOptionsFactory.CreateMono (); - + engine.FormattingPolicy = FormattingOptionsFactory.CreateMono(); + return engine; + } + + public static CompletionDataList CreateProvider(string text, bool isCtrlSpace, params IUnresolvedAssembly[] references) + { + int cursorPosition; + var engine = CreateEngine(text, out cursorPosition, references); var data = engine.GetCompletionData (cursorPosition, isCtrlSpace); return new CompletionDataList () {