-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: implement copy combined markdown on vs code open editor menu for a selection of opened files #3
Comments
That's a great idea. I've been thinking about adding it to the git files list too, so this would be a good thing to do in the same iteration. |
I'm new to typescript and also developing vs code extension so I tried to ask chat GPT to implement the feature in a fork of the repo and even tried some live debbuging myself, but unfortunatelly it seems to be very dificult at least, probably impossible, to access what editors are selected in the openEditors tab/view. What was actually pretty easy to implement was to enable the copy combined markdown to all open editors, but I couldn't manage to create a button in the open editors right click context menu binded to the command. Just to help, here is what I developed using gpt: .\package.json {
"name": "copy-combined-markdown",
"displayName": "Copy Combined Markdown",
"description": "Copy Combined Markdown is a Visual Studio Code plugin that allows you to select multiple files, right-click them, and generate a combined markdown file containing the content of all the selected files. This plugin is designed to provide context for ChatGPT.",
"version": "1.0.1",
"publisher": "skaramicke",
"repository": {
"type": "git",
"url": "https://github.com/skaramicke/vscode-copy-combined-markdown.git"
},
"license": "MIT",
"icon": "assets/icon.png",
"engines": {
"vscode": "^1.85.0"
},
"categories": [
"Other"
],
"activationEvents": [
"onCommand:copy-combined-markdown.copy"
],
"main": "./out/extension.js",
"contributes": {
"commands": [
{
"command": "copy-combined-markdown.copy",
"title": "Copy Combined Markdown",
"category": "Copy Combined Markdown"
},
{
"command": "copy-combined-markdown.copyFromOpenEditors",
"title": "Copy Combined Markdown from Open Editors",
"category": "Copy Combined Markdown"
}
],
"menus": {
"explorer/context": [
{
"command": "copy-combined-markdown.copy",
"group": "navigation"
}
]
}
},
"scripts": {
"vscode:prepublish": "yarn run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"lint": "eslint src --ext ts",
"test": "vscode-test",
"deploy": "vsce publish --yarn"
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "18.x",
"@types/vscode": "^1.85.0",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"@vscode/test-cli": "^0.0.4",
"@vscode/test-electron": "^2.3.8",
"eslint": "^8.56.0",
"typescript": "^5.3.3"
},
"dependencies": {
"@vscode/vsce": "^2.22.0"
}
} .\src\extension.ts import * as vscode from "vscode";
import * as fs from "fs";
const expandDirectories = (fileUris: string[]) => {
let expandedFileUris: string[] = [];
fileUris.forEach((uri) => {
if (fs.lstatSync(uri).isDirectory()) {
const files = fs.readdirSync(uri).map((file) => `${uri}/${file}`);
expandedFileUris = expandedFileUris.concat(expandDirectories(files));
} else {
expandedFileUris.push(uri);
}
});
return expandedFileUris;
};
export function activate(context: vscode.ExtensionContext) {
console.log(
'Congratulations, your extension "copy-combined-markdown" is now active!'
);
let disposable = vscode.commands.registerCommand(
"copy-combined-markdown.copy",
(_selectedFile: vscode.Uri, fileUris: vscode.Uri[]) => {
try {
if (fileUris.length === 0) {
vscode.window.showInformationMessage("No files selected.");
return;
}
const paths = fileUris.map((uri) => uri.fsPath);
// Expand directories recursively
let uniqueFileUris = [...new Set(expandDirectories(paths))];
Promise.all(
uniqueFileUris.map(async (path) => {
if (!fs.lstatSync(path).isDirectory()) {
const languageId = await vscode.workspace
.openTextDocument(path)
.then((doc) => doc.languageId);
const content = fs.readFileSync(path, "utf8");
// Count the longest sequence of '```' in the content
// and add one more to the end of the string
const longestSequence = content
.match(/`{3,}/g)
?.reduce((acc, cur) => {
return cur.length > acc.length ? cur : acc;
});
const numberOfBackticks = longestSequence
? longestSequence.length + 1
: 3;
const backticks = "`".repeat(numberOfBackticks);
// Replace the project root path with a relative path
const workspaceFolders = vscode.workspace.workspaceFolders;
const workspaceFolder = workspaceFolders
? workspaceFolders[0].uri.fsPath
: "";
const relativePath = path.replace(workspaceFolder, ".");
return `${relativePath}\n${backticks}${languageId}\n${content}\n${backticks}\n`;
} else {
return ""; // Return an empty string or handle directories differently if needed
}
})
)
.then((combinedMarkdownArray) => {
const combinedMarkdown = combinedMarkdownArray.join("\n");
vscode.env.clipboard.writeText(combinedMarkdown);
vscode.window.showInformationMessage(
`Combined markdown for ${combinedMarkdownArray.length} file${
combinedMarkdownArray.length !== 1 ? "s" : ""
} copied to clipboard!`
);
})
.catch((error) => {
vscode.window.showErrorMessage(`Error: ${error.message}`);
});
} catch (error: any) {
vscode.window.showErrorMessage(`Error: ${(error as Error).message}`);
}
}
);
// Novo comando para combinar markdown dos arquivos selecionados em "Open Editors"
let copyFromOpenEditors = vscode.commands.registerCommand(
"copy-combined-markdown.copyFromOpenEditors",
async () => {
try {
// Obtém todos os grupos de abas e itera sobre cada grupo e cada aba
const tabGroups = vscode.window.tabGroups.all;
// Array para armazenar o conteúdo combinado
const combinedMarkdownArray: string[] = [];
tabGroups.forEach((group, groupIndex) => {
group.tabs.forEach((tab, tabIndex) => {
// Checamos se a aba tem um documento associado (somente arquivos abertos como editores)
if (tab.input && tab.input instanceof vscode.TabInputText) {
const document = tab.input.uri;
const filePath = document.fsPath;
// Leitura do conteúdo do arquivo para combinar em markdown
const content = fs.readFileSync(filePath, "utf8");
const languageId = vscode.workspace
.openTextDocument(document)
.then((doc) => doc.languageId);
// Conta a sequência mais longa de '```' no conteúdo
const longestSequence = content
.match(/`{3,}/g)
?.reduce(
(acc, cur) => (cur.length > acc.length ? cur : acc),
""
);
const numberOfBackticks = longestSequence
? longestSequence.length + 1
: 3;
const backticks = "`".repeat(numberOfBackticks);
// Caminho relativo
const workspaceFolder =
vscode.workspace.workspaceFolders?.[0].uri.fsPath || "";
const relativePath = filePath.replace(workspaceFolder, ".");
combinedMarkdownArray.push(
`${relativePath}\n${backticks}${languageId}\n${content}\n${backticks}\n`
);
}
});
});
const combinedMarkdown = combinedMarkdownArray.join("\n");
await vscode.env.clipboard.writeText(combinedMarkdown);
vscode.window.showInformationMessage(
`Combined markdown for ${combinedMarkdownArray.length} file(s) copied to clipboard!`
);
} catch (error: any) {
vscode.window.showErrorMessage(`Error: ${(error as Error).message}`);
}
}
);
// Registra o novo comando no contexto da extensão
context.subscriptions.push(copyFromOpenEditors);
context.subscriptions.push(disposable);
}
export function deactivate() {} |
When working in bigger projects, sometimes you set the files you are working on as opened tabs and navigate through them in the "Open Editors" view as the folder structure can be a bit difficult to navigate all the time.
It would be great to have a feature that allows the user to use the "Copy Combined Markdown" on vs code open editor menu for a selection of opened files.
I personally prefer to use the Open Editor view on the secondary side bar (right vertical position), so below is a screenshot of my Open Editor menu justo to help better visualize it.
The text was updated successfully, but these errors were encountered: