Jelajahi Sumber

refactor: remove code blocks, use space-separated multi-file format

Developer 1 bulan lalu
induk
melakukan
7238cb62a0
2 mengubah file dengan 164 tambahan dan 45 penghapusan
  1. 160 4
      dist/extension.js
  2. 4 41
      src/copyContext.ts

+ 160 - 4
dist/extension.js

@@ -1,4 +1,160 @@
-"use strict";var $=Object.create;var a=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var P=Object.getPrototypeOf,j=Object.prototype.hasOwnProperty;var k=(t,e)=>{for(var o in e)a(t,o,{get:e[o],enumerable:!0})},u=(t,e,o,c)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of S(e))!j.call(t,n)&&n!==o&&a(t,n,{get:()=>e[n],enumerable:!(c=C(e,n))||c.enumerable});return t};var v=(t,e,o)=>(o=t!=null?$(P(t)):{},u(e||!t||!t.__esModule?a(o,"default",{value:t,enumerable:!0}):o,t)),T=t=>u(a({},"__esModule",{value:!0}),t);var W={};k(W,{activate:()=>M,deactivate:()=>E});module.exports=T(W);var r=v(require("vscode"));var s=v(require("vscode"));function m(t){let e=t.split(".").pop()??"";return{ts:"typescript",tsx:"typescriptreact",js:"javascript",jsx:"javascriptreact",json:"json",md:"markdown",css:"css",html:"html",py:"python",rs:"rust",go:"go",java:"java",rb:"ruby",php:"php",yml:"yaml",yaml:"yaml",sh:"bash",bash:"bash",sql:"sql",vue:"vue",svelte:"svelte"}[e]??e}function g(t,e){let o=m(t);return[`[${t}]`,"```"+o,e,"```",""].join(`
-`)}function L(t,e,o,c,n,i){let p=m(t);return[`[${`${t} L${o}C${c}-L${n}C${i}`}]`,"```"+p,e,"```",""].join(`
-`)}function R(t){return t.map(e=>g(e.filePath,e.content)).join(`
-`)}async function f(){let t=s.window.activeTextEditor;if(!t)return;let e=t.selection;if(e.isEmpty)return;let o=t.document,c=s.workspace.asRelativePath(o.uri),n=o.getText(e),i=e.start.line+1,p=e.start.character+1,l=e.end.line+1,F=e.end.character+1,b=L(c,n,i,p,l,F);await s.env.clipboard.writeText(b),d(`$(clippy) Copied ${c}:${i}-${l}`)}async function x(){let t=s.window.activeTextEditor;if(!t)return;let e=t.document,o=s.workspace.asRelativePath(e.uri),c=e.getText(),n=g(o,c);await s.env.clipboard.writeText(n),d(`$(clippy) Copied file: ${o}`)}async function y(){let t=s.workspace.workspaceFolders;if(!t||t.length===0)return;let e=await s.window.showOpenDialog({canSelectFiles:!0,canSelectFolders:!1,canSelectMany:!0,openLabel:"Select files to copy as context"});!e||e.length===0||await w(e)}async function h(t){!t||t.length===0||await w(t)}async function w(t){let e=[];for(let c of t)try{let n=await s.workspace.openTextDocument(c),i=s.workspace.asRelativePath(c);e.push({filePath:i,content:n.getText()})}catch{}if(e.length===0)return;let o=R(e);await s.env.clipboard.writeText(o),d(`$(clippy) Copied ${e.length} file(s)`)}function d(t){s.window.setStatusBarMessage(t,3e3)}function M(t){console.log("[vsix-cp-ai-ctx] activating..."),t.subscriptions.push(r.commands.registerCommand("vsix-cp-ai-ctx.copySelection",()=>{f()})),t.subscriptions.push(r.commands.registerCommand("vsix-cp-ai-ctx.copyFile",()=>{x()})),t.subscriptions.push(r.commands.registerCommand("vsix-cp-ai-ctx.copyWorkspaceFiles",()=>{y()})),t.subscriptions.push(r.commands.registerCommand("vsix-cp-ai-ctx.copySelectedFiles",e=>{let o=e?.selectedResources||[];h(o)})),console.log("[vsix-cp-ai-ctx] activated")}function E(){}0&&(module.exports={activate,deactivate});
+"use strict";
+var __create = Object.create;
+var __defProp = Object.defineProperty;
+var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
+var __getOwnPropNames = Object.getOwnPropertyNames;
+var __getProtoOf = Object.getPrototypeOf;
+var __hasOwnProp = Object.prototype.hasOwnProperty;
+var __export = (target, all) => {
+  for (var name in all)
+    __defProp(target, name, { get: all[name], enumerable: true });
+};
+var __copyProps = (to, from, except, desc) => {
+  if (from && typeof from === "object" || typeof from === "function") {
+    for (let key of __getOwnPropNames(from))
+      if (!__hasOwnProp.call(to, key) && key !== except)
+        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
+  }
+  return to;
+};
+var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
+  // If the importer is in node compatibility mode or this is not an ESM
+  // file that has been converted to a CommonJS file using a Babel-
+  // compatible transform (i.e. "__esModule" has not been set), then set
+  // "default" to the CommonJS "module.exports" for node compatibility.
+  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
+  mod
+));
+var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
+
+// src/extension.ts
+var extension_exports = {};
+__export(extension_exports, {
+  activate: () => activate,
+  deactivate: () => deactivate
+});
+module.exports = __toCommonJS(extension_exports);
+var vscode2 = __toESM(require("vscode"));
+
+// src/copyContext.ts
+var vscode = __toESM(require("vscode"));
+function formatFileContent(filePath, content) {
+  return [
+    `[${filePath}]`,
+    content,
+    ""
+  ].join("\n");
+}
+function formatFileSelection(filePath, content, startLine, startCol, endLine, endCol) {
+  const header = `${filePath} L${startLine}C${startCol}-L${endLine}C${endCol}`;
+  return [
+    `[${header}]`,
+    content,
+    ""
+  ].join("\n");
+}
+function formatMultiFile(entries) {
+  return entries.map((e) => formatFileContent(e.filePath, e.content)).join(" ");
+}
+async function copySelection() {
+  const editor = vscode.window.activeTextEditor;
+  if (!editor)
+    return;
+  const selection = editor.selection;
+  if (selection.isEmpty)
+    return;
+  const document = editor.document;
+  const filePath = vscode.workspace.asRelativePath(document.uri);
+  const text = document.getText(selection);
+  const startLine = selection.start.line + 1;
+  const startCol = selection.start.character + 1;
+  const endLine = selection.end.line + 1;
+  const endCol = selection.end.character + 1;
+  const result = formatFileSelection(filePath, text, startLine, startCol, endLine, endCol);
+  await vscode.env.clipboard.writeText(result);
+  showStatus(`$(clippy) Copied ${filePath}:${startLine}-${endLine}`);
+}
+async function copyActiveFile() {
+  const editor = vscode.window.activeTextEditor;
+  if (!editor)
+    return;
+  const document = editor.document;
+  const filePath = vscode.workspace.asRelativePath(document.uri);
+  const content = document.getText();
+  const result = formatFileContent(filePath, content);
+  await vscode.env.clipboard.writeText(result);
+  showStatus(`$(clippy) Copied file: ${filePath}`);
+}
+async function copyWorkspaceFiles() {
+  const workspaceFolders = vscode.workspace.workspaceFolders;
+  if (!workspaceFolders || workspaceFolders.length === 0)
+    return;
+  const uris = await vscode.window.showOpenDialog({
+    canSelectFiles: true,
+    canSelectFolders: false,
+    canSelectMany: true,
+    openLabel: "Select files to copy as context"
+  });
+  if (!uris || uris.length === 0)
+    return;
+  await copyFilesFromUris(uris);
+}
+async function copySelectedFiles(selectedUris) {
+  if (!selectedUris || selectedUris.length === 0)
+    return;
+  await copyFilesFromUris(selectedUris);
+}
+async function copyFilesFromUris(uris) {
+  const entries = [];
+  for (const uri of uris) {
+    try {
+      const doc = await vscode.workspace.openTextDocument(uri);
+      const filePath = vscode.workspace.asRelativePath(uri);
+      entries.push({ filePath, content: doc.getText() });
+    } catch {
+    }
+  }
+  if (entries.length === 0)
+    return;
+  const result = formatMultiFile(entries);
+  await vscode.env.clipboard.writeText(result);
+  showStatus(`$(clippy) Copied ${entries.length} file(s)`);
+}
+function showStatus(message) {
+  vscode.window.setStatusBarMessage(message, 3e3);
+}
+
+// src/extension.ts
+function activate(context) {
+  console.log("[vsix-cp-ai-ctx] activating...");
+  context.subscriptions.push(
+    vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelection", () => {
+      copySelection();
+    })
+  );
+  context.subscriptions.push(
+    vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyFile", () => {
+      copyActiveFile();
+    })
+  );
+  context.subscriptions.push(
+    vscode2.commands.registerCommand("vsix-cp-ai-ctx.copyWorkspaceFiles", () => {
+      copyWorkspaceFiles();
+    })
+  );
+  context.subscriptions.push(
+    vscode2.commands.registerCommand("vsix-cp-ai-ctx.copySelectedFiles", (args) => {
+      const selectedResources = args?.selectedResources || [];
+      copySelectedFiles(selectedResources);
+    })
+  );
+  console.log("[vsix-cp-ai-ctx] activated");
+}
+function deactivate() {
+}
+// Annotate the CommonJS export names for ESM import in node:
+0 && (module.exports = {
+  activate,
+  deactivate
+});
+//# sourceMappingURL=extension.js.map

