Warning, /frameworks/sonnet/src/plugins/nsspellchecker/nsspellcheckerdict.mm is written in an unsupported language. File is not indexed.

0001 /*
0002  * nsspellcheckerdict.mm
0003  *
0004  * SPDX-FileCopyrightText: 2015 Nick Shaforostoff <shaforostoff@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 #include "nsspellcheckerdict.h"
0009 #include "nsspellcheckerdebug.h"
0010 
0011 #import <AppKit/AppKit.h>
0012 
0013 using namespace Sonnet;
0014 
0015 NSSpellCheckerDict::NSSpellCheckerDict(const QString &lang)
0016     : SpellerPlugin(lang)
0017     , m_langCode([lang.toNSString() retain])
0018 {
0019     NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
0020     if ([checker setLanguage:m_langCode]) {
0021         qCDebug(SONNET_NSSPELLCHECKER) << "Loading dictionary for" << lang;
0022         [checker updatePanels];
0023     } else {
0024         qCWarning(SONNET_NSSPELLCHECKER) << "Loading dictionary for unsupported language" << lang;
0025     }
0026 }
0027 
0028 NSSpellCheckerDict::~NSSpellCheckerDict()
0029 {
0030     [m_langCode release];
0031 }
0032 
0033 bool NSSpellCheckerDict::isCorrect(const QString &word) const
0034 {
0035     NSString *nsWord = word.toNSString();
0036     NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
0037     NSRange range = [checker checkSpellingOfString:nsWord
0038         startingAt:0 language:m_langCode
0039         wrap:NO inSpellDocumentWithTag:0 wordCount:nullptr];
0040     if (range.length == 0) {
0041         // Check if the user configured a replacement text for this string. Sadly
0042         // we can only signal an error if that's the case, Sonnet has no other way
0043         // to take such substitutions into account.
0044         if (NSDictionary *replacements = [checker userReplacementsDictionary]) {
0045             return [replacements objectForKey:nsWord] == nil;
0046         } else {
0047             return true;
0048         }
0049     }
0050     return false;
0051 }
0052 
0053 QStringList NSSpellCheckerDict::suggest(const QString &word) const
0054 {
0055     NSString *nsWord = word.toNSString();
0056     NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
0057     NSArray *suggestions = [checker guessesForWordRange:NSMakeRange(0, word.length())
0058         inString:nsWord language:m_langCode inSpellDocumentWithTag:0];
0059     QStringList lst;
0060     NSDictionary *replacements = [checker userReplacementsDictionary];
0061     QString replacement;
0062     if ([replacements objectForKey:nsWord]) {
0063         // return the replacement text from the userReplacementsDictionary first.
0064         replacement = QString::fromNSString([replacements valueForKey:nsWord]);
0065         lst << replacement;
0066     }
0067     for (NSString *suggestion in suggestions) {
0068         // the replacement text from the userReplacementsDictionary will be in
0069         // the suggestions list; don't add it again.
0070         QString str = QString::fromNSString(suggestion);
0071         if (str != replacement) {
0072             lst << str;
0073         }
0074     }
0075     return lst;
0076 }
0077 
0078 bool NSSpellCheckerDict::storeReplacement(const QString &bad,
0079                                     const QString &good)
0080 {
0081     qCDebug(SONNET_NSSPELLCHECKER) << "Not storing replacement" << good << "for" << bad;
0082     return false;
0083 }
0084 
0085 bool NSSpellCheckerDict::addToPersonal(const QString &word)
0086 {
0087     NSString *nsWord = word.toNSString();
0088     NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
0089     if (![checker hasLearnedWord:nsWord]) {
0090         [checker learnWord:nsWord];
0091         [checker updatePanels];
0092     }
0093     return true;
0094 }
0095 
0096 bool NSSpellCheckerDict::addToSession(const QString &word)
0097 {
0098     qCDebug(SONNET_NSSPELLCHECKER) << "Not storing" << word << "in the session dictionary";
0099     return false;
0100 }