33

Can I add links in comments to a code block in Visual studio ?

For example:

// block 1
class class1
{
}

// block 2
class class2
{
    // review [___class1]
}

[___class1] is a link for class1

Thanks in advance.

1
  • 2
    this is a great question for future new features on visual studio, it would help a lot understanding and accessing code, for example I use to make a comment on my code containing pseudo coded or a algorithmic explanation of what every section of code does in real life, and it will be really nice to link each step to the corresponding section of code. Commented Oct 17, 2016 at 8:25

5 Answers 5

26

You can bookmark your code in Visual Studio, but that's stored in your user options file and isn't normally checked into source control. I don't know of a way to link to portions of code from other portions of code.

Your best bet might be to use document comments and a <see> tag:

/// <see cref="Fully.Qualified.Type.Name"/>

But that's going to be limited to fully qualified locations, i.e. types, methods, fields, and properties. A particular block (say, an if statement within a method) is right out, and it'll only link the documentation for that method/whatever to another documentation section, and then only if you generate the documentation using a tool like Sandcastle.

One other thing you might consider, and this is a very bad hack, is to use file hyperlinks, like so:

// file://c:/code/file.cs

There are caveats:

  • You have to use the full path name. Relative paths won't work, so it's going to be tied directly to your code, and won't work if you remap your source repository to another location
  • Visual Studio will stop at the first space, so spaces in file names or folder names will cause it to fail
  • You can't link to a portion of the code, just the whole file.
Sign up to request clarification or add additional context in comments.

4 Comments

Just a comment about the second cavet of the file link (third suggestion), you can simply wrap the link in double quotes and any number of spaces will work.
Microsoft should really add this feature, to be able to link to any portion of code from any portion of code.
Consider applying your up-votes here: developercommunity.visualstudio.com/t/…
You can just replace spaces with %20 and spaces will work fine. No quotes needed in that case.
9

If you're looking for JS/TS support, as of May 2021, VS Code added support for standard JsDoc tagging:

https://code.visualstudio.com/updates/v1_57#_jsdoc-atlink-support

So that now this works:

@see MyClassName
// or
{@link MyClassName}

If the symbol cannot be resolved, import it and optionally silence eslint:

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { MyClassName } from "path/to/MyClassName";

/**
* This is an example JSDoc referring to the other class
* @see MyClassName
*/
export const ExampleClass ...

Then it can be clicked through and the context popup be shown.

2 Comments

What about Python?
This question was originally meant for Visual Studio. For one specific to VS Code with great answer see stackoverflow.com/questions/44306153/…
2

It depends what you want - you can just add a URL and VS will turn it into a link automatically within the code view.

Whether this is translated into a link when you generate the documentation will depend on what tool you use for that.

EDIT: Okay, it's possible that I missed your meaning. It's not terribly clear exactly what you're trying to do.

If you want to provide some example code, you can use the example tag:

/// <example>
/// Foo f = new Foo();
/// </example>

Is that what you meant?

You can't link to a particular block of code, but you can link to a member or type, for example:

/// <remarks>
/// You can use the <see cref="DoSomething" /> method to do something similar.
/// </remarks>

2 Comments

I believe Homam wants to link to a code snippet in his own source code file.
I think he wants to hyperlink his code to other portions of code, specifically blocks of code.
0

You can always put links in comments (since they are simply text, like the rest of the file).

It depends on the IDE how these will be displayed - Visual Studio will make it a clickable hyperlink.

Edit:

If you want to reference other sections of your code from comments, there is no current support in visual studio for this. The closest you can get is with code documentation comments using a referntial tag such as see, this will still not produce a hyperlink in the IDE.

Comments

-1

Yes you can add links to the code just do

# https://something.something/

and to access the link press control and double click the link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.