Is it possible to traverse all the CodeElement objects in a given source file?
My real target is to be able to find out what is the type of a variable in the cursor position from a Visual Studio Add-in.. I have tried the following code:
but it has some problems:
- It works only in a scope of a class, not in a function scope or global scope(in case of c++ source)
- It works when the cursor is in the position of the declaration, I want it to work when the cursor is in the position of a use of the variable.
So is there any way to get this semantic information without building my own compiler?
My real target is to be able to find out what is the type of a variable in the cursor position from a Visual Studio Add-in.. I have tried the following code:
// identify where the cursor is TextSelection selection = (TextSelection)_applicationObject.ActiveWindow.Selection; // get the starting point EditPoint Start = selection.TopPoint.CreateEditPoint(); // get the element under the cursor CodeElement element = _applicationObject.ActiveDocument.ProjectItem. FileCodeModel.CodeElementFromPoint(Start, vsCMElement.vsCMElementVariable); if (element.Kind == vsCMElement.vsCMElementVariable) { CodeVariable theVar = element as CodeVariable; string t = theVar.Type.AsString; MessageBox(t); }but it has some problems:
- It works only in a scope of a class, not in a function scope or global scope(in case of c++ source)
- It works when the cursor is in the position of the declaration, I want it to work when the cursor is in the position of a use of the variable.
So is there any way to get this semantic information without building my own compiler?