Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indented code blocks in Hovers should use the correct language for the hover provider #11331

Closed
DanTup opened this issue Aug 31, 2016 · 23 comments
Assignees
Labels
api feature-request Request for new features or functionality verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@DanTup
Copy link
Contributor

DanTup commented Aug 31, 2016

It would be really helpful if we could provide the language that should be used for code blocks in Hovers. Currently we can pass a string of markdown (where code will be uncoloured) or a chunk of text where the whole thing will be considered code in a language.

In Dart, comments are already markdown, so we have code like this:

/// This is a great method.
/// 
/// Here is some example code:
/// 
///     final name = "Danny";
///     print(name);
danny() {

}

The string we get is exactly like that, minus the ///. In order to get Dart highlighting code the code, we would have to split the document up, detect the code blocks and then created MarkedStrings with the language set to Dart as each one. This is quite complicated because there are other things that are indented the same way with blank lines before/after (like nested lists). It would be great if we could just set the code language and pass markdown directly.

@jrieken
Copy link
Member

jrieken commented Sep 1, 2016

You can use the normal <triple-backtick>``<lang_name>``<newline>code<newline>``<triple-backtick>

@jrieken jrieken closed this as completed Sep 1, 2016
@jrieken jrieken added the *question Issue represents a question, should be posted to StackOverflow (VS Code) label Sep 1, 2016
@DanTup
Copy link
Contributor Author

DanTup commented Sep 1, 2016

@jrieken I understand that, but it means figuring out where the code blocks are and rewriting them. Code is already doing Markdown parsing, so it seems a bit silly for us to have to do it too. All language extensions comments are probably going to have code blocks in the same language, so this seemed like it could save duplicated effort of us all writing our own markdown re-writer if comments are already markdown and contain code blocks?

@jrieken
Copy link
Member

jrieken commented Sep 1, 2016

In Dart, comments are already markdown, so we have code like this:

Maybe it's a formatting issue with GH but I really don't see any markdown in that sample, esp no code-block

@DanTup
Copy link
Contributor Author

DanTup commented Sep 1, 2016

The lines indented by 4 spaces are code, but maybe it wasn't a very clear example.

Here's a better (real) example; the Future<T> class. This is the real dartdoc from the class. We get this string (stripped of the comment markers) and do some basic tweaking of it. There are a bunch of code samples inside it. Code's rendering in hovers does correctly interpret these things indented with 4-spaces as code, but it doesn't know that they're dart so they don't get syntax highlighting.

Since this comment isn't written by us (extension author) but the end user, we can't ask them to add backticks (and it's also not the convention, as in Dart we know it's Dart). Surrounding the code blocks with backticks ourselves (in the hover provider) is possible but does mean we'll have to parse the Markdown on some level (because nested lists look a little like code blocks; they have blank lines before/after and are indented with 4 spaces).

I'm not against rewriting the markdown ourselves, but I think a more elegant solution would be if we could just tell Code that all code blocks in this markdown should be considered Dart. If you don't think that's a good feature; that's fine; I just wanted to suggest it before I embarked on Markdown-parsing :-)

Hope this is clearer!

