vscode-gengetset icon indicating copy to clipboard operation
vscode-gengetset copied to clipboard

Feature request: Implement interface/class

Open kpau opened this issue 9 years ago • 2 comments

Feature request: Implement interface or class

The plug-in is very nice. It would be great if there is auto interface (or class) implementation feature, similar to Visual Studio. It should be used on classes which implement interfaces or other classes, and when used, it should generate default method implementations. The default behavior for the methods can be similar to Visual Studio: throw new Error('Not Implemented');.

Additional features

  • Detect which methods are already defined in the class, and not add them again.
  • When VS Code has more advanced cold-folding functionality, it would be nice to have these methods wrapped in // #region InterfaceName - // #endregion block (or whatever code-folding is supported).
  • Explicitly set access modifiers, and persist them for abstract methods.
  • When extending abstract class, the user should be able to choose whether the abstract methods should be kept abstract (with no body) or not (with the default implementation).
  • The user should be able to choose a single interface/class to be implemented.

Example:

When we have this code...

abstract class AbstractClass {
    public regularMethod(): string { return 'test'; }
    protected abstract abstractMethod(a: number): string;
}
// regardless abstract or not
class BaseClass {
    public classMethod(): number { return 1; }
}
abstract class TheInterface {
    abstract interfaceMethodA(): void;
    abstract interfaceMethodB(): void;
}
export class TheClass extends AbstractClass implements BaseClass, TheInterface {
    constructor() { super(); }
}

...and we use the feature, the final code for TheClass should be something like:

export class TheClass extends AbstractClass implements BaseClass, TheInterface {
    constructor() { super(); }

    // #region BaseClass
    public classMethod(): number {
        throw new Error('Not Implemented');
    }
    // #endregion

    // #region AbstractClass
    protected abstractMethod(a: number): string {
        throw new Error('Not Implemented');
    }
    // or (class must be set as abstract)
    protected abstract abstractMethod(a: number): string;
    // #endregion

    // #region TheInterface
    public interfaceMethodA(): void {
        throw new Error('Not Implemented');
    }
    public interfaceMethodB(): void {
        throw new Error('Not Implemented');
    }
    // #endregion
}

Regards, Kristiyan

kpau avatar Oct 17 '16 11:10 kpau

I was already thinking about this, since I'm missing this feature also.

The current level of implementing and extending classes in vscode is non existing which is pretty sad given the fact Typescript does recognize an issue when you forget to implement one of these functions :)

Maybe the current class parsing can be used to index the abstract classes and interfaces. When I have time I will look into this.

cybertim avatar Oct 17 '16 11:10 cybertim

This has been in VSCode since 2014.

Start writing a class that extends an abstract class (or interface), the class name is red underlined because you have not yet satisfied the interface requirements.

Place cursor on the class name.

A lightbulb is shown.

Right click the lightbulb and click "Implement inherited abstract class."

You can also use "Ctrl + ." when cursor is over the class name to pull up the lightbulb menu, instead of using the mouse.

beanstalkblue avatar Mar 30 '17 14:03 beanstalkblue