Visual Studio macro to jump to T4MVC link

I am using T4MVC and I am pleased with this and want to keep it - it eliminates the runtime flaws. Unfortunately, this makes navigation to views and content difficult (aka Views and Links in T4MVC). Even using Resharper, I cannot go to the specified element:

T4MVC and Resharper Navigation

Can I create a macro to create a macro? Having never created a VS IDE macro before, I don’t understand how to get some things, such as the internal results of the Go to Definition process, if possible.

If you are new to T4MVC, a macro can usually help here:

  • Given the token: Links.Content.Scripts.jQuery_js in the file MyView.cshtml, '(F12) Go to Definition'. This behaves correctly.

  • Upon reaching the appropriate task: public readonly string jQuery_js = "~/Content/Scripts/jQuery.js"; in the file created by T4MVC (which is very nice, thanks to David, but we really don't need to see it), grab the assigned line and close the file.

  • Go to Solution Explorer to PhysicalPath represented by the captured string.

This process will also work for views / layouts / master pages / partial files, etc.

If you provided a macro or a macro link to do this, or you have another solution, this is great. Otherwise, the hints on how to do step 3 simply in the VS macro will be especially appreciated and will get increased returns from me. I would post the macro here as an answer when finished.

Thanks!

+3
source share
1 answer

Here's a Visual Studio macro to help.

What is he doing

Now you are probably using T4MVC links in such places:

  • Layout = MVC.Shared.Views.MasterSiteTheme;
  • ScriptManager.AddResource (Links.Content.Script.jQueryXYZ_js);
  • <link type = "text / css" href = "@ Links.Content.Style.SiteTheme_css" />
  • return View (MVC.Account.Views.SignIn);
  • @ Html.Partial (MVC.Common.Views.ContextNavigationTree)
  • @ Html.ActionLink ("Login / Register", MVC.Account.SignIn ())

F12 (Go to Definition) already works for the last bullet (action), but this hack is designed to cover other scenarios (resources).

Macro

 Imports EnvDTE Imports System.IO Public Module NavT4Link Sub NavigateToLink() DTE.ExecuteCommand("Edit.GoToDefinition") Dim navpath As String = Path.GetFileName(DTE.ActiveDocument.FullName) Dim isContentLink As Boolean = navpath.Equals("T4MVC.cs") If (isContentLink Or navpath.EndsWith("Controller.generated.cs")) Then Dim t4doc As TextDocument = DTE.ActiveDocument.Object() navpath = CurrentLinePathConstant(t4doc) If isContentLink Then t4doc.Selection.MoveToPoint(t4doc.Selection.ActivePoint.CodeElement(vsCMElement.vsCMElementClass).StartPoint) t4doc.Selection.FindText("URLPATH") navpath = Path.Combine(CurrentLinePathConstant(t4doc), navpath) End If If navpath.StartsWith("~") Then DTE.ActiveDocument.Close(vsSaveChanges.vsSaveChangesPrompt) Dim proj As Object = DTE.Solution.FindProjectItem(DTE.ActiveDocument.FullName).ContainingProject navpath = Path.GetDirectoryName(proj.Fullname()) + navpath.TrimStart("~") DTE.ItemOperations.OpenFile(navpath) End If End If End Sub Function CurrentLinePathConstant(ByVal t4doc As TextDocument) As String t4doc.Selection.SelectLine() Dim sa() As String = t4doc.Selection.Text().Split("""") If sa.Length > 1 Then Return sa(sa.Length - 2) Else Return "" End Function End Module 

Installation

  • In Visual Studio, press "Alt-F8" to open Macro Explorer.
  • Right-click My Macros, select New Module ..., and click Add.
  • Replace all text with the code shown here.
  • Save and exit the macro editor.
  • Open "Tools: Options."
  • In the left pane, select "Environment: Keyboard."
  • In the "Show commands containing" text box, enter "T4."
  • In the "Press shortcut keys:" field, press the "F12" key.
  • Click Assign and OK.

In unencrypted VS, this installation process does not lead to the creation of a β€œbinding” macro. The workaround was to (CTRL-SHIFT-RR) write an empty macro and paste the code into it without renaming it. If someone knows a more documented approach to installing a macro in VS, please comment.

Notes / Cautions

This means replacing the current F12 functionality, so if it is not a T4MVC link, it will do the usual, otherwise it will continue to open the resource. It handles most cases, but not empty T4MVC pool controller methods. Those you dumped in the same place as before.

For Content / Asset / Link resources, for example, for navigating through them in the solution explorer, perhaps for image files, but I did not see this functionality in Visual Studio documents.

+2
source

Source: https://habr.com/ru/post/1396304/


All Articles