/**
 * An object representing a delayed computation.
 *
 * A [Future] is used to represent a potential value, or error,
 * that will be available at some time in the future.
 * Receivers of a [Future] can register callbacks
 * that handle the value or error once it is available.
 * For example:
 *
 *     Future<int> future = getFuture();
 *     future.then((value) => handleValue(value))
 *           .catchError((error) => handleError(error));
 *
 * A [Future] can complete in two ways:
 * with a value ("the future succeeds")
 * or with an error ("the future fails").
 * Users can install callbacks for each case.
 * The result of registering a pair of callbacks is a new Future (the
 * "successor") which in turn is completed with the result of invoking the
 * corresponding callback.
 * The successor is completed with an error if the invoked callback throws.
 * For example:
 *
 *     Future<int> successor = future.then((int value) {
 *         // Invoked when the future is completed with a value.
 *         return 42;  // The successor is completed with the value 42.
 *       },
 *       onError: (e) {
 *         // Invoked when the future is completed with an error.
 *         if (canHandle(e)) {
 *           return 499;  // The successor is completed with the value 499.
 *         } else {
 *           throw e;  // The successor is completed with the error e.
 *         }
 *       });
 *
 * If a future does not have a successor when it completes with an error,
 * it forwards the error message to the global error-handler.
 * This behavior makes sure that no error is silently dropped.
 * However, it also means that error handlers should be installed early,
 * so that they are present as soon as a future is completed with an error.
 * The following example demonstrates this potential bug:
 *
 *     var future = getFuture();
 *     new Timer(new Duration(milliseconds: 5), () {
 *       // The error-handler is not attached until 5 ms after the future has
 *       // been received. If the future fails before that, the error is
 *       // forwarded to the global error-handler, even though there is code
 *       // (just below) to eventually handle the error.
 *       future.then((value) { useValue(value); },
 *                   onError: (e) { handleError(e); });
 *     });
 *
 * When registering callbacks, it's often more readable to register the two
 * callbacks separately, by first using [then] with one argument
 * (the value handler) and using a second [catchError] for handling errors.
 * Each of these will forward the result that they don't handle
 * to their successors, and together they handle both value and error result.
 * It also has the additional benefit of the [catchError] handling errors in the
 * [then] value callback too.
 * Using sequential handlers instead of parallel ones often leads to code that
 * is easier to reason about.
 * It also makes asynchronous code very similar to synchronous code:
 *
 *     // Synchronous code.
 *     try {
 *       int value = foo();
 *       return bar(value);
 *     } catch (e) {
 *       return 499;
 *     }
 *
 * Equivalent asynchronous code, based on futures:
 *
 *     Future<int> future = new Future(foo);  // Result of foo() as a future.
 *     future.then((int value) => bar(value))
 *           .catchError((e) => 499);
 *
 * Similar to the synchronous code, the error handler (registered with
 * [catchError]) is handling any errors thrown by either `foo` or `bar`.
 * If the error-handler had been registered as the `onError` parameter of
 * the `then` call, it would not catch errors from the `bar` call.
 *
 * Futures can have more than one callback-pair registered. Each successor is
 * treated independently and is handled as if it was the only successor.
 *
 * A future may also fail to ever complete. In that case, no callbacks are
 * called.
 */
abstract class Future<T> {
  // <snip>
}

@jrieken
Copy link
Member

jrieken commented Sep 1, 2016

Thanks for clarifying - I actually didn't know that you can format code blocks using space indentation... Now it makes sense to me.

Thinking of a way to achieve this... We have this and it should be save to access the modeId of the current model in case the value is missing. I'll investigate

@jrieken jrieken reopened this Sep 1, 2016
@jrieken jrieken added feature-request Request for new features or functionality and removed *question Issue represents a question, should be posted to StackOverflow (VS Code) labels Sep 1, 2016
@jrieken
Copy link
Member

jrieken commented Sep 1, 2016

That means we won't allow to set the default (unless a really good idea for that comes up) but simply take the context as default

@DanTup
Copy link
Contributor Author

DanTup commented Sep 1, 2016

Ah sorry, I should've provided a better example to start and maybe it would've been more obvious what I meant!

I don't fully understand the last comment; do you mean you'll infer it from the language of the HoverProvider rather than letting us set it? (if so, that seems perfect to me!)

@jrieken
Copy link
Member

jrieken commented Sep 1, 2016

Yeah - it would be the default when no language is set. Slightly on thin ice with that cos it changes existing behaviour but I'll give it a shot beginning of September

@jrieken jrieken added this to the September 2016 milestone Sep 1, 2016
@DanTup
Copy link
Contributor Author

DanTup commented Sep 1, 2016

Awesome; thank you! 👍

@DanTup DanTup changed the title Support setting the default language for code blocks in Hovers Indented code blocks in Hovers should use language used to register the hover provider Sep 2, 2016
@DanTup DanTup changed the title Indented code blocks in Hovers should use language used to register the hover provider Indented code blocks in Hovers should use the correct language for the hover provider Sep 2, 2016
@jrieken jrieken closed this as completed in 78ae495 Sep 5, 2016
@jrieken jrieken added the verification-needed Verification of issue is requested label Sep 29, 2016
@octref
Copy link
Contributor

