feat: change ui from decorations to codelens
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
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.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,
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user