Make VS jump to line x in editor - c#

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.

Related

C# DLL calls property "get" for no reason

I am creating a custom C# Windows Forms control library (a DLL) in Visual Studio 2019 (Professional). My control has a property that takes the following form (this property is aimed to be accessed by applications which use the DLL):
public double Hello
{
get { throw new ApplicationException("Hi!"); }
}
(In my efforts to find out why this is happening, I've simplified the property to just throw an exception and do nothing else.)
For some reason, if I run my User Control (in Debug mode), the exception is raised - even though nowhere else in this code calls that property! (The IDE confirms this - saying "0 references" above it). Why does the property "get" accessor seem to be called for no reason? The stack trace shows that the "get" was called by "[External code]"...
This should be pretty easy to reproduce if you have Visual Studio 2019: create a new "Windows Forms Control Library (.NET Framework)" project under C#, then right click on "UserControl1.cs" in the Solution Explorer and click "View Code", then just add in the above code to the class.
I have reproduced your problem. Based on my test, I find that winformscontrollibary will
load all the properties you set in the code, because it needs to load them into the
property bar of form.
Like the following, if you write the following code.
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
public double Hello
{
get { return 1.02; }
}
public int Number
{
get { return 1; }
}
}
You will see the correct property(Hello and Number) in the right of form.
Therefore, if you write the code throw new ApplicationException("Hi!"); in the get method , it will throw the exception.

Unity does not even recognize "Collider" on my function "OnTriggerEnter()"

I'm a Unity beginner ,so right now I'm doing some small projects by watching some tutorials on youtube.
But, after looking at one tutorial that used the "OnTriggerEnter" function with the attribute "Collider". I realized that my editor didn't recognize "Collider" so I couldn't use "OnTriggerEnter".
I searched on the internet, but coudln't find any answer related to my question.
It's the first time, it's happened to me so I don't really know how to solve this problem...
Here's my little code, I'm just trying to move something when my player enters in a zone.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
public bool opening = false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// OnTriggerEnter is called when something enter in the trigger
void OnTriggerEnter(Collider obj)
{
if(obj.transform.name == "Player")
{
opening = true;
}
}
}
So, as I said, my unity editor which is Microsoft visual studio does not detect "Collider" (I don't have the possibility to pre-fill by pressing enter for example) and the color is white and not blue like "true" for example.
So because of that I can't move forward with the project, and I'd like to know what to do so that my editor recognizes "Collider" and I can make my project work!
Thank you in advance for your answers.
To solve that you can do the following:
Close Unity and Visual Studio.
Open Unity.
Open Visual Studio from Unity (Assets => Open C# Project).
If that doesn't work you need to check your current editor in:
Edit => Preferences => External Tools => External Script Editor
Visual Studio always have some errors like missing dependencies and stuff. I would recommend using Visual Studio Code application. Just like #Jack has explained. Browse to Edit => Preferences => External Tools => External Script Editor and change your script editor to Visual Studio Code and then try again.

Unable to connect Unity to Visual Studio

I am trying out Unity for my Game Design course, but I can't seem to get the code to work. I suspect it is because Visual Studio (which I write my code on) is not connecting to Unity properly, and here's why I think this:
For one thing, whenever I try to put the code on an object, it prints out this error:
Can't add script behaviour CallbackExecutor. The script needs to derive from MonoBehaviour!
This is despite the fact that nowhere is the code called CallbackExecutor, and the script apparently does derive from MonoBehaviour.
Second, when I load up the code onto Visual Studio, I get this error message:
C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj : error : The project file could not be loaded. Could not find file 'C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj'. C:\Users\cemya\Documents\D&D Roguelight Project\Assembly-CSharp.csproj
The problem is I don't know why it's not connecting. There's no option to import the package (which implies it's already imported), and I do have a package for Unity.
I'm using Visual Studios 2017 (specifically, I'm using the 2d setup most of the time), Unity version 2018.2.4f1, and the package is called Visual Studio 2017 Tools for Unity [Experimental], which is what was automatically downloaded when I began to set up coding for Unity. Since I got this version automatically, I believe that this should be a common problem, but I can't seem to find real answers on it (I even asked on the Unity forms themselves!)
I can show you the code if you think that would help.
UPDATE: I have just updated Unity to 2018.2.5f1 and tested it with the code that has actual code. All that happened is that the name for the error message is different:
Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour!
Also, here is the code for the one I'm testing, if it helps:
public class RollScript : MonoBehaviour
{
Random rand = new Random();
int r = 20;
int m = 5;
// Use this for initialization
void Start ()
{
int rolling = DieRoll(r, m);
print(rolling);
}
// Update is called once per frame
void Update ()
{
}
int DieRoll (int roll, int mod)
{
int get = rand.next(1, roll);
int result = get + mod;
return result;
}
}
UPDATE 2: I have checked with visual studio, and the error message there isn't there anymore, so that's an improvement.
UPDATE 3: I have created a new project and I have not run into issues there. I guess the problem was the code's connection to older editions.
This Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour! error has been mentioned in a few places on the internet, but with no satisfactory answers. This seems to be the only StackOverflow question about it.
I suspect it happens when there are un-fixed compile-time errors while you're adding a script to a GameObject. I had this problem just now, and after I fixed the errors, I was able to add the script. However, I tried to confirm this by adding a syntax error and then adding a script, but I still didn't get the Can't add script behavior error. So that's odd, but if you're encountering that error, I suggest fixing any compile-time errors and see if that works. (Whether it works or not, please reply to this question with the result.)

