A3: add untracked files to .gitignore from the Changes context menu
git.ignore appends one anchored, escaped literal pattern per path to the work tree root's .gitignore, skipping patterns already there. The Changes window gets a row context menu (right click, Shift+F10, context-menu key) with Open and Add to .gitignore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ce681871ad
commit
68599fa2ac
14 changed files with 633 additions and 5 deletions
200
ui/src/components/vcs/changes/ChangesContextMenu.vue
Normal file
200
ui/src/components/vcs/changes/ChangesContextMenu.vue
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The Changes window's context menu for a file row (C7 A3): Open (as a
|
||||
* double-click does) and Add to .gitignore (`git.ignore`, only for
|
||||
* untracked files; dimmed otherwise).
|
||||
*
|
||||
* Follows the WAI-ARIA menu pattern like the project tree's menu: it takes
|
||||
* the focus when it opens, Up/Down move between items (wrapping), Enter or
|
||||
* Space runs one, Escape, Tab or a press outside closes it. The position
|
||||
* is clamped into the window.
|
||||
*/
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { t } from "../../../i18n";
|
||||
import { useChangesStore, type ChangeRow } from "./changes";
|
||||
|
||||
const props = defineProps<{
|
||||
/** The row the menu is for. */
|
||||
row: ChangeRow;
|
||||
/** Window x of the pointer (or the row) in pixels. */
|
||||
x: number;
|
||||
/** Window y of the pointer (or the row) in pixels. */
|
||||
y: number;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
/** The menu should close (an item ran or the user dismissed it). */
|
||||
close: [];
|
||||
/** Open the row, as a double-click does. */
|
||||
open: [row: ChangeRow];
|
||||
}>();
|
||||
|
||||
const changes = useChangesStore();
|
||||
|
||||
/** The menu element. */
|
||||
const menuEl = ref<HTMLElement | null>(null);
|
||||
/** Index of the focused item. */
|
||||
const focused = ref(0);
|
||||
|
||||
/** One item of the menu. */
|
||||
interface MenuItem {
|
||||
/** Stable id, also used by tests. */
|
||||
id: string;
|
||||
/** Text shown. */
|
||||
label: string;
|
||||
/** Whether it can run now. */
|
||||
enabled: boolean;
|
||||
/** Runs it. */
|
||||
run: () => Promise<void>;
|
||||
}
|
||||
|
||||
/** The items, in order. */
|
||||
const items = computed<MenuItem[]>(() => [
|
||||
{
|
||||
id: "open",
|
||||
label: t("vcs.changes.menu.open"),
|
||||
enabled: true,
|
||||
run: () => {
|
||||
emit("open", props.row);
|
||||
return Promise.resolve();
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "ignore",
|
||||
label: t("vcs.changes.menu.ignore"),
|
||||
enabled: props.row.group === "untracked" && !changes.busy,
|
||||
run: () => changes.ignore([props.row]),
|
||||
},
|
||||
]);
|
||||
|
||||
/** The menu's position, kept inside the window. */
|
||||
const position = computed(() => ({
|
||||
left: `${String(Math.max(0, Math.min(props.x, window.innerWidth - 200)))}px`,
|
||||
top: `${String(Math.max(0, Math.min(props.y, window.innerHeight - 80)))}px`,
|
||||
}));
|
||||
|
||||
/**
|
||||
* Runs an item and closes the menu.
|
||||
*
|
||||
* @param item - the item.
|
||||
*/
|
||||
async function choose(item: MenuItem): Promise<void> {
|
||||
if (!item.enabled) {
|
||||
return;
|
||||
}
|
||||
emit("close");
|
||||
await item.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Keyboard handling of the menu.
|
||||
*
|
||||
* @param event - the key event.
|
||||
*/
|
||||
function onKeydown(event: KeyboardEvent): void {
|
||||
const count = items.value.length;
|
||||
switch (event.key) {
|
||||
case "ArrowDown":
|
||||
focused.value = (focused.value + 1) % count;
|
||||
break;
|
||||
case "ArrowUp":
|
||||
focused.value = (focused.value - 1 + count) % count;
|
||||
break;
|
||||
case "Enter":
|
||||
case " ": {
|
||||
const item = items.value[focused.value];
|
||||
if (item) {
|
||||
void choose(item);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "Escape":
|
||||
case "Tab":
|
||||
emit("close");
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the menu on a press outside it.
|
||||
*
|
||||
* @param event - the pointer event on the window.
|
||||
*/
|
||||
function onPointerDown(event: PointerEvent): void {
|
||||
if (menuEl.value && event.target instanceof Node && !menuEl.value.contains(event.target)) {
|
||||
emit("close");
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
window.addEventListener("pointerdown", onPointerDown, true);
|
||||
void nextTick(() => menuEl.value?.focus());
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener("pointerdown", onPointerDown, true);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
ref="menuEl"
|
||||
class="changes-menu"
|
||||
role="menu"
|
||||
tabindex="-1"
|
||||
:aria-label="t('vcs.changes.menu.label', { path: row.file.path })"
|
||||
:aria-activedescendant="`lt-changes-menu-${items[focused]?.id ?? ''}`"
|
||||
:style="position"
|
||||
data-test="changes-menu"
|
||||
@keydown="onKeydown"
|
||||
@contextmenu.prevent
|
||||
>
|
||||
<div
|
||||
v-for="(item, index) in items"
|
||||
:id="`lt-changes-menu-${item.id}`"
|
||||
:key="item.id"
|
||||
class="changes-menu__item"
|
||||
:class="{ 'changes-menu__item--focused': index === focused, 'changes-menu__item--disabled': !item.enabled }"
|
||||
role="menuitem"
|
||||
:aria-disabled="!item.enabled"
|
||||
:data-item="item.id"
|
||||
data-test="changes-menu-item"
|
||||
@mouseenter="focused = index"
|
||||
@click="choose(item)"
|
||||
>
|
||||
{{ item.label }}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.changes-menu {
|
||||
position: fixed;
|
||||
z-index: 50;
|
||||
min-width: 160px;
|
||||
padding: 4px 0;
|
||||
color: var(--lt-fg);
|
||||
background: var(--lt-bg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 4px;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.changes-menu__item {
|
||||
padding: 4px 16px;
|
||||
cursor: default;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.changes-menu__item--focused {
|
||||
background: var(--lt-editor-selection-bg);
|
||||
}
|
||||
|
||||
.changes-menu__item--disabled {
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
</style>
|
||||
|
|
@ -20,6 +20,9 @@
|
|||
* `vcs.showDiff {path, staged}` when something provides it (the diff
|
||||
* viewer, M4.3); until then the file opens in the editor (`file.open`),
|
||||
* unless it was deleted.
|
||||
* - **Context menu (C7 A3):** a right click on a row, or Shift+F10 / the
|
||||
* context-menu key on the selected one, opens `ChangesContextMenu.vue`:
|
||||
* Open, and Add to .gitignore for untracked files.
|
||||
* - **Commit box:** the message, an Amend checkbox and Commit. Commit
|
||||
* saves dirty buffers first (`flushDirty`); `Mod+Enter` in the box
|
||||
* commits too.
|
||||
|
|
@ -35,6 +38,7 @@ import { t } from "../../../i18n";
|
|||
import { run } from "../../../ipc";
|
||||
import { baseName } from "../../../stores/editor";
|
||||
import { useNotificationsStore } from "../../../stores/notifications";
|
||||
import ChangesContextMenu from "./ChangesContextMenu.vue";
|
||||
import { SHORT_HASH, useChangesStore, type ChangeRow, type FileState, type GroupId } from "./changes";
|
||||
|
||||
const changes = useChangesStore();
|
||||
|
|
@ -46,6 +50,27 @@ const list = ref<HTMLElement | null>(null);
|
|||
/** The commit message box. */
|
||||
const messageBox = ref<HTMLTextAreaElement | null>(null);
|
||||
|
||||
/** The open context menu: its row and where it shows; null when closed. */
|
||||
const menu = ref<{ row: ChangeRow; x: number; y: number } | null>(null);
|
||||
|
||||
/**
|
||||
* Opens the context menu of a row.
|
||||
*
|
||||
* @param row - the row; it becomes the selection.
|
||||
* @param x - window x of the menu.
|
||||
* @param y - window y of the menu.
|
||||
*/
|
||||
function openMenu(row: ChangeRow, x: number, y: number): void {
|
||||
changes.select(row.key);
|
||||
menu.value = { row, x, y };
|
||||
}
|
||||
|
||||
/** Closes the context menu and gives the focus back to the list. */
|
||||
function closeMenu(): void {
|
||||
menu.value = null;
|
||||
list.value?.focus();
|
||||
}
|
||||
|
||||
/** Rows moved by Page Up and Page Down. */
|
||||
const PAGE = 10;
|
||||
|
||||
|
|
@ -259,6 +284,10 @@ function onListKey(event: KeyboardEvent): void {
|
|||
} else if (event.key === " " && row) {
|
||||
event.preventDefault();
|
||||
void changes.toggle(row);
|
||||
} else if ((event.key === "ContextMenu" || (event.key === "F10" && event.shiftKey)) && row) {
|
||||
event.preventDefault();
|
||||
const rect = document.getElementById(optionId(changes.selectedIndex))?.getBoundingClientRect();
|
||||
openMenu(row, rect ? rect.left + 24 : 0, rect ? rect.bottom : 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -399,6 +428,7 @@ watch(
|
|||
data-test="changes-row"
|
||||
@click="changes.select(row.key)"
|
||||
@dblclick="openRow(row)"
|
||||
@contextmenu.prevent="openMenu(row, $event.clientX, $event.clientY)"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
|
|
@ -420,6 +450,14 @@ watch(
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<ChangesContextMenu
|
||||
v-if="menu"
|
||||
:row="menu.row"
|
||||
:x="menu.x"
|
||||
:y="menu.y"
|
||||
@close="closeMenu"
|
||||
@open="openRow"
|
||||
/>
|
||||
<form
|
||||
class="changes__commit"
|
||||
data-test="changes-commit"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@
|
|||
* when its work tree side changed or it is conflicted, and Untracked when
|
||||
* git does not track it. A partly staged file is in both Staged and
|
||||
* Changes. Ignored files are not listed by the backend.
|
||||
* - **Ignoring (C7 A3):** `git.ignore {paths}` for untracked rows adds a
|
||||
* literal pattern per file to the root `.gitignore`, then re-reads.
|
||||
* - **Staging:** `git.stage` / `git.unstage` per file or per group.
|
||||
* Unstaging a staged rename passes both paths, as the backend asks.
|
||||
* - **Commit:** `flushDirty()` first, so the files on disk hold what the
|
||||
|
|
@ -240,6 +242,11 @@ export const useChangesStore = defineStore("changes", () => {
|
|||
void onAmend(on);
|
||||
});
|
||||
|
||||
/** Whether the box is empty and Amend is off, so a prepared message may fill it. */
|
||||
function boxFree(): boolean {
|
||||
return !amend.value && message.value.trim() === "";
|
||||
}
|
||||
|
||||
/** Whether the prepared merge message was offered for the merge in progress. */
|
||||
let mergeOffered = false;
|
||||
|
||||
|
|
@ -249,7 +256,7 @@ export const useChangesStore = defineStore("changes", () => {
|
|||
* is; no merge also ends the current offer.
|
||||
*/
|
||||
async function offerMergeMessage(): Promise<void> {
|
||||
if (amend.value || message.value.trim() !== "") {
|
||||
if (!boxFree()) {
|
||||
return;
|
||||
}
|
||||
const result = await runWaiting("git.mergeMessage");
|
||||
|
|
@ -266,7 +273,7 @@ export const useChangesStore = defineStore("changes", () => {
|
|||
return;
|
||||
}
|
||||
mergeOffered = true;
|
||||
if (!amend.value && message.value.trim() === "") {
|
||||
if (boxFree()) {
|
||||
message.value = text;
|
||||
}
|
||||
}
|
||||
|
|
@ -343,6 +350,32 @@ export const useChangesStore = defineStore("changes", () => {
|
|||
return row.group === "staged" ? unstage([row]) : stage([row]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds untracked files to the root `.gitignore`, one literal pattern
|
||||
* each, and toasts the result.
|
||||
*
|
||||
* @param list - rows; those not in Untracked are ignored.
|
||||
*/
|
||||
async function ignore(list: ChangeRow[]): Promise<void> {
|
||||
const paths = [...new Set(list.filter((row) => row.group === "untracked").map((row) => row.file.path))];
|
||||
if (paths.length === 0 || busy.value) {
|
||||
return;
|
||||
}
|
||||
busy.value = true;
|
||||
try {
|
||||
if ((await mutate("git.ignore", { paths })) !== null) {
|
||||
notifications.notify({
|
||||
level: "info",
|
||||
messageKey: "notification.vcs.ignored",
|
||||
params: { path: paths.join(", ") },
|
||||
});
|
||||
await refresh();
|
||||
}
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stages or unstages a whole group.
|
||||
*
|
||||
|
|
@ -482,6 +515,7 @@ export const useChangesStore = defineStore("changes", () => {
|
|||
unstage,
|
||||
toggle,
|
||||
toggleGroup,
|
||||
ignore,
|
||||
commit,
|
||||
start,
|
||||
stop,
|
||||
|
|
|
|||
|
|
@ -183,6 +183,7 @@
|
|||
"notification.vcs.fetchFailed": "Fetch failed: {reason}",
|
||||
"notification.vcs.fetched": "Fetched from every remote",
|
||||
"notification.vcs.historyNoFile": "Open a file or pass a path to show its history",
|
||||
"notification.vcs.ignored": "Added to .gitignore: {path}",
|
||||
"notification.vcs.mergeAborted": "Merge aborted",
|
||||
"notification.vcs.mergeConflicts": "Merging {branch} stopped on conflicts (conflicted files: {count}). Resolve them, then commit.",
|
||||
"notification.vcs.mergeFailed": "Git did not merge {branch}: {reason}",
|
||||
|
|
@ -347,6 +348,9 @@
|
|||
"vcs.changes.group.untracked": "Untracked",
|
||||
"vcs.changes.list.label": "Changed files",
|
||||
"vcs.changes.loading": "Reading the Git status…",
|
||||
"vcs.changes.menu.ignore": "Add to .gitignore",
|
||||
"vcs.changes.menu.label": "Actions for {path}",
|
||||
"vcs.changes.menu.open": "Open",
|
||||
"vcs.changes.noCommits": "no commits yet",
|
||||
"vcs.changes.noProject": "Open a project to see its changes",
|
||||
"vcs.changes.noRepo": "This project is not in a Git repository",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue