If I enter some simple code such as:
public void Hello()
and then press enter and type a {, I get:
public void Hello()
{ }
I would like the result to be either:
public void Hello()
{ // no added '}'
or:
public void Hello()
{
} // the '}' on a new line
I've looked around Tools -> Options -> Text Editor -> C# -> Code Style and Tools -> Options -> Text Editor -> C# -> Formatting but cannot seem to find how to make the above changes.
Install Productivity Tools 2015
Auto brace completion is one of the many features it has to offer
Related
I am attempting to write a C# source generator that throws a warning/error under certain conditions using GeneratorExecutionContext.ReportDiagnostic. My source generator is able to run and output errors successfully upon building a sample project in Visual Studio. However, my errors do not show up as green/red squiggles in the Visual Studio editor. This is supposed to be possible with Roslyn analyzers, according to Microsoft's documentation, but nothing is said of source generators specifically. Since source generators are treated like Roslyn analyzers, though, I imagine this should be possible. I've managed to replicate my issue with a small example, consisting of a source generator project and a test project on which to run the generator. As a test, the generator reports a diagnostic error whenever it sees a method that doesn't return void. I intend for red squiggles to appear under the offending method's name:
Source generator:
[Generator]
public class SampleGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
DataReceiver r = (DataReceiver)context.SyntaxReceiver;
foreach(MethodDeclarationSyntax method in r.Methods)
{
IMethodSymbol symbol = (IMethodSymbol)context.Compilation.GetSemanticModel(method.SyntaxTree).GetDeclaredSymbol(method);
if(symbol.ReturnType.SpecialType != SpecialType.System_Void)
{
context.ReportDiagnostic(Diagnostic.Create(
new DiagnosticDescriptor(
"SG0001",
"Non-void method return type",
"Method {0} returns {1}. All methods must return void.",
"yeet",
DiagnosticSeverity.Error,
true), symbol.Locations.FirstOrDefault(), symbol.Name, symbol.ReturnType.Name));
}
}
context.AddSource("yert", "namespace test { public class testclass { } }");
}
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new DataReceiver());
}
}
public class DataReceiver : ISyntaxReceiver
{
public List<MethodDeclarationSyntax> Methods { get; } = new List<MethodDeclarationSyntax>();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if(syntaxNode is MethodDeclarationSyntax synt)
{
Methods.Add(synt);
}
}
}
Example code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static string ok() => "hello";
}
When I compile the example code with the generator, Visual Studio tells me that the build has errors, and correctly reports the custom diagnostic in the error list. I can click on the custom error, and my cursor moves to the offending method in the editor. However, no red squiggles appear. I know that my source generator is being run by Intellisense, because I am able to see the custom test namespace and class my generator defines.
Does Visual Studio support code underlining for diagnostics reported by C# source generators? If so, what is wrong with the above code? Thanks in advance.
I solved this by separating out the code analysis logic into its own class and adding an analyzer and source generator into the same assembly, with the analysis logic only doing code emmission in the source generator. The analysis logic runs in different contexts, each context having a different reportdiagnostic, so it accepted an Action to report diagnostic.
I am trying to see what goes on in the IAsyncStateMachine at runtime ,and i desperately need to see what variables it has and what it calls.I know you can see the code with the ILSpy ...but i need to debug it.
Is there any method?
I need to see what goes on inside the IAsyncStateMachine MoveNext method !
public sealed partial class MethodBuilder<T> : Errand.MethodBuilder {
public static new MethodBuilder<T> Create() => new MethodBuilder<T>();
public new void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine {
this.myStateMachine = stateMachine;
this.Task = new Task<T>(this);
stateMachine.MoveNext(); //i have to see the properties of stateMachine and inside this method !!!!!
}
public new void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine machine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine {
}
public void SetResult(T result) {
this.Task.isCompleted = true;
this.Task.result = result;
}
public new void SetStateMachine(IAsyncStateMachine stateMachine) => base.SetStateMachine(stateMachine);
public new void SetException(Exception ex) => base.SetException(ex);
}
From MSDN How to: Debug .NET Framework Source.
To enable .NET Framework source debugging
On the Tools menu, click Options.
In the Options dialog box, click the Debugging category.
In the General box, set Enable .NET Framework source stepping.
If you had Just My Code enabled, a warning dialog box tells you that
Just My Code is now disabled. Click OK.
If you did not have a symbol cache location set, another warning
dialog box tells you that a default symbol cache location is now
set. Click OK.
Under the Debugging category, click Symbols.
If you want to change the symbols cache location:
Open the Debugging node in the box on the left.
Under the Debugging node, click Symbols.
Edit the location in Cache symbols from symbol servers to this
directory or click Browse to choose a location.
If you want to download symbols immediately, click Load Symbols
using above locations.
This button is not available in design mode.
If you do not choose to download symbols now, symbols will be
downloaded automatically the next time that you start the debugging
your program.
Click OK to close the Options dialog box.
perhaps you can use debugger.launch
once the debugger launch visual studio will prompt for vs version selection
internal class Program
{
public static void Main(string[] args)
{
System.Diagnostics.Debugger.Launch();
Console.WriteLine("crap");
}
}
does anyone know how i can get an indentation after pressing enter on a open brace?
Instead of:
public void SomeFunction()
{
[cursor here]
}
this here:
public void SomeFunction()
{
[cursor here]
}
So after typing the open { i don't want to have the same indention as the brackets but be indented by one tab
I ran into this earlier today. I found out that it's not ReSharper, but Visual Studio. Solution here: Visual Studio 2013 Indentation Not Working While Writing
First off all, sorry if this question already exist but my english is limited for this task.
I use Visual Studio on C# and of course, IntelliSense.
When I write a new method, an If or any other "method/constructor", IntelliSense write like that (when I press Tab or Enter to validate the suggestion)
Actual result :
if (true)
{
// code
}
private void Test()
{
// code
}
// ...
What I want :
if (true) {
// code
}
private void Test() {
// code
}
// etc. ...
See what I want to do ?
I prefer the second 'theme', most readable for me and row economizer.
Is an option behind, hidden in the dark side of VS to do that ?
Thanks for help.
Seems you're asking for javascript style formatting here. I found this link that might help.
1.Click Tools | Options…
2.Scroll down to the Text Editor node
3.Expand the C# node
4.Expand the Formatting node
5.Click on the New Lines node
6.You will see a list of options like in the image below which give you full control over when Visual Studio should put your open brace on
a new line
You can set that on Options window:
Tools -> Options -> Text Editor -> C# -> Formatting -> New Lines
Im writing a policy plugin for VS which checks several issues with the code. If an issue occurs it will be displayed in the policy warnings tab. Now I want to jump to the line where the issue occurs in the editor when I double click it in the policy warning tab. How can I do that?
namespace PolicyPlugin
{
[Serializable]
public class MyPolicyPlugin : PolicyBase
{
//...
//called if the user clicks on a policy warning
public override void Activate(PolicyFailure failure)
{
// make jump to line x
}
}
}
Thanks!
You could try to get DTE automation object first:
EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
or use alternative ways to get it.
An then execute standard command (that's what happens when you press CTRL+G in Visual Studio)
DTE.ExecuteCommand("Edit.Goto", "1234")
Note: I'm not sure about exact ExecuteCommand method signature. Also you can manipulate IDE the same way for other commands.