-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import {DocumentLinkProvider, TextDocument, DocumentLink, ProviderResult, Range, workspace, window} from 'vscode'; | ||
|
||
const seeNoteRegex = /see note \[([^\]]+)\]/gi; | ||
const libraryNoteRegex = /library_note "(.*)"/g; | ||
|
||
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; | ||
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; | ||
} | ||
} | ||
} | ||
window.showErrorMessage(`Library note "${noteName}" not found.`); | ||
} | ||
} |