Skip to content

Commit

Permalink
Apply clang format in rime source code (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bambooin authored Feb 12, 2024
1 parent 872cecf commit 3368db1
Show file tree
Hide file tree
Showing 23 changed files with 152 additions and 172 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/commit-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ on:

jobs:
lint:
runs-on: ubuntu-latest
runs-on: macos-14
steps:
- name: Checkout last commit
uses: actions/checkout@v4
- name: Install clang-format
run: sudo apt install -y clang-format
run: brew install clang-format
- name: Lint
run: find src -name '*.cc' -o -name '*.h' | xargs clang-format -Werror --dry-run || { echo Please lint your code by '"'"find src -name '*.cc' -o -name '*.h' | xargs clang-format -i"'"'.; false; }
run: make clang-format-lint

linux:
needs: lint
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
RIME_ROOT ?= $(CURDIR)

RIME_SOURCE_PATH = plugins sample src test tools

ifeq ($(shell uname),Darwin) # for macOS
prefix ?= $(RIME_ROOT)/dist

Expand Down Expand Up @@ -37,6 +39,12 @@ install-debug uninstall-debug

all: release

clang-format-lint:
find ${RIME_SOURCE_PATH} -name '*.cc' -o -name '*.h' | xargs clang-format -Werror --dry-run || { echo Please lint your code by '"'"make clang-format-apply"'"'.; false; }

clang-format-apply:
find ${RIME_SOURCE_PATH} -name '*.cc' -o -name '*.h' | xargs clang-format --verbose -i

deps:
$(MAKE) -f deps.mk

Expand Down
4 changes: 2 additions & 2 deletions plugins/plugins_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void PluginManager::LoadPlugins(path plugins_dir) {
DLOG(INFO) << "found plugin: " << plugin_file;
string plugin_name = plugin_name_of(plugin_file);
if (plugin_libs_.find(plugin_name) == plugin_libs_.end()) {
LOG(INFO) << "loading plugin '" << plugin_name
<< "' from " << plugin_file;
LOG(INFO) << "loading plugin '" << plugin_name << "' from "
<< plugin_file;
try {
auto plugin_lib = boost::dll::shared_library(plugin_file);
plugin_libs_[plugin_name] = plugin_lib;
Expand Down
3 changes: 1 addition & 2 deletions sample/src/sample_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ static void rime_sample_initialize() {
r.Register("trivial_translator", new Component<sample::TrivialTranslator>);
}

static void rime_sample_finalize() {
}
static void rime_sample_finalize() {}

RIME_REGISTER_MODULE(sample)
43 changes: 19 additions & 24 deletions sample/src/trivial_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,34 @@ namespace sample {

TrivialTranslator::TrivialTranslator(const Ticket& ticket)
: Translator(ticket) {
dictionary_["yi"] = "\xe4\xb8\x80"; //
dictionary_["er"] = "\xe4\xba\x8c"; //
dictionary_["san"] = "\xe4\xb8\x89"; //
dictionary_["si"] = "\xe5\x9b\x9b"; //
dictionary_["wu"] = "\xe4\xba\x94"; //
dictionary_["liu"] = "\xe5\x85\xad"; //
dictionary_["qi"] = "\xe4\xb8\x83"; //
dictionary_["ba"] = "\xe5\x85\xab"; //
dictionary_["jiu"] = "\xe4\xb9\x9d"; //
dictionary_["yi"] = "\xe4\xb8\x80"; //
dictionary_["er"] = "\xe4\xba\x8c"; //
dictionary_["san"] = "\xe4\xb8\x89"; //
dictionary_["si"] = "\xe5\x9b\x9b"; //
dictionary_["wu"] = "\xe4\xba\x94"; //
dictionary_["liu"] = "\xe5\x85\xad"; //
dictionary_["qi"] = "\xe4\xb8\x83"; //
dictionary_["ba"] = "\xe5\x85\xab"; //
dictionary_["jiu"] = "\xe4\xb9\x9d"; //
dictionary_["ling"] = "\xe3\x80\x87"; //
dictionary_["shi"] = "\xe5\x8d\x81"; //
dictionary_["bai"] = "\xe7\x99\xbe"; //
dictionary_["shi"] = "\xe5\x8d\x81"; //
dictionary_["bai"] = "\xe7\x99\xbe"; //
dictionary_["qian"] = "\xe5\x8d\x83"; //
dictionary_["wan"] = "\xe8\x90\xac"; //
dictionary_["wan"] = "\xe8\x90\xac"; //
}

an<Translation> TrivialTranslator::Query(const string& input,
const Segment& segment) {
if (!segment.HasTag("abc"))
return nullptr;
DLOG(INFO) << "input = '" << input
<< "', [" << segment.start << ", " << segment.end << ")";
DLOG(INFO) << "input = '" << input << "', [" << segment.start << ", "
<< segment.end << ")";
string output = Translate(input);
if (output.empty()) {
return nullptr;
}
auto candidate = New<SimpleCandidate>(
"trivial",
segment.start,
segment.end,
output,
":-)");
auto candidate = New<SimpleCandidate>("trivial", segment.start, segment.end,
output, ":-)");
return New<UniqueTranslation>(candidate);
}

Expand All @@ -55,10 +51,10 @@ string TrivialTranslator::Translate(const string& input) {
const size_t kMaxPinyinLength = 6;
string result;
size_t input_len = input.length();
for (size_t i = 0; i < input_len; ) {
for (size_t i = 0; i < input_len;) {
int translated = 0;
size_t len = (std::max)(kMaxPinyinLength, input_len - i);
for ( ; len >= kMinPinyinLength; --len) {
for (; len >= kMinPinyinLength; --len) {
auto it = dictionary_.find(input.substr(i, len));
if (it != dictionary_.end()) {
result += it->second;
Expand All @@ -68,8 +64,7 @@ string TrivialTranslator::Translate(const string& input) {
}
if (translated) {
i += translated;
}
else {
} else {
return string();
}
}
Expand Down
3 changes: 1 addition & 2 deletions sample/src/trivial_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class TrivialTranslator : public Translator {
public:
TrivialTranslator(const Ticket& ticket);

virtual an<Translation> Query(const string& input,
const Segment& segment);
virtual an<Translation> Query(const string& input, const Segment& segment);

private:
string Translate(const string& input);
Expand Down
17 changes: 9 additions & 8 deletions sample/test/trivial_translator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using namespace rime;

TEST(/*DISABLED_*/TrivialTranslatorTest, Query) {
TEST(/*DISABLED_*/ TrivialTranslatorTest, Query) {
// make sure the component has been registered
auto component = Translator::Require("trivial_translator");
ASSERT_TRUE(component != NULL);
Expand All @@ -26,13 +26,14 @@ TEST(/*DISABLED_*/TrivialTranslatorTest, Query) {
// lookup test
const string test_input("yiqianerbaisanshisi");
// 一千二百三十四
const string expected_output("\xe4\xb8\x80"
"\xe5\x8d\x83"
"\xe4\xba\x8c"
"\xe7\x99\xbe"
"\xe4\xb8\x89"
"\xe5\x8d\x81"
"\xe5\x9b\x9b");
const string expected_output(
"\xe4\xb8\x80"
"\xe5\x8d\x83"
"\xe4\xba\x8c"
"\xe7\x99\xbe"
"\xe4\xb8\x89"
"\xe5\x8d\x81"
"\xe5\x9b\x9b");
Segment segment(0, test_input.length());
segment.tags.insert("abc");
auto translation = translator->Query(test_input, segment);
Expand Down
73 changes: 37 additions & 36 deletions sample/tools/sample_console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,62 @@
#include <string.h>
#include <rime_api.h>

void print_status(RimeStatus *status) {
printf("schema: %s / %s\n",
status->schema_id, status->schema_name);
void print_status(RimeStatus* status) {
printf("schema: %s / %s\n", status->schema_id, status->schema_name);
printf("status: ");
if (status->is_disabled) printf("disabled ");
if (status->is_composing) printf("composing ");
if (status->is_ascii_mode) printf("ascii ");
if (status->is_full_shape) printf("full_shape ");
if (status->is_simplified) printf("simplified ");
if (status->is_disabled)
printf("disabled ");
if (status->is_composing)
printf("composing ");
if (status->is_ascii_mode)
printf("ascii ");
if (status->is_full_shape)
printf("full_shape ");
if (status->is_simplified)
printf("simplified ");
printf("\n");
}

void print_composition(RimeComposition *composition) {
const char *preedit = composition->preedit;
if (!preedit) return;
void print_composition(RimeComposition* composition) {
const char* preedit = composition->preedit;
if (!preedit)
return;
size_t len = strlen(preedit);
size_t start = composition->sel_start;
size_t end = composition->sel_end;
//size_t cursor = composition->cursor_pos;
// size_t cursor = composition->cursor_pos;
for (size_t i = 0; i <= len; ++i) {
if (start < end) {
if (i == start) putchar('[');
else if (i == end) putchar(']');
if (i == start)
putchar('[');
else if (i == end)
putchar(']');
}
//if (i == cursor) putchar('|');
// if (i == cursor) putchar('|');
if (i < len)
putchar(preedit[i]);
putchar(preedit[i]);
}
printf("\n");
}

void print_menu(RimeMenu *menu) {
if (menu->num_candidates == 0) return;
printf("page: %d%c (of size %d)\n",
menu->page_no + 1,
menu->is_last_page ? '$' : ' ',
menu->page_size);
void print_menu(RimeMenu* menu) {
if (menu->num_candidates == 0)
return;
printf("page: %d%c (of size %d)\n", menu->page_no + 1,
menu->is_last_page ? '$' : ' ', menu->page_size);
for (int i = 0; i < menu->num_candidates; ++i) {
bool highlighted = i == menu->highlighted_candidate_index;
printf("%d. %c%s%c%s\n",
i + 1,
highlighted ? '[' : ' ',
menu->candidates[i].text,
highlighted ? ']' : ' ',
printf("%d. %c%s%c%s\n", i + 1, highlighted ? '[' : ' ',
menu->candidates[i].text, highlighted ? ']' : ' ',
menu->candidates[i].comment ? menu->candidates[i].comment : "");
}
}

void print_context(RimeContext *context) {
void print_context(RimeContext* context) {
if (context->composition.length > 0) {
print_composition(&context->composition);
print_menu(&context->menu);
}
else {
} else {
printf("(not composing)\n");
}
}
Expand Down Expand Up @@ -96,8 +98,8 @@ bool execute_special_command(const char* line, RimeSessionId session_id) {
if (rime->get_schema_list(&list)) {
printf("schema list:\n");
for (size_t i = 0; i < list.size; ++i) {
printf("%lu. %s [%s]\n", (i + 1),
list.list[i].name, list.list[i].schema_id);
printf("%lu. %s [%s]\n", (i + 1), list.list[i].name,
list.list[i].schema_id);
}
rime->free_schema_list(&list);
}
Expand Down Expand Up @@ -126,7 +128,7 @@ void on_message(void* context_object,

static RIME_MODULE_LIST(sample_modules, "default", "sample");

int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
RimeApi* rime = rime_get_api();

RIME_STRUCT(RimeTraits, traits);
Expand All @@ -152,7 +154,7 @@ int main(int argc, char *argv[]) {
const int kMaxLength = 99;
char line[kMaxLength + 1] = {0};
while (fgets(line, kMaxLength, stdin) != NULL) {
for (char *p = line; *p; ++p) {
for (char* p = line; *p; ++p) {
if (*p == '\r' || *p == '\n') {
*p = '\0';
break;
Expand All @@ -164,8 +166,7 @@ int main(int argc, char *argv[]) {
continue;
if (!rime->simulate_key_sequence(session_id, line)) {
fprintf(stderr, "Error processing key sequence: %s\n", line);
}
else {
} else {
print(session_id);
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/rime/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,11 @@ class path : public std::filesystem::path {
explicit path(const char* utf8_path) : fs_path(utf8_path) {}
#endif

path& operator/=(const path& p) {
return *this = fs_path::operator/=(p);
}
path& operator/=(const fs_path& p) {
return *this = fs_path::operator/=(p);
}
path& operator/=(const path& p) { return *this = fs_path::operator/=(p); }
path& operator/=(const fs_path& p) { return *this = fs_path::operator/=(p); }
// convert UTF-8 encoded string to native encoding, then append.
path& operator/=(const std::string& p) {
return *this /= path(p);
}
path& operator/=(const char* p) {
return *this /= path(p);
}
path& operator/=(const std::string& p) { return *this /= path(p); }
path& operator/=(const char* p) { return *this /= path(p); }

friend path operator/(const path& lhs, const path& rhs) {
return path(lhs) /= rhs;
Expand Down
22 changes: 15 additions & 7 deletions src/rime/dict/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
#include <rime/dict/vocabulary.h>
#include <rime/dict/string_table.h>

#define RIME_TABLE_UNION(U, V, A, a, B, b) \
struct U { \
V value; \
const A& a() const { return *reinterpret_cast<const A*>(this); } \
const B& b() const { return *reinterpret_cast<const B*>(this); } \
A& a() { return *reinterpret_cast<A*>(this); } \
B& b() { return *reinterpret_cast<B*>(this); } \
#define RIME_TABLE_UNION(U, V, A, a, B, b) \
struct U { \
V value; \
const A& a() const { \
return *reinterpret_cast<const A*>(this); \
} \
const B& b() const { \
return *reinterpret_cast<const B*>(this); \
} \
A& a() { \
return *reinterpret_cast<A*>(this); \
} \
B& b() { \
return *reinterpret_cast<B*>(this); \
} \
}

namespace rime {
Expand Down
8 changes: 3 additions & 5 deletions test/algebra_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ static const char* kTransformation = "xform/^([zcs])h(.*)$/$1$2/";

static const int kNumOfInstructions = 5;
static const char* kInstructions[kNumOfInstructions] = {
"xform/^(\\l+)\\d$/$1/",
"erase/^[wxy].*$/",
"derive/^([zcs])h(.*)$/$1$2/",
"abbrev/^(\\l).+$/$1/",
"abbrev/^([zcs]h).+$/$1/",
"xform/^(\\l+)\\d$/$1/", "erase/^[wxy].*$/",
"derive/^([zcs])h(.*)$/$1$2/", "abbrev/^(\\l).+$/$1/",
"abbrev/^([zcs]h).+$/$1/",
};

TEST(RimeAlgebraTest, SpellingManipulation) {
Expand Down
Loading

0 comments on commit 3368db1

Please sign in to comment.