extension.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 copyWorkspaceFiles() {
  77. const workspaceFolders = vscode.workspace.workspaceFolders;
  78. if (!workspaceFolders || workspaceFolders.length === 0)
  79. return;
  80. const uris = await vscode.window.showOpenDialog({
  81. canSelectFiles: true,
  82. canSelectFolders: false,
  83. canSelectMany: true,
  84. openLabel: "Select files to copy as context"
  85. });
  86. if (!uris || uris.length === 0)
  87. return;
  88. await copyFilesFromUris(uris);
  89. }
  90. async function copySelectedFiles(selectedUris) {
  91. if (!selectedUris || selectedUris.length === 0)
  92. return;
  93. await copyFilesFromUris(selectedUris);
  94. }
  95. async function copyFilesFromUris(uris) {
  96. const entries = [];
  97. for (const uri of uris) {
  98. try {
  99. await vscode.workspace.openTextDocument(uri);
  100. const filePath = vscode.workspace.asRelativePath(uri);
  101. entries.push({ filePath });
  102. } catch {
  103. }
  104. }
  105. if (entries.length === 0)
  106. return;
  107. const result = formatMultiFile(entries);
  108. await vscode.env.clipboard.writeText(result);
  109. showStatus(`$(clippy) Copied ${entries.length} file(s)`);
  110. }
  111. function showStatus(message) {
  112. vscode.window.setStatusBarMessage(message, 3e3);
  113. }
  114. // src/extension.ts
  115. function activate(context) {
  116. console.log("[vsix-cp-ai-ctx] activating...");
  117. context.subscriptions.push(
  118. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelection", () => {
  119. copySelection();
  120. })
  121. );
  122. context.subscriptions.push(
  123. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyFile", () => {
  124. copyActiveFile();
  125. })
  126. );
  127. context.subscriptions.push(
  128. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyWorkspaceFiles", () => {
  129. copyWorkspaceFiles();
  130. })
  131. );
  132. context.subscriptions.push(
  133. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelectedFiles", (args) => {
  134. const selectedResources = args?.selectedResources || [];
  135. copySelectedFiles(selectedResources);
  136. })
  137. );
  138. console.log("[vsix-cp-ai-ctx] activated");
  139. }
  140. function deactivate() {
  141. }
  142. // Annotate the CommonJS export names for ESM import in node:
  143. 0 && (module.exports = {
  144. activate,
  145. deactivate
  146. });
  147. //# sourceMappingURL=extension.js.map