From 5255ea13004eca2ab03413be82c6fc9e77f310cf Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Wed, 18 May 2016 16:52:31 +0200 Subject: [PATCH] The C# language level is now set for T4 files to avoid C# 6 suggestions. Fixes #58. --- .../GammaJul.ReSharper.ForTea.csproj | 1 + .../GammaJul.ReSharper.ForTea.nuspec | 3 +- .../Psi/T4CSharpLanguageLevelProvider.cs | 51 +++++++++++++++++++ GammaJul.ReSharper.ForTea/T4Environment.cs | 30 +++++++---- 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 GammaJul.ReSharper.ForTea/Psi/T4CSharpLanguageLevelProvider.cs diff --git a/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.csproj b/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.csproj index 38d5872..9a16cd0 100644 --- a/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.csproj +++ b/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.csproj @@ -220,6 +220,7 @@ + diff --git a/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.nuspec b/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.nuspec index 6727deb..eefb081 100644 --- a/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.nuspec +++ b/GammaJul.ReSharper.ForTea/GammaJul.ReSharper.ForTea.nuspec @@ -7,8 +7,9 @@ Julien Lebosquain Adds support for editing T4 text templating files (.tt) - - Added support for ReSharper 2016.1 (#72) + - Added support for ReSharper 2016.1 (#72). - The TransformText method is now resolved from base classes (#64). + - C# 6.0 language features aren't enabled anymore in VS versions before VS2015 update 2 (#58). https://github.com/MrJul/ForTea/ https://raw.github.com/MrJul/ForTea/master/Logo/ForTea%20128x128.png diff --git a/GammaJul.ReSharper.ForTea/Psi/T4CSharpLanguageLevelProvider.cs b/GammaJul.ReSharper.ForTea/Psi/T4CSharpLanguageLevelProvider.cs new file mode 100644 index 0000000..b160396 --- /dev/null +++ b/GammaJul.ReSharper.ForTea/Psi/T4CSharpLanguageLevelProvider.cs @@ -0,0 +1,51 @@ +#region License +// Copyright 2012 Julien Lebosquain +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + + +using System; +using JetBrains.Annotations; +using JetBrains.ReSharper.Psi; +using JetBrains.ReSharper.Psi.CSharp; +using JetBrains.ReSharper.Psi.CSharp.Impl; +using JetBrains.ReSharper.Psi.Modules; + +namespace GammaJul.ReSharper.ForTea.Psi { + + [SolutionFeaturePart] + public class T4CSharpLanguageLevelProvider : CSharpLanguageLevelProvider { + + [NotNull] private readonly T4Environment _t4Environment; + + public override bool IsApplicable(IPsiModule psiModule) + => psiModule is T4PsiModule; + + public override bool CanSetLanguageLevel(IPsiModule psiModule) + => false; + + public override CSharpLanguageLevel GetLanguageLevel(IPsiModule psiModule) + => _t4Environment.CSharpLanguageLevel; + + public override void SetLanguageLevel(IPsiModule psiModule, CSharpLanguageLevel languageLevel) { + throw new InvalidOperationException("Language level cannot be set for a T4 module."); + } + + public T4CSharpLanguageLevelProvider([NotNull] T4Environment t4Environment) { + _t4Environment = t4Environment; + } + + } + +} \ No newline at end of file diff --git a/GammaJul.ReSharper.ForTea/T4Environment.cs b/GammaJul.ReSharper.ForTea/T4Environment.cs index 77c727a..5e3b7f1 100644 --- a/GammaJul.ReSharper.ForTea/T4Environment.cs +++ b/GammaJul.ReSharper.ForTea/T4Environment.cs @@ -22,6 +22,7 @@ using JetBrains.Application; using JetBrains.Application.Components; using JetBrains.Application.platforms; +using JetBrains.ReSharper.Psi.CSharp; using JetBrains.VsIntegration.Shell; using JetBrains.Util; using Microsoft.Win32; @@ -42,20 +43,18 @@ public class T4Environment { [CanBeNull] private IList _includePaths; [NotNull] - public Optional Components { - get { return _components.Value; } - } - + public Optional Components + => _components.Value; + /// /// Gets the version of the Visual Studio we're running under, two components only, Major.Minor. Example: “8.0”. /// [NotNull] - public Version2 VsVersion2 { - get { return _vsEnvironmentInformation.VsVersion2; } - } + public Version2 VsVersion2 + => _vsEnvironmentInformation.VsVersion2; /// - /// Gets the platform ID (.NET 4.0 under VS2010, .NET 4.5 under VS2012). + /// Gets the platform ID (.NET 4.0 under VS2010, .NET 4.5 under VS2012+). /// [NotNull] public PlatformID PlatformID { @@ -66,6 +65,11 @@ public PlatformID PlatformID { } } + /// + /// Gets the C# language version. + /// + public CSharpLanguageLevel CSharpLanguageLevel { get; } + /// /// Gets the default included assemblies. /// @@ -81,9 +85,8 @@ public IEnumerable TextTemplatingAssemblyNames { /// /// Gets whether the current environment is supported. VS2005 and VS2008 aren't. /// - public bool IsSupported { - get { return _platformID != null; } - } + public bool IsSupported + => _platformID != null; /// /// Gets the common include paths from the registry. @@ -135,6 +138,7 @@ public T4Environment([NotNull] IVsEnvironmentInformation vsEnvironmentInformatio case VsVersions.Vs2010: _platformID = new PlatformID(FrameworkIdentifier.NetFramework, new Version(4, 0)); + CSharpLanguageLevel = CSharpLanguageLevel.CSharp40; _textTemplatingAssemblyNames = new[] { "Microsoft.VisualStudio.TextTemplating.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "Microsoft.VisualStudio.TextTemplating.Interfaces.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" @@ -143,6 +147,7 @@ public T4Environment([NotNull] IVsEnvironmentInformation vsEnvironmentInformatio case VsVersions.Vs2012: _platformID = new PlatformID(FrameworkIdentifier.NetFramework, new Version(4, 5)); + CSharpLanguageLevel = CSharpLanguageLevel.CSharp50; _textTemplatingAssemblyNames = new[] { "Microsoft.VisualStudio.TextTemplating.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "Microsoft.VisualStudio.TextTemplating.Interfaces.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", @@ -152,6 +157,7 @@ public T4Environment([NotNull] IVsEnvironmentInformation vsEnvironmentInformatio case VsVersions.Vs2013: _platformID = new PlatformID(FrameworkIdentifier.NetFramework, new Version(4, 5)); + CSharpLanguageLevel = CSharpLanguageLevel.CSharp50; _textTemplatingAssemblyNames = new[] { "Microsoft.VisualStudio.TextTemplating.12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "Microsoft.VisualStudio.TextTemplating.Interfaces.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", @@ -161,6 +167,8 @@ public T4Environment([NotNull] IVsEnvironmentInformation vsEnvironmentInformatio case VsVersions.Vs2015: _platformID = new PlatformID(FrameworkIdentifier.NetFramework, new Version(4, 5)); + const int vs2015Update2Build = 25123; + CSharpLanguageLevel = vsEnvironmentInformation.VsVersion4.Build >= vs2015Update2Build ? CSharpLanguageLevel.CSharp60 : CSharpLanguageLevel.CSharp50; _textTemplatingAssemblyNames = new[] { "Microsoft.VisualStudio.TextTemplating.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "Microsoft.VisualStudio.TextTemplating.Interfaces.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",