@statiolake/coc-diffview 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 statiolake
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # coc-diffview
2
+
3
+ Git diff and file-history views for coc.nvim. The file list and file history
4
+ are mounted together in a panel View Container supplied by
5
+ `@statiolake/coc-ui`; selecting a file opens a disposable two-pane Neovim diff
6
+ backed by Git object contents.
package/lib/index.js ADDED
@@ -0,0 +1,322 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ activate: () => activate
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_node_path = __toESM(require("node:path"));
37
+ var import_node_fs = require("node:fs");
38
+ var import_coc = require("coc.nvim");
39
+ var DiffFilesProvider = class {
40
+ changeEmitter = new import_coc.Emitter();
41
+ onDidChangeTreeData = this.changeEmitter.event;
42
+ files = [];
43
+ set(files) {
44
+ this.files = files;
45
+ this.changeEmitter.fire();
46
+ }
47
+ getChildren() {
48
+ return this.files;
49
+ }
50
+ getTreeItem(file) {
51
+ const item = new import_coc.TreeItem(file.path, import_coc.TreeItemCollapsibleState.None);
52
+ item.description = file.status;
53
+ item.tooltip = `${file.status} ${file.path}`;
54
+ item.command = {
55
+ command: "coc-diffview.openFile",
56
+ title: "Open Diff",
57
+ arguments: [file]
58
+ };
59
+ return item;
60
+ }
61
+ };
62
+ var FileHistoryProvider = class {
63
+ changeEmitter = new import_coc.Emitter();
64
+ onDidChangeTreeData = this.changeEmitter.event;
65
+ commits = [];
66
+ set(commits) {
67
+ this.commits = commits;
68
+ this.changeEmitter.fire();
69
+ }
70
+ getChildren() {
71
+ return this.commits;
72
+ }
73
+ getTreeItem(commit) {
74
+ const item = new import_coc.TreeItem(`${commit.hash.slice(0, 10)} ${commit.subject}`);
75
+ item.description = commit.decoration;
76
+ item.tooltip = `${commit.hash}
77
+ ${commit.subject}`;
78
+ item.command = {
79
+ command: "coc-diffview.showCommit",
80
+ title: "Show Commit Diff",
81
+ arguments: [commit.root, commit.hash]
82
+ };
83
+ return item;
84
+ }
85
+ };
86
+ var Diffview = class {
87
+ constructor(ui, git, context) {
88
+ this.ui = ui;
89
+ this.git = git;
90
+ const container = ui.registerViewContainer({
91
+ id: "diff",
92
+ title: "Diff",
93
+ icon: "\uEAE1",
94
+ location: "panel",
95
+ order: 1
96
+ });
97
+ const filesView = ui.registerView({
98
+ id: "diff.files",
99
+ containerId: "diff",
100
+ name: "Files",
101
+ order: 1
102
+ });
103
+ const filesTree = ui.createTreeView("diff.files", {
104
+ treeDataProvider: this.files
105
+ });
106
+ const historyView = ui.registerView({
107
+ id: "diff.fileHistory",
108
+ containerId: "diff",
109
+ name: "File History",
110
+ order: 2,
111
+ visibility: "collapsed"
112
+ });
113
+ const historyTree = ui.createTreeView("diff.fileHistory", {
114
+ treeDataProvider: this.history
115
+ });
116
+ context.subscriptions.push(
117
+ container,
118
+ filesView,
119
+ filesTree,
120
+ historyView,
121
+ historyTree,
122
+ import_coc.commands.registerCommand(
123
+ "coc-diffview.open",
124
+ () => this.openWorkingTree()
125
+ ),
126
+ import_coc.commands.registerCommand("coc-diffview.branch", () => this.openBranch()),
127
+ import_coc.commands.registerCommand(
128
+ "coc-diffview.fileHistory",
129
+ () => this.openFileHistory()
130
+ ),
131
+ import_coc.commands.registerCommand(
132
+ "coc-diffview.showCommit",
133
+ (root, hash) => this.openCommit(root, hash)
134
+ ),
135
+ import_coc.commands.registerCommand(
136
+ "coc-diffview.openFile",
137
+ (file) => this.openFile(file)
138
+ ),
139
+ import_coc.commands.registerCommand("coc-diffview.close", () => this.close())
140
+ );
141
+ }
142
+ files = new DiffFilesProvider();
143
+ history = new FileHistoryProvider();
144
+ session;
145
+ async openWorkingTree() {
146
+ const root = await this.currentRepository();
147
+ if (!root) return;
148
+ await this.showFiles(root, "HEAD", "WORKTREE");
149
+ }
150
+ async openBranch() {
151
+ const root = await this.currentRepository();
152
+ if (!root) return;
153
+ const base = await this.mergeBase(root);
154
+ if (!base) {
155
+ await import_coc.window.showErrorMessage("Could not determine a branch merge base.");
156
+ return;
157
+ }
158
+ await this.showFiles(root, base, "HEAD");
159
+ }
160
+ async openFileHistory() {
161
+ const document = await import_coc.workspace.document;
162
+ const resource = import_coc.Uri.parse(document.textDocument.uri);
163
+ if (resource.scheme !== "file") return;
164
+ const filename = resource.fsPath;
165
+ const root = await this.git.repositoryRoot(filename);
166
+ if (!root) return;
167
+ this.history.set(await this.git.history(root, filename));
168
+ await this.ui.showView("diff.fileHistory");
169
+ }
170
+ async openCommit(root, hash) {
171
+ const parent = `${hash}^`;
172
+ await this.showFiles(root, parent, hash);
173
+ }
174
+ async showFiles(root, base, target) {
175
+ const args = target === "WORKTREE" ? ["diff", "--name-status", base] : ["diff", "--name-status", base, target];
176
+ const output = await this.git.diff(root, args);
177
+ this.files.set(parseFiles(output, root, base, target));
178
+ await this.ui.showView("diff.files");
179
+ }
180
+ async openFile(file) {
181
+ await this.close();
182
+ const relative = file.path;
183
+ const left = await this.fileContents(file.root, file.base, relative);
184
+ const right = await this.fileContents(file.root, file.target, relative);
185
+ const editor = await this.editorWindow();
186
+ if (!editor) return;
187
+ await import_coc.workspace.nvim.call("win_gotoid", [editor]);
188
+ await this.ui.openLocation(
189
+ import_coc.Uri.file(import_node_path.default.join(file.root, relative)).toString(),
190
+ 0,
191
+ 0
192
+ );
193
+ const rightWindow = await import_coc.workspace.nvim.call("win_getid");
194
+ await import_coc.workspace.nvim.command("leftabove vsplit");
195
+ const leftWindow = await import_coc.workspace.nvim.call("win_getid");
196
+ const leftBuffer = await import_coc.workspace.nvim.call("nvim_create_buf", [
197
+ false,
198
+ true
199
+ ]);
200
+ await import_coc.workspace.nvim.call("nvim_buf_set_name", [
201
+ leftBuffer,
202
+ `coc-diffview://${file.base}/${relative}`
203
+ ]);
204
+ await import_coc.workspace.nvim.call("nvim_buf_set_lines", [
205
+ leftBuffer,
206
+ 0,
207
+ -1,
208
+ false,
209
+ left.split("\n")
210
+ ]);
211
+ await import_coc.workspace.nvim.call("nvim_win_set_buf", [leftWindow, leftBuffer]);
212
+ await import_coc.workspace.nvim.command(
213
+ "setlocal buftype=nofile bufhidden=wipe noswapfile nomodifiable diff"
214
+ );
215
+ if (file.target !== "WORKTREE") {
216
+ const rightBuffer = await import_coc.workspace.nvim.call("nvim_create_buf", [
217
+ false,
218
+ true
219
+ ]);
220
+ await import_coc.workspace.nvim.call("nvim_buf_set_name", [
221
+ rightBuffer,
222
+ `coc-diffview://${file.target}/${relative}`
223
+ ]);
224
+ await import_coc.workspace.nvim.call("nvim_buf_set_lines", [
225
+ rightBuffer,
226
+ 0,
227
+ -1,
228
+ false,
229
+ right.split("\n")
230
+ ]);
231
+ await import_coc.workspace.nvim.call("nvim_win_set_buf", [rightWindow, rightBuffer]);
232
+ await import_coc.workspace.nvim.call("win_gotoid", [rightWindow]);
233
+ await import_coc.workspace.nvim.command(
234
+ "setlocal buftype=nofile bufhidden=wipe noswapfile nomodifiable"
235
+ );
236
+ }
237
+ await import_coc.workspace.nvim.command("setlocal diff");
238
+ this.session = { leftWindow, rightWindow };
239
+ }
240
+ async close() {
241
+ if (!this.session) return;
242
+ const { leftWindow, rightWindow } = this.session;
243
+ this.session = void 0;
244
+ const rightValid = await import_coc.workspace.nvim.call("nvim_win_is_valid", [
245
+ rightWindow
246
+ ]);
247
+ if (rightValid) {
248
+ await import_coc.workspace.nvim.call("win_gotoid", [rightWindow]);
249
+ await import_coc.workspace.nvim.command("diffoff");
250
+ }
251
+ const leftValid = await import_coc.workspace.nvim.call("nvim_win_is_valid", [
252
+ leftWindow
253
+ ]);
254
+ if (leftValid)
255
+ await import_coc.workspace.nvim.call("nvim_win_close", [leftWindow, true]);
256
+ }
257
+ async currentRepository() {
258
+ const document = await import_coc.workspace.document;
259
+ const resource = import_coc.Uri.parse(document.textDocument.uri);
260
+ return resource.scheme === "file" ? this.git.repositoryRoot(resource.fsPath) : this.git.repositoryRoot();
261
+ }
262
+ async mergeBase(root) {
263
+ for (const branch of ["origin/develop", "origin/master", "origin/main"]) {
264
+ try {
265
+ return (await this.git.diff(root, ["merge-base", "HEAD", branch])).trim();
266
+ } catch {
267
+ }
268
+ }
269
+ return void 0;
270
+ }
271
+ async fileContents(root, ref, relative) {
272
+ if (ref === "WORKTREE") {
273
+ try {
274
+ return await import_node_fs.promises.readFile(import_node_path.default.join(root, relative), "utf8");
275
+ } catch {
276
+ return await this.git.diff(root, ["show", `HEAD:${relative}`]);
277
+ }
278
+ }
279
+ try {
280
+ return await this.git.diff(root, ["show", `${ref}:${relative}`]);
281
+ } catch {
282
+ return "";
283
+ }
284
+ }
285
+ async editorWindow() {
286
+ const windows = await import_coc.workspace.nvim.call("nvim_list_wins");
287
+ for (const windowId of windows) {
288
+ const buffer = await import_coc.workspace.nvim.call("nvim_win_get_buf", [
289
+ windowId
290
+ ]);
291
+ const type = await import_coc.workspace.nvim.call("getbufvar", [
292
+ buffer,
293
+ "&buftype"
294
+ ]);
295
+ if (!type) return windowId;
296
+ }
297
+ return void 0;
298
+ }
299
+ };
300
+ function parseFiles(output, root, base, target) {
301
+ return output.split("\n").filter(Boolean).map((line) => {
302
+ const [status, ...parts] = line.split(" ");
303
+ return { root, status, path: parts.at(-1), base, target };
304
+ });
305
+ }
306
+ async function activate(context) {
307
+ const uiExtension = import_coc.extensions.getExtensionById("@statiolake/coc-ui");
308
+ const gitExtension = import_coc.extensions.getExtensionById(
309
+ "@statiolake/coc-git"
310
+ );
311
+ if (!uiExtension?.exports || !gitExtension?.exports) {
312
+ throw new Error(
313
+ "coc-diffview requires active coc-ui and coc-git extensions"
314
+ );
315
+ }
316
+ new Diffview(uiExtension.exports, gitExtension.exports, context);
317
+ }
318
+ // Annotate the CommonJS export names for ESM import in node:
319
+ 0 && (module.exports = {
320
+ activate
321
+ });
322
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@statiolake/coc-diffview",
3
+ "version": "0.1.0",
4
+ "description": "Git diff and file-history views for coc.nvim",
5
+ "author": "statiolake",
6
+ "license": "MIT",
7
+ "main": "lib/index.js",
8
+ "engines": {
9
+ "coc": "^0.0.82"
10
+ },
11
+ "extensionDependencies": [
12
+ "@statiolake/coc-ui",
13
+ "@statiolake/coc-git"
14
+ ],
15
+ "activationEvents": [
16
+ "*"
17
+ ],
18
+ "scripts": {
19
+ "build": "node esbuild.mjs",
20
+ "prepare": "node esbuild.mjs",
21
+ "typecheck": "tsc --noEmit"
22
+ },
23
+ "contributes": {
24
+ "commands": [
25
+ {
26
+ "command": "coc-diffview.open",
27
+ "title": "Show Working Tree Diff"
28
+ },
29
+ {
30
+ "command": "coc-diffview.branch",
31
+ "title": "Show Branch Diff"
32
+ },
33
+ {
34
+ "command": "coc-diffview.fileHistory",
35
+ "title": "Show File History"
36
+ },
37
+ {
38
+ "command": "coc-diffview.showCommit",
39
+ "title": "Show Commit Diff"
40
+ },
41
+ {
42
+ "command": "coc-diffview.openFile",
43
+ "title": "Open File Diff"
44
+ },
45
+ {
46
+ "command": "coc-diffview.close",
47
+ "title": "Close File Diff"
48
+ }
49
+ ]
50
+ },
51
+ "devDependencies": {
52
+ "@statiolake/coc-git": "git+https://github.com/statiolake/coc-git.git",
53
+ "@statiolake/coc-ui": "git+https://github.com/statiolake/coc-ui.git",
54
+ "@types/node": "^16.18.0",
55
+ "coc.nvim": "^0.0.83-next.24",
56
+ "esbuild": "^0.25.0",
57
+ "typescript": "^5.3.3"
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/statiolake/coc-diffview.git"
62
+ },
63
+ "bugs": {
64
+ "url": "https://github.com/statiolake/coc-diffview/issues"
65
+ },
66
+ "homepage": "https://github.com/statiolake/coc-diffview#readme",
67
+ "publishConfig": {
68
+ "access": "public"
69
+ },
70
+ "files": [
71
+ "lib/index.js"
72
+ ]
73
+ }