vscode#TextLine TypeScript Examples

The following examples show how to use vscode#TextLine. 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: documentActions.ts    From vscode-todo-md with MIT License 6 votes vote down vote up
export function insertCompletionDateWorkspaceEdit(edit: WorkspaceEdit, document: TextDocument, line: TextLine, task: TheTask, forceIncludeTime = false) {
	const dateInIso = getDateInISOFormat(new Date(), forceIncludeTime || $config.completionDateIncludeTime);
	const newCompletionDate = helpCreateSpecialTag(SpecialTagName.CompletionDate, $config.completionDateIncludeDate ? dateInIso : undefined);
	if (task.completionDateRange) {
		edit.replace(document.uri, task.completionDateRange, newCompletionDate);
	} else {
		edit.insert(document.uri, new Position(line.lineNumber, line.range.end.character), ` ${newCompletionDate}`);
	}
	if (task.start) {
		insertDurationWorkspaceEdit(edit, document, line, task);
	}
}
Example #2
Source File: documentActions.ts    From vscode-todo-md with MIT License 6 votes vote down vote up
export function insertDurationWorkspaceEdit(edit: WorkspaceEdit, document: TextDocument, line: TextLine, task: TheTask) {
	if (!task.start) {
		return;
	}

	const newDurationDate = helpCreateSpecialTag(SpecialTagName.Duration, durationTo(task, true, $config.durationIncludeSeconds));
	if (task.durationRange) {
		edit.replace(document.uri, task.durationRange, newDurationDate);
	} else {
		edit.insert(document.uri, line.range.end, ` ${newDurationDate}`);
	}
}
Example #3
Source File: documentActions.ts    From vscode-todo-md with MIT License 5 votes vote down vote up
export function archiveTaskWorkspaceEdit(edit: WorkspaceEdit, archiveFileEdit: WorkspaceEdit, archiveDocument: TextDocument, uri: Uri, line: TextLine, shouldDelete: boolean) {
	appendTaskToFileWorkspaceEdit(archiveFileEdit, archiveDocument, line.text);// Add task to archive file
	if (shouldDelete) {
		edit.delete(uri, line.rangeIncludingLineBreak);// Delete task from active file
	}
}
Example #4
Source File: documentActions.ts    From vscode-todo-md with MIT License 5 votes vote down vote up
function addOverdueSpecialTagWorkspaceEdit(edit: WorkspaceEdit, uri: Uri, line: TextLine, overdueDateString: string) {
	edit.insert(uri, new Position(line.lineNumber, line.range.end.character), ` {overdue:${overdueDateString}}`);
}
Example #5
Source File: parse.ts    From vscode-todo-md with MIT License 4 votes vote down vote up
/**
 * Main parsing function. 1 Line - 1 Task.
 */
