Skip to content

Commit

Permalink
Clang tidy and update version in addon config file
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Jan 18, 2025
1 parent 1d28eb3 commit 3652f5f
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 74 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.10)
project(fcitx5-lua VERSION 5.0.13)

set(REQUIRED_FCITX_VERSION 5.1.12)
find_package(ECM REQUIRED 1.0.0)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

Expand All @@ -10,7 +11,7 @@ include(ECMSetupVersion)
include(ECMUninstallTarget)

find_package(Lua 5.3 REQUIRED)
find_package(Fcitx5Core 5.1.12 REQUIRED)
find_package(Fcitx5Core ${REQUIRED_FCITX_VERSION} REQUIRED)
find_package(Gettext REQUIRED)
find_package(Pthread REQUIRED)
find_package(Fcitx5Module REQUIRED COMPONENTS QuickPhrase)
Expand Down
10 changes: 10 additions & 0 deletions src/addonloader/luaaddon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
*
*/
#include "luaaddon.h"
#include "luaaddonstate.h"
#include "luahelper.h"
#include <exception>
#include <fcitx-config/rawconfig.h>
#include <fcitx-utils/library.h>
#include <fcitx/addoninfo.h>
#include <fcitx/inputcontext.h>
#include <memory>
#include <string>
#include <utility>

