From dd8b0e7b251bcbbf5a81f0c7109114fb40483949 Mon Sep 17 00:00:00 2001 From: wangshi Date: Thu, 6 Feb 2025 15:18:37 +0800 Subject: [PATCH 1/4] refact snippest Signed-off-by: wangshi --- .../{ => src}/l10n/bundle.l10n.zh-cn.json | 0 .../src/snippets/aki_class_snippets.json | 40 +++++++++++++++++++ .../src/snippets/aki_enum_snippets.json | 21 ++++++++++ .../src/snippets/aki_function_snippets.json | 31 ++++++++++++++ .../src/snippets/aki_struct_snippets.json | 40 +++++++++++++++++++ 5 files changed, 132 insertions(+) rename src/vscode_plugin/{ => src}/l10n/bundle.l10n.zh-cn.json (100%) create mode 100644 src/vscode_plugin/src/snippets/aki_class_snippets.json create mode 100644 src/vscode_plugin/src/snippets/aki_enum_snippets.json create mode 100644 src/vscode_plugin/src/snippets/aki_function_snippets.json create mode 100644 src/vscode_plugin/src/snippets/aki_struct_snippets.json diff --git a/src/vscode_plugin/l10n/bundle.l10n.zh-cn.json b/src/vscode_plugin/src/l10n/bundle.l10n.zh-cn.json similarity index 100% rename from src/vscode_plugin/l10n/bundle.l10n.zh-cn.json rename to src/vscode_plugin/src/l10n/bundle.l10n.zh-cn.json diff --git a/src/vscode_plugin/src/snippets/aki_class_snippets.json b/src/vscode_plugin/src/snippets/aki_class_snippets.json new file mode 100644 index 00000000..bfd0321d --- /dev/null +++ b/src/vscode_plugin/src/snippets/aki_class_snippets.json @@ -0,0 +1,40 @@ +{ + "Aki class": { + "prefix": "akiclass", + "body": [ + "class A {", + " public:", + " A() {", + " value_ = -1;", + " result = -1;", + " };", + " explicit A(double test) { value_ = test; }", + " ~A() = default;", + " static double MultiplyObject(A obj1, A obj2) { return obj1.value_ * obj2.value_; }", + " double Multiply(double mult) {", + " value_ *= mult;", + " return value_;", + " }", + " double GetValue() const { return value_; }", + " void SetValue(double value) { value_ = value; }", + " double result;", + " private:", + " double value_;", + "}; // A", + "// Bind class A.", + "JSBIND_CLASS(A) {", + " // Bind default constructor of class.", + " JSBIND_CONSTRUCTOR<>();", + " // Bind a parameterized constructor of class.", + " JSBIND_CONSTRUCTOR();", + " // Bind member method of class.", + " JSBIND_METHOD(MultiplyObject);", + " JSBIND_METHOD(Multiply);", + " // Listen to class member properties in C++.", + " JSBIND_FIELD(\"value\", GetValue, SetValue);", + " // Bind member property of class.", + " JSBIND_PROPERTY(result);", + "}" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/aki_enum_snippets.json b/src/vscode_plugin/src/snippets/aki_enum_snippets.json new file mode 100644 index 00000000..aeb1719c --- /dev/null +++ b/src/vscode_plugin/src/snippets/aki_enum_snippets.json @@ -0,0 +1,21 @@ +{ + "Aki enum": { + "prefix": "akienum", + "body": [ + "enum TypeFlags { NONE, NUM, STRING, BUTT = -1 };", + "// Bind C++ enum", + "JSBIND_ENUM(TypeFlags) {", + " // Bind C++ enum value.", + " JSBIND_ENUM_VALUE(NONE);", + " JSBIND_ENUM_VALUE(NUM);", + " JSBIND_ENUM_VALUE(STRING);", + "}", + "TypeFlags Passing(TypeFlags flag) { return flag; }", + "JSBIND_ADDON(entry)", + "JSBIND_GLOBAL() {", + " // Bind global function of using enum value.", + " JSBIND_FUNCTION(Passing);", + "}" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/aki_function_snippets.json b/src/vscode_plugin/src/snippets/aki_function_snippets.json new file mode 100644 index 00000000..9d6f9d1f --- /dev/null +++ b/src/vscode_plugin/src/snippets/aki_function_snippets.json @@ -0,0 +1,31 @@ +{ + "Aki sync function": { + "prefix": "akisyncfunc", + "body": [ + "std::string SayHello(std::string msg) { return msg + \" too.\"; }", + "// Register the AKI plugin name: It is the name of the compiled *.so file, following the same rules as NAPI.", + "JSBIND_ADDON(entry)", + "// Used to define the scope of global functions that need to be bound.", + "JSBIND_GLOBAL() {", + " // 'SayHello' function can be directly called from JavaScript.", + " JSBIND_FUNCTION(SayHello);", + "}" + ] + }, + "Aki async function": { + "prefix": "akiasyncfunc", + "body": [ + "std::string AsyncSayHello(std::string msg) {", + " // Do something;", + " return msg + \" too.\";", + "}", + "// Register the AKI plugin name: It is the name of the compiled *.so file, following the same rules as NAPI.", + "JSBIND_ADDON(entry)", + "// Used to define the scope of global functions that need to be bound.", + "JSBIND_GLOBAL() {", + " // function can be called asynchronously from JavaScript with Promises.", + " JSBIND_PFUNCTION(AsyncSayHello);", + "}" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/aki_struct_snippets.json b/src/vscode_plugin/src/snippets/aki_struct_snippets.json new file mode 100644 index 00000000..f6d5594a --- /dev/null +++ b/src/vscode_plugin/src/snippets/aki_struct_snippets.json @@ -0,0 +1,40 @@ +{ + "Aki struct": { + "prefix": "akistruct", + "body": [ + "struct B {", + " public:", + " B() {", + " value_ = -1;", + " result = -1;", + " };", + " explicit B(double test) { value_ = test; }", + " ~B() = default;", + " static double MultiplyObject(B obj1, B obj2) { return obj1.value_ * obj2.value_; }", + " double Multiply(double mult) {", + " value_ *= mult;", + " return value_;", + " }", + " double GetValue() const { return value_; }", + " void SetValue(double value) { value_ = value; }", + " double result;", + " private:", + " double value_;", + "}; // B", + "// Bind struct B.", + "JSBIND_CLASS(B) {", + " // Bind default constructor of struct.", + " JSBIND_CONSTRUCTOR<>();", + " // Bind a parameterized constructor of struct.", + " JSBIND_CONSTRUCTOR();", + " // Bind member method of struct.", + " JSBIND_METHOD(MultiplyObject);", + " JSBIND_METHOD(Multiply);", + " // Listen to struct member properties in C++.", + " JSBIND_FIELD(\"value\", GetValue, SetValue);", + " // Bind member property of struct.", + " JSBIND_PROPERTY(result);", + "}" + ] + } +} \ No newline at end of file -- Gitee From 57cd7a6e6ee7932e7204b6caa9954582b5b06099 Mon Sep 17 00:00:00 2001 From: wangshi Date: Thu, 6 Feb 2025 15:19:08 +0800 Subject: [PATCH 2/4] refact snippest Signed-off-by: wangshi --- .../src/snippets/napi_asyncwork_snippets.json | 30 ++++ .../src/snippets/napi_callback_snippets.json | 9 + .../src/snippets/napi_class_snippets.json | 88 +++++++++ .../src/snippets/napi_enum_snippets.json | 11 ++ .../src/snippets/napi_error_snippets.json | 19 ++ .../src/snippets/napi_external_snippets.json | 82 +++++++++ .../src/snippets/napi_promise_snippets.json | 23 +++ .../src/snippets/napi_struct_snippets.json | 89 ++++++++++ .../src/snippets/napi_thread_snippets.json | 53 ++++++ .../src/snippets/napi_variable_snippets.json | 167 ++++++++++++++++++ 10 files changed, 571 insertions(+) create mode 100644 src/vscode_plugin/src/snippets/napi_asyncwork_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_callback_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_class_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_enum_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_error_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_external_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_promise_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_struct_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_thread_snippets.json create mode 100644 src/vscode_plugin/src/snippets/napi_variable_snippets.json diff --git a/src/vscode_plugin/src/snippets/napi_asyncwork_snippets.json b/src/vscode_plugin/src/snippets/napi_asyncwork_snippets.json new file mode 100644 index 00000000..1ff8a0eb --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_asyncwork_snippets.json @@ -0,0 +1,30 @@ +{ + "Napi Async Func": { + "prefix": "napiasyncwork", + "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) {", + " std::this_thread::sleep_for(std::chrono::seconds(2));", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"ExecuteAsyncWork\");", + "}", + "static void CompleteAsyncWork(napi_env env, napi_status status, void* data) {", + " // Todo: you can use \"napicallfunc\" command that execute the callback.", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork\");", + "}", + "napi_value StartAsyncWork(napi_env env, napi_callback_info info)", + "{", + " // 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);", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"Main thread.\");", + " return nullptr;", + "}" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_callback_snippets.json b/src/vscode_plugin/src/snippets/napi_callback_snippets.json new file mode 100644 index 00000000..4ef3e4da --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_callback_snippets.json @@ -0,0 +1,9 @@ +{ + "Napi Callback": { + "prefix": "napicallfunc", + "body": [ + "// Invokes a js function with the provided arguments and returns the result.", + "napi_call_function(env, recv, callbackFunc, argc, callbackArg, &result);" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_class_snippets.json b/src/vscode_plugin/src/snippets/napi_class_snippets.json new file mode 100644 index 00000000..fcdafbb5 --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_class_snippets.json @@ -0,0 +1,88 @@ +{ + "Napi Class": { + "prefix": "napiclass", + "body": [ + "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 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);", + " static napi_value Hello(napi_env env, napi_callback_info info);", + " std::string value_;", + " napi_env env_;", + " napi_ref wrapper_;", + "};", + "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)->~A();", + "}", + "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 A()`.", + " napi_value jsThis;", + " // Retrieve the callback's context and arguments.", + " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", + " A* obj = new A();", + " obj->env_ = env;", + " // Wrap the C++ object obj in the ArkTS object jsThis.", + " napi_wrap(env, jsThis, reinterpret_cast(obj), A::Destructor, nullptr, &obj->wrapper_);", + " return jsThis;", + " } else {", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A must be invoked as a constructor with `new`\");", + " return nullptr;", + " }", + "}", + "napi_value A::GetValue(napi_env env, napi_callback_info info)", + "{", + " 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 A::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);", + " 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 A::Hello(napi_env env, napi_callback_info info)", + "{", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A::Hello World!\");", + " return nullptr;", + "}", + "napi_value A::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, \"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;", + "}" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_enum_snippets.json b/src/vscode_plugin/src/snippets/napi_enum_snippets.json new file mode 100644 index 00000000..dafd8c31 --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_enum_snippets.json @@ -0,0 +1,11 @@ +{ + "Napi Enum": { + "prefix": "napienum", + "body": [ + "// Enum in.", + "// Todo: you can use \"napiint32in\" or \"napistringutf8in\" command that get int32_t or string value from js.", + "// Enum out.", + "// Todo: you can use \"napiint32out\" or \"napistringutf8out\" command that return int32_t or string value to js" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_error_snippets.json b/src/vscode_plugin/src/snippets/napi_error_snippets.json new file mode 100644 index 00000000..25fd038c --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_error_snippets.json @@ -0,0 +1,19 @@ +{ + "Napi get last error info ": { + "prefix": "napigetlasterrorinfo", + "body": [ + "// Retrieve detailed information about the last N-API error that occurred in the given environment.", + "const napi_extended_error_info* errorInfo;", + "napi_get_last_error_info(env, &errorInfo);" + ] + }, + "Napi throw error ": { + "prefix": "napithrowerror", + "body": [ + "// Create and throw a JavaScript Error with a specified code and message.", + "const char* code = \"ERROR_CODE\";", + "const char* msg = \"error message.\";", + "napi_throw_error(env, code, msg);" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_external_snippets.json b/src/vscode_plugin/src/snippets/napi_external_snippets.json new file mode 100644 index 00000000..7e186781 --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_external_snippets.json @@ -0,0 +1,82 @@ +{ + "Napi struct reference in": { + "prefix": "napistructrefin", + "body": [ + "napi_value args[1];", + "size_t argc = 1;", + "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + "// Get struct reference from args[0]. data is a reference which passed to JS through napi_create_external.", + "void* data;", + "napi_get_value_external(env, args[0], &data);" + ] + }, + "Napi struct reference out": { + "prefix": "napistructrefout", + "body": [ + "/** struct MyStruct {", + " * public:", + " * MyStruct();", + " * ~MyStruct();", + " * void SetValue(int value);", + " * int GetValue() const;", + " * private:", + " * int value_;", + " * };", + " */", + "MyStruct* data = new MyStruct();", + "napi_value result;", + "// Create a JS external value that wraps a pointer to native data.", + "napi_create_external(env, data, nullptr, nullptr, &result);" + ] + }, + "Napi class reference in": { + "prefix": "napiclassrefin", + "body": [ + "size_t argc = 1;", + "napi_value args[1];", + "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + "void* data;", + "// Get class reference from args[0]. data is a reference which passed to JS through napi_create_external.", + "napi_get_value_external(env, args[0], &data);" + ] + }, + "Napi class reference out": { + "prefix": "napiclassrefout", + "body": [ + "/** class MyClass {", + " * public:", + " * MyClass();", + " * ~MyClass();", + " * void SetValue(int value);", + " * int GetValue() const;", + " * private:", + " * int value_;", + " * };", + " */", + "MyClass* data = new MyClass();", + "napi_value result;", + "// Create a JS external value that wraps a pointer to native data.", + "napi_create_external(env, data, nullptr, nullptr, &result);" + ] + }, + "Napi array reference in": { + "prefix": "napiarrayrefin", + "body": [ + "size_t argc = 1;", + "napi_value args[1];", + "int32_t* cppArray;", + "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + "// Get int32_t array from args[0].", + "napi_get_value_external(env, args[0], (void**)&cppArray);" + ] + }, + "Napi array reference out": { + "prefix": "napiarrayrefout", + "body": [ + "int32_t* cppArray = new int32_t[5]{1, 2, 3, 4, 5};", + "napi_value jsArray;", + "// FinalizeCallback is a optional callback that used to release data. (e.g.:delete[] cppArray;).", + "napi_create_external(env, cppArray, FinalizeCallback, nullptr, &jsArray);" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_promise_snippets.json b/src/vscode_plugin/src/snippets/napi_promise_snippets.json new file mode 100644 index 00000000..ca43750b --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_promise_snippets.json @@ -0,0 +1,23 @@ +{ + "Napi create promise": { + "prefix": "napicreatepromise", + "body": [ + "// Create a new js Promise.", + "napi_create_promise(env, &deferred, &promise);" + ] + }, + "Napi resolve deferred": { + "prefix": "napiresolvedeferred", + "body": [ + "// Fulfill a js Promise with a given value.", + "napi_resolve_deferred(env, deferred, resolution);" + ] + }, + "Napi reject deferred": { + "prefix": "napirejectdeferred", + "body": [ + "// Reject a js Promise with a given reason.", + "napi_reject_deferred(env, deferred, rejection);" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_struct_snippets.json b/src/vscode_plugin/src/snippets/napi_struct_snippets.json new file mode 100644 index 00000000..a85dfcd3 --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_struct_snippets.json @@ -0,0 +1,89 @@ +{ + "Napi Struct": { + "prefix": "napistruct", + "body": [ + "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_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"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/src/snippets/napi_thread_snippets.json b/src/vscode_plugin/src/snippets/napi_thread_snippets.json new file mode 100644 index 00000000..b394eba1 --- /dev/null +++ b/src/vscode_plugin/src/snippets/napi_thread_snippets.json @@ -0,0 +1,53 @@ +{ + "Napi async thread": { + "prefix": "napiasyncthreadsafefunc", + "body": [ + "struct MyContext {", + " std::string importantString;", + "};", + "// Thread-safe JavaScript function object.", + "napi_threadsafe_function g_threadsafeFunction;", + "static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)", + "{", + " size_t argc = 1;", + " napi_value argv[1];", + " MyContext* myContext = static_cast(context);", + " napi_create_string_utf8(env, myContext->importantString.c_str(), NAPI_AUTO_LENGTH, &argv[0]);", + " // Execute the callback function in a JavaScript environment. After the data is callbacked to JS, it will not wait for JS to finish executing.", + " napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);", + "}", + "static void FinalizeFunction(napi_env env, void* data, void* hint)", + "{", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"FinalizeFunction called.\");", + " MyContext* myContext = static_cast(data);", + " delete myContext;", + "}", + "static void ThreadFunction(void *data)", + "{", + " // Asynchronously call a JavaScript function in another thread.", + " std::this_thread::sleep_for(std::chrono::seconds(1));", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Another thread sleep 1s.\");", + " // Invoke a JavaScript function asynchronously from a native thread. After this, it will execute CallbackFunction to callback data to js.", + " napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);", + " // Decrement the reference count of a threadsafe function, potentially destroying it. After this, it will execute FinalizeFunction to release data.", + " napi_release_threadsafe_function(g_threadsafeFunction, napi_tsfn_release);", + "}", + "napi_value StartThreadsafefunc(napi_env env, napi_callback_info info)", + "{", + " size_t argc = 1;", + " napi_value args[1];", + " napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", + " MyContext* context = new MyContext{\"Hello from C++\"};", + " napi_value name;", + " napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);", + " // Create a thread-safe function that can be called from worker threads.", + " napi_create_threadsafe_function(env, args[0], nullptr, name, 0, 1, context, FinalizeFunction, context, CallbackFunction, &g_threadsafeFunction);", + " // Start a new thread.", + " std::thread newThread(ThreadFunction, nullptr);", + " newThread.join();", + " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Main thread.\");", + " return nullptr;", + "}" + ] + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/snippets/napi_variable_snippets.json b/src/vscode_plugin/src/snippets/napi_variable_snippets.json new file mode 100644 index 00000000..b1b9da35 --- /dev/null +++ b/src/vscode_plugin/src/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": "napiuint32out", + "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 -- Gitee From 15bac8371fc12fe9a9c3732f163707e2d2f3cb29 Mon Sep 17 00:00:00 2001 From: wangshi Date: Thu, 6 Feb 2025 15:19:55 +0800 Subject: [PATCH 3/4] refact snippest Signed-off-by: wangshi --- .../snippets/aki_class_snippets.json | 40 ----- .../snippets/aki_enum_snippets.json | 21 --- .../snippets/aki_function_snippets.json | 31 ---- .../snippets/aki_struct_snippets.json | 40 ----- .../snippets/napi_asyncwork_snippets.json | 30 ---- .../snippets/napi_callback_snippets.json | 9 - .../snippets/napi_class_snippets.json | 88 --------- .../snippets/napi_enum_snippets.json | 11 -- .../snippets/napi_error_snippets.json | 19 -- .../snippets/napi_external_snippets.json | 82 --------- .../snippets/napi_promise_snippets.json | 23 --- .../snippets/napi_struct_snippets.json | 89 ---------- .../snippets/napi_thread_snippets.json | 53 ------ .../snippets/napi_variable_snippets.json | 167 ------------------ 14 files changed, 703 deletions(-) delete mode 100644 src/vscode_plugin/snippets/aki_class_snippets.json delete mode 100644 src/vscode_plugin/snippets/aki_enum_snippets.json delete mode 100644 src/vscode_plugin/snippets/aki_function_snippets.json delete mode 100644 src/vscode_plugin/snippets/aki_struct_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_asyncwork_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_callback_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_class_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_enum_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_error_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_external_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_promise_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_struct_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_thread_snippets.json delete mode 100644 src/vscode_plugin/snippets/napi_variable_snippets.json diff --git a/src/vscode_plugin/snippets/aki_class_snippets.json b/src/vscode_plugin/snippets/aki_class_snippets.json deleted file mode 100644 index bfd0321d..00000000 --- a/src/vscode_plugin/snippets/aki_class_snippets.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "Aki class": { - "prefix": "akiclass", - "body": [ - "class A {", - " public:", - " A() {", - " value_ = -1;", - " result = -1;", - " };", - " explicit A(double test) { value_ = test; }", - " ~A() = default;", - " static double MultiplyObject(A obj1, A obj2) { return obj1.value_ * obj2.value_; }", - " double Multiply(double mult) {", - " value_ *= mult;", - " return value_;", - " }", - " double GetValue() const { return value_; }", - " void SetValue(double value) { value_ = value; }", - " double result;", - " private:", - " double value_;", - "}; // A", - "// Bind class A.", - "JSBIND_CLASS(A) {", - " // Bind default constructor of class.", - " JSBIND_CONSTRUCTOR<>();", - " // Bind a parameterized constructor of class.", - " JSBIND_CONSTRUCTOR();", - " // Bind member method of class.", - " JSBIND_METHOD(MultiplyObject);", - " JSBIND_METHOD(Multiply);", - " // Listen to class member properties in C++.", - " JSBIND_FIELD(\"value\", GetValue, SetValue);", - " // Bind member property of class.", - " JSBIND_PROPERTY(result);", - "}" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/aki_enum_snippets.json b/src/vscode_plugin/snippets/aki_enum_snippets.json deleted file mode 100644 index aeb1719c..00000000 --- a/src/vscode_plugin/snippets/aki_enum_snippets.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "Aki enum": { - "prefix": "akienum", - "body": [ - "enum TypeFlags { NONE, NUM, STRING, BUTT = -1 };", - "// Bind C++ enum", - "JSBIND_ENUM(TypeFlags) {", - " // Bind C++ enum value.", - " JSBIND_ENUM_VALUE(NONE);", - " JSBIND_ENUM_VALUE(NUM);", - " JSBIND_ENUM_VALUE(STRING);", - "}", - "TypeFlags Passing(TypeFlags flag) { return flag; }", - "JSBIND_ADDON(entry)", - "JSBIND_GLOBAL() {", - " // Bind global function of using enum value.", - " JSBIND_FUNCTION(Passing);", - "}" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/aki_function_snippets.json b/src/vscode_plugin/snippets/aki_function_snippets.json deleted file mode 100644 index 9d6f9d1f..00000000 --- a/src/vscode_plugin/snippets/aki_function_snippets.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "Aki sync function": { - "prefix": "akisyncfunc", - "body": [ - "std::string SayHello(std::string msg) { return msg + \" too.\"; }", - "// Register the AKI plugin name: It is the name of the compiled *.so file, following the same rules as NAPI.", - "JSBIND_ADDON(entry)", - "// Used to define the scope of global functions that need to be bound.", - "JSBIND_GLOBAL() {", - " // 'SayHello' function can be directly called from JavaScript.", - " JSBIND_FUNCTION(SayHello);", - "}" - ] - }, - "Aki async function": { - "prefix": "akiasyncfunc", - "body": [ - "std::string AsyncSayHello(std::string msg) {", - " // Do something;", - " return msg + \" too.\";", - "}", - "// Register the AKI plugin name: It is the name of the compiled *.so file, following the same rules as NAPI.", - "JSBIND_ADDON(entry)", - "// Used to define the scope of global functions that need to be bound.", - "JSBIND_GLOBAL() {", - " // function can be called asynchronously from JavaScript with Promises.", - " JSBIND_PFUNCTION(AsyncSayHello);", - "}" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/aki_struct_snippets.json b/src/vscode_plugin/snippets/aki_struct_snippets.json deleted file mode 100644 index f6d5594a..00000000 --- a/src/vscode_plugin/snippets/aki_struct_snippets.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "Aki struct": { - "prefix": "akistruct", - "body": [ - "struct B {", - " public:", - " B() {", - " value_ = -1;", - " result = -1;", - " };", - " explicit B(double test) { value_ = test; }", - " ~B() = default;", - " static double MultiplyObject(B obj1, B obj2) { return obj1.value_ * obj2.value_; }", - " double Multiply(double mult) {", - " value_ *= mult;", - " return value_;", - " }", - " double GetValue() const { return value_; }", - " void SetValue(double value) { value_ = value; }", - " double result;", - " private:", - " double value_;", - "}; // B", - "// Bind struct B.", - "JSBIND_CLASS(B) {", - " // Bind default constructor of struct.", - " JSBIND_CONSTRUCTOR<>();", - " // Bind a parameterized constructor of struct.", - " JSBIND_CONSTRUCTOR();", - " // Bind member method of struct.", - " JSBIND_METHOD(MultiplyObject);", - " JSBIND_METHOD(Multiply);", - " // Listen to struct member properties in C++.", - " JSBIND_FIELD(\"value\", GetValue, SetValue);", - " // Bind member property of struct.", - " JSBIND_PROPERTY(result);", - "}" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_asyncwork_snippets.json b/src/vscode_plugin/snippets/napi_asyncwork_snippets.json deleted file mode 100644 index 1ff8a0eb..00000000 --- a/src/vscode_plugin/snippets/napi_asyncwork_snippets.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "Napi Async Func": { - "prefix": "napiasyncwork", - "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) {", - " std::this_thread::sleep_for(std::chrono::seconds(2));", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"ExecuteAsyncWork\");", - "}", - "static void CompleteAsyncWork(napi_env env, napi_status status, void* data) {", - " // Todo: you can use \"napicallfunc\" command that execute the callback.", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"CompleteAsyncWork\");", - "}", - "napi_value StartAsyncWork(napi_env env, napi_callback_info info)", - "{", - " // 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);", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"ASYNC\", \"Main thread.\");", - " return nullptr;", - "}" - ] - } -} \ 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 deleted file mode 100644 index 4ef3e4da..00000000 --- a/src/vscode_plugin/snippets/napi_callback_snippets.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Napi Callback": { - "prefix": "napicallfunc", - "body": [ - "// Invokes a js function with the provided arguments and returns the result.", - "napi_call_function(env, recv, callbackFunc, argc, callbackArg, &result);" - ] - } -} \ 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 deleted file mode 100644 index fcdafbb5..00000000 --- a/src/vscode_plugin/snippets/napi_class_snippets.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "Napi Class": { - "prefix": "napiclass", - "body": [ - "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 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);", - " static napi_value Hello(napi_env env, napi_callback_info info);", - " std::string value_;", - " napi_env env_;", - " napi_ref wrapper_;", - "};", - "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)->~A();", - "}", - "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 A()`.", - " napi_value jsThis;", - " // Retrieve the callback's context and arguments.", - " napi_get_cb_info(env, info, nullptr, nullptr, &jsThis, nullptr);", - " A* obj = new A();", - " obj->env_ = env;", - " // Wrap the C++ object obj in the ArkTS object jsThis.", - " napi_wrap(env, jsThis, reinterpret_cast(obj), A::Destructor, nullptr, &obj->wrapper_);", - " return jsThis;", - " } else {", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A must be invoked as a constructor with `new`\");", - " return nullptr;", - " }", - "}", - "napi_value A::GetValue(napi_env env, napi_callback_info info)", - "{", - " 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 A::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);", - " 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 A::Hello(napi_env env, napi_callback_info info)", - "{", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"A::Hello World!\");", - " return nullptr;", - "}", - "napi_value A::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, \"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;", - "}" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_enum_snippets.json b/src/vscode_plugin/snippets/napi_enum_snippets.json deleted file mode 100644 index dafd8c31..00000000 --- a/src/vscode_plugin/snippets/napi_enum_snippets.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "Napi Enum": { - "prefix": "napienum", - "body": [ - "// Enum in.", - "// Todo: you can use \"napiint32in\" or \"napistringutf8in\" command that get int32_t or string value from js.", - "// Enum out.", - "// Todo: you can use \"napiint32out\" or \"napistringutf8out\" command that return int32_t or string value to js" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_error_snippets.json b/src/vscode_plugin/snippets/napi_error_snippets.json deleted file mode 100644 index 25fd038c..00000000 --- a/src/vscode_plugin/snippets/napi_error_snippets.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "Napi get last error info ": { - "prefix": "napigetlasterrorinfo", - "body": [ - "// Retrieve detailed information about the last N-API error that occurred in the given environment.", - "const napi_extended_error_info* errorInfo;", - "napi_get_last_error_info(env, &errorInfo);" - ] - }, - "Napi throw error ": { - "prefix": "napithrowerror", - "body": [ - "// Create and throw a JavaScript Error with a specified code and message.", - "const char* code = \"ERROR_CODE\";", - "const char* msg = \"error message.\";", - "napi_throw_error(env, code, msg);" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_external_snippets.json b/src/vscode_plugin/snippets/napi_external_snippets.json deleted file mode 100644 index 7e186781..00000000 --- a/src/vscode_plugin/snippets/napi_external_snippets.json +++ /dev/null @@ -1,82 +0,0 @@ -{ - "Napi struct reference in": { - "prefix": "napistructrefin", - "body": [ - "napi_value args[1];", - "size_t argc = 1;", - "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", - "// Get struct reference from args[0]. data is a reference which passed to JS through napi_create_external.", - "void* data;", - "napi_get_value_external(env, args[0], &data);" - ] - }, - "Napi struct reference out": { - "prefix": "napistructrefout", - "body": [ - "/** struct MyStruct {", - " * public:", - " * MyStruct();", - " * ~MyStruct();", - " * void SetValue(int value);", - " * int GetValue() const;", - " * private:", - " * int value_;", - " * };", - " */", - "MyStruct* data = new MyStruct();", - "napi_value result;", - "// Create a JS external value that wraps a pointer to native data.", - "napi_create_external(env, data, nullptr, nullptr, &result);" - ] - }, - "Napi class reference in": { - "prefix": "napiclassrefin", - "body": [ - "size_t argc = 1;", - "napi_value args[1];", - "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", - "void* data;", - "// Get class reference from args[0]. data is a reference which passed to JS through napi_create_external.", - "napi_get_value_external(env, args[0], &data);" - ] - }, - "Napi class reference out": { - "prefix": "napiclassrefout", - "body": [ - "/** class MyClass {", - " * public:", - " * MyClass();", - " * ~MyClass();", - " * void SetValue(int value);", - " * int GetValue() const;", - " * private:", - " * int value_;", - " * };", - " */", - "MyClass* data = new MyClass();", - "napi_value result;", - "// Create a JS external value that wraps a pointer to native data.", - "napi_create_external(env, data, nullptr, nullptr, &result);" - ] - }, - "Napi array reference in": { - "prefix": "napiarrayrefin", - "body": [ - "size_t argc = 1;", - "napi_value args[1];", - "int32_t* cppArray;", - "napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", - "// Get int32_t array from args[0].", - "napi_get_value_external(env, args[0], (void**)&cppArray);" - ] - }, - "Napi array reference out": { - "prefix": "napiarrayrefout", - "body": [ - "int32_t* cppArray = new int32_t[5]{1, 2, 3, 4, 5};", - "napi_value jsArray;", - "// FinalizeCallback is a optional callback that used to release data. (e.g.:delete[] cppArray;).", - "napi_create_external(env, cppArray, FinalizeCallback, nullptr, &jsArray);" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_promise_snippets.json b/src/vscode_plugin/snippets/napi_promise_snippets.json deleted file mode 100644 index ca43750b..00000000 --- a/src/vscode_plugin/snippets/napi_promise_snippets.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "Napi create promise": { - "prefix": "napicreatepromise", - "body": [ - "// Create a new js Promise.", - "napi_create_promise(env, &deferred, &promise);" - ] - }, - "Napi resolve deferred": { - "prefix": "napiresolvedeferred", - "body": [ - "// Fulfill a js Promise with a given value.", - "napi_resolve_deferred(env, deferred, resolution);" - ] - }, - "Napi reject deferred": { - "prefix": "napirejectdeferred", - "body": [ - "// Reject a js Promise with a given reason.", - "napi_reject_deferred(env, deferred, rejection);" - ] - } -} \ No newline at end of file diff --git a/src/vscode_plugin/snippets/napi_struct_snippets.json b/src/vscode_plugin/snippets/napi_struct_snippets.json deleted file mode 100644 index a85dfcd3..00000000 --- a/src/vscode_plugin/snippets/napi_struct_snippets.json +++ /dev/null @@ -1,89 +0,0 @@ -{ - "Napi Struct": { - "prefix": "napistruct", - "body": [ - "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_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"CLASS\", \"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_thread_snippets.json b/src/vscode_plugin/snippets/napi_thread_snippets.json deleted file mode 100644 index b394eba1..00000000 --- a/src/vscode_plugin/snippets/napi_thread_snippets.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "Napi async thread": { - "prefix": "napiasyncthreadsafefunc", - "body": [ - "struct MyContext {", - " std::string importantString;", - "};", - "// Thread-safe JavaScript function object.", - "napi_threadsafe_function g_threadsafeFunction;", - "static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)", - "{", - " size_t argc = 1;", - " napi_value argv[1];", - " MyContext* myContext = static_cast(context);", - " napi_create_string_utf8(env, myContext->importantString.c_str(), NAPI_AUTO_LENGTH, &argv[0]);", - " // Execute the callback function in a JavaScript environment. After the data is callbacked to JS, it will not wait for JS to finish executing.", - " napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);", - "}", - "static void FinalizeFunction(napi_env env, void* data, void* hint)", - "{", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"FinalizeFunction called.\");", - " MyContext* myContext = static_cast(data);", - " delete myContext;", - "}", - "static void ThreadFunction(void *data)", - "{", - " // Asynchronously call a JavaScript function in another thread.", - " std::this_thread::sleep_for(std::chrono::seconds(1));", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Another thread sleep 1s.\");", - " // Invoke a JavaScript function asynchronously from a native thread. After this, it will execute CallbackFunction to callback data to js.", - " napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);", - " // Decrement the reference count of a threadsafe function, potentially destroying it. After this, it will execute FinalizeFunction to release data.", - " napi_release_threadsafe_function(g_threadsafeFunction, napi_tsfn_release);", - "}", - "napi_value StartThreadsafefunc(napi_env env, napi_callback_info info)", - "{", - " size_t argc = 1;", - " napi_value args[1];", - " napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);", - " MyContext* context = new MyContext{\"Hello from C++\"};", - " napi_value name;", - " napi_create_string_utf8(env, \"testThreadsafefunc\", NAPI_AUTO_LENGTH, &name);", - " // Create a thread-safe function that can be called from worker threads.", - " napi_create_threadsafe_function(env, args[0], nullptr, name, 0, 1, context, FinalizeFunction, context, CallbackFunction, &g_threadsafeFunction);", - " // Start a new thread.", - " std::thread newThread(ThreadFunction, nullptr);", - " newThread.join();", - " OH_LOG_Print(LOG_APP, LOG_INFO, LOG_DOMAIN, \"THREAD SAFE\", \"Main thread.\");", - " return nullptr;", - "}" - ] - } -} \ 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 deleted file mode 100644 index b1b9da35..00000000 --- a/src/vscode_plugin/snippets/napi_variable_snippets.json +++ /dev/null @@ -1,167 +0,0 @@ -{ - "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": "napiuint32out", - "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 -- Gitee From f187ea094a7b6fb1ebbe28b02afa5e8600c9e5c3 Mon Sep 17 00:00:00 2001 From: wangshi Date: Thu, 6 Feb 2025 15:20:56 +0800 Subject: [PATCH 4/4] refactory h2dts Signed-off-by: wangshi --- src/vscode_plugin/src/common/constants.ts | 68 +++++++++++++++ .../src/{test/test.h => common/define.ts} | 8 +- .../src/{test/hello.h => common/error.ts} | 16 ++-- .../testdts.d.ts => common/eventtype.ts} | 17 ++-- src/vscode_plugin/src/common/widget.ts | 41 +++++++++ src/vscode_plugin/src/controller/h2dtsctrl.ts | 69 +++++++++++++++ .../src/controller/icontroller.ts | 47 ++++++++++ src/vscode_plugin/src/extension.ts | 48 +++++----- src/vscode_plugin/src/model/h2dtsmod.ts | 87 +++++++++++++++++++ src/vscode_plugin/src/model/imodel.ts | 49 +++++++++++ src/vscode_plugin/src/view/h2dtsview.ts | 77 ++++++++++++++++ src/vscode_plugin/src/view/iview.ts | 27 ++++++ 12 files changed, 510 insertions(+), 44 deletions(-) create mode 100644 src/vscode_plugin/src/common/constants.ts rename src/vscode_plugin/src/{test/test.h => common/define.ts} (86%) rename src/vscode_plugin/src/{test/hello.h => common/error.ts} (65%) rename src/vscode_plugin/src/{test/testdts.d.ts => common/eventtype.ts} (65%) create mode 100644 src/vscode_plugin/src/common/widget.ts create mode 100644 src/vscode_plugin/src/controller/h2dtsctrl.ts create mode 100644 src/vscode_plugin/src/controller/icontroller.ts create mode 100644 src/vscode_plugin/src/model/h2dtsmod.ts create mode 100644 src/vscode_plugin/src/model/imodel.ts create mode 100644 src/vscode_plugin/src/view/h2dtsview.ts create mode 100644 src/vscode_plugin/src/view/iview.ts diff --git a/src/vscode_plugin/src/common/constants.ts b/src/vscode_plugin/src/common/constants.ts new file mode 100644 index 00000000..3a7fd62b --- /dev/null +++ b/src/vscode_plugin/src/common/constants.ts @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// The module 'vscode' contains the VS Code extensibility API +// Import the module and reference it with the alias vscode in your code below + +import * as vscode from 'vscode'; + +export const SELECTED_DIR = vscode.l10n.t('You selected a directory:'); +export const SELECTE_DIR = vscode.l10n.t('Please select a directory.'); +export const NO_RES_SELECTED = vscode.l10n.t('No resource selected.'); +export const HDF_FRAMEWORK = vscode.l10n.t('Hdf Framework'); +export const SA_FRAMEWORK = vscode.l10n.t('SystemAbility Framework'); +export const NAPI_FRAMEWORK = vscode.l10n.t('N-API Framework'); +export const SELECT_VERSION = vscode.l10n.t('Please select the version...'); +export const INPUT_SERVICEID = vscode.l10n.t('Please input serviceId like 19000...'); +export const INPUT_NO_EMPTY = vscode.l10n.t('Input cannot be empty'); +export const INPUT_NUMBER = vscode.l10n.t('Please input a number...'); +export const SELECT_FRAMWORK = vscode.l10n.t('Please select framework...'); +export const CONFIRM_SELECT = vscode.l10n.t('Please confirm your selection...'); +export const INPUT_INCONSISTENT = vscode.l10n.t('Inconsistent input'); +export const PARSE_COMPLETE = vscode.l10n.t('Parse complete.'); +export const GEN_COMPLETE = vscode.l10n.t('Generation complete:'); +export const OPEN_IN_EXPLORER = vscode.l10n.t('Open in Explorer'); +export const PICK_MAKE = vscode.l10n.t('Use Makefile for compilation.'); +export const PICK_CMAKE = vscode.l10n.t('Use CMakeLists.txt for compilation.'); +export const TOOL_PICK_PLACEHOLDER = vscode.l10n.t('Please select the way you want to compile: '); +export const OH_CROSS_COMPILE_TITLE = vscode.l10n.t('OpenHarmony Cross Compile'); +export const COMPILATION_METHOD_LOST = vscode.l10n.t('Unable to comfirm the compilation method, compilation aborted.'); +export const ARCH_PICK_64 = vscode.l10n.t('To compile 64-bit third-party library.'); +export const ARCH_PICK_32 = vscode.l10n.t('To compile 32-bit third-party library.'); +export const ARCH_PICK_PLACEHOLDER = vscode.l10n.t('Please select the target system architecture for compilation: '); +export const ARCHITECTURE_LOST = vscode.l10n.t('Unable to comfirm target system architecture, compilation aborted.'); +export const LOCAL = vscode.l10n.t('Local'); +export const LOCAL_DESCRIPTION = vscode.l10n.t('Please select the \'native\' folder in local OpenHarmony SDK files.'); +export const DOWNLOAD = vscode.l10n.t('Download'); +export const DOWNLOAD_DESCRIPTION = vscode.l10n.t('Download a specified version of OpenHarmony SDK from internet.'); +export const SOURCE_PICK_PLACEHOLDER = vscode.l10n.t('Please select the SDK you want to use: '); +export const NATIVE_CHECK_FAILED = vscode.l10n.t('Unable to verify the integrity of the native tools in OpenHarmony SDK, please try again and select a correct path of the \'native\' folder.'); +export const FOLDER_LOST = vscode.l10n.t('No folder selected, compilation aborted.'); +export const API9_LABEL = vscode.l10n.t('API Version 9'); +export const API9_DETAIL = vscode.l10n.t('Please select a folder to install this SDK. It is compatible with OpenHarmony 3.2 Release.'); +export const API10_LABEL = vscode.l10n.t('API Version 10'); +export const API10_DETAIL = vscode.l10n.t('Please select a folder to install this SDK. It is compatible with OpenHarmony 4.0 Release.'); +export const API11_LABEL = vscode.l10n.t('API Version 11'); +export const API11_DETAIL = vscode.l10n.t('Please select a folder to install this SDK. It is compatible with OpenHarmony 4.1 Release.'); +export const API12_LABEL = vscode.l10n.t('API Version 12'); +export const API12_DETAIL = vscode.l10n.t('Please select a folder to install this SDK. It is compatible with OpenHarmony 5.0.0 Release.'); +export const VERSION_PICK_PLACEHOLDER = vscode.l10n.t('Please specify the SDK version: '); +export const DOWNLOADING_TITLE = vscode.l10n.t('Downloading and installing SDK'); +export const DOWNLOADING_START = vscode.l10n.t('Start downloading...'); +export const DOWNLOADING_COMPLETE = vscode.l10n.t('Download complete. Extracting .tar.gz files... '); +export const SDK_INSTALLED = vscode.l10n.t('SDK installation complete.'); +export const SDK_VERSION_LOST = vscode.l10n.t('Unable to specify the version of OpenHarmony SDK, compilation aborted.'); +export const SDK_SOURCE_LOST = vscode.l10n.t('Unable to comfirm the source of OpenHarmony SDK, compilation aborted.'); +export const CMAKE_MAKE_LOST = vscode.l10n.t('Cannot detect CMakeLists.txt or Makefile!'); \ No newline at end of file diff --git a/src/vscode_plugin/src/test/test.h b/src/vscode_plugin/src/common/define.ts similarity index 86% rename from src/vscode_plugin/src/test/test.h rename to src/vscode_plugin/src/common/define.ts index 6b945d07..eabd9a9a 100644 --- a/src/vscode_plugin/src/test/test.h +++ b/src/vscode_plugin/src/common/define.ts @@ -13,10 +13,4 @@ * limitations under the License. */ -#ifndef TEST_H -#define TEST_H - -namespace OHOS { - int testFunc(int v1, int v2, bool v3); -} -#endif \ No newline at end of file +export type Callback = (...args: any[]) => void; \ No newline at end of file diff --git a/src/vscode_plugin/src/test/hello.h b/src/vscode_plugin/src/common/error.ts similarity index 65% rename from src/vscode_plugin/src/test/hello.h rename to src/vscode_plugin/src/common/error.ts index c251f379..902eda45 100644 --- a/src/vscode_plugin/src/test/hello.h +++ b/src/vscode_plugin/src/common/error.ts @@ -13,11 +13,13 @@ * limitations under the License. */ -#ifndef HELLO_H -#define HELLO_H -#include +// The module 'vscode' contains the VS Code extensibility API +// Import the module and reference it with the alias vscode in your code below -namespace OHOS { - std::string Helloworld(std::string sendMsg); -} -#endif \ No newline at end of file +import * as vscode from 'vscode'; + +export const ERROR_UNKNOW = -1; +export const ERROR_NOEXIST = -2; +export const ERROR_NET = -3; +export const ERROR_FILE = -4; +export const ERROR_PARSE = -5; \ No newline at end of file diff --git a/src/vscode_plugin/src/test/testdts.d.ts b/src/vscode_plugin/src/common/eventtype.ts similarity index 65% rename from src/vscode_plugin/src/test/testdts.d.ts rename to src/vscode_plugin/src/common/eventtype.ts index a773eeca..6ae5eba4 100644 --- a/src/vscode_plugin/src/test/testdts.d.ts +++ b/src/vscode_plugin/src/common/eventtype.ts @@ -13,11 +13,12 @@ * limitations under the License. */ -type MyType = number; -export class test -{ - a: string; - b: number; - c: boolean; -} -export function testFunc(v1: string, v2: boolean, v3: MyType): test; \ No newline at end of file +// The module 'vscode' contains the VS Code extensibility API +// Import the module and reference it with the alias vscode in your code below + +import * as vscode from 'vscode'; + +export const EVENT_PROGRESS = "progress"; +export const EVENT_ERROR = "error"; +export const EVENT_WARNING = "warn"; +export const EVENT_INFORMATION = "info"; \ No newline at end of file diff --git a/src/vscode_plugin/src/common/widget.ts b/src/vscode_plugin/src/common/widget.ts new file mode 100644 index 00000000..4173c1a9 --- /dev/null +++ b/src/vscode_plugin/src/common/widget.ts @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// The module 'vscode' contains the VS Code extensibility API +// Import the module and reference it with the alias vscode in your code below + +import * as vscode from 'vscode'; +import { EVENT_ERROR, EVENT_INFORMATION, EVENT_WARNING } from './eventtype'; + +export function toastMsg(event: string, msg: string) { + switch(event) { + case EVENT_ERROR: + console.error(msg); + vscode.window.showErrorMessage(msg); + break; + case EVENT_INFORMATION: + console.info(msg); + vscode.window.showInformationMessage(msg); + break; + case EVENT_WARNING: + console.warn(msg); + vscode.window.showWarningMessage(msg); + break; + default: + console.log(msg); + vscode.window.showInformationMessage(msg); + break; + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/controller/h2dtsctrl.ts b/src/vscode_plugin/src/controller/h2dtsctrl.ts new file mode 100644 index 00000000..7e92f669 --- /dev/null +++ b/src/vscode_plugin/src/controller/h2dtsctrl.ts @@ -0,0 +1,69 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { Uri } from "vscode"; +import { H2dtsMod } from "../model/h2dtsmod"; +import { IModel } from "../model/imodel"; +import { H2dtsView } from "../view/h2dtsview"; + +import { IView } from "../view/iview"; +import { IController } from "./icontroller"; +import { EVENT_ERROR } from "../common/eventtype"; + +export class H2dtsCtrl extends IController { + name: string; + view: IView; + model: IModel; + uri: Uri; + constructor(uri: Uri) { + super(); + this.name = 'h2dtsctrl'; + this.model = H2dtsMod.getInstance(); + this.view = new H2dtsView; + this.uri = uri; + } + + public init(): void { + if (this.uri && this.uri.fsPath) { + this.view.init(this); + this.model.init(this.uri); + this.view.showProgress(); + } + } + + public start(): void { + try { + this.model.doStart(); + } catch(e) { + let errmsg = "h2dts start error: " + JSON.stringify(e) + console.error(errmsg); + this.view.showMsg(EVENT_ERROR, errmsg); + } + + } + + public stop(): void { + throw new Error("Method not implemented."); + } + + public pause(): void { + throw new Error("Method not implemented."); + } + + public resume(): void { + throw new Error("Method not implemented."); + } + +} \ No newline at end of file diff --git a/src/vscode_plugin/src/controller/icontroller.ts b/src/vscode_plugin/src/controller/icontroller.ts new file mode 100644 index 00000000..8b934084 --- /dev/null +++ b/src/vscode_plugin/src/controller/icontroller.ts @@ -0,0 +1,47 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import * as vscode from 'vscode'; +import { IView } from "../view/iview"; +import { IModel } from "../model/imodel"; +import { Callback } from "../common/define"; + +export abstract class IController { + abstract name: string; + abstract view: IView; + abstract model: IModel; + abstract uri: vscode.Uri; + + private callbacks: Map = new Map(); + + constructor() { + } + + public abstract init(): void; + + public abstract start(): void; + + public abstract stop(): void; + + public abstract pause(): void; + + public abstract resume(): void; + + public onEvent(event: string, cb: Callback): void { + this.callbacks.set(event, cb); + }; + public offEvent(event: string): void { + this.callbacks.delete(event); + }; +} \ No newline at end of file diff --git a/src/vscode_plugin/src/extension.ts b/src/vscode_plugin/src/extension.ts index 825cf963..a251b447 100644 --- a/src/vscode_plugin/src/extension.ts +++ b/src/vscode_plugin/src/extension.ts @@ -28,6 +28,7 @@ import { genServiceFile } from './gen/gensa'; import { genDtsFile } from './gen/gendts'; import { genHdfFile } from './gen/genhdf'; import { genDtsCppFile, genCppFile } from './gen/gendtscpp'; +import { H2dtsCtrl } from './controller/h2dtsctrl'; // ��ȡ���ػ��ַ��� const SELECTED_DIR = vscode.l10n.t('You selected a directory:'); @@ -468,29 +469,32 @@ export function activate(context: vscode.ExtensionContext) { }); context.subscriptions.push(h2hdf); - const h2dts = vscode.commands.registerCommand('extension.h2dts', async (uri) => { - // The code you place here will be executed every time your command is executed - if (uri && uri.fsPath) { - vscode.window.withProgress({ - location: vscode.ProgressLocation.Notification, - title: "Generating .d.ts ...", - cancellable: false - }, async (progress) => { - // parse - let parseRes = await parseHeaderFile(uri.fsPath); - console.log('parse header file res: ', parseRes); - progress.report({ increment: 50, message: PARSE_COMPLETE }); + const h2dts = vscode.commands.registerCommand('extension.h2dts', async (uri: vscode.Uri) => { + let h2dtsCtrl = new H2dtsCtrl(uri); + h2dtsCtrl.init(); + + // // The code you place here will be executed every time your command is executed + // if (uri && uri.fsPath) { + // vscode.window.withProgress({ + // location: vscode.ProgressLocation.Notification, + // title: "Generating .d.ts ...", + // cancellable: false + // }, async (progress) => { + // // parse + // let parseRes = await parseHeaderFile(uri.fsPath); + // console.log('parse header file res: ', parseRes); + // progress.report({ increment: 50, message: PARSE_COMPLETE }); - let rootInfo: GenInfo = { - parseObj: parseRes, - rawFilePath: uri.fsPath, // e://xxx.h - fileName: path.basename(uri.fsPath, '.h') // xxx - }; - // generator - let outPath = genDtsFile(rootInfo); - progress.report({ increment: 100, message: GEN_COMPLETE + outPath }); - }); - } + // let rootInfo: GenInfo = { + // parseObj: parseRes, + // rawFilePath: uri.fsPath, // e://xxx.h + // fileName: path.basename(uri.fsPath, '.h') // xxx + // }; + // // generator + // let outPath = genDtsFile(rootInfo); + // progress.report({ increment: 100, message: GEN_COMPLETE + outPath }); + // }); + // } }); context.subscriptions.push(h2dts); diff --git a/src/vscode_plugin/src/model/h2dtsmod.ts b/src/vscode_plugin/src/model/h2dtsmod.ts new file mode 100644 index 00000000..dbf24343 --- /dev/null +++ b/src/vscode_plugin/src/model/h2dtsmod.ts @@ -0,0 +1,87 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import * as vscode from 'vscode'; +import * as path from 'path'; +import { IModel } from "./imodel"; +import { parseHeaderFile } from '../parse/parsec'; +import { GenInfo } from '../gen/datatype'; +import { genDtsFile } from '../gen/gendts'; +import { GEN_COMPLETE, PARSE_COMPLETE } from '../common/constants'; + +import { + EVENT_ERROR, + EVENT_INFORMATION, + EVENT_PROGRESS, + EVENT_WARNING +} from '../common/eventtype'; + +export class H2dtsMod extends IModel { + name: string; + private static instance: H2dtsMod; + constructor() { + super(); + this.name = 'h2dtsmod'; + } + + static getInstance(): IModel { + if (!H2dtsMod.instance) { + H2dtsMod.instance = new H2dtsMod(); + } + return H2dtsMod.instance; + } + + init(uri: vscode.Uri): void { + this.uri = uri; + } + + async doStart(): Promise { + try { + if (this.uri) { + // parse + let parseRes = await parseHeaderFile(this.uri.fsPath); + console.log('parse header file res: ', parseRes); + this.emmitEventForKey(EVENT_PROGRESS, 50, PARSE_COMPLETE); + + let rootInfo: GenInfo = { + parseObj: parseRes, + rawFilePath: this.uri.fsPath, // e://xxx.h + fileName: path.basename(this.uri.fsPath, '.h') // xxx + }; + // generator + let outPath = genDtsFile(rootInfo); + this.emmitEventForKey(EVENT_PROGRESS, 100, PARSE_COMPLETE); + } else { + console.error('parse header file error with undefine uri.'); + } + } catch (e) { + let errmsg = 'parse header file error: ' + JSON.stringify(e); + console.error(errmsg); + this.emmitEventForKey(EVENT_ERROR, -1, errmsg); + } + } + + async doStop(): Promise { + throw new Error("Method not implemented."); + } + + async doPause(): Promise { + throw new Error("Method not implemented."); + } + + async doResume(): Promise { + throw new Error("Method not implemented."); + } + +} \ No newline at end of file diff --git a/src/vscode_plugin/src/model/imodel.ts b/src/vscode_plugin/src/model/imodel.ts new file mode 100644 index 00000000..88005a32 --- /dev/null +++ b/src/vscode_plugin/src/model/imodel.ts @@ -0,0 +1,49 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import * as vscode from 'vscode'; +import { Callback } from "../common/define" + +export abstract class IModel { + abstract name: string; + uri: vscode.Uri | undefined = undefined; + callbacks: Map = new Map(); + + abstract init(uri: vscode.Uri): void; + abstract doStart(): void; + abstract doStop(): void; + abstract doPause(): void; + abstract doResume(): void; + + public onEvent(event: string, cb: Callback): void { + this.callbacks.set(event, cb); + }; + + public offEvent(event: string): void { + this.callbacks.delete(event); + }; + + public emmitAllEvent(...args: any[]): void { + this.callbacks.forEach(callback => callback(...args)); + } + + public emmitEventForKey(event: string, ...args: any[]): void { + const callback = this.callbacks.get(event); + if (callback) { + callback(...args); + } else { + console.error(`No callback found with key "${event}".`); + } + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/view/h2dtsview.ts b/src/vscode_plugin/src/view/h2dtsview.ts new file mode 100644 index 00000000..a414eec0 --- /dev/null +++ b/src/vscode_plugin/src/view/h2dtsview.ts @@ -0,0 +1,77 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +import * as vscode from 'vscode'; +import { H2dtsMod } from "../model/h2dtsmod"; +import { IModel } from "../model/imodel"; +import { IView } from "./iview"; +import { + EVENT_ERROR, + EVENT_INFORMATION, + EVENT_PROGRESS, + EVENT_WARNING +} from '../common/eventtype'; +import { IController } from '../controller/icontroller'; +import { toastMsg } from '../common/widget'; + +export class H2dtsView extends IView { + name: string; + model: IModel; + controller: IController | undefined; + progress: vscode.Progress<{ message?: string; increment?: number; }> | undefined = undefined; + constructor() { + super(); + this.name = 'h2dtsview'; + this.model = H2dtsMod.getInstance(); + } + + init(controller: IController): void { + this.controller = controller; + + this.model.onEvent(EVENT_PROGRESS, (percent, info) => { + if (this.progress) { + this.progress.report({ increment: percent, message: info }) + } + }) + this.model.onEvent(EVENT_ERROR, (errno, errmsg) => { + vscode.window.showErrorMessage(errmsg); + }) + this.model.onEvent(EVENT_WARNING, (errno, errmsg) => { + vscode.window.showWarningMessage(errmsg); + }) + } + + showProgress(): void { + try { + vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: "Generating .d.ts ...", + cancellable: false + }, async (progress: vscode.Progress<{ message?: string; increment?: number; }>) => { + this.progress = progress; + if (this.controller) { + this.controller.start(); + } + }) + } catch (error) { + let errmsg = this.name + " showProgress error: " + JSON.stringify(error); + toastMsg(EVENT_ERROR, errmsg); + } + + } + + showMsg(event: string, msg: string): void { + toastMsg(event, msg); + } +} \ No newline at end of file diff --git a/src/vscode_plugin/src/view/iview.ts b/src/vscode_plugin/src/view/iview.ts new file mode 100644 index 00000000..ea4f7292 --- /dev/null +++ b/src/vscode_plugin/src/view/iview.ts @@ -0,0 +1,27 @@ +/* +* Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import { IController } from "../controller/icontroller"; +import { IModel } from "../model/imodel"; + +export abstract class IView { + abstract name: string; + abstract model: IModel; + abstract controller: IController | undefined; + + abstract init(controller: IController): void; + abstract showProgress(): void; + abstract showMsg(event: string, msg: string): void; +} \ No newline at end of file -- Gitee