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

Code Action Idea: Fix Missing Argument From Context #29

Open
hediet opened this issue Jun 16, 2022 · 6 comments
Open

Code Action Idea: Fix Missing Argument From Context #29

hediet opened this issue Jun 16, 2022 · 6 comments

Comments

@hediet
Copy link

hediet commented Jun 16, 2022

class MyService {}

class Foo {
    constructor(
        private readonly myService: MyService
    ) {}

    blah() {
        bar('hello'); // Cursor is here
    }
}

function bar(baz: string, service: MyService) {
    // ...
}

image

Cursor is on the squiggles.


- Use missing argument from context ->

class MyService {}

class Foo {
    constructor(
        private readonly myService: MyService
    ) {}

    blah() {
        bar('hello', this.myService);
    }
}

function bar(baz: string, service: MyService) {
    // ...
}

- Use missing argument from parameter ->

class MyService {}

class Foo {
    constructor(
        private readonly myService: MyService
    ) {}

    blah(myService: MyServic) {
        bar('hello', myService);
    }
}

function bar(baz: string, service: MyService) {
    // ...
}

If you could record my code changes, you would see that parameter "wiring" is a task I do very often.

@hediet hediet changed the title Code Action Idea: Fix Missing Parameter From Context Code Action Idea: Fix Missing Argument From Context Jun 16, 2022
@hediet
Copy link
Author

hediet commented Jun 16, 2022

Resharper has an entire Wizard for this.
When you use "Introduce parameter", it will go through all callers an offers options to fix those.

Basically, these are the resolution options:

  • For functions and methods: Use/Introduce parameter (and go to callers of this method when a new parameter has been added)
  • For classes: Use/Introduce field (and go to callers of the constructor when a new constructor parameter is added)
  • Use variable in scope

Ideally you could use breadth search on all symbols in scope to find valid expressions.

Also, if there are multiple possible expressions, pre-select the one that has the best levenshtein distance to parameter name (e.g. parameter name is input1: ITextModel, in scope are this.input1TextModel: ITextModel and this.input2TextModel: ITextModel - then you should suggest this.input1TextModel as first option).

I think this feature could be a wow-factor.

@lgrammel
Copy link
Contributor

Wow, thanks for this awesome writeup! "Introduce parameter" is one of the refactorings I was thinking about :)

Just for me to understand your flow better (because the title says "Fix missing argument"), would you primarily need to

  • just fix missing argument issues
  • or run an "introduce parameter" refactoring on a function/method/constructor
  • or both?

@hediet
Copy link
Author

hediet commented Jun 16, 2022

I would use this for both. If this recorder tool existed, I could show you exactly what I did and in which way automatisation would have helped me! (and how much time I spent on doing it manually)

@hediet
Copy link
Author

hediet commented Jun 17, 2022

Another code example where it would have saved me some seconds:

image

This feature could find all three missing arguments from the context.

@hediet
Copy link
Author

hediet commented Jun 19, 2022

My prototype works nicely:

It is a ts server plugin and heavily uses type checking.

@lgrammel
Copy link
Contributor

This is pretty cool!

P42 is currently limited to type information from a single file for performance reasons - I would need to extend the architecture to be able to get type information that's comparable to what the TS service offers. Do you know if there is a way to access the TS type information from other extensions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants