extension.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, content) {
  40. return [
  41. `[${filePath}]`,
  42. content,
  43. ""
  44. ].join("\n");
  45. }
  46. function formatFileSelection(filePath, content, startLine, startCol, endLine, endCol) {
  47. const header = `${filePath} L${startLine}C${startCol}-L${endLine}C${endCol}`;
  48. return [
  49. `[${header}]`,
  50. content,
  51. ""
  52. ].join("\n");
  53. }
  54. function formatMultiFile(entries) {
  55. return entries.map((e) => formatFileContent(e.filePath, e.content)).join(" ");
  56. }
  57. async function copySelection() {
  58. const editor = vscode.window.activeTextEditor;
  59. if (!editor)
  60. return;
  61. const selection = editor.selection;
  62. if (selection.isEmpty)
  63. return;
  64. const document = editor.document;
  65. const filePath = vscode.workspace.asRelativePath(document.uri);
  66. const text = document.getText(selection);
  67. const startLine = selection.start.line + 1;
  68. const startCol = selection.start.character + 1;
  69. const endLine = selection.end.line + 1;
  70. const endCol = selection.end.character + 1;
  71. const result = formatFileSelection(filePath, text, startLine, startCol, endLine, endCol);
  72. await vscode.env.clipboard.writeText(result);
  73. showStatus(`$(clippy) Copied ${filePath}:${startLine}-${endLine}`);
  74. }
  75. async function copyActiveFile() {
  76. const editor = vscode.window.activeTextEditor;
  77. if (!editor)
  78. return;
  79. const document = editor.document;
  80. const filePath = vscode.workspace.asRelativePath(document.uri);
  81. const content = document.getText();
  82. const result = formatFileContent(filePath, content);
  83. await vscode.env.clipboard.writeText(result);
  84. showStatus(`$(clippy) Copied file: ${filePath}`);
  85. }
  86. async function copyWorkspaceFiles() {
  87. const workspaceFolders = vscode.workspace.workspaceFolders;
  88. if (!workspaceFolders || workspaceFolders.length === 0)
  89. return;
  90. const uris = await vscode.window.showOpenDialog({
  91. canSelectFiles: true,
  92. canSelectFolders: false,
  93. canSelectMany: true,
  94. openLabel: "Select files to copy as context"
  95. });
  96. if (!uris || uris.length === 0)
  97. return;
  98. await copyFilesFromUris(uris);
  99. }
  100. async function copySelectedFiles(selectedUris) {
  101. if (!selectedUris || selectedUris.length === 0)
  102. return;
  103. await copyFilesFromUris(selectedUris);
  104. }
  105. async function copyFilesFromUris(uris) {
  106. const entries = [];
  107. for (const uri of uris) {
  108. try {
  109. const doc = await vscode.workspace.openTextDocument(uri);
  110. const filePath = vscode.workspace.asRelativePath(uri);
  111. entries.push({ filePath, content: doc.getText() });
  112. } catch {
  113. }
  114. }
  115. if (entries.length === 0)
  116. return;
  117. const result = formatMultiFile(entries);
  118. await vscode.env.clipboard.writeText(result);
  119. showStatus(`$(clippy) Copied ${entries.length} file(s)`);
  120. }
  121. function showStatus(message) {
  122. vscode.window.setStatusBarMessage(message, 3e3);
  123. }
  124. // src/extension.ts
  125. function activate(context) {
  126. console.log("[vsix-cp-ai-ctx] activating...");
  127. context.subscriptions.push(
  128. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelection", () => {
  129. copySelection();
  130. })
  131. );
  132. context.subscriptions.push(
  133. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyFile", () => {
  134. copyActiveFile();
  135. })
  136. );
  137. context.subscriptions.push(
  138. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyWorkspaceFiles", () => {
  139. copyWorkspaceFiles();
  140. })
  141. );
  142. context.subscriptions.push(
  143. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelectedFiles", (args) => {
  144. const selectedResources = args?.selectedResources || [];
  145. copySelectedFiles(selectedResources);
  146. })
  147. );
  148. console.log("[vsix-cp-ai-ctx] activated");
  149. }
  150. function deactivate() {
  151. }
  152. // Annotate the CommonJS export names for ESM import in node:
  153. 0 && (module.exports = {
  154. activate,
  155. deactivate
  156. });
  157. //# sourceMappingURL=extension.js.map