vscode#DocumentLinkProvider TypeScript Examples

The following examples show how to use vscode#DocumentLinkProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: librarynote.ts    From vscode-lean4 with Apache License 2.0 6 votes vote down vote up
export class LibraryNoteLinkProvider implements DocumentLinkProvider {
    provideDocumentLinks(document: TextDocument): ProviderResult<DocumentLink[]> {
        const links: DocumentLink[] = [];
        for (const m of document.getText().matchAll(seeNoteRegex)) {
            const link = new DocumentLink(new Range(
                document.positionAt(m.index), document.positionAt(m.index + m[0].length)));
            link.tooltip = m[1];
            links.push(link);
        }
        return links;
    }

    async resolveDocumentLink(link: DocumentLink): Promise<DocumentLink> {
        const noteName = link.tooltip;
        try{
            for (const leanFile of await workspace.findFiles('**/*.lean')) {
                const content = (await workspace.fs.readFile(leanFile)).toString();
                for (const m of content.matchAll(libraryNoteRegex)) {
                    console.log(m[1]);
                    if (m[1] === noteName) {
                        const lineNo = content.substr(0, m.index).split(/\r\n|\r|\n/).length;
                        link.target = leanFile.with({ fragment: `L${lineNo}` });
                        return link;
                    }
                }
            }
            await window.showErrorMessage(`Library note "${noteName}" not found.`);
        }
        catch (ex){
            await window.showErrorMessage('Exception while searching for library note: ' + ex);
        }
    }
}