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

TS Server: Return description of why a given refactoring cannot be applied #35098

Closed
mjbvz opened this issue Nov 14, 2019 · 5 comments · Fixed by #37871
Closed

TS Server: Return description of why a given refactoring cannot be applied #35098

mjbvz opened this issue Nov 14, 2019 · 5 comments · Fixed by #37871
Assignees
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Domain: TSServer Issues related to the TSServer In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Nov 14, 2019

Follow up on #34930

Search terms

  • refactor / refactoring

Problem

In VS Code, I often try to extract something to a function only to discover that this is not possible for the my current selection. The current UX for this is poor, since we offer no explanation of why the selection is not extractable.

Proposal

To fix this, we would like to have TS Server return a human readable message about why a refactoring cannot be applied. Client could then surface this information to users

With this proposal, TS Server would no silently longer dropping invalid refactorings like it does today and instead always return them but with an additional error message attached..

Protocol proposal

I believe this simplest approach is to add a new errorDescription field on ApplicableRefactorInfo:

interface ApplicableRefactorInfo {
    name: string;
    description: string;
    inlineable?: boolean;
    actions: RefactorActionInfo[];

    errorDescription?: string;
}

Client would be expected to not to try to resolve invalid refactorings.

To implement this in a way that won't break existing clients, we also need a way for a client to express that they can handle these invalid refactorings:

interface GetApplicableRefactorsRequestArgs extends FileLocationOrRangeRequestArgs {
    // better name?
    returnInvalidRefactorings?: boolean;
}

If returnInvalidRefactorings is not set, the TS Server should preserve its existing behavior, i.e. drop invalid refactorings

@amcasey
Copy link
Member

amcasey commented Nov 15, 2019

As we discussed offline, this seems likely to be of interest primarily when the user has explicitly requested a particular refactoring or family of refactoring (e.g. extraction). If that's the case, it might be preferable to treat the presence of a refactoring/family identifier as an indicator that errors are desirable.

@amcasey
Copy link
Member

amcasey commented Nov 15, 2019

For experimental purposes, we might want to add such a flag to the protocol and let the editor decide which portion of the flood of information is exposed to the user.

@mjbvz
Copy link
Contributor Author

mjbvz commented Nov 18, 2019

A few other points from our discussion last Thursday:

  • As @amcasey notes, this proposal may be tied to TSServer: Refactor trigger reason #35096

  • We likely only care about why a general class of refactorings are not valid. I.e. for extract constant we don't want to list out every possible scope that we could extract to and why we can't extract to it, instead: if any extract constant is possible then return that, otherwise return a single extract constant item that states why the selected expression could not be extracted (the error would probably come from the closest scope)

  • We currently have no good way to surface this information in VS Code. I'm looking into how to do this this month

@mjbvz
Copy link
Contributor Author

mjbvz commented Nov 19, 2019

Here's the VS Code side of the proposal and some thoughts on how we would surface the error information to users: microsoft/vscode#85160

@StingyJack
Copy link

I cant select an if statement like the one below in my nodejs typescript program to extract it to a function. Several options are visible in the context menu but I cant pick any of them. Where should I report that issue?

if (typeof (ConnectTo.connections[partnerCode]) == 'undefined'||!((<mongoose.Connection>ConnectTo.connections[partnerCode]).readyState==1||(<mongoose.Connection>ConnectTo.connections[partnerCode]).readyState==2)) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Refactorings e.g. extract to constant or function, rename symbol Domain: TSServer Issues related to the TSServer In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants