-
Notifications
You must be signed in to change notification settings - Fork 893
feat(editor): show git change gutter in the editor #921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wang-mingxue
wants to merge
2
commits into
crynta:main
Choose a base branch
from
wang-mingxue:feat/editor-git-gutter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { parseUnifiedDiff } from "./gitGutter"; | ||
|
|
||
| describe("parseUnifiedDiff", () => { | ||
| it("returns nothing for empty input", () => { | ||
| const c = parseUnifiedDiff(""); | ||
| expect(c.added.size + c.modified.size + c.deleted.size).toBe(0); | ||
| }); | ||
|
|
||
| it("marks a pure insertion as added on the new-file line", () => { | ||
| // Insert one line after line 1 (context), so the new line 2 is added. | ||
| const diff = [ | ||
| "diff --git a/f b/f", | ||
| "--- a/f", | ||
| "+++ b/f", | ||
| "@@ -1,2 +1,3 @@", | ||
| " a", | ||
| "+inserted", | ||
| " b", | ||
| "", | ||
| ].join("\n"); | ||
| const c = parseUnifiedDiff(diff); | ||
| expect([...c.added]).toEqual([2]); | ||
| expect(c.modified.size).toBe(0); | ||
| expect(c.deleted.size).toBe(0); | ||
| }); | ||
|
|
||
| it("marks a replaced line as modified", () => { | ||
| // Line 2 replaced: one deletion immediately followed by one addition. | ||
| const diff = [ | ||
| "@@ -1,3 +1,3 @@", | ||
| " a", | ||
| "-old", | ||
| "+new", | ||
| " c", | ||
| "", | ||
| ].join("\n"); | ||
| const c = parseUnifiedDiff(diff); | ||
| expect([...c.modified]).toEqual([2]); | ||
| expect(c.added.size).toBe(0); | ||
| }); | ||
|
|
||
| it("marks a boundary for a pure deletion", () => { | ||
| // Delete line 2; the deletion boundary lands on the following new-file line. | ||
| const diff = ["@@ -1,3 +1,2 @@", " a", "-gone", " c", ""].join("\n"); | ||
| const c = parseUnifiedDiff(diff); | ||
| expect([...c.deleted]).toEqual([2]); | ||
| expect(c.added.size + c.modified.size).toBe(0); | ||
| }); | ||
|
|
||
| it("does not treat added content beginning with '-' as a header", () => { | ||
| const diff = ["@@ -1,1 +1,2 @@", " a", "+- a bullet", ""].join("\n"); | ||
| const c = parseUnifiedDiff(diff); | ||
| expect([...c.added]).toEqual([2]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { type Extension, StateEffect, StateField } from "@codemirror/state"; | ||
| import { EditorView, gutter, GutterMarker } from "@codemirror/view"; | ||
|
|
||
| // 1-based new-file line numbers, grouped by change kind. `deleted` marks lines | ||
| // that have a deletion immediately above/at them (shown as a boundary bar). | ||
| export type GitChanges = { | ||
| added: Set<number>; | ||
| modified: Set<number>; | ||
| deleted: Set<number>; | ||
| }; | ||
|
|
||
| export const emptyGitChanges = (): GitChanges => ({ | ||
| added: new Set(), | ||
| modified: new Set(), | ||
| deleted: new Set(), | ||
| }); | ||
|
|
||
| /** | ||
| * Parse a `git diff` unified patch into per-line change kinds against the NEW | ||
| * (worktree) file, so a gutter can mark added / modified / deleted lines. | ||
| * Note: diff is worktree-vs-index (recomputed on save); good enough for the | ||
| * edit→save loop. For staged-line accuracy switch the source to `git diff HEAD`. | ||
| */ | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| export function parseUnifiedDiff(diffText: string): GitChanges { | ||
| const res = emptyGitChanges(); | ||
| if (!diffText) return res; | ||
|
|
||
| let newLine = 0; | ||
| let pendingDel = 0; // deletions seen since the last addition/context line | ||
| for (const raw of diffText.split("\n")) { | ||
| if (raw === "") continue; | ||
| if (raw.startsWith("@@")) { | ||
| const m = /@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(raw); | ||
| if (m) newLine = Number.parseInt(m[1], 10); | ||
| pendingDel = 0; | ||
| continue; | ||
| } | ||
| // File headers / metadata — never part of a hunk body. | ||
| if ( | ||
| raw.startsWith("+++ ") || | ||
| raw.startsWith("--- ") || | ||
| raw.startsWith("diff ") || | ||
| raw.startsWith("index ") || | ||
| raw.startsWith("new file") || | ||
| raw.startsWith("deleted file") || | ||
| raw.startsWith("rename ") || | ||
| raw.startsWith("similarity ") || | ||
| raw.startsWith("\\") | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| const c = raw[0]; | ||
| if (c === "+") { | ||
| if (pendingDel > 0) { | ||
| res.modified.add(newLine); | ||
| pendingDel--; | ||
| } else { | ||
| res.added.add(newLine); | ||
| } | ||
| newLine++; | ||
| } else if (c === "-") { | ||
| pendingDel++; | ||
| } else { | ||
| // context line: flush any unmatched deletions as a boundary marker here | ||
| if (pendingDel > 0) { | ||
| res.deleted.add(newLine); | ||
| pendingDel = 0; | ||
| } | ||
| newLine++; | ||
| } | ||
| } | ||
| if (pendingDel > 0) res.deleted.add(Math.max(1, newLine - 1)); | ||
| return res; | ||
| } | ||
|
|
||
| export const setGitChanges = StateEffect.define<GitChanges>(); | ||
|
|
||
| const gitChangesField = StateField.define<GitChanges>({ | ||
| create: emptyGitChanges, | ||
| update(value, tr) { | ||
| for (const e of tr.effects) if (e.is(setGitChanges)) return e.value; | ||
| return value; | ||
| }, | ||
| }); | ||
|
|
||
| class ChangeMarker extends GutterMarker { | ||
| constructor(kind: "added" | "modified" | "deleted") { | ||
| super(); | ||
| this.elementClass = `cm-change-${kind}`; | ||
| } | ||
| } | ||
|
|
||
| const addedMarker = new ChangeMarker("added"); | ||
| const modifiedMarker = new ChangeMarker("modified"); | ||
| const deletedMarker = new ChangeMarker("deleted"); | ||
|
|
||
| const changeGutter = gutter({ | ||
| class: "cm-changeGutter", | ||
| lineMarker(view, line) { | ||
| const changes = view.state.field(gitChangesField, false); | ||
| if (!changes) return null; | ||
| const lineNo = view.state.doc.lineAt(line.from).number; | ||
| if (changes.modified.has(lineNo)) return modifiedMarker; | ||
| if (changes.added.has(lineNo)) return addedMarker; | ||
| if (changes.deleted.has(lineNo)) return deletedMarker; | ||
| return null; | ||
| }, | ||
| lineMarkerChange: (update) => | ||
| update.state.field(gitChangesField) !== | ||
| update.startState.field(gitChangesField), | ||
| }); | ||
|
|
||
| const changeGutterTheme = EditorView.baseTheme({ | ||
| ".cm-changeGutter": { width: "3px", padding: "0" }, | ||
| ".cm-changeGutter .cm-gutterElement": { padding: "0" }, | ||
| ".cm-change-added": { backgroundColor: "#3fb950" }, | ||
| ".cm-change-modified": { backgroundColor: "#2f81f7" }, | ||
| ".cm-change-deleted": { boxShadow: "inset 0 -2px 0 0 #f85149" }, | ||
| }); | ||
|
|
||
| export const gitChangeGutter: Extension = [ | ||
| gitChangesField, | ||
| changeGutter, | ||
| changeGutterTheme, | ||
| ]; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.