+ 4 - 41
src/copyContext.ts

@@ -1,54 +1,20 @@
 import * as vscode from 'vscode';
 
-/**
- * Infer language identifier from file extension.
- */
-function getLanguage(filePath: string): string {
-  const ext = filePath.split('.').pop() ?? '';
-  const langMap: Record<string, string> = {
-    ts: 'typescript',
-    tsx: 'typescriptreact',
-    js: 'javascript',
-    jsx: 'javascriptreact',
-    json: 'json',
-    md: 'markdown',
-    css: 'css',
-    html: 'html',
-    py: 'python',
-    rs: 'rust',
-    go: 'go',
-    java: 'java',
-    rb: 'ruby',
-    php: 'php',
-    yml: 'yaml',
-    yaml: 'yaml',
-    sh: 'bash',
-    bash: 'bash',
-    sql: 'sql',
-    vue: 'vue',
-    svelte: 'svelte',
-  };
-  return langMap[ext] ?? ext;
-}
-
 /**
  * Format a single file's content as AI context.
- * Output: [path/to/file.ts]\n```lang\ncontent\n```\n
+ * Output: [path/to/file.ts]\ncontent\n
  */
 export function formatFileContent(filePath: string, content: string): string {
-  const lang = getLanguage(filePath);
   return [
     `[${filePath}]`,
-    '```' + lang,
     content,
-    '```',
     '',
   ].join('\n');
 }
 
 /**
  * Format a selection within a file as AI context.
- * Output: [path/to/file.ts LnCn-LnCn]\n```lang\ncontent\n```\n
+ * Output: [path/to/file.ts LnCn-LnCn]\ncontent\n
  */
 export function formatFileSelection(
   filePath: string,
@@ -58,22 +24,19 @@ export function formatFileSelection(
   endLine: number,
   endCol: number,
 ): string {
-  const lang = getLanguage(filePath);
   const header = `${filePath} L${startLine}C${startCol}-L${endLine}C${endCol}`;
   return [
     `[${header}]`,
-    '```' + lang,
     content,
-    '```',
     '',
   ].join('\n');
 }
 
 /**
- * Format multiple file contents, joining with blank lines.
+ * Format multiple file contents, joining with spaces (no newline between files).
  */
 export function formatMultiFile(entries: { filePath: string; content: string }[]): string {
-  return entries.map(e => formatFileContent(e.filePath, e.content)).join('\n');
+  return entries.map(e => formatFileContent(e.filePath, e.content)).join(' ');
 }
 
 /**