Skip to content

Add official API for fading out unused code #51104

Closed

Description

@mjbvz

Add an api that other extensions and languages can use to grey/fade out unused code and variables. JS/TS already implements this using a proposed API

/cc Johannes Rieken (@jrieken) Dirk Bäumer (@dbaeumer)

Activity

  1. added this to the June 2018 milestone on Jun 4, 2018
  2. mjbvz commented on Jun 4, 2018

    @mjbvz
    ContributorAuthor

    Current Proposed Design
    Use diagnostics to power this feature:

    /**
     * Additional metadata about the type of diagnostic.
     */
    export enum DiagnosticTag {
    	/**
    	 * Unused or unnecessary code.
    	 */
    	Unnecessary = 1,
    }
    
    export interface Diagnostic {
    	/**
    	 * Additional metadata about the type of the diagnostic.
    	 */
    	customTags?: DiagnosticTag[];
    }

    This approach works well because there are often quick fixes associated with unused code.

    Alternative Proposal
    Design a new API that allows more generic semantic coloring. Perhaps something similar to DiagnosticCollection

    enum HighlightingKind {
        Unused,
    }
    
    class Highlight {
        kind: HighlightingKind;
        range: Range;
    }
    
    class HighlightCollection {
        // like a diagnostic collection of highlights
    } 

    This approach would require that we also introduce a new "silent" DiagnosticSeverity that does not render anything in the editor.

  3. mattacosta commented on Jun 5, 2018

    @mattacosta
    Contributor

    How does this work with existing language servers that already generate warnings for unused variables? Does the region become grayed out, or do squigglies have precedence, or are both rendered? Is the behavior the same if a language server has a setting that allows the severity of warnings to be increased to errors or decreased to some other severity?

  4. mjbvz commented on Jun 5, 2018

    @mjbvz
    ContributorAuthor

    Matt Acosta (@mattacosta) This will not effect existing behavior; warnings and errors for unused variables will remain the same. The current proposed api is opt-in and just adds supplemental information to a diagnostic which tells vscode to also render it as faded/grey

  5. jrieken commented on Jun 6, 2018

    @jrieken
    Contributor

    Another proposal would be a semantic highlights provider (pull instead of push model). So something like this:

    enum HighlightingKind {
        Unused,
    }
    
    // maybe allow real styles, e.g color, font-style, etc
    class Highlight {
        kind: HighlightingKind;
        range: Range;
    }
    
    interface HighlightProvider {
      provideHighlights(document, range, token): ProviderResult<Highlights>
    }
  6. KamasamaK commented on Jun 8, 2018

    @KamasamaK

    I know the title specifically mentions "fading out unused code", but it seems to me that this would be a more general API. Would it be appropriate to also include something like Deprecated as a DiagnosticTag or HighlightingKind which could have its own associated styling?

  7. DustinCampbell commented on Jun 11, 2018

    @DustinCampbell
    Member

    The Visual Studio editor uses a pull model in it's ITagger stuff. The code you referenced is from the Roslyn infrastructure to support the VS editor.

    FWIW, we've found the VS editor's pull model to be very advantageous. Computing semantic highlighting can be very expensive. So, it's nice to be able to...

    1. Compute highlights asynchronously and then provide a notification when they are ready.
    2. Only provide highlights for the current view port (along with some lines above and below) rather than the entire view.
  8. AndrewKvalheim commented on Jun 11, 2018

    @AndrewKvalheim

    Weighing in on the gray/faded decision—the current behavior of removing syntax highlighting is confusing because the concern of whether code is used is distinct from whether it's syntactically valid. A common process for me is

    1. Write unused code.
    2. Observe that its syntax is valid.
    3. Use the code.

    and completely graying out the code blocks at the second step.

  9. mjbvz commented on Jun 12, 2018

    @mjbvz
    ContributorAuthor

    Dustin Campbell (@DustinCampbell) Is the CustomTags value on diagnostics used at all for colorization of unused variables in VS? That's what I based the current proposed vscode API off of but I don't know how VS actually uses customTags

  10. DustinCampbell commented on Jun 12, 2018

    @DustinCampbell
    Member

    The CustomTags property can be used for information that might be interesting to a consumer that's inspecting the diagnostic (for example, a code fix that responds to that diagnostic). In VS, the presentation layer looks for "Unnecessary" among the CustomTags to decide whether the span should be "faded" (not grayed) -- which is orthogonal from colorization and diagnostic severity.

  11. mjbvz commented on Jun 12, 2018

    @mjbvz
    ContributorAuthor

    Thanks Dustin Campbell (@DustinCampbell). Our current greyin-out serves the same purpose as fading-out in VS. I'm going to switch us over to use fading out too, grey out was just easier to introduce to start

    Johannes Rieken (@jrieken) Looks like VS is handling unused fade-out separately from general semantic coloring. Any opposition to doing this as well on our side? That way we don't have to get into implementing generalized semantic colorization just yet. Using diagnostics also makes sense from a language implementer perspective, at least for js/ts

  12. jrieken commented on Jun 13, 2018

    @jrieken
    Contributor

    Looks like VS is handling unused fade-out separately from general semantic coloring. Any opposition to doing this as well on our side?

    Unsure, looks like a one-off API and we shouldn't shy away from doing the real thing (not saying it must be you and me implementing it 🤣)

  13. 6 remaining items

  14. mjbvz commented on Jun 14, 2018

    @mjbvz
    ContributorAuthor

    I reviewed some existing vs code language implementation and believe they would be able to easily adopt a CustomTags type property. Most already show unused variables as warnings. For what it's worth, intellij also seems to be using diagnostics for their dead code highlighting.

    Other types of tags we could consider adding in the future:

    • Deprecated
    • Experimental / platform specific
    • Redundant (probably the same as unnecessary)
    • Uninitialized

    The important part is that these potential tags would provide both a colorization modifier and hover information that tells users exactly why the colorization is occurring and how to fix it.

  15. mattacosta commented on Jun 14, 2018

    @mattacosta
    Contributor

    Matt Bierner (@mjbvz) I'm not sure if I entirely follow. As I understand it, your proposal is to modify the definition of an LSP diagnostic in order to reuse the diagnostic provider, but I don't see a reason why a language service, with its own custom definition of a diagnostic, couldn't just send them to its own highlighting provider which would in effect just pass-through the relevant data.

    In general though, my primary concern with the diagnostic approach is that it may not work well for certain languages and/or situations. For example, it's entirely reasonable for unused code within a preprocessor directive to not have a diagnostic at all, since there is nothing inherently wrong, or for a diagnostic span to not cover the same area that should be faded.

  16. mjbvz commented on Jun 14, 2018

    @mjbvz
    ContributorAuthor

    Yes they could. There are a few problems with this approach however:

    • Language services now have to maintain both diagnostics and highlights. How this is implemented may not be consistent
    • The colorization and diagnostics can get out of sync
    • It's more chatty
    • We don't have a story for generalized semantic coloring yet and solving it is complex

    Even in the preprocessor case, I believe we still would want to provide some sort of feedback to users on why a piece of code is faded out (and optionally quick fixes and other tools to fix this). Using diagnostics lets us to that

  17. DustinCampbell commented on Jun 15, 2018

    @DustinCampbell
    Member

    Another concern is that the analysis to determine the symbol kind of identifier (semantic highlighting) and where a piece of code or not is very different and the latter is potential much more expensive.

  18. mattacosta commented on Jun 15, 2018

    @mattacosta
    Contributor

    Agreed.

    I also wanted to add that as more and more tags are added, you are also asking the code that generates the diagnostics to perform more and more potentially complex tasks. All for an editor that may not even support fading (or some other yet to be added feature) in the first place.

  19. DustinCampbell commented on Jun 15, 2018

    @DustinCampbell
    Member

    Matt Acosta (@mattacosta): To be clear, I was arguing that faded code detection should be part of diagnostic analysis. Semantic highlighting is an orthogonal feature. However, deciding that something should be faded often happens during diagnostic analysis. For example, it would be during diagnostic analysis that an unnecessary cast would be identified. That could be a warning, error, ignored, etc. However, the presentation in the editor should be that it's faded.

  20. mattacosta commented on Jun 15, 2018

    @mattacosta
    Contributor

    Dustin Campbell (@DustinCampbell) The way I see it, semantic highlighting can have many possible triggers. In the case of unused variables or an unnecessary cast, I agree that diagnostics should be one of those triggers, but we should avoid using tags on a bunch of (probably hidden) diagnostics as the sole trigger. For example, someone may want to trigger semantic highlighting (in this case fading) on a symbol that has no diagnostics attached to it. In this case, it wouldn't make sense to add a diagnostic with a tag just so the editor can find it later, because the highlighter could run its own logic to find it.

  21. DustinCampbell commented on Jun 15, 2018

    @DustinCampbell
    Member

    For example, someone may want to trigger semantic highlighting (in this case fading) on a symbol that has no diagnostics attached to it.

    Can you think of a concrete user scenario or is that just hypothetical?

    FWIW, I think there's a lot of "violent agreement" here. As I mentioned, in Visual Studio, we provide separate classifiers (i.e. "highlighters") to handle different semantic highlighting. This is similar to the "many possible triggers" that you described. 😄

  22. locked and limited conversation to collaborators on Aug 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions