-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
checkText.ts
151 lines (140 loc) · 4.96 KB
/
checkText.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { CSpellUserSettings } from '@cspell/cspell-types';
import assert from 'assert';
import { isTextDocument, TextDocument } from '../Models/TextDocument';
import type { ValidationIssue } from '../Models/ValidationIssue';
import * as Settings from '../Settings';
import { Document, resolveDocumentToTextDocument } from '../spellCheckFile';
import { MatchRange } from '../util/TextRange';
import { clean } from '../util/util';
import { DocumentValidator, DocumentValidatorOptions } from './docValidator';
import { calcTextInclusionRanges } from './textValidator';
import { validateText } from './validator';
/**
* Annotate text with issues and include / exclude zones.
* @param text - the text to annotate.
* @param settings - the settings to use.
* @returns the Check Text result
* @deprecated
*/
export async function checkText(text: string, settings: CSpellUserSettings): Promise<CheckTextInfo> {
const doc = clean({
uri: '',
text,
languageId: settings.languageId || 'plaintext',
locale: settings.language,
});
return checkTextDocument(doc, { noConfigSearch: true }, { loadDefaultConfiguration: false, ...settings });
}
/**
* Annotate text with issues and include / exclude zones.
* @param text - the text to annotate.
* @param settings - the settings to use.
* @returns the Check Text result
* @deprecated
*/
export async function checkTextOld(text: string, settings: CSpellUserSettings): Promise<CheckTextInfo> {
const validationResult = validateText(text, settings);
const finalSettings = Settings.finalizeSettings(settings);
const includeRanges = calcTextInclusionRanges(text, finalSettings);
const issues = await validationResult;
return genResult(text, issues, includeRanges);
}
export interface CheckTextInfo {
// Full text
text: string;
// Set of include items
items: TextInfoItem[];
}
export interface TextInfoItem {
// the segment of text that is either include or excluded
text: string;
startPos: number;
endPos: number;
flagIE: IncludeExcludeFlag;
isError?: boolean;
}
export enum IncludeExcludeFlag {
INCLUDE = 'I',
EXCLUDE = 'E',
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface CheckTextOptions extends DocumentValidatorOptions {}
/**
* Calculate document issues and include / exclude zones.
* @param doc - document to check
* @param options - check options
* @param settings - optional settings
* @returns
*/
export async function checkTextDocument(
doc: TextDocument | Document,
options: CheckTextOptions,
settings: CSpellUserSettings = {}
): Promise<CheckTextInfo> {
doc = isTextDocument(doc) ? doc : await resolveDocumentToTextDocument(doc);
return genCheckText(new DocumentValidator(doc, options, settings));
}
export async function genCheckText(docValidator: DocumentValidator): Promise<CheckTextInfo> {
await docValidator.prepare();
const issues = docValidator.checkDocument(true);
const preparations = docValidator._getPreparations();
assert(preparations);
return genResult(docValidator.document.text, issues, preparations.includeRanges);
}
function genResult(text: string, issues: ValidationIssue[], includeRanges: MatchRange[]) {
const result: TextInfoItem[] = [];
let lastPos = 0;
for (const { startPos, endPos } of includeRanges) {
result.push({
text: text.slice(lastPos, startPos),
startPos: lastPos,
endPos: startPos,
flagIE: IncludeExcludeFlag.EXCLUDE,
});
result.push({
text: text.slice(startPos, endPos),
startPos,
endPos,
flagIE: IncludeExcludeFlag.INCLUDE,
});
lastPos = endPos;
}
result.push({
text: text.slice(lastPos),
startPos: lastPos,
endPos: text.length,
flagIE: IncludeExcludeFlag.EXCLUDE,
});
function* merge() {
let i = 0;
for (const r of result) {
if (i >= issues.length || issues[i].offset >= r.endPos) {
yield r;
continue;
}
const span = { ...r };
while (i < issues.length && issues[i].offset < span.endPos) {
const issue = issues[i];
const endPos = issue.offset;
const text = span.text.slice(0, endPos - span.startPos);
const endPosError = issue.offset + issue.text.length;
yield { ...span, text, endPos };
yield {
...span,
isError: true,
startPos: issue.offset,
endPos: endPosError,
text: issue.text,
};
span.text = span.text.slice(endPosError - span.startPos);
span.startPos = endPosError;
i += 1;
}
yield span;
}
}
return {
text,
items: [...merge()].filter((i) => i.startPos < i.endPos),
};
}