Skip to content

Commit

Permalink
refactor: Get rid of openTextDocument in extractDanglingRefs util
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Aug 5, 2020
1 parent 232d3a9 commit b01bd93
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/features/fsWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const textDocumentChangeListener = async (event: TextDocumentChangeEvent) => {
}
};

const textDocumentChangeListenerDebounced = debounce(textDocumentChangeListener, 100);
const textDocumentChangeListenerDebounced = debounce(textDocumentChangeListener, 500);

export const activate = (context: ExtensionContext) => {
const fileWatcher = workspace.createFileSystemWatcher(`**/*`);
Expand Down
35 changes: 29 additions & 6 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, Selection, workspace, Position, Uri } from 'vscode';
import { window, Selection, workspace, Range, Position, Uri } from 'vscode';
import fs from 'fs';
import path from 'path';

Expand Down Expand Up @@ -1345,10 +1345,8 @@ describe('extractDanglingRefs()', () => {
await createFile(`${name0}.md`);

expect(
await extractDanglingRefs(
await workspace.openTextDocument({
language: 'markdown',
content: `
extractDanglingRefs(
`
[[dangling-ref]]
[[dangling-ref]]
[[dangling-ref2|Test Label]]
Expand All @@ -1362,7 +1360,6 @@ describe('extractDanglingRefs()', () => {
\`\`\`
[[${name0}]]
`,
}),
),
).toEqual(['dangling-ref', 'dangling-ref2', 'folder1/long-dangling-ref', 'dangling-ref3']);
});
Expand Down Expand Up @@ -1406,4 +1403,30 @@ describe('findDanglingRefsByFsPath()', () => {
'dangling-ref3',
]);
});

it('should find dangling refs from the just edited document', async () => {
const name0 = rndName();

await createFile(`${name0}.md`, '[[dangling-ref]]');

const doc = await openTextDocument(`${name0}.md`);

const editor = await window.showTextDocument(doc);

const refsByFsPath = await findDanglingRefsByFsPath(getWorkspaceCache().markdownUris);

expect(Object.keys(refsByFsPath)).toHaveLength(1);
expect(Object.values(refsByFsPath)[0]).toEqual(['dangling-ref']);

await editor.edit((edit) => edit.insert(new Position(1, 0), '[[dangling-ref2]]'));

const refsByFsPath2 = await findDanglingRefsByFsPath(getWorkspaceCache().markdownUris);

expect(Object.keys(refsByFsPath2)).toHaveLength(1);
expect(Object.values(refsByFsPath2)[0]).toEqual(['dangling-ref', 'dangling-ref2']);

await editor.edit((edit) => edit.delete(new Range(new Position(0, 0), new Position(2, 0))));

expect(await findDanglingRefsByFsPath(getWorkspaceCache().markdownUris)).toEqual({});
});
});
51 changes: 22 additions & 29 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,31 @@ export const fsPathToRef = ({
return trimLeadingSlash(ref.includes('.') ? ref.slice(0, ref.lastIndexOf('.')) : ref);
};

export const extractDanglingRefs = async (document: vscode.TextDocument) => {
const matches = matchAll(new RegExp(refPattern, 'gi'), document.getText());

if (matches.length) {
const refs = matches.reduce<string[]>((refs, match) => {
const [, , $2] = match;
const offset = (match.index || 0) + 2;

const refStart = document.positionAt(offset);
const lineStart = document.lineAt(refStart);

if (
isInFencedCodeBlock(document, lineStart.lineNumber) ||
isInCodeSpan(document, lineStart.lineNumber, refStart.character)
) {
return refs;
}
const refRegexp = new RegExp(refPattern, 'gi');

const { ref } = parseRef($2);
export const extractDanglingRefs = (content: string) => {
const refs: string[] = [];

if (!findUriByRef(getWorkspaceCache().allUris, ref)) {
refs.push(ref);
}
content.split(/\r?\n/g).forEach((lineText, lineNum) => {
for (const match of matchAll(refRegexp, lineText)) {
const [, , reference] = match;
if (reference) {
const offset = (match.index || 0) + 2;

return refs;
}, []);
if (isInFencedCodeBlock(content, lineNum) || isInCodeSpan(content, lineNum, offset)) {
continue;
}

return Array.from(new Set(refs));
}
const { ref } = parseRef(reference);

return [];
if (!findUriByRef(getWorkspaceCache().allUris, ref)) {
refs.push(ref);
}
}
}
});

return Array.from(new Set(refs));
};

export const findDanglingRefsByFsPath = async (uris: vscode.Uri[]) => {
Expand All @@ -138,9 +132,8 @@ export const findDanglingRefsByFsPath = async (uris: vscode.Uri[]) => {
continue;
}

const refs = await extractDanglingRefs(
await vscode.workspace.openTextDocument(vscode.Uri.file(fsPath)),
);
const doc = workspace.textDocuments.find((doc) => doc.uri.fsPath === fsPath);
const refs = extractDanglingRefs(doc ? doc.getText() : fs.readFileSync(fsPath).toString());

if (refs.length) {
refsByFsPath[fsPath] = refs;
Expand Down

0 comments on commit b01bd93

Please sign in to comment.