export function parseLine(textLine: TextLine): CommentReturn | EmptyLineReturn | TaskReturn {
	const line = textLine.text.trim();
	if (!line.length) {
		return {
			lineType: 'empty',
		};
	}

	const lineNumber = textLine.lineNumber;
	if (line.startsWith('# ')) {
		return {
			lineType: 'comment',
		};
	}

	let indent: string | undefined;
	if (textLine.firstNonWhitespaceCharacterIndex !== 0) {
		indent = textLine.text.slice(0, textLine.firstNonWhitespaceCharacterIndex);
	}
	const indentLvl = Math.floor(textLine.firstNonWhitespaceCharacterIndex / $state.activeDocumentTabSize);

	/** Offset of the current word (Used to calculate ranges for decorations) */
	let index = textLine.firstNonWhitespaceCharacterIndex;
	const words = line.split(' ');

	let done = false;
	const rawText = textLine.text;
	const contexts = [];
	const contextRanges: Range[] = [];
	const projects = [];
	const projectRanges: Range[] = [];
	const specialTagRanges: Range[] = [];
	const text: string[] = [];
	let priority: Priority | undefined;
	let priorityRange: Range | undefined;
	const tags: string[] = [];
	const tagsDelimiterRanges: Range[] = [];
	const tagsRange: Range[] = [];
	let count: Count | undefined;
	let completionDate: string | undefined;
	let creationDate: string | undefined;
	let start: string | undefined;
	let startRange: Range | undefined;
	let duration: string | undefined;
	let durationRange: Range | undefined;
	let overdue: string | undefined;
	let isHidden: boolean | undefined;
	let isCollapsed: boolean | undefined;
	let due: DueDate | undefined;
	let dueRange: Range | undefined;
	let overdueRange: Range | undefined;
	let collapseRange: Range | undefined;
	let completionDateRange: Range | undefined;

	for (const word of words) {
		switch (word[0]) {
			case '{': {
				if (word[word.length - 1] !== '}') {
					text.push(word);
					break;
				}
				const firstColonIndex = word.indexOf(':');
				const specialTag = word.slice(1, firstColonIndex);
				const specialTagValue = word.slice(firstColonIndex + 1, -1);
				const range = new Range(lineNumber, index, lineNumber, index + word.length);
				if (specialTag === SpecialTagName.Due) {
					if (specialTagValue.length) {
						due = new DueDate(specialTagValue);
						dueRange = range;
					}
				} else if (specialTag === SpecialTagName.Overdue) {
					overdue = specialTagValue;
					overdueRange = range;
				} else if (specialTag === SpecialTagName.CreationDate) {
					creationDate = specialTagValue;
					specialTagRanges.push(range);
				} else if (specialTag === SpecialTagName.CompletionDate) {
					// Presence of completion date indicates that the task is done
					done = true;
					if (word !== '{cm}') {
						completionDate = specialTagValue;
					}
					completionDateRange = range;
					specialTagRanges.push(range);
				} else if (specialTag === SpecialTagName.Count) {
					if (specialTagValue === undefined) {
						break;
					}
					const [current, needed] = specialTagValue.split('/');
					const currentValue = parseInt(current, 10);
					const neededValue = parseInt(needed, 10);
					if (!Number.isFinite(currentValue) || !Number.isFinite(neededValue)) {
						break;
					}
					specialTagRanges.push(range);
					if (currentValue === neededValue) {
						done = true;
					}
					count = {
						range,
						current: currentValue,
						needed: neededValue,
					};
				} else if (specialTag === SpecialTagName.Hidden) {
					isHidden = true;
					specialTagRanges.push(range);
				} else if (specialTag === SpecialTagName.Collapsed) {
					isCollapsed = true;
					collapseRange = range;
					specialTagRanges.push(range);
				} else if (specialTag === SpecialTagName.Started) {
					start = specialTagValue;
					startRange = range;
					specialTagRanges.push(range);
				} else if (specialTag === SpecialTagName.Duration) {
					duration = specialTagValue;
					durationRange = range;
					specialTagRanges.push(range);
				} else {
					text.push(word);
				}
				break;
			}
			case '#': {
				const tempTags = word.split('#').filter(tag => tag.length);
				let temp = index;
				for (const tag of tempTags) {
					tagsDelimiterRanges.push(new Range(lineNumber, temp, lineNumber, temp + 1));
					tagsRange.push(new Range(lineNumber, temp + 1, lineNumber, temp + 1 + tag.length));
					temp += tag.length + 1;
					tags.push(tag);
				}
				text.push(word);
				break;
			}
			case '@': {
				if (word.length !== 1) {
					contexts.push(word.slice(1));
					contextRanges.push(new Range(lineNumber, index, lineNumber, index + word.length));
				}
				text.push(word);
				break;
			}
			case '+': {
				if (word.length !== 1) {
					projects.push(word.slice(1));
					projectRanges.push(new Range(lineNumber, index, lineNumber, index + word.length));
				}
				text.push(word);
				break;
			}
			case '(': {
				if (/^\([A-Z]\)$/.test(word)) {
					priority = word[1] as Priority;
					priorityRange = new Range(lineNumber, index, lineNumber, index + word.length);
				} else {
					text.push(word);
				}
				break;
			}
			default: {
				text.push(word);
			}
		}
		index += word.length + 1;// 1 is space sign
	}

	return {
		lineType: 'task',
		value: new TheTask({
			indent,
			tags,
			rawText,
			tagsDelimiterRanges,
			tagsRange,
			projects,
			projectRanges,
			done,
			priority,
			start,
			startRange,
			duration,
			durationRange,
			completionDate,
			creationDate,
			priorityRange,
			specialTagRanges,
			due,
			dueRange,
			overdueRange,
			collapseRange,
			completionDateRange,
			count,
			overdue,
			isHidden,
			isCollapsed,
			contexts,
			contextRanges,
			title: text.join(' '),
			lineNumber,
			indentLvl,
		}),
	};
}