Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split analyzer template by language #544

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using EnvDTE;
using VSLangProj;

public class RoslynSDKAnalyzerCSharpTemplateWizard : RoslynSDKChildTemplateWizard
{
public static Project Project { get; private set; }

public override void OnProjectFinishedGenerating(Project project)
{
Project = project;

// There is no good way for the test project to reference the main project, so we will use the wizard.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
if (project.Object is VSProject vsProject)
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
{
var referenceProject = vsProject.References.AddProject(RoslynSDKAnalyzerTemplateWizard.Project);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using EnvDTE;
using VSLangProj;

public class RoslynSDKAnalyzerVisualBasicTemplateWizard : RoslynSDKChildTemplateWizard
{
public static Project Project { get; private set; }

public override void OnProjectFinishedGenerating(Project project)
{
Project = project;

// There is no good way for the test project to reference the main project, so we will use the wizard.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
if (project.Object is VSProject vsProject)
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
{
var referenceProject = vsProject.References.AddProject(RoslynSDKAnalyzerTemplateWizard.Project);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using EnvDTE;
using VSLangProj;

public class RoslynSDKCodeFixCSharpTemplateWizard : RoslynSDKChildTemplateWizard
{
public static Project Project { get; private set; }

public override void OnProjectFinishedGenerating(Project project)
{
Project = project;

// There is no good way for the test project to reference the main project, so we will use the wizard.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
if (project.Object is VSProject vsProject)
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
{
_ = vsProject.References.AddProject(RoslynSDKAnalyzerTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKAnalyzerCSharpTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixTemplateWizard.Project);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using EnvDTE;
using VSLangProj;

public class RoslynSDKCodeFixVisualBasicTemplateWizard : RoslynSDKChildTemplateWizard
{
public static Project Project { get; private set; }

public override void OnProjectFinishedGenerating(Project project)
{
Project = project;

// There is no good way for the test project to reference the main project, so we will use the wizard.
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
if (project.Object is VSProject vsProject)
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
{
_ = vsProject.References.AddProject(RoslynSDKAnalyzerTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKAnalyzerVisualBasicTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixTemplateWizard.Project);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public override void OnProjectFinishedGenerating(Project project)
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
{
_ = vsProject.References.AddProject(RoslynSDKAnalyzerTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKAnalyzerCSharpTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKAnalyzerVisualBasicTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixCSharpTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixVisualBasicTemplateWizard.Project);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ public override void OnProjectFinishedGenerating(Project project)
#pragma warning restore VSTHRD010 // Invoke single-threaded types on Main thread
{
_ = vsProject.References.AddProject(RoslynSDKAnalyzerTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKAnalyzerCSharpTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKAnalyzerVisualBasicTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixCSharpTemplateWizard.Project);
_ = vsProject.References.AddProject(RoslynSDKCodeFixVisualBasicTemplateWizard.Project);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,21 @@
<ProjectTemplateLink ProjectName="$safeprojectname$">
ProjectTemplates\CSharp\Diagnostic\Analyzer\DiagnosticAnalyzer.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$safeprojectname$.CSharp">
ProjectTemplates\CSharp\Diagnostic\Analyzer.CS\CSAnalyzer.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$safeprojectname$.VisualBasic">
ProjectTemplates\CSharp\Diagnostic\Analyzer.VB\VBAnalyzer.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$safeprojectname$.CodeFixes">
ProjectTemplates\CSharp\Diagnostic\CodeFix\CodeFixProvider.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$safeprojectname$.CodeFixes.CSharp">
ProjectTemplates\CSharp\Diagnostic\CodeFix.CS\CSCodeFix.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$safeprojectname$.CodeFixes.VisualBasic">
ProjectTemplates\CSharp\Diagnostic\CodeFix.VB\VBCodeFix.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$safeprojectname$.Package">
ProjectTemplates\CSharp\Diagnostic\Package\Package.vstemplate
</ProjectTemplateLink>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0"
Type="Project"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>DiagnosticAnalyzer.CSharp</Name>
<Description>&lt;No description available&gt;</Description>
<Icon></Icon>
<ProjectType>CSharp</ProjectType>
<RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
<SortOrder>952</SortOrder>
<TemplateID>bb967cab-2ca5-4dac-8809-65b2b28a6350</TemplateID>
<CreateNewFolder>true</CreateNewFolder>
<CreateInPlace>true</CreateInPlace>
<DefaultName>DiagnosticAnalyzer.CSharp</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<PromptForSaveOnCreation>true</PromptForSaveOnCreation>
</TemplateData>
<TemplateContent>
<Project TargetFileName="$projectname$.csproj" File="CSAnalyzer.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="Empty.txt">Empty.txt</ProjectItem>
</Project>
</TemplateContent>
<WizardExtension>
<Assembly>Roslyn.SDK.Template.Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>RoslynSDKAnalyzerCSharpTemplateWizard</FullClassName>
</WizardExtension>
</VSTemplate>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="3.3.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0"
Type="Project"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>DiagnosticAnalyzer.VisualBasic</Name>
<Description>&lt;No description available&gt;</Description>
<Icon></Icon>
<ProjectType>CSharp</ProjectType>
<RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
<SortOrder>952</SortOrder>
<TemplateID>bb967cab-2ca5-4dac-8809-65b2b28a6350</TemplateID>
<CreateNewFolder>true</CreateNewFolder>
<CreateInPlace>true</CreateInPlace>
<DefaultName>DiagnosticAnalyzer.VisualBasic</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<PromptForSaveOnCreation>true</PromptForSaveOnCreation>
</TemplateData>
<TemplateContent>
<Project TargetFileName="$projectname$.csproj" File="VBAnalyzer.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="Empty.txt">Empty.txt</ProjectItem>
</Project>
</TemplateContent>
<WizardExtension>
<Assembly>Roslyn.SDK.Template.Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>RoslynSDKAnalyzerVisualBasicTemplateWizard</FullClassName>
</WizardExtension>
</VSTemplate>
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace $saferootprojectname$
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public class $saferootidentifiername$Analyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "$saferootidentifiername$";
Expand All @@ -28,6 +23,9 @@ public class $saferootidentifiername$Analyzer : DiagnosticAnalyzer

public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);

// TODO: Consider registering other actions that act on syntax instead of or in addition to symbols
// See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Analyzer%20Actions%20Semantics.md for more information
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.3.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.3.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>$saferootprojectname$.CSharp</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.3.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0"
Type="Project"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>CodeFixProvider.CSharp</Name>
<Description>&lt;No description available&gt;</Description>
<Icon></Icon>
<ProjectType>CSharp</ProjectType>
<RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
<SortOrder>952</SortOrder>
<TemplateID>bb967cab-2ca5-4dac-8809-65b2b28a6350</TemplateID>
<CreateNewFolder>true</CreateNewFolder>
<CreateInPlace>true</CreateInPlace>
<DefaultName>CodeFixProvider.CSharp</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<PromptForSaveOnCreation>true</PromptForSaveOnCreation>
</TemplateData>
<TemplateContent>
<Project TargetFileName="$projectname$.csproj" File="CSCodeFix.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="CSharpCodeFixProvider.cs">CSCodeFixProvider.cs</ProjectItem>
</Project>
</TemplateContent>
<WizardExtension>
<Assembly>Roslyn.SDK.Template.Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>RoslynSDKCodeFixCSharpTemplateWizard</FullClassName>
</WizardExtension>
</VSTemplate>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Rename;

namespace $saferootprojectname$
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CSharp$saferootidentifiername$CodeFixProvider)), Shared]
public class CSharp$saferootidentifiername$CodeFixProvider : Abstract$saferootidentifiername$CodeFixProvider<TypeDeclarationSyntax>
{
protected override async Task<Solution> MakeUppercaseAsync(Document document, TypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
// Compute new uppercase name.
var identifierToken = typeDeclaration.Identifier;
var newName = identifierToken.Text.ToUpperInvariant();

// Get the symbol representing the type to be renamed.
var semanticModel = await document.GetSemanticModelAsync(cancellationToken);
var typeSymbol = semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken);

// Produce a new solution that has all references to that type renamed, including the declaration.
var originalSolution = document.Project.Solution;
var optionSet = originalSolution.Workspace.Options;
var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, typeSymbol, newName, optionSet, cancellationToken).ConfigureAwait(false);

// Return the new solution with the now-uppercase type name.
return newSolution;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<RootNamespace>$saferootprojectname$.VisualBasic</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="3.3.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<VSTemplate Version="3.0.0"
Type="Project"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>CodeFixProvider.VisualBasic</Name>
<Description>&lt;No description available&gt;</Description>
<Icon></Icon>
<ProjectType>CSharp</ProjectType>
<RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>
<SortOrder>952</SortOrder>
<TemplateID>bb967cab-2ca5-4dac-8809-65b2b28a6350</TemplateID>
<CreateNewFolder>true</CreateNewFolder>
<CreateInPlace>true</CreateInPlace>
<DefaultName>CodeFixProvider.VisualBasic</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<PromptForSaveOnCreation>true</PromptForSaveOnCreation>
</TemplateData>
<TemplateContent>
<Project TargetFileName="$projectname$.csproj" File="VBCodeFix.csproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="true" TargetFileName="VisualBasicCodeFixProvider.cs">VBCodeFixProvider.cs</ProjectItem>
</Project>
</TemplateContent>
<WizardExtension>
<Assembly>Roslyn.SDK.Template.Wizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>RoslynSDKCodeFixVisualBasicTemplateWizard</FullClassName>
</WizardExtension>
</VSTemplate>
Loading