-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handling ScriptureDocuments correctly
- Loading branch information
Ben King
committed
Dec 23, 2024
1 parent
5ec1ff9
commit ff8bf90
Showing
28 changed files
with
3,560 additions
and
2,392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/punctuation-checker/src/allowed-character/allowed-character-issue-finder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Diagnostic, DiagnosticSeverity, Localizer, Range, ScriptureNode } from '@sillsdev/lynx'; | ||
|
||
import { DiagnosticFactory } from '../diagnostic-factory'; | ||
import { DiagnosticList } from '../diagnostic-list'; | ||
import { IssueFinder, IssueFinderFactory } from '../issue-finder'; | ||
import { ALLOWED_CHARACTER_CHECKER_LOCALIZER_NAMESPACE } from './allowed-character-checker'; | ||
import { AllowedCharacterSet } from './allowed-character-set'; | ||
|
||
export class AllowedCharacterIssueFinderFactory implements IssueFinderFactory { | ||
constructor( | ||
private readonly localizer: Localizer, | ||
private readonly allowedCharacterSet: AllowedCharacterSet, | ||
) {} | ||
|
||
public createIssueFinder(diagnosticFactory: DiagnosticFactory): IssueFinder { | ||
return new AllowedCharacterIssueFinder(this.localizer, diagnosticFactory, this.allowedCharacterSet); | ||
} | ||
} | ||
|
||
export class AllowedCharacterIssueFinder implements IssueFinder { | ||
private diagnosticList: DiagnosticList; | ||
private readonly characterRegex: RegExp = /./gu; | ||
private static readonly DIAGNOSTIC_CODE: string = 'disallowed-character'; | ||
|
||
constructor( | ||
private readonly localizer: Localizer, | ||
private readonly diagnosticFactory: DiagnosticFactory, | ||
private readonly allowedCharacterSet: AllowedCharacterSet, | ||
) { | ||
this.diagnosticList = new DiagnosticList(); | ||
} | ||
|
||
public produceDiagnostics(input: string): Diagnostic[] { | ||
this.diagnosticList = new DiagnosticList(); | ||
|
||
let match: RegExpExecArray | null; | ||
while ((match = this.characterRegex.exec(input))) { | ||
const character = match[0]; | ||
|
||
this.checkCharacter(character, match.index, match.index + match[0].length); | ||
} | ||
|
||
return this.diagnosticList.toArray(); | ||
} | ||
|
||
public produceDiagnosticsForScripture(nodes: ScriptureNode | ScriptureNode[]): Diagnostic[] { | ||
this.diagnosticList = new DiagnosticList(); | ||
|
||
if (!Array.isArray(nodes)) { | ||
nodes = [nodes]; | ||
} | ||
|
||
for (const node of nodes) { | ||
let match: RegExpExecArray | null; | ||
while ((match = this.characterRegex.exec(node.getText()))) { | ||
const character = match[0]; | ||
|
||
this.checkCharacter(character, match.index, match.index + match[0].length, node.range); | ||
} | ||
} | ||
|
||
return this.diagnosticList.toArray(); | ||
} | ||
|
||
private checkCharacter( | ||
character: string, | ||
characterStartIndex: number, | ||
characterEndIndex: number, | ||
enclosingRange?: Range, | ||
): void { | ||
if (!this.allowedCharacterSet.isCharacterAllowed(character)) { | ||
this.addDisallowedCharacterWarning(character, characterStartIndex, characterEndIndex, enclosingRange); | ||
} | ||
} | ||
|
||
private addDisallowedCharacterWarning( | ||
character: string, | ||
characterStartIndex: number, | ||
characterEndIndex: number, | ||
enclosingRange?: Range, | ||
) { | ||
const code: string = AllowedCharacterIssueFinder.DIAGNOSTIC_CODE; | ||
const diagnostic: Diagnostic = this.diagnosticFactory | ||
.newBuilder() | ||
.setCode(code) | ||
.setSeverity(DiagnosticSeverity.Warning) | ||
.setRange(characterStartIndex, characterEndIndex, enclosingRange) | ||
.setMessage( | ||
this.localizer.t(`diagnosticMessagesByCode.${code}`, { | ||
ns: ALLOWED_CHARACTER_CHECKER_LOCALIZER_NAMESPACE, | ||
character: character, | ||
}), | ||
) | ||
.build(); | ||
|
||
this.diagnosticList.addDiagnostic(diagnostic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Diagnostic, ScriptureNode } from '@sillsdev/lynx'; | ||
|
||
import { DiagnosticFactory } from './diagnostic-factory'; | ||
|
||
export interface IssueFinder { | ||
produceDiagnostics(text: string): Diagnostic[]; | ||
|
||
produceDiagnosticsForScripture(nodes: ScriptureNode | ScriptureNode[]): Diagnostic[]; | ||
} | ||
|
||
export interface IssueFinderFactory { | ||
createIssueFinder(diagnosticFactory: DiagnosticFactory): IssueFinder; | ||
} |
Oops, something went wrong.