lyx_mirror/src/InsetList.cpp

199 lines
3.8 KiB
C++
Raw Normal View History

/**
* \file InsetList.cpp
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author Martin Vermeer
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "InsetList.h"
#include "insets/Inset.h"
#include "support/debug.h"
#include <algorithm>
using namespace std;
namespace lyx {
namespace {
typedef InsetList::Element Table;
struct ElementPosLess
{
bool operator()(Table const & t1, Table const & t2) const
{
return t1.pos < t2.pos;
}
};
Bulk cleanup/fix incorrect annotation at the end of namespaces. This commit does a bulk fix of incorrect annotations (comments) at the end of namespaces. The commit was generated by initially running clang-format, and then from the diff of the result extracting the hunks corresponding to fixes of namespace comments. The changes being applied and all the results have been manually reviewed. The source code successfully builds on macOS. Further details on the steps below, in case they're of interest to someone else in the future. 1. Checkout a fresh and up to date version of src/ git pull && git checkout -- src && git status src 2. Ensure there's a suitable .clang-format in place, i.e. with options to fix the comment at the end of namespaces, including: FixNamespaceComments: true SpacesBeforeTrailingComments: 1 and that clang-format is >= 5.0.0, by doing e.g.: clang-format -dump-config | grep Comments: clang-format --version 3. Apply clang-format to the source: clang-format -i $(find src -name "*.cpp" -or -name "*.h") 4. Create and filter out hunks related to fixing the namespace git diff -U0 src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace.patch 5. Filter out hunks corresponding to simple fixes into to a separate patch: pcregrep -M -e '^diff[^\n]+\nindex[^\n]+\n--- [^\n]+\n\+\+\+ [^\n]+\n' \ -e '^@@ -[0-9]+ \+[0-9]+ @@[^\n]*\n-\}[^\n]*\n\+\}[^\n]*\n' \ fix_namespace.patch > fix_namespace_simple.patch 6. Manually review the simple patch and then apply it, after first restoring the source. git checkout -- src patch -p1 < fix_namespace_simple.path 7. Manually review the (simple) changes and then stage the changes git diff src git add src 8. Again apply clang-format and filter out hunks related to any remaining fixes to the namespace, this time filter with more context. There will be fewer hunks as all the simple cases have already been handled: clang-format -i $(find src -name "*.cpp" -or -name "*.h") git diff src > tmp.patch grepdiff '^} // namespace' --output-matching=hunk tmp.patch > fix_namespace2.patch 9. Manually review/edit the resulting patch file to remove hunks for files which need to be dealt with manually, noting the file names and line numbers. Then restore files to as before applying clang-format and apply the patch: git checkout src patch -p1 < fix_namespace2.patch 10. Manually fix the files noted in the previous step. Stage files, review changes and commit.
2017-07-23 11:11:54 +00:00
} // namespace
2016-06-06 19:55:39 +00:00
InsetList::InsetList(InsetList const & il) : list_(il.list_)
{
List::iterator it = list_.begin();
List::iterator end = list_.end();
for (; it != end; ++it)
it->inset = it->inset->clone();
}
InsetList::InsetList(InsetList const & il, pos_type beg, pos_type end)
{
InsetList::const_iterator cit = il.begin();
InsetList::const_iterator cend = il.end();
for (; cit != cend; ++cit) {
if (cit->pos < beg)
continue;
if (cit->pos >= end)
break;
// Add a new entry in the insetlist_.
insert(cit->inset->clone(), cit->pos - beg);
}
}
InsetList::~InsetList()
{
List::iterator it = list_.begin();
List::iterator end = list_.end();
for (; it != end; ++it)
delete it->inset;
}
void InsetList::setBuffer(Buffer & b)
{
List::iterator it = list_.begin();
List::iterator end = list_.end();
for (; it != end; ++it)
it->inset->setBuffer(b);
}
void InsetList::resetBuffer()
{
List::iterator it = list_.begin();
List::iterator end = list_.end();
for (; it != end; ++it)
it->inset->resetBuffer();
}
InsetList::iterator InsetList::insetIterator(pos_type pos)
{
Element search_elem(pos, 0);
return lower_bound(list_.begin(), list_.end(), search_elem,
ElementPosLess());
}
InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
{
Element search_elem(pos, 0);
return lower_bound(list_.begin(), list_.end(), search_elem,
ElementPosLess());
}
void InsetList::insert(Inset * inset, pos_type pos)
{
List::iterator end = list_.end();
List::iterator it = insetIterator(pos);
if (it != end && it->pos == pos) {
LYXERR0("ERROR (InsetList::insert): "
<< "There is an inset in position: " << pos);
} else {
list_.insert(it, Element(pos, inset));
}
}
void InsetList::erase(pos_type pos)
{
List::iterator end = list_.end();
List::iterator it = insetIterator(pos);
if (it != end && it->pos == pos) {
delete it->inset;
list_.erase(it);
}
}
Inset * InsetList::release(pos_type pos)
{
List::iterator end = list_.end();
List::iterator it = insetIterator(pos);
if (it != end && it->pos == pos) {
Inset * tmp = it->inset;
it->inset = 0;
return tmp;
}
return 0;
}
Inset * InsetList::get(pos_type pos) const
{
List::const_iterator end = list_.end();
List::const_iterator it = insetIterator(pos);
if (it != end && it->pos == pos)
return it->inset;
return 0;
}
void InsetList::increasePosAfterPos(pos_type pos)
{
List::iterator end = list_.end();
List::iterator it = insetIterator(pos);
for (; it != end; ++it)
++it->pos;
}
void InsetList::decreasePosAfterPos(pos_type pos)
{
List::iterator end = list_.end();
List::iterator it = insetIterator(pos);
for (; it != end; ++it)
--it->pos;
}
pos_type InsetList::find(InsetCode code, pos_type startpos) const
{
List::const_iterator it = insetIterator(startpos);
List::const_iterator end = list_.end();
for (; it != end ; ++it) {
if (it->inset->lyxCode() == code)
return it->pos;
}
return -1;
}
int InsetList::count(InsetCode code, pos_type startpos) const
{
int num = 0;
List::const_iterator it = insetIterator(startpos);
List::const_iterator end = list_.end();
for (; it != end ; ++it) {
if (it->inset->lyxCode() == code)
++num;
}
return num;
}
} // namespace lyx