roslyn analyzers raising warnings which are later removed

I have an analyzer based on the default template for analyzers.
My problem is that when a do a full rebuild, some (but not all) the warnings appear on the error list.
When I open the files, the warnings start vanishing as the analyzer is re-executed on the open file. Eventually all the warnings disappear.
Am I registering these analyzers incorrectly. Ideally I only want them to execute once the code model is loaded properly.
Any suggestions on how to improve this would be great.
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(
this.HandleClassDeclaration,
SyntaxKind.ClassDeclaration);
}
This code analysers documentation (in this case) on the class declaration.
It reports a diagnosic when the Xml documentation nodes don't exist.
private void HandleClassDeclaration(SyntaxNodeAnalysisContext context)
{
// THE CHECK IN MY ANSWER BELOW GOES HERE...
var declaration = (ClassDeclarationSyntax)context.Node;
{
var hasDocumentation = declaration.HasDocumentation();
if (!hasDocumentation)
{
var diagnostic = Diagnostic.Create(this.Descriptor, declaration.Identifier.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
}
I am using this code to find the documentation.
public static DocumentationCommentTriviaSyntax GetDocumentationCommentTriviaSyntax(this SyntaxNode node)
{
if (node == null)
{
return null;
}
foreach (var leadingTrivia in node.GetLeadingTrivia())
{
var structure = leadingTrivia.GetStructure() as DocumentationCommentTriviaSyntax;
if (structure != null)
{
return structure;
}
}
return null;
}
can you switch your error list to build only view and see whether the warning is still there?
if warning is still there, that means the warning is generated by command line build. if the warning goes away once you opened the document, that means live analysis thinks there is no issue. and due to this difference between build and live analysis, the issue could happen.
if that is the case, it would be a bug in roslyn. (more specifically, bug in compilation option between live analysis and command line build - build inside of VS is also command line build with slightly different options)
For anyone else trying to analyse documentation in Roslyn, this little check is needed.
// <summary>
// check that the compiler is in a build mode that enables documentation analysis.
// it's not clear when this is off, but command line builds, and full rebuilds
// seem to have it turned off from time to time.
// </summary>
internal static bool IsDocumentationModeOn(this SyntaxNodeAnalysisContext context)
{
return context.Node.SyntaxTree?.Options.DocumentationMode
!= DocumentationMode.None;
}

EditorWindow.GetWindow doesn't work

This is my code:
public class CrazyWindow: EditorWindow
{
[MenuItem("Window/CrazyWindow")]
public static void Window()
{
EditorWindow.GetWindow(typeof(CrazyWindow));
Debug.Log("It should have appeared!");
}
string test = "";
public void OnGUI()
{
test = EditorGUILayout.TextField ("Text Field", test );
}
}
I'm using Unity3D v. 4.3.4f1 (free version) on Windows 7.
I have no idea why this is happening, as I can see in tutorials in the internet, that's how it should be done. The script is also in the Editor folder.
I'm able to click on the option "CrazyWindow" in the window menu, and I also get the Debug message informing me that the window should be working, but nothing happens besides that. No window is created at all!
What might be the cause of my problem?
Problem solved.
As Bart mentioned, I was using a custom Editor Layout, which was the case for the window not showing.
I just switched to one of the factory editor layouts and: ta dah, the window was there...
Pretty buggy thought.
Try renaming the 'CrazyWindow' part in the MenuItem and of the class itself. Unity remembers whether a window is visible or not and somehow something goes wrong there. Probably it thinks your window is visible (in cache) while actually it is not.
As Bart said, it remembers useless things
Just make it remember what we want it to
private void OnLostFocus() {
GetWindow<CrazyWindow>().Close();
}

Categories