Is there a replacement for FileCodeModel and CodeElements? #308
-
Hello, I'm used to using EnvDTE.FileCodeModel to extract code elements (EnvDTE.CodeElements) from a code file. I looked at DocumentView, which exposes TextBuffer, CurrentSnapshot, and ITextSnapshotLine, but i'm looking for a better way to break down the elements from each code line like CodeElements provides. Thank you, Jeff |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Depending on your use case, you might be able to use the Roslyn workspace. https://stackoverflow.com/a/28199656/4397397 That will only work for C# and VB.NET files, and is dramatically different to the file code model of Visual Studio, so it may be completely unsuitable for you, but I thought I'd mention it anyway. 😄 |
Beta Was this translation helpful? Give feedback.
-
It appears that you can still get to the FileCodeModel from the DTE object like the following code, even in VS2022 I am able to get the DTE object in VS2022 from the InitializeAsync method of a VSPackage without adding an Editor Adornment item (which causes unwanted effects if you are not trying to change the appearance of the editor). I created a VSIX project with a command and then added a Package. To get the DTE object, I added the following code to the InitializeAsync method of the Package.
I created a Globals class to save the DTE object in because you will need it later in the command Execute method. using EnvDTE; public static DTE dTE2 { get; set; } Finally, in the ExecuteAsync method of the Command Class I inserted the following code to test the FileCodeModel:
|
Beta Was this translation helpful? Give feedback.
It appears that you can still get to the FileCodeModel from the DTE object like the following code, even in VS2022
var codeElts = dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements;
I am able to get the DTE object in VS2022 from the InitializeAsync method of a VSPackage without adding an Editor Adornment item (which causes unwanted effects if you are not trying to change the appearance of the editor). I created a VSIX project with a command and then added a Package. To get the DTE object, I added the following code to the InitializeAsync method of the Package.