diff options
-rw-r--r-- | SwifTools/MacOSXChecker.mm | 2 | ||||
-rw-r--r-- | SwifTools/SpellParser.h | 2 | ||||
-rw-r--r-- | SwifTools/UnitTest/SpellParserTest.cpp | 24 | ||||
-rw-r--r-- | Swift/QtUI/QtTextEdit.cpp | 26 | ||||
-rw-r--r-- | Swift/QtUI/QtTextEdit.h | 6 |
5 files changed, 32 insertions, 28 deletions
diff --git a/SwifTools/MacOSXChecker.mm b/SwifTools/MacOSXChecker.mm index 519f06c..aefe3e7 100644 --- a/SwifTools/MacOSXChecker.mm +++ b/SwifTools/MacOSXChecker.mm @@ -69,7 +69,7 @@ void MacOSXChecker::checkFragment(const std::string& fragment, PositionPairList& if (range.location != NSNotFound) { if (range.location < nextLocation) break; - misspelledPositions.push_back(PositionPair(static_cast<int>(range.location), static_cast<int>(range.location + range.length))); + misspelledPositions.push_back(PositionPair(range.location, range.location + range.length)); nextLocation = range.location + range.length + 1; } } while (range.location != NSNotFound); diff --git a/SwifTools/SpellParser.h b/SwifTools/SpellParser.h index 5b057c9..3c1cf8b 100644 --- a/SwifTools/SpellParser.h +++ b/SwifTools/SpellParser.h @@ -26,7 +26,7 @@ namespace Swift { ID_CHAR = 4 }; - typedef boost::tuple<int, int> PositionPair; + typedef boost::tuple<size_t, size_t> PositionPair; typedef std::vector<PositionPair > PositionPairList; class SpellParser{ diff --git a/SwifTools/UnitTest/SpellParserTest.cpp b/SwifTools/UnitTest/SpellParserTest.cpp index ccfd7e5..bf974ec 100644 --- a/SwifTools/UnitTest/SpellParserTest.cpp +++ b/SwifTools/UnitTest/SpellParserTest.cpp @@ -34,21 +34,21 @@ class SpellParserTest : public CppUnit::TestFixture { } void testSimpleCheckFragment() { parser_->check("fragment test", position_); - int size = position_.size(); - CPPUNIT_ASSERT_EQUAL(2, size); - CPPUNIT_ASSERT_EQUAL(0, boost::get<0>(position_.front())); - CPPUNIT_ASSERT_EQUAL(8, boost::get<1>(position_.front())); - CPPUNIT_ASSERT_EQUAL(9, boost::get<0>(position_.back())); - CPPUNIT_ASSERT_EQUAL(13, boost::get<1>(position_.back())); + auto size = position_.size(); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), size); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), boost::get<0>(position_.front())); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(8), boost::get<1>(position_.front())); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(9), boost::get<0>(position_.back())); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(13), boost::get<1>(position_.back())); } void testWWWCheckFragment() { parser_->check("www.link.com fragment test", position_); - int size = position_.size(); - CPPUNIT_ASSERT_EQUAL(2, size); - CPPUNIT_ASSERT_EQUAL(13, boost::get<0>(position_.front())); - CPPUNIT_ASSERT_EQUAL(21, boost::get<1>(position_.front())); - CPPUNIT_ASSERT_EQUAL(22, boost::get<0>(position_.back())); - CPPUNIT_ASSERT_EQUAL(26, boost::get<1>(position_.back())); + auto size = position_.size(); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), size); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(13), boost::get<0>(position_.front())); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(21), boost::get<1>(position_.front())); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(22), boost::get<0>(position_.back())); + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(26), boost::get<1>(position_.back())); } private: const std::unique_ptr<SpellParser> parser_ = std::unique_ptr<SpellParser>(new SpellParser()); diff --git a/Swift/QtUI/QtTextEdit.cpp b/Swift/QtUI/QtTextEdit.cpp index 168e6fc..e63cd4f 100644 --- a/Swift/QtUI/QtTextEdit.cpp +++ b/Swift/QtUI/QtTextEdit.cpp @@ -131,23 +131,25 @@ void QtTextEdit::handleTextChanged() { } } -void QtTextEdit::replaceMisspelledWord(const QString& word, int cursorPosition) { +void QtTextEdit::replaceMisspelledWord(const QString& word, size_t cursorPosition) { QTextCursor cursor = textCursor(); - PositionPair wordPosition = getWordFromCursor(cursorPosition); - cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); - cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); - QTextCharFormat normalFormat; - cursor.insertText(word, normalFormat); + auto wordPosition = getWordFromCursor(cursorPosition); + if (wordPosition) { + cursor.setPosition(boost::get<0>(*wordPosition), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(*wordPosition), QTextCursor::KeepAnchor); + QTextCharFormat normalFormat; + cursor.insertText(word, normalFormat); + } } -PositionPair QtTextEdit::getWordFromCursor(int cursorPosition) { +boost::optional<PositionPair> QtTextEdit::getWordFromCursor(size_t cursorPosition) { PositionPairList misspelledPositions = highlighter_->getMisspelledPositions(); for (auto& misspelledPosition : misspelledPositions) { if (cursorPosition >= boost::get<0>(misspelledPosition) && cursorPosition <= boost::get<1>(misspelledPosition)) { return misspelledPosition; } } - return boost::make_tuple(-1,-1); + return boost::optional<PositionPair>(boost::make_tuple(-1,-1)); } QSize QtTextEdit::sizeHint() const { @@ -195,14 +197,14 @@ void QtTextEdit::addSuggestions(QMenu* menu, QContextMenuEvent* event) if (checker_ && highlighter_) { QAction* insertPoint = menu->actions().first(); QTextCursor cursor = cursorForPosition(event->pos()); - PositionPair wordPosition = getWordFromCursor(cursor.position()); - if (boost::get<0>(wordPosition) < 0) { + auto wordPosition = getWordFromCursor(cursor.position()); + if (!wordPosition) { // The click was executed outside a spellable word so no // suggestions are necessary return; } - cursor.setPosition(boost::get<0>(wordPosition), QTextCursor::MoveAnchor); - cursor.setPosition(boost::get<1>(wordPosition), QTextCursor::KeepAnchor); + cursor.setPosition(boost::get<0>(*wordPosition), QTextCursor::MoveAnchor); + cursor.setPosition(boost::get<1>(*wordPosition), QTextCursor::KeepAnchor); std::vector<std::string> wordList; checker_->getSuggestions(Q2PSTRING(cursor.selectedText()), wordList); if (wordList.size() == 0) { diff --git a/Swift/QtUI/QtTextEdit.h b/Swift/QtUI/QtTextEdit.h index 7ce5d88..178f258 100644 --- a/Swift/QtUI/QtTextEdit.h +++ b/Swift/QtUI/QtTextEdit.h @@ -6,6 +6,8 @@ #pragma once +#include <boost/optional.hpp> + #include <QPointer> #include <QTextEdit> @@ -52,10 +54,10 @@ namespace Swift { private: void addSuggestions(QMenu* menu, QContextMenuEvent* event); - void replaceMisspelledWord(const QString& word, int cursorPosition); + void replaceMisspelledWord(const QString& word, size_t cursorPosition); void setUpSpellChecker(); void spellCheckerSettingsWindow(); - PositionPair getWordFromCursor(int cursorPosition); + boost::optional<PositionPair> getWordFromCursor(size_t cursorPosition); void updateStyleSheet(); private: |