Sonnet

nsspellcheckerdict.mm
1/*
2 * nsspellcheckerdict.mm
3 *
4 * SPDX-FileCopyrightText: 2015 Nick Shaforostoff <shaforostoff@gmail.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8#include "nsspellcheckerdict.h"
9#include "nsspellcheckerdebug.h"
10
11#import <AppKit/AppKit.h>
12
13using namespace Sonnet;
14
15NSSpellCheckerDict::NSSpellCheckerDict(const QString &lang)
16 : SpellerPlugin(lang)
17 , m_langCode([lang.toNSString() retain])
18{
19 NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
20 if ([checker setLanguage:m_langCode]) {
21 qCDebug(SONNET_NSSPELLCHECKER) << "Loading dictionary for" << lang;
22 [checker updatePanels];
23 } else {
24 qCWarning(SONNET_NSSPELLCHECKER) << "Loading dictionary for unsupported language" << lang;
25 }
26}
27
28NSSpellCheckerDict::~NSSpellCheckerDict()
29{
30 [m_langCode release];
31}
32
33bool NSSpellCheckerDict::isCorrect(const QString &word) const
34{
35 NSString *nsWord = word.toNSString();
36 NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
37 NSRange range = [checker checkSpellingOfString:nsWord
38 startingAt:0 language:m_langCode
39 wrap:NO inSpellDocumentWithTag:0 wordCount:nullptr];
40 if (range.length == 0) {
41 // Check if the user configured a replacement text for this string. Sadly
42 // we can only signal an error if that's the case, Sonnet has no other way
43 // to take such substitutions into account.
44 if (NSDictionary *replacements = [checker userReplacementsDictionary]) {
45 return [replacements objectForKey:nsWord] == nil;
46 } else {
47 return true;
48 }
49 }
50 return false;
51}
52
53QStringList NSSpellCheckerDict::suggest(const QString &word) const
54{
55 NSString *nsWord = word.toNSString();
56 NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
57 NSArray *suggestions = [checker guessesForWordRange:NSMakeRange(0, word.length())
58 inString:nsWord language:m_langCode inSpellDocumentWithTag:0];
59 QStringList lst;
60 NSDictionary *replacements = [checker userReplacementsDictionary];
61 QString replacement;
62 if ([replacements objectForKey:nsWord]) {
63 // return the replacement text from the userReplacementsDictionary first.
64 replacement = QString::fromNSString([replacements valueForKey:nsWord]);
65 lst << replacement;
66 }
67 for (NSString *suggestion in suggestions) {
68 // the replacement text from the userReplacementsDictionary will be in
69 // the suggestions list; don't add it again.
70 QString str = QString::fromNSString(suggestion);
71 if (str != replacement) {
72 lst << str;
73 }
74 }
75 return lst;
76}
77
78bool NSSpellCheckerDict::storeReplacement(const QString &bad,
79 const QString &good)
80{
81 qCDebug(SONNET_NSSPELLCHECKER) << "Not storing replacement" << good << "for" << bad;
82 return false;
83}
84
85bool NSSpellCheckerDict::addToPersonal(const QString &word)
86{
87 NSString *nsWord = word.toNSString();
88 NSSpellChecker *checker = [NSSpellChecker sharedSpellChecker];
89 if (![checker hasLearnedWord:nsWord]) {
90 [checker learnWord:nsWord];
91 [checker updatePanels];
92 }
93 return true;
94}
95
96bool NSSpellCheckerDict::addToSession(const QString &word)
97{
98 qCDebug(SONNET_NSSPELLCHECKER) << "Not storing" << word << "in the session dictionary";
99 return false;
100}
The sonnet namespace.
QString fromNSString(const NSString *string)
qsizetype length() const const
NSString * toNSString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.