【问题标题】:Hotkeys for Previous and Next call stack frames in Visual StudioVisual Studio 中上一个和下一个调用堆栈帧的热键
【发布时间】:2008-10-23 11:28:33
【问题描述】:

Visual Studio 提供了许多导航热键: F8 用于当前面板中的下一项(搜索结果、错误...), Control+KN 用于书签, Alt+- 用于返回等等。

我找不到一个热键,我什至找不到它的菜单命令,所以我无法自己创建热键。

我不知道是否存在:Previous 和 Next 调用堆栈帧。

我在编程的时候尽量不用鼠标,但是当我需要回栈时,我必须用它来双击上一帧。

有人吗?做一个宏怎么样?

【问题讨论】:

  • 好的。不用鼠标我也能做到。对不起。我可以打开堆栈窗口,使用箭头键导航并在相关框架处按 Enter。但我认为这不是最好的解决方案。
  • 我认为没有办法做到这一点(VS 2008)。

标签: visual-studio keyboard-shortcuts


【解决方案1】:

我编写了 2 个宏来获得它:PreviousStackFrameNextStackFrame 并分配了快捷方式

Function StackFrameIndex(ByRef aFrames As EnvDTE.StackFrames, ByRef aFrame As EnvDTE.StackFrame) As Long
    For StackFrameIndex = 1 To aFrames.Count
        If aFrames.Item(StackFrameIndex) Is aFrame Then Exit Function
    Next
    StackFrameIndex = -1
End Function

Sub NavigateStack(ByVal aShift As Long)
    If DTE.Debugger.CurrentProgram Is Nothing Then
        DTE.StatusBar.Text = "No program is currently being debugged."
        Exit Sub
    End If

    Dim ind As Long = StackFrameIndex(DTE.Debugger.CurrentThread.StackFrames, DTE.Debugger.CurrentStackFrame)
    If ind = -1 Then
        DTE.StatusBar.Text = "Stack navigation failed"
        Exit Sub
    End If

    ind = ind + aShift
    If ind <= 0 Or ind > DTE.Debugger.CurrentThread.StackFrames.Count Then
        DTE.StatusBar.Text = "Stack frame index is out of range"
        Exit Sub
    End If

    DTE.Debugger.CurrentStackFrame = DTE.Debugger.CurrentThread.StackFrames.Item(ind)
    DTE.StatusBar.Text = "Stack frame index: " & ind & " of " & DTE.Debugger.CurrentThread.StackFrames.Count
End Sub

Sub PreviousStackFrame()
    NavigateStack(1)
End Sub

Sub NextStackFrame()
    NavigateStack(-1)
End Sub

【讨论】:

  • 我假设这适用于 VS 2008 或 2010,与 2012 或 2013 无关?
【解决方案2】:

我已经用AutoHotkey 解决了这个问题。我几个月前做的。 假设您想使用 Control+1 和 Control+2 并且 Control+Alt+C 必然会显示调用堆栈窗口:

^1::SendInput !^c{down}{enter}
^2::SendInput !^c{up}{enter}

它似乎工作得很好。如果您还没有使用 AutoHotkey 向 Visual Studio 展示谁是老大,请试一试。你的问题表明你会从中受益匪浅。这是一个改变游戏规则的人。祝你好运。

【讨论】:

    【解决方案3】:

    我认为没有明确的下一帧/前一帧键绑定,但这就是我所做的。

    CTRL-ALT-C 已经绑定到“Debug.CallStack” 这将使您专注于调用堆栈工具窗口

    一旦聚焦在 Callstack 窗口中...向上和向下箭头将移动您通过调用堆栈帧

    然后我绑定了

    CTRL-C、CTRL-S 到“DebuggerContextMenus.CallStackWindow.SwitchToFrame” 和 CTRL-C、CTRL-C 转“DebuggerContextMenus.CallStackWindow.SwitchToCode”

    这两者都会带您回到特定帧的代码窗口。

    希望对您有所帮助。

    【讨论】:

      【解决方案4】:

      非常感谢@Oleg Svechkarenko 的回答,为我提供了制定此解决方案的起点。由于现代版本的 Visual Studio 不再有官方宏支持,这应该适合那些使用 Visual Commander 插件的人,但可能很容易适应任何其他宏扩展。

      这是命令的代码,我创建了一个用于向上导航调用堆栈和一个用于向下导航,除了传递给MoveStackIndex()的参数外,其他代码相同,然后将键盘快捷键绑定到它们:

      using EnvDTE;
      using EnvDTE80;
      using System;
      
      public class C : VisualCommanderExt.ICommand
      {
          enum MoveDirection
          {
              Up,
              Down,
          }
      
          private bool IsValidFrame(StackFrame frame)
          {
              string language = frame.Language;
      
              bool result = (language == "C#"  || 
                             language == "C++" || 
                             language == "VB"  || 
                             language == "Python");
      
              return result;
          }
      
          private void MoveStackIndex(EnvDTE80.DTE2 DTE, MoveDirection direction)
          {
              StackFrame currentFrame = DTE.Debugger.CurrentStackFrame;
      
              bool foundTarget = false;
              bool pastCurrent = false;
      
              StackFrame lastValid = null;
              foreach (StackFrame frame in DTE.Debugger.CurrentThread.StackFrames)
              {
                  bool isCurrent = frame == currentFrame;
      
                  if (direction == MoveDirection.Down)
                  {
                      if (isCurrent)
                      {
                          if (lastValid == null)
                          {
                              // No valid frames below this one
                              break;
                          }
                          else
                          {
                              DTE.Debugger.CurrentStackFrame = lastValid;
                              foundTarget = true;
                              break;                      
                          }
                      }
                      else
                      {
                          if (IsValidFrame(frame))
                          {
                              lastValid = frame;
                          }
                      }
                  }
      
                  if (direction == MoveDirection.Up && pastCurrent)
                  {
                      if (IsValidFrame(frame))
                      {
                          DTE.Debugger.CurrentStackFrame = frame;
                          foundTarget = true;
                          break;
                      }               
                  }
      
                  if (isCurrent)
                  {
                      pastCurrent = true;
                  }
              }
      
              if (!foundTarget)
              {
                  DTE.StatusBar.Text = "Failed to find valid stack frame in that direction.";
              }
          }
      
          public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) 
          {
              if (DTE.Debugger.CurrentProgram == null)
              {
                  DTE.StatusBar.Text = "Debug session not active.";
              }
              else
              {
                  // NOTE: Change param 2 to MoveDirection.Up for the up command
                  MoveStackIndex(DTE, MoveDirection.Down);
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        查看工具->选项->环境->键盘。输入“stack”或“frame”,将出现相关菜单。似乎没有下一个和上一个调用堆栈帧。

        【讨论】:

        • 感谢您的快速回复!但这正是我所说的:“......甚至找不到它的菜单命令。”
        • @Adrian Aisemberg,我想他正在回答你的问题。我想他是说没有这样的快捷键。
        • 那么用宏创建一个怎么样?
        猜你喜欢
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 2016-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        相关资源
        最近更新 更多