vscode正则管理插件
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
let tagList = []; // 存储标签信息
// 激活插件时加载存储的标签信息
function loadTagList() {
const filePath = path.join(vscode.workspace.rootPath, 'tags.json');
if (fs.existsSync(filePath)) {
const data = fs.readFileSync(filePath, 'utf-8');
tagList = JSON.parse(data);
}
}
// 保存标签信息
function saveTagList() {
const filePath = path.join(vscode.workspace.rootPath, 'tags.json');
fs.writeFileSync(filePath, JSON.stringify(tagList, null, 2));
vscode.window.showInformationMessage('Tags saved successfully!');
}
// 打开Tag Manager
function openTagManager() {
const panel = vscode.window.createWebviewPanel(
'tagManager',
'Tag Manager',
vscode.ViewColumn.One,
{}
);
panel.webview.html = getWebviewContent();
panel.webview.onDidReceiveMessage(
(message) => {
switch (message.command) {
case 'add':
addTag(message.tag);
break;
case 'remove':
removeTag(message.index);
break;
case 'edit':
editTag(message.index, message.updatedTag);
break;
case 'save':
saveTagList(); // 用户点击保存
break;
}
},
undefined,
[]
);
}
// 获取网页内容,显示标签列表和添加/删除功能
function getWebviewContent() {
return `
<html>
<body>
<h1>Tag Manager</h1>
<button onclick="addTag()">Add Tag</button>
<ul id="tagList"></ul>
<script>
const vscode = acquireVsCodeApi();
function loadTagList() {
const tags = ${JSON.stringify(tagList)};
const listElement = document.getElementById('tagList');
listElement.innerHTML = '';
tags.forEach((tag, index) => {
const listItem = document.createElement('li');
listItem.innerHTML = \`<input type="text" value="\${tag.name}" onchange="editTagName(\${index}, this.value)" /> -
<input type="text" value="\${tag.tag}" onchange="editTagTag(\${index}, this.value)" /> -
<input type="text" value="\${tag.description}" onchange="editTagDescription(\${index}, this.value)" />
<button onclick="removeTag(\${index})">Remove</button>\`;
listElement.appendChild(listItem);
});
}
function addTag() {
const name = prompt('Enter tag name');
const tag = prompt('Enter tag');
const description = prompt('Enter tag description');
if (name && tag) {
vscode.postMessage({ command: 'add', tag: { name, tag, description } });
}
}
function editTagName(index, value) {
updateTag(index, 'name', value);
}
function editTagTag(index, value) {
updateTag(index, 'tag', value);
}
function editTagDescription(index, value) {
updateTag(index, 'description', value);
}
function updateTag(index, field, value) {
const updatedTag = {...tags[index], [field]: value};
vscode.postMessage({ command: 'edit', index, updatedTag });
}
function removeTag(index) {
vscode.postMessage({ command: 'remove', index });
}
window.onload = loadTagList;
</script>
</body>
</html>
`;
}
// 增加标签
function addTag(tag) {
tagList.push(tag);
saveTagList();
vscode.window.showInformationMessage(`Added tag: ${tag.name}`);
}
// 删除标签
function removeTag(index) {
tagList.splice(index, 1);
saveTagList();
vscode.window.showInformationMessage(`Removed tag at index: ${index}`);
}
// 编辑标签
function editTag(index, updatedTag) {
tagList[index] = updatedTag;
saveTagList();
vscode.window.showInformationMessage(`Edited tag: ${updatedTag.name}`);
}
// 从选中的文本中添加标签
function addTagFromSelection() {
const editor = vscode.window.activeTextEditor;
if (editor) {
const selection = editor.selection;
const selectedText = editor.document.getText(selection);
if (selectedText) {
const name = `Tag from ${selectedText.substring(0, 20)}`; // 根据选中的文本生成标签名称
const tag = selectedText; // 使用选中的文本作为标签
const description = `Description for ${selectedText.substring(0, 20)}`;
addTag({ name, tag, description });
vscode.window.showInformationMessage(`Added tag from selection: ${name}`);
} else {
vscode.window.showWarningMessage('No text selected.');
}
}
}
// 导入标签数据,进行增量导入
function importTags(tags) {
tags.forEach(newTag => {
// 如果tag已存在,不进行更新或删除
if (!tagList.some(tag => tag.tag === newTag.tag)) {
tagList.push(newTag);
}
});
saveTagList();
vscode.window.showInformationMessage('Tags imported successfully!');
}
// 保存标签
function saveTags() {
saveTagList();
}
function activate(context) {
loadTagList();
let openCommand = vscode.commands.registerCommand('extension.openTagManager', openTagManager);
let addTagFromSelectionCommand = vscode.commands.registerCommand('extension.addTagFromSelection', addTagFromSelection);
let saveTagsCommand = vscode.commands.registerCommand('extension.saveTags', saveTags);
context.subscriptions.push(openCommand);
context.subscriptions.push(addTagFromSelectionCommand);
context.subscriptions.push(saveTagsCommand);
}
function deactivate() {}
module.exports = {
activate,
deactivate
};
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
let tagList = []; // 存储标签信息
// 激活插件时加载存储的标签信息
function loadTagList() {
const filePath = path.join(vscode.workspace.rootPath, 'tags.json');
if (fs.existsSync(filePath)) {
const data = fs.readFileSync(filePath, 'utf-8');
tagList = JSON.parse(data);
}
}
// 保存标签信息
function saveTagList() {
const filePath = path.join(vscode.workspace.rootPath, 'tags.json');
fs.writeFileSync(filePath, JSON.stringify(tagList, null, 2));
vscode.window.showInformationMessage('Tags saved successfully!');
}
// 打开Tag Manager
function openTagManager() {
const panel = vscode.window.createWebviewPanel(
'tagManager',
'Tag Manager',
vscode.ViewColumn.One,
{}
);
panel.webview.html = getWebviewContent();
panel.webview.onDidReceiveMessage(
message => {
switch (message.command) {
case 'add':
addTag(message.tag);
break;
case 'remove':
removeTag(message.index);
break;
case 'edit':
editTag(message.index, message.updatedTag);
break;
case 'save':
saveTagList(); // 用户点击保存
break;
}
},
undefined,
[]
);
}
// 获取网页内容,显示标签列表和添加/删除功能
function getWebviewContent() {
return `
<html>
<body>
<h1>Tag Manager</h1>
<button onclick="addTag()">Add Tag</button>
<ul id="tagList"></ul>
<script>
const vscode = acquireVsCodeApi();
function loadTagList() {
const tags = ${JSON.stringify(tagList)};
const listElement = document.getElementById('tagList');
listElement.innerHTML = '';
tags.forEach((tag, index) => {
const listItem = document.createElement('li');
listItem.innerHTML = \`<input type="text" value="\${tag.name}" onchange="editTagName(\${index}, this.value)" /> -
<input type="text" value="\${tag.tag}" onchange="editTagTag(\${index}, this.value)" /> -
<input type="text" value="\${tag.description}" onchange="editTagDescription(\${index}, this.value)" />
<button onclick="removeTag(\${index})">Remove</button>\`;
listElement.appendChild(listItem);
});
}
function addTag() {
const name = prompt('Enter tag name');
const tag = prompt('Enter tag');
const description = prompt('Enter tag description');
if (name && tag) {
vscode.postMessage({ command: 'add', tag: { name, tag, description } });
}
}
function editTagName(index, value) {
updateTag(index, 'name', value);
}
function editTagTag(index, value) {
updateTag(index, 'tag', value);
}
function editTagDescription(index, value) {
updateTag(index, 'description', value);
}
function updateTag(index, field, value) {
const updatedTag = {...tags[index], [field]: value};
vscode.postMessage({ command: 'edit', index, updatedTag });
}
function removeTag(index) {
vscode.postMessage({ command: 'remove', index });
}
window.onload = loadTagList;
</script>
</body>
</html>
`;
}
// 增加标签
function addTag(tag) {
tagList.push(tag);
saveTagList();
vscode.window.showInformationMessage(`Added tag: ${tag.name}`);
}
// 删除标签
function removeTag(index) {
tagList.splice(index, 1);
saveTagList();
vscode.window.showInformationMessage(`Removed tag at index: ${index}`);
}
// 编辑标签
function editTag(index, updatedTag) {
tagList[index] = updatedTag;
saveTagList();
vscode.window.showInformationMessage(`Edited tag: ${updatedTag.name}`);
}
// 从选中的文本中添加标签
function addTagFromSelection() {
const editor = vscode.window.activeTextEditor;
if (editor) {
const selection = editor.selection;
const selectedText = editor.document.getText(selection);
if (selectedText) {
const name = `Tag from ${selectedText.substring(0, 20)}`; // 根据选中的文本生成标签名称
const tag = selectedText; // 使用选中的文本作为标签
const description = `Description for ${selectedText.substring(0, 20)}`;
addTag({ name, tag, description });
vscode.window.showInformationMessage(`Added tag from selection: ${name}`);
} else {
vscode.window.showWarningMessage('No text selected.');
}
}
}
// 导入标签数据,进行增量导入
function importTags(tags) {
tags.forEach(newTag => {
// 如果tag已存在,不进行更新或删除
if (!tagList.some(tag => tag.tag === newTag.tag)) {
tagList.push(newTag);
}
});
saveTagList();
vscode.window.showInformationMessage('Tags imported successfully!');
}
// 保存标签
function saveTags() {
saveTagList();
}
function activate(context) {
loadTagList();
let openCommand = vscode.commands.registerCommand('extension.openTagManager', openTagManager);
let addTagFromSelectionCommand = vscode.commands.registerCommand('extension.addTagFromSelection', addTagFromSelection);
let saveTagsCommand = vscode.commands.registerCommand('extension.saveTags', saveTags);
context.subscriptions.push(openCommand);
context.subscriptions.push(addTagFromSelectionCommand);
context.subscriptions.push(saveTagsCommand);
}
function deactivate() {}
module.exports = {
activate,
deactivate
};