octref commented Sep 29, 2016

I'm guessing this isn't the expected behavior, as dart code doesn't syntax highlight either hover or markdown comments in code...Or is it because you haven't updated Dart-Code to utilize this functionality?
@DanTup

hover

@jrieken How could I verify this works?

@DanTup
Copy link
Contributor Author

DanTup commented Sep 29, 2016

@octref It did work when I tested it in Insiders 18 days ago (see Dart-Code/Dart-Code#160 (comment)) but I haven't tested it recently. I'll make a note to give it another look tomorrow when I'm at a PC.

@octref octref reopened this Sep 29, 2016
@isidorn isidorn modified the milestones: October 2016, September 2016 Sep 30, 2016
@DanTup
Copy link
Contributor Author

DanTup commented Sep 30, 2016

@octref Indeed, it seems broken now; the colouring is all gone (was like that before I updated today - was probably a week old? and with todays version).

@octref octref removed the verification-needed Verification of issue is requested label Oct 1, 2016
@octref
Copy link
Contributor

octref commented Oct 1, 2016

@DanTup It's moved to October.

@chrmarti As this is not working and moved to Oct I'm removing verification-needed to get it out of the query.

@jrieken
Copy link
Member

jrieken commented Oct 4, 2016

@alexandrudima Is this due to the mode/tokenization changes you made?

@jrieken
Copy link
Member

jrieken commented Oct 4, 2016

Yeah - looks like a regression from 04f43c3 where the fallback to the editor mode id got lost

@jrieken jrieken closed this as completed in 9e91f0a Oct 4, 2016
@alexdima
Copy link
Member

alexdima commented Oct 5, 2016

@jrieken reopening to assess if this is a possible candidate for the stable branch

@alexdima alexdima reopened this Oct 5, 2016
@alexdima alexdima modified the milestones: September 2016, October 2016 Oct 5, 2016
@alexdima alexdima added the candidate Issue identified as probable candidate for fixing in the next release label Oct 5, 2016
@egamma
Copy link
Member

egamma commented Oct 6, 2016

Removing the candidate for September stable since it is not a regression.

@egamma egamma removed the candidate Issue identified as probable candidate for fixing in the next release label Oct 6, 2016
@egamma egamma modified the milestones: October 2016, September 2016 Oct 6, 2016
@egamma
Copy link
Member

egamma commented Oct 6, 2016

Fixed in the latest.

@egamma egamma closed this as completed Oct 6, 2016
@jrieken jrieken added the verification-needed Verification of issue is requested label Oct 24, 2016
@octref octref added the verified Verification succeeded label Oct 26, 2016
@octref
Copy link
Contributor

octref commented Oct 26, 2016

Hover works, code in comment doesn't.

dart

I'm looking at this file: https://github.com/angular/angular.dart/blob/master/lib/formatter/filter.dart

Also wondering, is this something we should do? In the case of angular.dart there are also a lot of HTML code in comment. Marking them as dart doesn't make sense, but providing a custom way for author to say which lang their code is in doesn't sounds good either.

@octref octref reopened this Oct 26, 2016
@octref octref removed the verified Verification succeeded label Oct 26, 2016
@alexdima
Copy link
Member

@octref How should we know that half the comment is in dart and half the comment is in html ?

@octref
Copy link
Contributor

octref commented Oct 26, 2016

@alexandrudima I thought the comment section are supposed to behave like hover, which marks everything as dart, in this dart file.

I think we should only support github-style 3-backstick code blocks in comment.

@jrieken jrieken closed this as completed Oct 27, 2016
@jrieken
Copy link
Member

jrieken commented Oct 27, 2016

This issue was about falling back to the language currently being shown in the editor if the markdown code block doesn't specify one.

@chrmarti chrmarti added the verified Verification succeeded label Oct 27, 2016
@chrmarti
Copy link
Collaborator

Using @DanTup's dart extension this works for me. Looking at dart-lang/sdk's future.dart with an untrained eye. Marking verified.

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

8 participants