extension.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. // src/extension.ts
  30. var extension_exports = {};
  31. __export(extension_exports, {
  32. activate: () => activate,
  33. deactivate: () => deactivate
  34. });
  35. module.exports = __toCommonJS(extension_exports);
  36. var vscode2 = __toESM(require("vscode"));
  37. // src/copyContext.ts
  38. var vscode = __toESM(require("vscode"));
  39. function formatFileContent(filePath) {
  40. return `[${filePath}]`;
  41. }
  42. function formatFileSelection(filePath, startLine, startCol, endLine, endCol) {
  43. const header = `${filePath} L${startLine}C${startCol}-L${endLine}C${endCol}`;
  44. return `[${header}]`;
  45. }
  46. function formatMultiFile(entries) {
  47. return entries.map((e) => formatFileContent(e.filePath)).join("");
  48. }
  49. async function copySelection() {
  50. const editor = vscode.window.activeTextEditor;
  51. if (!editor)
  52. return;
  53. const selection = editor.selection;
  54. if (selection.isEmpty)
  55. return;
  56. const document = editor.document;
  57. const filePath = vscode.workspace.asRelativePath(document.uri);
  58. const startLine = selection.start.line + 1;
  59. const startCol = selection.start.character + 1;
  60. const endLine = selection.end.line + 1;
  61. const endCol = selection.end.character + 1;
  62. const result = formatFileSelection(filePath, startLine, startCol, endLine, endCol);
  63. await vscode.env.clipboard.writeText(result);
  64. showStatus(`$(clippy) Copied ${filePath}:${startLine}-${endLine}`);
  65. }
  66. async function copyActiveFile() {
  67. const editor = vscode.window.activeTextEditor;
  68. if (!editor)
  69. return;
  70. const document = editor.document;
  71. const filePath = vscode.workspace.asRelativePath(document.uri);
  72. const result = formatFileContent(filePath);
  73. await vscode.env.clipboard.writeText(result);
  74. showStatus(`$(clippy) Copied file: ${filePath}`);
  75. }
  76. async function copySingleItem(itemUri) {
  77. const itemPath = vscode.workspace.asRelativePath(itemUri);
  78. const result = `[${itemPath}]`;
  79. await vscode.env.clipboard.writeText(result);
  80. showStatus(`$(clippy) Copied: ${itemPath}`);
  81. }
  82. async function copyMultiFiles(selectedUris) {
  83. if (!selectedUris || selectedUris.length === 0)
  84. return;
  85. await copyFilesFromUris(selectedUris);
  86. }
  87. async function collectFilesRecursively(dirUri) {
  88. const result = [];
  89. const entries = await vscode.workspace.fs.readDirectory(dirUri);
  90. for (const [name, type] of entries) {
  91. const childUri = vscode.Uri.joinPath(dirUri, name);
  92. if (type === vscode.FileType.Directory) {
  93. const children = await collectFilesRecursively(childUri);
  94. result.push(...children);
  95. } else if (type === vscode.FileType.File) {
  96. result.push(childUri);
  97. }
  98. }
  99. return result;
  100. }
  101. async function copyFilesFromUris(uris) {
  102. const entries = [];
  103. for (const uri of uris) {
  104. let fileUris = [uri];
  105. try {
  106. const stat = await vscode.workspace.fs.stat(uri);
  107. if (stat.type === vscode.FileType.Directory) {
  108. fileUris = await collectFilesRecursively(uri);
  109. }
  110. } catch {
  111. }
  112. for (const fu of fileUris) {
  113. entries.push({ filePath: vscode.workspace.asRelativePath(fu) });
  114. }
  115. }
  116. if (entries.length === 0)
  117. return;
  118. const result = formatMultiFile(entries);
  119. await vscode.env.clipboard.writeText(result);
  120. showStatus(`$(clippy) Copied ${entries.length} file(s)`);
  121. }
  122. function showStatus(message) {
  123. vscode.window.setStatusBarMessage(message, 3e3);
  124. }
  125. // src/extension.ts
  126. function activate(context) {
  127. console.log("[copy-ai-ctx] activating...");
  128. context.subscriptions.push(
  129. vscode2.commands.registerCommand("copy-ai-ctx.copySelection", () => {
  130. copySelection();
  131. })
  132. );
  133. context.subscriptions.push(
  134. vscode2.commands.registerCommand("copy-ai-ctx.copyFile", () => {
  135. copyActiveFile();
  136. })
  137. );
  138. context.subscriptions.push(
  139. vscode2.commands.registerCommand("copy-ai-ctx.copySelectedFiles", (args) => {
  140. if (!args) {
  141. const editor = vscode2.window.activeTextEditor;
  142. if (!editor)
  143. return;
  144. copySingleItem(editor.document.uri);
  145. return;
  146. }
  147. copySingleItem(args);
  148. })
  149. );
  150. context.subscriptions.push(
  151. vscode2.commands.registerCommand("copy-ai-ctx.copyMultiFiles", (...args) => {
  152. if (args.length < 2) {
  153. const editor = vscode2.window.activeTextEditor;
  154. if (!editor)
  155. return;
  156. copyMultiFiles([editor.document.uri]);
  157. return;
  158. }
  159. const resources = Array.isArray(args[1]) ? args[1] : args.slice(1);
  160. if (resources.length === 0)
  161. return;
  162. copyMultiFiles(resources);
  163. })
  164. );
  165. console.log("[copy-ai-ctx] activated");
  166. }
  167. function deactivate() {
  168. }
  169. // Annotate the CommonJS export names for ESM import in node:
  170. 0 && (module.exports = {
  171. activate,
  172. deactivate
  173. });
  174. //# sourceMappingURL=extension.js.map