extension.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 getLanguage(filePath) {
  40. const ext = filePath.split(".").pop() ?? "";
  41. const langMap = {
  42. ts: "typescript",
  43. tsx: "typescriptreact",
  44. js: "javascript",
  45. jsx: "javascriptreact",
  46. json: "json",
  47. md: "markdown",
  48. css: "css",
  49. html: "html",
  50. py: "python",
  51. rs: "rust",
  52. go: "go",
  53. java: "java",
  54. rb: "ruby",
  55. php: "php",
  56. yml: "yaml",
  57. yaml: "yaml",
  58. sh: "bash",
  59. bash: "bash",
  60. sql: "sql",
  61. vue: "vue",
  62. svelte: "svelte"
  63. };
  64. return langMap[ext] ?? ext;
  65. }
  66. function formatFileContent(filePath, content) {
  67. const lang = getLanguage(filePath);
  68. return [
  69. `[${filePath}]`,
  70. "```" + lang,
  71. content,
  72. "```",
  73. ""
  74. ].join("\n");
  75. }
  76. function formatFileSelection(filePath, content, startLine, startCol, endLine, endCol) {
  77. const lang = getLanguage(filePath);
  78. const header = `${filePath} L${startLine}C${startCol}-L${endLine}C${endCol}`;
  79. return [
  80. `[${header}]`,
  81. "```" + lang,
  82. content,
  83. "```",
  84. ""
  85. ].join("\n");
  86. }
  87. function formatMultiFile(entries) {
  88. return entries.map((e) => formatFileContent(e.filePath, e.content)).join("\n");
  89. }
  90. async function copySelection() {
  91. const editor = vscode.window.activeTextEditor;
  92. if (!editor)
  93. return;
  94. const selection = editor.selection;
  95. if (selection.isEmpty)
  96. return;
  97. const document = editor.document;
  98. const filePath = vscode.workspace.asRelativePath(document.uri);
  99. const text = document.getText(selection);
  100. const startLine = selection.start.line + 1;
  101. const startCol = selection.start.character + 1;
  102. const endLine = selection.end.line + 1;
  103. const endCol = selection.end.character + 1;
  104. const result = formatFileSelection(filePath, text, startLine, startCol, endLine, endCol);
  105. await vscode.env.clipboard.writeText(result);
  106. showStatus(`$(clippy) Copied ${filePath}:${startLine}-${endLine}`);
  107. }
  108. async function copyActiveFile() {
  109. const editor = vscode.window.activeTextEditor;
  110. if (!editor)
  111. return;
  112. const document = editor.document;
  113. const filePath = vscode.workspace.asRelativePath(document.uri);
  114. const content = document.getText();
  115. const result = formatFileContent(filePath, content);
  116. await vscode.env.clipboard.writeText(result);
  117. showStatus(`$(clippy) Copied file: ${filePath}`);
  118. }
  119. async function copyWorkspaceFiles() {
  120. const workspaceFolders = vscode.workspace.workspaceFolders;
  121. if (!workspaceFolders || workspaceFolders.length === 0)
  122. return;
  123. const uris = await vscode.window.showOpenDialog({
  124. canSelectFiles: true,
  125. canSelectFolders: false,
  126. canSelectMany: true,
  127. openLabel: "Select files to copy as context"
  128. });
  129. if (!uris || uris.length === 0)
  130. return;
  131. await copyFilesFromUris(uris);
  132. }
  133. async function copySelectedFiles(selectedUris) {
  134. if (!selectedUris || selectedUris.length === 0)
  135. return;
  136. await copyFilesFromUris(selectedUris);
  137. }
  138. async function copyFilesFromUris(uris) {
  139. const entries = [];
  140. for (const uri of uris) {
  141. try {
  142. const doc = await vscode.workspace.openTextDocument(uri);
  143. const filePath = vscode.workspace.asRelativePath(uri);
  144. entries.push({ filePath, content: doc.getText() });
  145. } catch {
  146. }
  147. }
  148. if (entries.length === 0)
  149. return;
  150. const result = formatMultiFile(entries);
  151. await vscode.env.clipboard.writeText(result);
  152. showStatus(`$(clippy) Copied ${entries.length} file(s)`);
  153. }
  154. function showStatus(message) {
  155. vscode.window.setStatusBarMessage(message, 3e3);
  156. }
  157. // src/extension.ts
  158. function activate(context) {
  159. console.log("[vsix-cp-ai-ctx] activating...");
  160. context.subscriptions.push(
  161. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelection", () => {
  162. copySelection();
  163. })
  164. );
  165. context.subscriptions.push(
  166. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyFile", () => {
  167. copyActiveFile();
  168. })
  169. );
  170. context.subscriptions.push(
  171. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyWorkspaceFiles", () => {
  172. copyWorkspaceFiles();
  173. })
  174. );
  175. context.subscriptions.push(
  176. vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelectedFiles", (args) => {
  177. const selectedResources = args?.selectedResources || [];
  178. copySelectedFiles(selectedResources);
  179. })
  180. );
  181. console.log("[vsix-cp-ai-ctx] activated");
  182. }
  183. function deactivate() {
  184. }
  185. // Annotate the CommonJS export names for ESM import in node:
  186. 0 && (module.exports = {
  187. activate,
  188. deactivate
  189. });
  190. //# sourceMappingURL=extension.js.map