diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index c2eeda75c5fcfede1a5981892094ec986205c0a5..3d361525b05a754b59ce3dbf536b75193ee0c88f 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -111,6 +111,18 @@ { "language": "cpp", "path": "./snippets/napi_class_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_async_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_promise_snippets.json" + }, + { + "language": "cpp", + "path": "./snippets/napi_callback_snippets.json" } ] }, diff --git a/src/vscode_plugin/snippets/napi_async_snippets.json b/src/vscode_plugin/snippets/napi_async_snippets.json new file mode 100644 index 0000000000000000000000000000000000000000..c9eabeb67f5846d0251ca70ffed6a62ef94482f9 --- /dev/null +++ b/src/vscode_plugin/snippets/napi_async_snippets.json @@ -0,0 +1,43 @@ +{ + "Napi Async Func": { + "prefix": "napiasyncfunc", + "body": [ + "struct AsyncData{", + " napi_async_work work;", + " napi_ref callbackRef;", + " // save async work param in.", + " // save async work result.", + "};", + "static void ExecuteAsyncWork(napi_env env, void* data) {", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"ExecuteAsyncWork\");", + "}", + "static void CompleteAsyncWork(napi_env env, napi_status status, void* data) {", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork\");", + "}", + "napi_value StartAsyncWork(napi_env env, napi_callback_info info)", + "{", + " napi_value resourceName = nullptr;", + " // Create a js string which is used as the name for the asynchronous work object being created.", + " napi_create_string_utf8(env, \"asyncWork\", NAPI_AUTO_LENGTH, &resourceName);", + " // Create an asynchronous work object.", + " napi_create_async_work(env, nullptr, resourceName, ExecuteAsyncWork, CompleteAsyncWork, asyncData, &asyncData->work);", + " // Add the asynchronous work object to a queue.", + " napi_queue_async_work(env, asyncData->work);", + " return nullptr;", + "}", + "EXTERN_C_START", + "// Initialize the module.", + "static napi_value Init(napi_env env, napi_value exports)", + "{", + " // Define properties and methods for a N-API object.", + " napi_property_descriptor desc[] = {", + " { \"startAsyncWork\", nullptr, StartAsyncWork, nullptr, nullptr, nullptr, napi_default, nullptr },", + " };", + " // Add an array of properties to a ArkTs object.", + " napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);", + " return exports;", + "}", + "EXTERN_C_END" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_callback_snippets.json b/src/vscode_plugin/snippets/napi_callback_snippets.json new file mode 100644 index 0000000000000000000000000000000000000000..f8589865e1a38ccfd32caac5ec299d2bce42b56b --- /dev/null +++ b/src/vscode_plugin/snippets/napi_callback_snippets.json @@ -0,0 +1,29 @@ +{ + "Napi Callback": { + "prefix": "napicallback", + "body": [ + "static napi_value CallbackSample(napi_env env, napi_callback_info info)", + "{", + " napi_value callback;", + " napi_value callbackArg[1] = nullptr;", + " napi_value result;", + " // Invokes a js function with the provided arguments and returns the result.", + " napi_call_function(env, nullptr, callback, 1, callbackArg, &result);", + " return nullptr;", + "}", + "EXTERN_C_START", + "// Initialize the module.", + "static napi_value Init(napi_env env, napi_value exports)", + "{", + " // Define properties and methods for a N-API object.", + " napi_property_descriptor desc[] = {", + " { \"callbackSample\", nullptr, CallbackSample, nullptr, nullptr, nullptr, napi_default, nullptr }", + " };", + " // Add an array of properties to a ArkTs object.", + " napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);", + " return exports;", + "}", + "EXTERN_C_END" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index a5c03b409eb826e85658ddb8ae125cd587eb10f7..b94eb08f51371a619413e50ec259735e3a7eef94 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -50,6 +50,7 @@ " // Retrieve obj (the C++ object) previously wrapped in jsThis (the ArkTS object), and perform subsequent operations.", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", " // Todo: you can use \"napistringutf8out\" command that return string utf8 value to js.", + " return stringOut;", "}", "napi_value HelloWorld::SetValue(napi_env env, napi_callback_info info)", "{", diff --git a/src/vscode_plugin/snippets/napi_promise_snippets.json b/src/vscode_plugin/snippets/napi_promise_snippets.json new file mode 100644 index 0000000000000000000000000000000000000000..7a11abbf62096680fbba38bbb2c6382522e7db9e --- /dev/null +++ b/src/vscode_plugin/snippets/napi_promise_snippets.json @@ -0,0 +1,37 @@ +{ + "Napi Promise": { + "prefix": "napipromise", + "body": [ + "static napi_value PromiseSample(napi_env env, napi_callback_info info)", + "{", + " napi_value promise;", + " napi_deferred deferred;", + " // Create a new js Promise.", + " napi_create_promise(env, &deferred, &promise);", + " napi_value valueOut = nullptr;", + " bool isResolved = true;", + " if (isResolved) {", + " // Fulfill a js Promise with a given value.", + " napi_resolve_deferred(env, deferred, valueOut);", + " } else {", + " // Reject a js Promise with a given reason.", + " napi_reject_deferred(env, deferred, valueOut);", + " }", + " return promise;", + "}", + "EXTERN_C_START", + "// Initialize the module.", + "static napi_value Init(napi_env env, napi_value exports)", + "{", + " // Define properties and methods for a N-API object.", + " napi_property_descriptor desc[] = {", + " { \"promiseSample\", nullptr, PromiseSample, nullptr, nullptr, nullptr, napi_default, nullptr }", + " };", + " // Add an array of properties to a ArkTs object.", + " napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);", + " return exports;", + "}", + "EXTERN_C_END" + ] + } +} \ No newline at end of file