From 5ac9c39f6636fb7c0466adae9100d63eafceb9c2 Mon Sep 17 00:00:00 2001 From: Craig Jones Date: Mon, 2 Nov 2020 19:39:45 -0800 Subject: [PATCH 1/2] Just added some comments while reading the code to understand it. --- src/expandBraces.ts | 1 + src/extension.ts | 7 +++++-- src/helpers.ts | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/expandBraces.ts b/src/expandBraces.ts index 482dd2f..20c9e14 100644 --- a/src/expandBraces.ts +++ b/src/expandBraces.ts @@ -1,5 +1,6 @@ import { DictionaryWords } from "./types/Dictionary"; +// Expands the vim-abolish style multiple-word-ending specifications into distinct pairs export default function expandBraces(dict: DictionaryWords): DictionaryWords { let redo = 0; let newDict: DictionaryWords = {}; diff --git a/src/extension.ts b/src/extension.ts index 72773c6..dfce015 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -30,10 +30,12 @@ export function deactivate() {} function correctTheWord(event: TextDocumentChangeEvent): void { if (!event.contentChanges.length) { + // the event was fired with no physical changes (e.g. the dirty flag was set directly) return; } if (!!event.contentChanges[0].text.match(/[A-Za-z]/)) { + // The new text is a single letter, meaning the user is still typing a word return; } @@ -45,11 +47,12 @@ function correctTheWord(event: TextDocumentChangeEvent): void { const { selection } = editor; const originalPosition = selection.start.translate(0, 1); const startPos = new Position(0, 0); + + // Here, we get all of the document text leading up to the current cursor location + // and then find the biggest last portion that matches letters and special symbols const text = editor.document.getText( new Range(startPos, originalPosition) ); - - // matches letters and special letters const lastWord = getLastWord(text); // if (triggers.length) { diff --git a/src/helpers.ts b/src/helpers.ts index 60e6442..f4a4528 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -3,6 +3,7 @@ import { DictionaryWords, Dictionary } from './types/Dictionary'; import * as largeList from './defaultList.json'; import expandBraces from './expandBraces'; +// Loads up the pairs of corrections to check that are pertinent for the current context export function getWords(config: WorkspaceConfiguration) { const editor = window.activeTextEditor; const dictionary = config.get('dictionary', [ @@ -29,6 +30,7 @@ export function getWords(config: WorkspaceConfiguration) { return expandBraces(words); } +// Pulls the "auto-correct" section out of the VSCode configuration export function getConfig(): WorkspaceConfiguration { const editor = window.activeTextEditor; const config = workspace.getConfiguration( @@ -38,7 +40,13 @@ export function getConfig(): WorkspaceConfiguration { return config; } +// Retrieves the last "word" from the given inputText. +// A "word" is any string of letters and/or certain special symbols, optionally followed by one non-alphanumeric. +// (The returned word does not include the trailing non-alphanumeric.) export function getLastWord(inputText: string): string | undefined { + // \p{L} matches anything letter-ish + // \w (lowercase w) matches any alphanumeric [A-Za-z0-9_] + // \W (uppercase W) matches anything except an alphanumeric [A-Za-z0-9_] const re = /((\p{L}|[><=+.,;@*()?!#$€%§&_'"\/\\-])+)[-_><\W]?$/gu; const match = re.exec(inputText); return match && match.length > 1 ? match[1] : undefined From 200acd48daa43de8d22aaa5a28d67598881102bf Mon Sep 17 00:00:00 2001 From: Craig Jones Date: Tue, 3 Nov 2020 12:58:28 -0800 Subject: [PATCH 2/2] Just added some comments while reading the code to understand it. --- src/expandBraces.ts | 4 +++- src/extension.ts | 11 +++++++++++ src/helpers.ts | 17 ++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/expandBraces.ts b/src/expandBraces.ts index 20c9e14..fe50af9 100644 --- a/src/expandBraces.ts +++ b/src/expandBraces.ts @@ -1,6 +1,8 @@ import { DictionaryWords } from "./types/Dictionary"; -// Expands the vim-abolish style multiple-word-ending specifications into distinct pairs +/** + * Expands the vim-abolish style multiple-word-ending specifications into distinct pairs + */ export default function expandBraces(dict: DictionaryWords): DictionaryWords { let redo = 0; let newDict: DictionaryWords = {}; diff --git a/src/extension.ts b/src/extension.ts index dfce015..b80bb42 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,6 +6,9 @@ let config: WorkspaceConfiguration; let words: DictionaryWords; // let triggers: string[]; +/** + * Initialize this extension. + */ export function activate(_context: ExtensionContext) { config = getConfig(); words = getWords(config); @@ -28,12 +31,19 @@ export function activate(_context: ExtensionContext) { // this method is called when your extension is deactivated export function deactivate() {} +/** + * Check to see if we've reached the end of a "word" and, if so, + * see if the word is in the list of common typos (dict key) and, + * if so, fix it. + */ function correctTheWord(event: TextDocumentChangeEvent): void { if (!event.contentChanges.length) { // the event was fired with no physical changes (e.g. the dirty flag was set directly) return; } + // TODO Under what circumstances might there be more than one change? Do we need to handle that scenario? + if (!!event.contentChanges[0].text.match(/[A-Za-z]/)) { // The new text is a single letter, meaning the user is still typing a word return; @@ -50,6 +60,7 @@ function correctTheWord(event: TextDocumentChangeEvent): void { // Here, we get all of the document text leading up to the current cursor location // and then find the biggest last portion that matches letters and special symbols + // TODO It might be more efficient to only grab the text from the start of the current line (i.e. editor.document.lineAt(position)) const text = editor.document.getText( new Range(startPos, originalPosition) ); diff --git a/src/helpers.ts b/src/helpers.ts index f4a4528..c4f0736 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -3,7 +3,9 @@ import { DictionaryWords, Dictionary } from './types/Dictionary'; import * as largeList from './defaultList.json'; import expandBraces from './expandBraces'; -// Loads up the pairs of corrections to check that are pertinent for the current context +/** + * Loads up the pairs of corrections to check (that are pertinent for the current context) + */ export function getWords(config: WorkspaceConfiguration) { const editor = window.activeTextEditor; const dictionary = config.get('dictionary', [ @@ -30,7 +32,9 @@ export function getWords(config: WorkspaceConfiguration) { return expandBraces(words); } -// Pulls the "auto-correct" section out of the VSCode configuration +/** + * Pulls the "auto-correct" section out of the VSCode configuration + */ export function getConfig(): WorkspaceConfiguration { const editor = window.activeTextEditor; const config = workspace.getConfiguration( @@ -40,9 +44,12 @@ export function getConfig(): WorkspaceConfiguration { return config; } -// Retrieves the last "word" from the given inputText. -// A "word" is any string of letters and/or certain special symbols, optionally followed by one non-alphanumeric. -// (The returned word does not include the trailing non-alphanumeric.) +/** + * Retrieves the last "word" from the given inputText. + * A "word" is any string of letters and/or certain special symbols, + * optionally followed by one non-alphanumeric. + * (The returned word does not include the trailing non-alphanumeric.) + */ export function getLastWord(inputText: string): string | undefined { // \p{L} matches anything letter-ish // \w (lowercase w) matches any alphanumeric [A-Za-z0-9_]