diff --git a/src/vscode_plugin/package.json b/src/vscode_plugin/package.json index 15b4348892adc29db080b10254950aff7dc97ccc..3fc54373eb21a807186081d807c1c77ffbdedbbb 100644 --- a/src/vscode_plugin/package.json +++ b/src/vscode_plugin/package.json @@ -124,6 +124,10 @@ "language": "cpp", "path": "./snippets/napi_callback_snippets.json" }, + { + "language": "cpp", + "path": "./snippets/napi_variable_snippets.json" + }, { "language": "cpp", "path": "./snippets/napi_enum_snippets.json" diff --git a/src/vscode_plugin/snippets/napi_class_snippets.json b/src/vscode_plugin/snippets/napi_class_snippets.json index b94eb08f51371a619413e50ec259735e3a7eef94..b6dc29900b3be59048ba8272d3f73064316b8096 100644 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ b/src/vscode_plugin/snippets/napi_class_snippets.json @@ -2,13 +2,13 @@ "Napi Class": { "prefix": "napiclass", "body": [ - "class HelloWorld {", + "class A {", " public:", " static napi_value Init(napi_env env, napi_value exports);", " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", " private:", - " explicit HelloWorld();", - " ~HelloWorld();", + " explicit A();", + " ~A();", " static napi_value New(napi_env env, napi_callback_info info);", " static napi_value GetValue(napi_env env, napi_callback_info info);", " static napi_value SetValue(napi_env env, napi_callback_info info);", @@ -17,55 +17,59 @@ " napi_env env_;", " napi_ref wrapper_;", "};", - "HelloWorld::HelloWorld(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", - "HelloWorld::~HelloWorld() { napi_delete_reference(env_, wrapper_); }", - "void HelloWorld::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", + "A::A(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", + "A::~A() { napi_delete_reference(env_, wrapper_); }", + "void A::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", "{", - " reinterpret_cast(nativeObject)->~HelloWorld();", + " reinterpret_cast(nativeObject)->~A();", "}", - "napi_value HelloWorld::New(napi_env env, napi_callback_info info)", + "napi_value A::New(napi_env env, napi_callback_info info)", "{", " napi_value newTarget;", " // Check if the constructor was invoked with new.", " napi_get_new_target(env, info, &newTarget);", " if (newTarget != nullptr) {", - " // Invoked as the constructor `new HelloWorld()`.", + " // Invoked as the constructor `new A()`.", " napi_value jsThis;", " // Retrieve the callback's context and arguments.", " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", - " HelloWorld* obj = new HelloWorld();", + " A* obj = new A();", " obj->env_ = env;", " // Wrap the C++ object obj in the ArkTS object jsThis.", - " napi_wrap(env, jsThis, reinterpret_cast(obj), HelloWorld::Destructor, nullptr, &obj->wrapper_);", + " napi_wrap(env, jsThis, reinterpret_cast(obj), A::Destructor, nullptr, &obj->wrapper_);", " return jsThis;", " } else {", - " OH_LOG_ERROR(LOG_APP, \"HelloWorld must be invoked as a constructor with `new`\");", + " OH_LOG_ERROR(LOG_APP, \"A must be invoked as a constructor with `new`\");", " return nullptr;", " }", "}", - "napi_value HelloWorld::GetValue(napi_env env, napi_callback_info info)", + "napi_value A::GetValue(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napiobjectin\" command that get object param from js.", - " HelloWorld* obj;", + " napi_value jsThis;", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " A* obj;", " // 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)", + "napi_value A::SetValue(napi_env env, napi_callback_info info)", "{", - " // Todo: you can use \"napiobjectin\" command that get object param from js.", - " HelloWorld* obj;", + " napi_value jsThis;", + " size_t argc = 1;", + " napi_value args[1];", + " napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);", + " A* obj;", " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", " // Todo: you can use \"napistringutf8in\" command that get string utf8 param from js.", " return nullptr;", "}", - "napi_value HelloWorld::Hello(napi_env env, napi_callback_info info)", + "napi_value A::Hello(napi_env env, napi_callback_info info)", "{", - " OH_LOG_INFO(LOG_APP, \"HelloWorld::Hello World!\");", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A::Hello World!\");", " return nullptr;", "}", - "napi_value HelloWorld::Init(napi_env env, napi_value exports)", + "napi_value A::Init(napi_env env, napi_value exports)", "{", " // Define properties and methods for a N-API object", " napi_property_descriptor properties[] = {", @@ -74,9 +78,9 @@ " };", " napi_value cons;", " // Define a js class", - " napi_define_class(env, \"HelloWorld\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", - " // Set the 'HelloWorld' class on the exports object.", - " napi_set_named_property(env, exports, \"HelloWorld\", cons);", + " napi_define_class(env, \"A\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", + " // Set the 'A' class on the exports object.", + " napi_set_named_property(env, exports, \"A\", cons);", " return exports;", "}" ] diff --git a/src/vscode_plugin/snippets/napi_struct_snippets.json b/src/vscode_plugin/snippets/napi_struct_snippets.json index 69feb2ebf5c98bcf98a4643af140cadda3f7ac8d..6f23b81dc89649705849e2f45ea4e77c82e6d42c 100644 --- a/src/vscode_plugin/snippets/napi_struct_snippets.json +++ b/src/vscode_plugin/snippets/napi_struct_snippets.json @@ -2,12 +2,88 @@ "Napi Struct": { "prefix": "napistruct", "body": [ - "// Struct in", - "// Todo: you can use \"napiobjectin\" command that get object value from js.", - "// Todo: you can use \"napigetvaluefromobject\" command that get value from js object.", - "// Struct out", - "// Todo: you can use \"napiobjectout\" command that return object to js.", - "// Todo: you can use \"napisetvalue2object\" command that set value to objectOut." + "struct B {", + " public:", + " static napi_value Init(napi_env env, napi_value exports);", + " static void Destructor(napi_env env, void* nativeObject, void* finalize_hint);", + " private:", + " explicit B();", + " ~B();", + " static napi_value New(napi_env env, napi_callback_info info);", + " static napi_value GetValue(napi_env env, napi_callback_info info);", + " static napi_value SetValue(napi_env env, napi_callback_info info);", + " static napi_value Hello(napi_env env, napi_callback_info info);", + " std::string value_;", + " napi_env env_;", + " napi_ref wrapper_;", + "};", + "B::B(): value_(\"\"), env_(nullptr), wrapper_(nullptr) {}", + "B::~B() { napi_delete_reference(env_, wrapper_); }", + "void B::Destructor(napi_env env, void* nativeObject, [[maybe_unused]] void* finalize_hint)", + "{", + " reinterpret_cast(nativeObject)->~B();", + "}", + "napi_value B::New(napi_env env, napi_callback_info info)", + "{", + " napi_value newTarget;", + " // Check if the constructor was invoked with new.", + " napi_get_new_target(env, info, &newTarget);", + " if (newTarget != nullptr) {", + " // Invoked as the constructor `new B()`.", + " napi_value jsThis;", + " // Retrieve the callback's context and arguments.", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " B* obj = new B();", + " obj->env_ = env;", + " // Wrap the C++ object obj in the ArkTS object jsThis.", + " napi_wrap(env, jsThis, reinterpret_cast(obj), B::Destructor, nullptr, &obj->wrapper_);", + " return jsThis;", + " } else {", + " OH_LOG_ERROR(LOG_APP, \"B must be invoked as a constructor with `new`\");", + " return nullptr;", + " }", + "}", + "napi_value B::GetValue(napi_env env, napi_callback_info info)", + "{", + " napi_value jsThis;", + " // Get js context and arguments", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " B* obj;", + " // 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 B::SetValue(napi_env env, napi_callback_info info)", + "{", + " napi_value jsThis;", + " size_t argc = 1;", + " napi_value args[1];", + " napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);", + " B* obj;", + " napi_unwrap(env, jsThis, reinterpret_cast(&obj));", + " // Todo: you can use \"napistringutf8in\" command that get string utf8 param from js.", + " return nullptr;", + "}", + "napi_value B::Hello(napi_env env, napi_callback_info info)", + "{", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"Struct\", \"B::Hello World!\");", + " return nullptr;", + "}", + "napi_value B::Init(napi_env env, napi_value exports)", + "{", + " // Define properties and methods for a N-API object.", + " napi_property_descriptor properties[] = {", + " { \"value\", 0, 0, GetValue, SetValue, 0, napi_default, 0 },", + " { \"hello\", nullptr, Hello, nullptr, nullptr, nullptr, napi_default, nullptr }", + " };", + " napi_value cons;", + " // Define a js class", + " napi_define_class(env, \"B\", NAPI_AUTO_LENGTH, New, nullptr, 2, properties, &cons);", + " // Set the 'B' class on the exports object.", + " napi_set_named_property(env, exports, \"B\", cons);", + " return exports;", + "}" ] } } \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_variable_snippets.json b/src/vscode_plugin/snippets/napi_variable_snippets.json new file mode 100644 index 0000000000000000000000000000000000000000..b6e2340960cf257065b78e6fc15fc3d66861d717 --- /dev/null +++ b/src/vscode_plugin/snippets/napi_variable_snippets.json @@ -0,0 +1,167 @@ +{ + "Napi double in": { + "prefix": "napidoublein", + "body": [ + "double value0 = 0;", + "// Convert a JS number type value to a C double type.", + "napi_get_value_double(env, args[0], &value0);" + ] + }, + "Napi int32_t in": { + "prefix": "napiint32in", + "body": [ + "int32_t value0 = 0;", + "// Convert a JS number type value to a C int32_t type.", + "napi_get_value_int32(env, args[0], &value0);" + ] + }, + "Napi uint32_t in": { + "prefix": "napiuint32in", + "body": [ + "uint32_t value0 = 0;", + "// Convert a JS number type value to a C uint32_t type.", + "napi_get_value_uint32(env, args[0], &value0);" + ] + }, + "Napi int64_t in": { + "prefix": "napiint64in", + "body": [ + "int64_t value0 = 0;", + "// Convert a JS number type value to a C int64_t type.", + "napi_get_value_int64(env, args[0], &value0);" + ] + }, + "Napi bool in": { + "prefix": "napiboolin", + "body": [ + "bool value0 = false;", + "// Convert a JS boolean type value to a C bool type.", + "napi_get_value_bool(env, args[0], &value0);" + ] + }, + "Napi string utf8 in": { + "prefix": "napistringutf8in", + "body": [ + "size_t strUtf8Length = 0;", + "// Get the length of the js string.", + "napi_get_value_string_utf8(env, args[0], NULL, 0, &strUtf8Length);", + "char *value0 = new char[strUtf8Length + 1];", + "// Get the js string as a UTF-8 encoded C string.", + "napi_get_value_string_utf8(env, args[0], value0, strUtf8Length + 1, &strUtf8Length);", + "// Todo: Assign value0 to actual business value", + "delete[] value0;" + ] + }, + "Napi string utf16 in": { + "prefix": "napistringutf16in", + "body": [ + "size_t strUtf16Length = 0;", + "// Get the length of the js string.", + "napi_get_value_string_utf16(env, args[0], NULL, 0, &strUtf16Length);", + "char16_t *value0 = new char16_t[strUtf16Length + 1];", + "// Get the js string as a UTF-16 encoded C string.", + "napi_get_value_string_utf16(env, args[0], value0, strUtf16Length + 1, &strUtf16Length);", + "// Todo: Assign value0 to actual business value", + "delete[] value0;" + ] + }, + "Napi is array": { + "prefix": "napiisarray", + "body": [ + "bool isArray = false;", + "// Check if the provided napi_value is a JavaScript array.", + "napi_is_array(env, args[0], &isArray);" + ] + }, + "Napi array in": { + "prefix": "napiarrayin", + "body": [ + "uint32_t length = 0;", + "// Retrieve the length of a JavaScript array.", + "napi_get_array_length(env, args[0], &length);", + "for (uint32_t i = 0; i < length; i++) {", + " napi_value element;", + " // Get an element from a JavaScript array by index.", + " napi_get_element(env, args[0], i, &element);", + "}" + ] + }, + "Napi array out": { + "prefix": "napiarrayout", + "body": [ + "napi_value resultArray;", + "size_t length = 3;", + "// Create a new JavaScript array with a specified length.", + "napi_create_array_with_length(env, length, &resultArray);", + "for (uint32_t i = 0; i < length; i++) {", + " napi_value element;", + " napi_create_int32(env, i, &element);", + " // Set an element to a JavaScript array by index.", + " napi_set_element(env, resultArray, i, element);", + "}" + ] + }, + "Napi double out": { + "preix": "napidoubleout", + "body": [ + "double doubleRes = 0;", + "napi_value doubleOut;", + "// Convert a C double type to a JavaScript number type.", + "napi_create_double(env, doubleRes, &doubleOut);" + ] + }, + "Napi int32_t out": { + "prefix": "napiint32out", + "body": [ + "int32_t int32Res = 0;", + "napi_value int32Out;", + "// Convert a C int32_t type to a JavaScript number type.", + "napi_create_int32(env, int32Res, &int32Out);" + ] + }, + "Napi uint32_t out": { + "prefix": "", + "body": [ + "uint32_t uint32Res = 0;", + "napi_value uint32Out;", + "// Convert a C uint32_t type to a JavaScript number type.", + "napi_create_uint32(env, uint32Res, &uint32Out);" + ] + }, + "Napi int64_t out": { + "prefix": "napiint64out", + "body": [ + "int64_t int64Res = 0;", + "napi_value int64Out;", + "// Convert a C int64_t type to a JavaScript number type.", + "napi_create_int64(env, int64Res, &int64Out);" + ] + }, + "Napi bool out": { + "prefix": "napiboolout", + "body": [ + "bool boolRes = false;", + "napi_value boolOut;", + "// Convert a C bool type to a JavaScript boolean type.", + "napi_get_boolean(env, boolRes, &boolOut);" + ] + }, + "Napi string utf8 out": { + "prefix": "napistringutf8out", + "body": [ + "const char* stringRes = \"hello world!\";", + "napi_value stringOut;", + "// Convert a C string uft8 type to a JavaScript string type", + "napi_create_string_utf8(env, stringRes, NAPI_AUTO_LENGTH, &stringOut);" + ] + }, + "Napi string utf16 out": { + "prefix": "napistringutf16out", + "body": [ + "const char16_t* stringRes = u\"hello world!\";", + "napi_value stringOut;", + "// Convert a C string uft16 type to a JavaScript string type", + "napi_create_string_utf16(env, stringRes, NAPI_AUTO_LENGTH, &stringOut);" + ] + } +} \ No newline at end of file