From 4414205cae960278915fc168e847111ee6b560f9 Mon Sep 17 00:00:00 2001 From: RblSb Date: Tue, 7 Nov 2023 22:54:53 +0300 Subject: [PATCH] Generate missing constructor args (#119) --- .../diagnostics/MissingArgumentsAction.hx | 8 +++- test/codeActions/AddMissingArgsTest.hx | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/haxeLanguageServer/features/haxe/codeAction/diagnostics/MissingArgumentsAction.hx b/src/haxeLanguageServer/features/haxe/codeAction/diagnostics/MissingArgumentsAction.hx index 62fe6bd1..e71b8ce6 100644 --- a/src/haxeLanguageServer/features/haxe/codeAction/diagnostics/MissingArgumentsAction.hx +++ b/src/haxeLanguageServer/features/haxe/codeAction/diagnostics/MissingArgumentsAction.hx @@ -209,8 +209,12 @@ class MissingArgumentsAction { static function getCallNamePos(document:HaxeDocument, argToken:TokenTree):Null { final parent = argToken.access().findParent(helper -> { - return switch (helper!.token!.tok) { - case Const(CIdent(_)): true; + final token = helper?.token ?? return false; + return switch (token.tok) { + case Const(CIdent(_)): + final parent = token.parent ?? return false; + !parent.matches(Kwd(KwdNew)); + case Kwd(KwdNew): true; case _: false; } }); diff --git a/test/codeActions/AddMissingArgsTest.hx b/test/codeActions/AddMissingArgsTest.hx index 553f9ecb..2490f977 100644 --- a/test/codeActions/AddMissingArgsTest.hx +++ b/test/codeActions/AddMissingArgsTest.hx @@ -139,4 +139,41 @@ class AddMissingArgsTest extends DisplayTestCase { }); }); } + + /** + function main() { + new Foo({-1-}1{-2-}); + } + class Foo { + public function new() {} + } + --- + function main() { + new Foo(1); + } + class Foo { + public function new(i:Int) {} + } + **/ + @:timeout(500) + function testConstructorArg(async:utest.Async) { + ctx.cacheFile(); + ctx.startServer(() -> { + var action:CodeAction = {title: ""}; + // diagnostics selects full arg range + final params = codeActionParams(range(1, 2)); + final diag = createDiagnostic(range(1, 2)); + final action:Null> = MissingArgumentsAction.createMissingArgumentsAction(ctx.context, action, params, diag); + assert(action != null); + action.then(action -> { + ctx.removeCacheFile(); + applyTextEdit(action.edit); + eq(ctx.result, ctx.doc.content); + async.done(); + }, (err) -> { + ctx.removeCacheFile(); + throw err; + }); + }); + } }