97 lines
2.2 KiB
TypeScript
97 lines
2.2 KiB
TypeScript
import { findFieldRange } from "@/frontmatter/frontmatterFields";
|
|
import {
|
|
isTaskFileName,
|
|
TASK_FILE_EXTENSION,
|
|
TASK_LANGUAGE_ID,
|
|
} from "@/model/taskFile";
|
|
import * as vscode from "vscode";
|
|
import { EditableField } from "./editFrontmatterField";
|
|
|
|
const NBSP = "\u00A0";
|
|
|
|
type FieldLensDef = {
|
|
field: EditableField;
|
|
icon: string;
|
|
label: string;
|
|
command: string;
|
|
};
|
|
|
|
const FIELDS: FieldLensDef[] = [
|
|
{
|
|
field: EditableField.STATUS,
|
|
icon: "$(symbol-misc)",
|
|
label: "Change status",
|
|
command: "xboctukFlow.editTaskStatus",
|
|
},
|
|
{
|
|
field: EditableField.PRIORITY,
|
|
icon: "$(arrow-up)",
|
|
label: "Change priority",
|
|
command: "xboctukFlow.editTaskPriority",
|
|
},
|
|
{
|
|
field: EditableField.TAGS,
|
|
icon: "$(tag)",
|
|
label: "Edit tags",
|
|
command: "xboctukFlow.editTaskTags",
|
|
},
|
|
{
|
|
field: EditableField.TAGS,
|
|
icon: "$(gear)",
|
|
label: "Configure project tags",
|
|
command: "xboctukFlow.openProjectConfig",
|
|
},
|
|
{
|
|
field: EditableField.ASSIGNEE,
|
|
icon: "$(account)",
|
|
label: "Assign me",
|
|
command: "xboctukFlow.setAssigneeMe",
|
|
},
|
|
];
|
|
|
|
function isTaskDocument(document: vscode.TextDocument): boolean {
|
|
if (document.languageId === TASK_LANGUAGE_ID) return true;
|
|
return isTaskFileName(document.fileName);
|
|
}
|
|
|
|
class TaskFieldCodeLensProvider implements vscode.CodeLensProvider {
|
|
provideCodeLenses(document: vscode.TextDocument): vscode.CodeLens[] {
|
|
if (!isTaskDocument(document)) return [];
|
|
|
|
const lenses: vscode.CodeLens[] = [];
|
|
const text = document.getText();
|
|
const uri = document.uri.toString();
|
|
|
|
for (const { field, icon, label, command } of FIELDS) {
|
|
const range = findFieldRange(text, field);
|
|
if (!range) continue;
|
|
|
|
lenses.push(
|
|
new vscode.CodeLens(
|
|
new vscode.Range(range.startLine, 0, range.startLine, 0),
|
|
{
|
|
title: `${icon}${NBSP}${label}`,
|
|
command,
|
|
arguments: [uri],
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
return lenses;
|
|
}
|
|
}
|
|
|
|
export function registerTaskCodeLenses(context: vscode.ExtensionContext): void {
|
|
const provider = new TaskFieldCodeLensProvider();
|
|
context.subscriptions.push(
|
|
vscode.languages.registerCodeLensProvider(
|
|
[
|
|
{ language: TASK_LANGUAGE_ID },
|
|
{ pattern: `**/*${TASK_FILE_EXTENSION}` },
|
|
],
|
|
provider,
|
|
),
|
|
);
|
|
}
|