TurboPack / SynEdit

SynEdit is a syntax highlighting edit control, not based on the Windows common controls.
216 stars 72 forks source link

New spell checking component #237

Closed pyscripter closed 1 year ago

pyscripter commented 1 year ago

Discussed in https://github.com/TurboPack/SynEdit/discussions/203

Originally posted by **pyscripter** March 16, 2022 # New spell checking I have replaced the SynEdit spell checker with a new one based on [Windows Spell checking](https://docs.microsoft.com/en-us/windows/win32/api/spellcheck/). Note that this is built in Windows and unrelated to the Microsoft Office. There many advantages compared to alternatives. - It is free. - It is fast. - Very easy to use. - Minimal code to add to your project. - No need to distribute dictionaries. If the user wants a given language dictionary they can get it through Windows language settings. - It persists words added, ignored and autocorrected. - It detects duplicate words. The drawback is that spell checking is available in Windows 8 and later. ## Implementation You should include one (and only one) TSynSpellCheck in your app, possibly in a data module. This can serve as many editor as you need. # Published properties ```pascal property PenColor: TColor read FPenColor write SetPenColor default clRed; property UnderlineStyle: TUnderlineStyle read FUnderlineStyle write SetUnderlineStyle default usMicrosoftWord; property AttributesChecked: TStringList read FAttibutesChecked write SetAttributesChecked; property Editor: TCustomSynEdit read FEditor write SetEditor; property CheckAsYouType: Boolean read FCheckAsYouType write FCheckAsYouType; ``` AttibutesChecked contains the names of the attributes that are spell checked when a highlighter is used. # Public properties ```pascal function SpellChecker: ISpellChecker; property LanguageCode: string read FLanguageCode write SetLanguageCode; class function SupportedLanguages: TArray; ``` The LanguageCode (BCP47 language tag) is set to the user locale name on component construction. It can be changed at runtime to say a persisted user option. The SupportedLanguages function returns an array of language codes for which a dictionary is available. Additional dictionaries are available through Windows language settings. You normally do not need to access the SpellChecker interface. ## Usage: The user interface is designed to be almost identical to that of Microsoft Word. SynSpellCheck.pas defines and registers 10 standard actions, which you can add to an Action list and use them in the editor popup menu or the application menu. The following actions are always enabled when the active control is a CustomSynEdit: - TSynSpellCheckFile, - TSynSpellCheckLine, - TSynSpellCheckSelection, - TSynSpellCheckWord, - TSynSpellClearErrors, - TSynSpellCheckAsYouType, whilst the following actions are only available when the cursor sits on a spelling error: ``` TSynSpellErrorAdd, TSynSpellErrorIgnoreOnce, TSynSpellErrorIgnore, TSynSpellErrorDelete ``` A non-registered action TSynSpellErrorReplace is used for suggestions and replacements. Integrating spell checking in your application requires minimal code. You add the actions to the editor popup menu and handle the OnPopUp event as demonstrated it the demo. You could instead use the OnContextPopup handler which would also allow you to adjust the position of the popup menu. ## Demo A demo was added that shows how to use the component. All the functionality resides in the popup menu and all the code (about 80 lines) in the OnPopup handler. ![image](https://user-images.githubusercontent.com/1311616/158553631-0e5d37ac-3a57-4867-8edb-6eda38a20f1a.png) ![image](https://user-images.githubusercontent.com/1311616/158553216-2333a9f4-91dd-409a-af78-615b550c237f.png)