diff --git a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs index fa71c629e6..85cf16368f 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/CSharpCompletionEngineBase.cs @@ -682,6 +682,8 @@ namespace ICSharpCode.NRefactory.CSharp.Completion wrapper.Append(new string('}', closingBrackets)); } var parser = new CSharpParser (); + foreach (var sym in CompletionContextProvider.ConditionalSymbols) + parser.CompilerSettings.ConditionalSymbols.Add (sym); parser.InitialLocation = new TextLocation(memberLocation.Line - generatedLines, 1); var result = parser.Parse(wrapper.ToString ()); return result; diff --git a/ICSharpCode.NRefactory.CSharp/Completion/ICompletionContextProvider.cs b/ICSharpCode.NRefactory.CSharp/Completion/ICompletionContextProvider.cs index 91ee3f2bba..c590007fbe 100644 --- a/ICSharpCode.NRefactory.CSharp/Completion/ICompletionContextProvider.cs +++ b/ICSharpCode.NRefactory.CSharp/Completion/ICompletionContextProvider.cs @@ -35,6 +35,10 @@ namespace ICSharpCode.NRefactory.CSharp.Completion { public interface ICompletionContextProvider { + IList ConditionalSymbols { + get; + } + void GetCurrentMembers (int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember); Tuple GetMemberTextToCaret(int caretOffset, IUnresolvedTypeDefinition currentType, IUnresolvedMember currentMember); @@ -46,6 +50,13 @@ namespace ICSharpCode.NRefactory.CSharp.Completion { readonly IDocument document; readonly CSharpUnresolvedFile unresolvedFile; + readonly List symbols = new List (); + + public IList ConditionalSymbols { + get { + return symbols; + } + } public DefaultCompletionContextProvider (IDocument document, CSharpUnresolvedFile unresolvedFile) { @@ -56,7 +67,11 @@ namespace ICSharpCode.NRefactory.CSharp.Completion this.document = document; this.unresolvedFile = unresolvedFile; } - + + public void AddSymbol (string sym) + { + symbols.Add (sym); + } public void GetCurrentMembers(int offset, out IUnresolvedTypeDefinition currentType, out IUnresolvedMember currentMember) { //var document = engine.document; diff --git a/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs b/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs index a5aea58aba..ea913279be 100644 --- a/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs +++ b/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.cs @@ -3765,7 +3765,7 @@ namespace ICSharpCode.NRefactory.CSharp } conversionVisitor.Unit.FileName = fileName; - conversionVisitor.Unit.ConditionalSymbols = top.Conditionals.ToArray (); + conversionVisitor.Unit.ConditionalSymbols = top.Conditionals.Concat (compilerSettings.ConditionalSymbols).ToArray (); return conversionVisitor.Unit; } diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateBackingStoreAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateBackingStoreAction.cs index 9d90ba3380..1c83f09586 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateBackingStoreAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateBackingStoreAction.cs @@ -46,6 +46,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring // create field var backingStore = new FieldDeclaration (); + if (property.Modifiers.HasFlag (Modifiers.Static)) + backingStore.Modifiers |= Modifiers.Static; backingStore.ReturnType = property.ReturnType.Clone (); var initializer = new VariableInitializer (backingStoreName); diff --git a/ICSharpCode.NRefactory.CSharp/Resolver/OverloadResolution.cs b/ICSharpCode.NRefactory.CSharp/Resolver/OverloadResolution.cs index e94ffa42a2..ce1ce09570 100644 --- a/ICSharpCode.NRefactory.CSharp/Resolver/OverloadResolution.cs +++ b/ICSharpCode.NRefactory.CSharp/Resolver/OverloadResolution.cs @@ -249,7 +249,6 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver RunTypeInference(candidate); CheckApplicability(candidate); ConsiderIfNewCandidateIsBest(candidate); - ValidateMethodConstraints(candidate); return true; } diff --git a/ICSharpCode.NRefactory.Demo/CSDemo.cs b/ICSharpCode.NRefactory.Demo/CSDemo.cs index 4380dc96b9..9569f3f1d2 100644 --- a/ICSharpCode.NRefactory.Demo/CSDemo.cs +++ b/ICSharpCode.NRefactory.Demo/CSDemo.cs @@ -65,7 +65,7 @@ namespace ICSharpCode.NRefactory.Demo void CSharpParseButtonClick(object sender, EventArgs e) { - syntaxTree = SyntaxTree.Parse(csharpCodeTextBox.Text); + syntaxTree = new CSharpParser().Parse(csharpCodeTextBox.Text, "demo.cs"); csharpTreeView.Nodes.Clear(); foreach (var element in syntaxTree.Children) { csharpTreeView.Nodes.Add(MakeTreeNode(element)); diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateBackingStoreTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateBackingStoreTests.cs new file mode 100644 index 0000000000..9a0bb3fc39 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateBackingStoreTests.cs @@ -0,0 +1,77 @@ +// +// CreateBackingStoreTests.cs +// +// Author: +// Mike Krüger +// +// 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; +using ICSharpCode.NRefactory.CSharp.Refactoring; +using System.Text; + +namespace ICSharpCode.NRefactory.CSharp.CodeActions +{ + [TestFixture] + public class CreateBackingStoreTests : ContextActionTestBase + { + [Test()] + public void TestSimpleStore () + { + Test (@"class TestClass +{ + string $Test { get; set; } +}", @"class TestClass +{ + string test; + string Test { + get { + return test; + } + set { + test = value; + } + } +}"); + } + + [Test()] + public void TestStaticStore () + { + Test (@"class TestClass +{ + public static string $Test { get; set; } +}", @"class TestClass +{ + static string test; + public static string Test { + get { + return test; + } + set { + test = value; + } + } +}"); + } + } +} + diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs index 948902409a..5c340b5e38 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeCompletion/CodeCompletionBugTests.cs @@ -244,6 +244,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeCompletion } } var mb = new DefaultCompletionContextProvider(doc, unresolvedFile); + mb.AddSymbol ("TEST"); var engine = new CSharpCompletionEngine(doc, mb, new TestFactory(), pctx, rctx); engine.EolMarker = Environment.NewLine; @@ -5375,6 +5376,45 @@ public class FooBar }); } + [Test()] + public void TestCompletionInPreprocessorIf() + { + CombinedProviderTest( + @"using System; +public class FooBar +{ + public static void Main (string[] args) + { + #if TEST + $Console.$ + #endif + } +} + +", provider => { + Assert.IsNotNull(provider.Find("WriteLine")); + }); + } + + [Test()] + public void TestCompletionInUndefinedPreprocessorIf() + { + CombinedProviderTest( + @"using System; +public class FooBar +{ + public static void Main (string[] args) + { + #if UNDEFINED + $Console.$ + #endif + } +} + +", provider => { + Assert.IsNull(provider.Find("WriteLine")); + }); + } } } diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 3d9ef01c62..2d605658d4 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -327,6 +327,7 @@ + diff --git a/ICSharpCode.NRefactory/Properties/GlobalAssemblyInfo.cs b/ICSharpCode.NRefactory/Properties/GlobalAssemblyInfo.cs index 3c6b82ac4b..5a195fa3f2 100644 --- a/ICSharpCode.NRefactory/Properties/GlobalAssemblyInfo.cs +++ b/ICSharpCode.NRefactory/Properties/GlobalAssemblyInfo.cs @@ -23,4 +23,4 @@ using System.Runtime.InteropServices; // [AssemblyFileVersion] is the version of the NuGet package, // should follow http://semver.org/ rules -[assembly: AssemblyFileVersion("5.1.0")] +[assembly: AssemblyFileVersion("5.2.0")] diff --git a/Packages/ICSharpCode.NRefactory.nuspec b/Packages/ICSharpCode.NRefactory.nuspec index 230579c4ec..91791ce34f 100644 --- a/Packages/ICSharpCode.NRefactory.nuspec +++ b/Packages/ICSharpCode.NRefactory.nuspec @@ -2,7 +2,7 @@ ICSharpCode.NRefactory - 5.1.0 + 5.2.0 NRefactory Daniel Grunwald, Mike Krüger, Erik Källén Daniel Grunwald @@ -19,15 +19,15 @@ - - - - - - - - - + + + + + + + + + diff --git a/README b/README index edf6012b1f..bfeeffc852 100644 --- a/README +++ b/README @@ -1,16 +1,7 @@ Overview of the NRefactory library: -Features: - - C# Parser - - Abstract Syntax Tree with pattern-matching support - - Semantic Analysis for C# (supports C# 4.0 + async/await) - - Code Completion for C# - - Pretty Printer for C# - - Lots of C# refactorings - -Non-Features: - - VB support is not implemented yet. - - NRefactory cannot generate IL code +Introductory documentation: + http://www.codeproject.com/Articles/408663/Using-NRefactory-for-analyzing-Csharp-code How to download: - Binaries are provided as a NuGet package (http://nuget.org/packages/ICSharpCode.NRefactory) @@ -22,6 +13,17 @@ How to compile: next to the directory containing NRefactory. 2. Open NRefactory.sln in your favorite .NET IDE and compile. +Features: + - C# Parser + - Abstract Syntax Tree with pattern-matching support + - Semantic Analysis for C# (supports C# 4.0 + async/await) + - Code Completion for C# + - Pretty Printer for C# + - Lots of C# refactorings + +Non-Features: + - VB support is not implemented yet. + - NRefactory cannot generate IL code Dependencies: .NET 4.0