Skip to content

Commit

Permalink
don't dedup the candidate provided by plugins.
Browse files Browse the repository at this point in the history
allow placeholder candidate for plugins
  • Loading branch information
mikeandmore committed Aug 14, 2011
1 parent 5b16645 commit 52431af
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
46 changes: 29 additions & 17 deletions src/ime-core/imi_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ PyUnicode_AsWString(PyObject* obj)
return res;
}

static void
ExtractSequence(TPluginCandidates& result, PyObject* py_seq)
{
Py_ssize_t len = PySequence_Length(py_seq);
for (Py_ssize_t i = 0; i < len; i++) {
PyObject* tuple_item_obj = PySequence_GetItem(py_seq, i);
if (!PyTuple_Check(tuple_item_obj)) {
continue;
}
PyObject* rank_obj = PyTuple_GetItem(tuple_item_obj, 0);
PyObject* candi_obj = PyTuple_GetItem(tuple_item_obj, 1);
if (rank_obj == NULL || !PyInt_Check(rank_obj) || candi_obj == NULL
|| !PyUnicode_Check(candi_obj)) {
continue;
}

result.push_back(TPluginCandidateItem((int) PyInt_AsLong(rank_obj),
PyUnicode_AsWString(candi_obj)));
}
}

TPluginCandidates
CIMIPythonPlugin::provide_candidates(const TPluginPreedit& str,
Expand All @@ -177,24 +197,16 @@ CIMIPythonPlugin::provide_candidates(const TPluginPreedit& str,
*waitTime = -1;
} else if (PyInt_Check(ret_obj)) {
*waitTime = (int) PyInt_AsLong(ret_obj);
} else if (PySequence_Check(ret_obj)) {
// extract all items inside this sequence.
Py_ssize_t len = PySequence_Length(ret_obj);
for (Py_ssize_t i = 0; i < len; i++) {
PyObject* tuple_item_obj = PySequence_GetItem(ret_obj, i);
if (!PyTuple_Check(tuple_item_obj)) {
continue;
}
PyObject* rank_obj = PyTuple_GetItem(tuple_item_obj, 0);
PyObject* candi_obj = PyTuple_GetItem(tuple_item_obj, 1);
if (rank_obj == NULL || !PyInt_Check(rank_obj) || candi_obj == NULL
|| !PyUnicode_Check(candi_obj)) {
continue;
}

res.push_back(TPluginCandidateItem((int) PyInt_AsLong(rank_obj),
PyUnicode_AsWString(candi_obj)));
} else if (PyTuple_Check(ret_obj)) {
PyObject* time_obj = PyTuple_GetItem(ret_obj, 0);
PyObject* seq_obj = PyTuple_GetItem(ret_obj, 1);
if (PyInt_Check(time_obj) && PyList_Check(seq_obj)) {
*waitTime = (int) PyInt_AsLong(time_obj);
ExtractSequence(res, seq_obj);
}
} else if (PyList_Check(ret_obj)) {
// extract all items inside this sequence.
ExtractSequence(res, ret_obj);
}
Py_XDECREF(str_obj);
Py_XDECREF(ret_obj);
Expand Down
13 changes: 10 additions & 3 deletions src/ime-core/imi_uiobjects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,7 @@ CCandidateList::insertCandidate(wstring wstr, int type, int rank, int userIdx)
}
if (m_candiStringsIndex.find(wstr) == m_candiStringsIndex.end()) {
m_candiStringsIndex.insert(std::make_pair(wstr, m_candiStrings.size()));
m_candiStrings.insert(m_candiStrings.begin() + rank, wstr);
m_candiTypes.insert(m_candiTypes.begin() + rank, type);
m_candiUserIndex.push_back(userIdx);
insertCandidateNoDedup(wstr, type, rank, userIdx);
} else {
int idx = m_candiStringsIndex[wstr];
if (rank >= idx) {
Expand All @@ -284,6 +282,15 @@ CCandidateList::insertCandidate(wstring wstr, int type, int rank, int userIdx)
}
}

void
CCandidateList::insertCandidateNoDedup(wstring wstr, int type, int rank,
int userIdx)
{
m_candiStrings.insert(m_candiStrings.begin() + rank, wstr);
m_candiTypes.insert(m_candiTypes.begin() + rank, type);
m_candiUserIndex.push_back(userIdx);
}

void
CCandidateList::shrinkList()
{
Expand Down
4 changes: 4 additions & 0 deletions src/ime-core/imi_uiobjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class ICandidateList : public virtual IECharType {
int userIdx = -1) = 0;
virtual void insertCandidate(wstring wstr, int type, int rank,
int userIdx = -1) = 0;
virtual void insertCandidateNoDedup(wstring wstr, int type, int rank,
int userIdx = -1) = 0;

virtual CCandiStrings & getCandiStrings() = 0;
virtual CCandiTypeVec & getCandiTypeVec() = 0;
Expand Down Expand Up @@ -220,6 +222,8 @@ class CCandidateList : virtual public ICandidateList {
virtual void pushBackCandidate(wstring wstr, int type, int userIdx = -1);
virtual void insertCandidate(wstring wstr, int type, int rank,
int userIdx = -1);
virtual void insertCandidateNoDedup(wstring wstr, int type, int rank,
int userIdx = -1);

virtual CCandiStrings & getCandiStrings();
virtual CCandiTypeVec & getCandiTypeVec();
Expand Down
5 changes: 3 additions & 2 deletions src/ime-core/imi_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ CIMIView::_pluginProvideCandidates(wstring preedit, ICandidateList* pcl)

for (size_t j = 0; j < candidates.size(); j++) {
const TPluginCandidateItem& item = candidates[j];
pcl->insertCandidate(item.m_candidate, ICandidateList::PLUGIN_TAIL,
item.m_rank);
pcl->insertCandidateNoDedup(item.m_candidate,
ICandidateList::PLUGIN_TAIL,
item.m_rank);
}
}
}
Expand Down

0 comments on commit 52431af

Please sign in to comment.