Visual Studio Jump to Beginning of Function

 ̄綄美尐妖づ 提交于 2020-06-24 07:02:49

问题


I know that Ctrl+} will take you to the corresponding brace in Visual Studio, but say I'm in the middle of a gigantic function and I don't know where the top or the bottom is, is there a shortcut to get directly to the function declaration?

void function()
{
//so many lines of code
//can't see the top or the bottom curly brace
//can i get to the top of the function with a shortcut?
}

回答1:


I have a fresh install of VS2017. As of 15.9.1, the default for me is Alt+Shift+[.

This is the shortcut for EditorContextMenus.Navigate.GoToContainingBlock. So you may have to execute this shortcut multiple times if you are a few block layers deep, but it'll get you where you want to go.




回答2:


Alt+Ctrl+UP,Tab,Tab,Enter
This sequence will move you through Project selctor > Scope selector > Function selector > Current Function.

Ctrl+M,Ctrl+M
This sequence will toggle between collapse/expand current block.
Place cursor at any line that is immediately enclosed by the function. Collapse. Place cursor at the end of the collapsed function, i.e after { ... }. Expand the function to get to its last brace.

Note:
If you have difficulty in finding a line immediately enclosed by the function(for example, when the function has lots of nested blocks), you can always goto the beginning to collapse the function.




回答3:


I usually double press the white line that is located left of the code. It closes the function but it also takes you to the declaration of the function.




回答4:


Update

With last updates Visual Studio, now default keyboard shortcut for EditorContextMenus.Navigate.GoToContainingBlock is Shift+Alt+[


Old Answer:

Visual Studio 2017 version 15.8.0 comes with a new shortcut Ctrl + Alt + UpArrow - Go to Enclosing Block.

Go to Enclosing Block (Ctrl + Alt + UpArrow) allows you to quickly navigate up to the beginning of the enclosing code block.

Source

This command allows also move to function declaration if you are inside function.

If shortcut doesn't work for you




回答5:


You can do it with Macros for Visual Studio extension.

Here's the code for macros:

// BeginningOfFunction moves the caret to the beginning of the containing definition.

var textSelection = dte.ActiveDocument.Selection;

// Define Visual Studio constants
var vsCMElementFunction = 2;

var codeElement = textSelection.ActivePoint.CodeElement(vsCMElementFunction);


if (codeElement != null)
{
    textSelection.MoveToPoint(codeElement.GetStartPoint());
    dte.ActiveDocument.Activate();
}

It is one of the sample macros of the extension. Edited it a little, because for some reason sample didn't work for me. You can get to the end of the function by changing codeElement.GetStartPoint() to codeElement.GetEndPoint().




回答6:


I found one trick in visual studio:

Place the cursor on the empty to get the context (name of the function), copy the name of the function, then click the drop down arrow where all functions will be listed, paste the function name, enter. Then you are at the beginning of that function!



来源:https://stackoverflow.com/questions/27805217/visual-studio-jump-to-beginning-of-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!