Homam
Homam

Reputation: 23871

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

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.

Upvotes: 33

Views: 25491

Answers (5)

Vamanrao
Vamanrao

Reputation: 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

Upvotes: -1

chrismarx
chrismarx

Reputation: 12555

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-link-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.

Upvotes: 8

Randolpho
Randolpho

Reputation: 56439

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.

Upvotes: 26

Oded
Oded

Reputation: 499242

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502985

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>

Upvotes: 2

Related Questions