Skip to content

Commit

Permalink
fix: Fix word break issue with large documents (#3156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Apr 7, 2024
1 parent bb46dab commit 6ca6d11
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/_server/src/DocumentValidationController.mts
Expand Up @@ -7,6 +7,7 @@ import type { TextDocumentInfoWithText, TextDocumentRef } from './api.js';
import type { CSpellUserSettings } from './config/cspellConfig/index.mjs';
import type { DocumentSettings } from './config/documentSettings.mjs';
import { defaultCheckLimit } from './constants.mjs';
import { breakTextAtLimit } from './utils/breakTextAtLimit.mjs';

interface DocValEntry {
uri: string;
Expand Down Expand Up @@ -88,7 +89,8 @@ export async function createDocumentValidator(
): Promise<DocumentValidator> {
const settings = await pSettings;
const limit = (settings.checkLimit || defaultCheckLimit) * 1024;
const content = ('getText' in textDocument ? textDocument.getText() : textDocument.text).slice(0, limit);
const fullContent = 'getText' in textDocument ? textDocument.getText() : textDocument.text;
const content = breakTextAtLimit(fullContent, limit);
const { uri, languageId, version } = textDocument;
const docInfo = { uri, content, languageId, version };
const doc = createTextDocument(docInfo);
Expand Down
16 changes: 16 additions & 0 deletions packages/_server/src/utils/breakTextAtLimit.mts
@@ -0,0 +1,16 @@
export function breakTextAtLimit(text: string, limit: number): string {
if (!text[limit]) return text;

const regIsNotChar = /[^\p{L}._0-9-]/uy;

regIsNotChar.lastIndex = limit;
if (regIsNotChar.test(text)) return text.slice(0, limit);

let idx = limit - 1;
for (; idx > 0; --idx) {
if ((text.charCodeAt(idx) & 0xfc00) === 0xdc00) continue;
regIsNotChar.lastIndex = idx;
if (regIsNotChar.test(text)) return text.slice(0, idx + 1);
}
return '';
}
19 changes: 19 additions & 0 deletions packages/_server/src/utils/breakTextAtLimit.test.mts
@@ -0,0 +1,19 @@
import { describe, expect, test } from 'vitest';

import { breakTextAtLimit } from './breakTextAtLimit.mjs';

describe('breakTextAtLimit', () => {
test.each`
line | limit | expected
${'a'} | ${1} | ${'a'}
${'abc'} | ${1} | ${''}
${'abc def'} | ${3} | ${'abc'}
${'abc def'} | ${4} | ${'abc '}
${'abc def'} | ${5} | ${'abc '}
${'abc def'} | ${6} | ${'abc '}
${'abc def'} | ${7} | ${'abc def'}
${'abc def'} | ${8} | ${'abc def'}
`('breakTextAtLimit $line $limit', ({ line, limit, expected }) => {
expect(breakTextAtLimit(line, limit)).toBe(expected);
});
});

0 comments on commit 6ca6d11

Please sign in to comment.