namespace fcitx {

Expand Down
3 changes: 3 additions & 0 deletions src/addonloader/luaaddon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
#include "luaaddon_public.h"
#include "luaaddonstate.h"
#include "luahelper.h"
#include <fcitx-config/rawconfig.h>
#include <fcitx-utils/library.h>
#include <fcitx/addoninfo.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonmanager.h>
#include <fcitx/instance.h>
#include <memory>
#include <string>

namespace fcitx {

Expand Down
3 changes: 3 additions & 0 deletions src/addonloader/luaaddonloader.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ OnDemand=True
Configurable=False
Library=libluaaddonloader

[Addon/Dependencies]
0=core:@REQUIRED_FCITX_VERSION@

[Addon/OptionalDependencies]
0=quickphrase

9 changes: 8 additions & 1 deletion src/addonloader/luaaddonloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
#include "luaaddonloader.h"
#include "config.h"
#include "luaaddon.h"
#include "luahelper.h"
#include "luastate.h"
#include <exception>
#include <fcitx-utils/library.h>
#include <fcitx/addoninfo.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonmanager.h>
#include <memory>
#include <stdexcept>

namespace fcitx {
Expand Down Expand Up @@ -84,4 +91,4 @@ AddonInstance *LuaAddonLoaderFactory::create(AddonManager *manager) {

} // namespace fcitx

FCITX_ADDON_FACTORY_V2(luaaddonloader, fcitx::LuaAddonLoaderFactory)
FCITX_ADDON_FACTORY_V2(luaaddonloader, fcitx::LuaAddonLoaderFactory);
6 changes: 4 additions & 2 deletions src/addonloader/luaaddonloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#ifndef _FCITX5_LUA_ADDONLOADER_LUAADDONLOADER_H_
#define _FCITX5_LUA_ADDONLOADER_LUAADDONLOADER_H_

#include "config.h"
#include "luahelper.h"
#include <fcitx-utils/library.h>
#include <fcitx/addonfactory.h>
#include <fcitx/addoninfo.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonloader.h>
#include <memory>
#include <string>

namespace fcitx {

Expand Down
157 changes: 90 additions & 67 deletions src/addonloader/luaaddonstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,36 @@
*/
#include "luaaddonstate.h"
#include "base.lua.h"
#include "luahelper.h"
#include "luastate.h"
#include "quickphrase_public.h"
#include <cstddef>
#include <cstdint>
#include <fcitx-config/rawconfig.h>
#include <fcitx-utils/handlertable.h>
#include <fcitx-utils/library.h>
#include <fcitx-utils/standardpath.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx-utils/trackableobject.h>
#include <fcitx-utils/utf8.h>
#include <fcitx/addonmanager.h>
#include <fcitx/event.h>
#include <fcitx/inputcontext.h>
#include <fcitx/instance.h>
#include <fcntl.h>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

namespace fcitx {

static void LuaPError(int err, const char *s) {
namespace {

void LuaPError(int err, const char *s) {
switch (err) {
case LUA_ERRSYNTAX:
FCITX_LUA_ERROR() << "syntax error during pre-compilation " << s;
Expand All @@ -40,12 +62,65 @@ static void LuaPError(int err, const char *s) {
}
}

static void LuaPrintError(LuaState *lua) {
void LuaPrintError(LuaState *lua) {
if (lua_gettop(lua) > 0) {
FCITX_LUA_ERROR() << lua_tostring(lua, -1);
}
}

void rawConfigToLua(LuaState *state, const RawConfig &config) {
if (!config.hasSubItems()) {
lua_pushlstring(state, config.value().data(), config.value().size());
return;
}

lua_newtable(state);
if (!config.value().empty()) {
lua_pushstring(state, "");
lua_pushlstring(state, config.value().data(), config.value().size());
lua_rawset(state, -3);
}
if (config.hasSubItems()) {
auto options = config.subItems();
for (auto &option : options) {
auto subConfig = config.get(option);
lua_pushstring(state, option.data());
rawConfigToLua(state, *subConfig);
lua_rawset(state, -3);
}
}
}

void luaToRawConfig(LuaState *state, RawConfig &config) {
int type = lua_type(state, -1);
if (type == LUA_TSTRING) {
if (const auto *str = lua_tostring(state, -1)) {
auto l = lua_rawlen(state, -1);
config.setValue(std::string(str, l));
}
return;
}

if (type == LUA_TTABLE) {
/* table is in the stack at index 't' */
lua_pushnil(state); /* first key */
while (lua_next(state, -2) != 0) {
if (lua_type(state, -2) == LUA_TSTRING) {
if (const auto *str = lua_tostring(state, -2)) {
if (str[0]) {
luaToRawConfig(state, config[str]);
} else if (lua_type(state, -1) == LUA_TSTRING) {
luaToRawConfig(state, config);
}
}
}
lua_pop(state, 1);
}
}
}

} // namespace

LuaAddonState::LuaAddonState(Library *luaLibrary, const std::string &name,
const std::string &library, AddonManager *manager)
: instance_(manager->instance()),
Expand Down Expand Up @@ -86,12 +161,12 @@ LuaAddonState::LuaAddonState(Library *luaLibrary, const std::string &name,
{"UTF16ToUTF8", &LuaAddonState::UTF16ToUTF8},
{"UTF8ToUTF16", &LuaAddonState::UTF8ToUTF16},
};
auto addon = GetLuaAddonState(state);
auto *addon = GetLuaAddonState(state);
luaL_newlib(addon->state_, fcitxlib);
return 1;
};
auto open_fcitx = [](lua_State *state) {
auto s = GetLuaAddonState(state)->state_.get();
auto *s = GetLuaAddonState(state)->state_.get();
if (int rv = luaL_loadstring(s, baseLua) ||
lua_pcallk(s, 0, LUA_MULTRET, 0, 0, nullptr);
rv != LUA_OK) {
Expand Down Expand Up @@ -236,15 +311,15 @@ std::tuple<> LuaAddonState::unwatchEventImpl(int id) {
}

std::tuple<std::string> LuaAddonState::currentInputMethodImpl() {
auto ic = inputContext_.get();
auto *ic = inputContext_.get();
if (ic) {
return {instance_->inputMethod(ic)};
}
return {""};
}

std::tuple<std::string> LuaAddonState::currentProgramImpl() {
auto ic = inputContext_.get();
auto *ic = inputContext_.get();
if (ic) {
return {ic->program()};
}
Expand All @@ -253,7 +328,7 @@ std::tuple<std::string> LuaAddonState::currentProgramImpl() {

std::tuple<> LuaAddonState::setCurrentInputMethodImpl(const char *str,
bool local) {
auto ic = inputContext_.get();
auto *ic = inputContext_.get();
if (ic) {
instance_->setCurrentInputMethod(ic, str, local);
}
Expand All @@ -280,7 +355,7 @@ std::tuple<int> LuaAddonState::addConverterImpl(const char *function) {
LuaPError(rv, "lua_pcall() failed");
LuaPrintError(*this);
} else if (lua_gettop(state_) >= 1) {
auto s = lua_tostring(state_, -1);
const auto *s = lua_tostring(state_, -1);
if (s) {
orig = s;
}
Expand All @@ -296,7 +371,7 @@ std::tuple<> LuaAddonState::removeConverterImpl(int id) {
}

std::tuple<> LuaAddonState::commitStringImpl(const char *str) {
if (auto ic = inputContext_.get()) {
if (auto *ic = inputContext_.get()) {
ic->commitString(str);
}
return {};
Expand All @@ -307,10 +382,8 @@ bool LuaAddonState::handleQuickPhrase(
const QuickPhraseAddCandidateCallback &callback) {
ScopedICSetter setter(inputContext_, ic->watch());
bool flag = true;
for (auto iter = quickphraseHandler_.begin(),
end = quickphraseHandler_.end();
iter != end; ++iter) {
lua_getglobal(state_, iter->second.data());
for (auto &handler : quickphraseHandler_) {
lua_getglobal(state_, handler.second.data());
lua_pushstring(state_, input.data());
int rv = lua_pcall(state_, 1, 1, 0);
if (rv != 0) {
Expand All @@ -330,7 +403,8 @@ bool LuaAddonState::handleQuickPhrase(
lua_pushinteger(state_, i);
/* stack, table, integer */
lua_gettable(state_, -2);
std::string result, display;
std::string result;
std::string display;
int action;
if (lua_type(state_, -1) == LUA_TTABLE) {
lua_pushinteger(state_, 1);
Expand Down Expand Up @@ -380,7 +454,7 @@ std::tuple<int> LuaAddonState::addQuickPhraseHandlerImpl(const char *function) {
if (!quickphraseCallback_ && quickphrase()) {
quickphraseCallback_ = quickphrase()->call<IQuickPhrase::addProvider>(
[this](InputContext *ic, const std::string &input,
QuickPhraseAddCandidateCallback callback) {
const QuickPhraseAddCandidateCallback &callback) {
return handleQuickPhrase(ic, input, callback);
});
}
Expand Down Expand Up @@ -409,7 +483,7 @@ LuaAddonState::standardPathLocateImpl(int type, const char *path,
}

std::tuple<std::string> LuaAddonState::UTF16ToUTF8Impl(const char *str) {
auto data = reinterpret_cast<const uint16_t *>(str);
const auto *data = reinterpret_cast<const uint16_t *>(str);
std::string result;
size_t i = 0;
while (data[i]) {
Expand Down Expand Up @@ -456,57 +530,6 @@ std::tuple<std::string> LuaAddonState::UTF8ToUTF16Impl(const char *str) {
result.size() * sizeof(uint16_t));
}

void rawConfigToLua(LuaState *state, const RawConfig &config) {
if (!config.hasSubItems()) {
lua_pushlstring(state, config.value().data(), config.value().size());
return;
}

lua_newtable(state);
if (!config.value().empty()) {
lua_pushstring(state, "");
lua_pushlstring(state, config.value().data(), config.value().size());
lua_rawset(state, -3);
}
if (config.hasSubItems()) {
auto options = config.subItems();
for (auto &option : options) {
auto subConfig = config.get(option);
lua_pushstring(state, option.data());
rawConfigToLua(state, *subConfig);
lua_rawset(state, -3);
}
}
}

void luaToRawConfig(LuaState *state, RawConfig &config) {
int type = lua_type(state, -1);
if (type == LUA_TSTRING) {
if (auto str = lua_tostring(state, -1)) {
auto l = lua_rawlen(state, -1);
config.setValue(std::string(str, l));
}
return;
}

if (type == LUA_TTABLE) {
/* table is in the stack at index 't' */
lua_pushnil(state); /* first key */
while (lua_next(state, -2) != 0) {
if (lua_type(state, -2) == LUA_TSTRING) {
if (auto str = lua_tostring(state, -2)) {
if (str[0]) {
luaToRawConfig(state, config[str]);
} else if (lua_type(state, -1) == LUA_TSTRING) {
luaToRawConfig(state, config);
}
}
}
lua_pop(state, 1);
}
}
}

RawConfig LuaAddonState::invokeLuaFunction(InputContext *ic,
const std::string &name,
const RawConfig &config) {
Expand Down
21 changes: 20 additions & 1 deletion src/addonloader/luaaddonstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@

#include "luahelper.h"
#include "luastate.h"
#include <exception>
#include <fcitx-config/rawconfig.h>
#include <fcitx-utils/handlertable.h>
#include <fcitx-utils/library.h>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/signals.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx-utils/trackableobject.h>
#include <fcitx-utils/tuplehelpers.h>
#include <fcitx/addoninfo.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonmanager.h>
#include <fcitx/event.h>
#include <fcitx/instance.h>
#include <functional>
#include <map>
#include <memory>
#include <quickphrase_public.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>

namespace fcitx {

Expand Down Expand Up @@ -208,7 +227,7 @@ class LuaAddonState {
std::tuple<std::string> UTF16ToUTF8Impl(const char *str);

std::tuple<std::vector<std::string>>
standardPathLocateImpl(int type, const char *name, const char *suffix);
standardPathLocateImpl(int type, const char *path, const char *suffix);

std::tuple<> commitStringImpl(const char *str);
FCITX_ADDON_DEPENDENCY_LOADER(quickphrase, instance_->addonManager());
Expand Down
Loading

0 comments on commit 3652f5f

Please sign in to comment.