vscode#DocumentLink TypeScript Examples

The following examples show how to use vscode#DocumentLink. 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
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;
    }
Example #2
Source File: librarynote.ts    From vscode-lean4 with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
Example #3
Source File: parse.ts    From vscode-todo-md with MIT License 4 votes vote down vote up
/**
 * Some features require knowledge beyond 1 line.
 * Parsing links, for example, is taken from vscode api that runs on a document. This function maps it to each line.
 *
 * Also things that require information about other lines, like nested task needs to find a parent task.
 */
export async function parseDocument(document: TextDocument): Promise<ParsedDocument> {
	const tasks: TheTask[] = [];
	const commentLines: Range[] = [];
	let startLine = 0;

	const links = await commands.executeCommand<DocumentLink[]>('vscode.executeLinkProvider', document.uri) ?? [];

	// Ignore markdown yaml frontmatter
	const frontMatterHeaderRegex = /^---(?:.|\r|\n)*^---/m;
	const frontMatterHeaderMatch = frontMatterHeaderRegex.exec(document.getText());
	if (frontMatterHeaderMatch) {
		startLine = frontMatterHeaderMatch[0]?.split(/\r\n|\r|\n/).length || 0;
	}

	for (let i = startLine; i < document.lineCount; i++) {
		const parsedLine = parseLine(document.lineAt(i));
		switch (parsedLine.lineType) {
			case 'empty': continue;
			case 'comment': {
				commentLines.push(new Range(i, 0, i, 0));
				continue;
			}
			default: {
				// ──── Links ─────────────────────────────────────────────────
				const linksOnThisLine = links.filter(link => link.range.start.line === i && link.target !== undefined);
				if (linksOnThisLine.length !== 0) {
					parsedLine.value.links = linksOnThisLine.map(link => ({
						characterRange: [link.range.start.character, link.range.end.character],
						value: link.target!.toString(true),
						scheme: link.target!.scheme,
					}));
				}
				// ──── Overdue ───────────────────────────────────────────────
				if (parsedLine.value.overdue && parsedLine.value.due?.raw) {
					parsedLine.value.due = new DueDate(parsedLine.value.due.raw, {
						overdueStr: parsedLine.value.overdue,
					});
				}
				// ──── Handle nested tasks (find parent task lineNumber) ─────
				if (parsedLine.value.indentLvl) {
					for (let j = tasks.length - 1; j >= 0; j--) {
						if (tasks[j].indentLvl < parsedLine.value.indentLvl) {
							parsedLine.value.parentTaskLineNumber = tasks[j].lineNumber;
							break;
						}
					}
				}
				tasks.push(parsedLine.value);
			}
		}
	}
	// Move nested tasks inside the task
	const tasksMap: {
		[lineNumber: number]: TheTask;
	} = Object.create(null);
	for (const task of tasks) {
		tasksMap[task.lineNumber] = new TheTask({
			...task,
			subtasks: [],
		});
	}
	const tasksAsTree: TheTask[] = [];
	for (const task of tasks) {
		if (task.parentTaskLineNumber !== undefined) {
			tasksMap[task.parentTaskLineNumber].subtasks.push(tasksMap[task.lineNumber]);
		} else {
			tasksAsTree.push(tasksMap[task.lineNumber]);
		}
	}

	return {
		tasksAsTree,
		tasks,
		commentLines,
	};
}