Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostboats committed May 31, 2024
1 parent edc5dfe commit 656576a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 30 deletions.
48 changes: 26 additions & 22 deletions commands/insertHandleUUID.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,38 @@ let handleDisposable = vscode.commands.registerCommand('bg3-mod-helper.insertHan
return;
}

const { customWorkspacePath } = getConfig();
const workspaceEdit = new vscode.WorkspaceEdit();

// Generate all handles first
const handleData = await Promise.all(editor.selections.map(selection => {
for (const selection of editor.selections) {
const selectedText = editor.document.getText(selection);
const handle = generateHandle();
return { selection, handle, selectedText };
}));

// Collect all the necessary changes for XML files
let changes = [];
for (const data of handleData) {
changes.push({
handle: data.handle,
text: data.selectedText
const initialHandleValue = selectedText || 'Enter initial handle content here'; // Provide a default or use selected text

const userText = await vscode.window.showInputBox({
value: initialHandleValue,
prompt: "Enter initial value for the handle"
});
}

// Apply edits to the editor first
await editor.edit(editBuilder => {
for (const data of handleData) {
editBuilder.replace(data.selection, data.handle);
if (userText !== undefined) {
const handle = generateHandle();
workspaceEdit.replace(editor.document.uri, selection, handle);

// Prepare changes for localization files
let changes = [{
handle: handle,
text: userText // Using the user-entered text as the handle content
}];

// Update localization files with the handle
await updateLocaXmlFiles(changes);
console.log(`Handle ${handle} created with initial value: ${userText}`);
}
});
}

// Update XML files with all changes (avoid I/O conflicts)
if (changes.length > 0) {
await updateLocaXmlFiles(changes);
if (await vscode.workspace.applyEdit(workspaceEdit)) {
await editor.document.save(); // Save the document after making edits
vscode.window.showInformationMessage('Handles inserted and localization files updated successfully.');
} else {
vscode.window.showErrorMessage('Failed to insert handles.');
}
});

Expand Down
3 changes: 2 additions & 1 deletion extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const debugCommand = require('./commands/debug');
const unpackGameDataCommand = require('./commands/unpackGameData');

const setupFunctionDescriptionHoverProvider = require('./hovers/functionDescriptions');
const setupUuidsHandlesHoverProvider = require('./hovers/uuidsHandlesCollector');
const { setupUuidsHandlesHoverProvider, registerTextEditCommand }= require('./hovers/uuidsHandlesCollector');
const { resizeImageTooltip, resizeImageController, resizeImageHotbar, resizeImageCustom } = require('./commands/resizeImage');

const { getFullPath, saveConfigFile, loadConfigFile } = require('./support_files/helper_functions');
Expand Down Expand Up @@ -135,6 +135,7 @@ function activate(context) {
));

let uuidsHandlesHoverProvider = setupUuidsHandlesHoverProvider();
let uuidsHandlesHoverProviderr = registerTextEditCommand();

let functionsHoverProvider = setupFunctionDescriptionHoverProvider();

Expand Down
58 changes: 55 additions & 3 deletions hovers/uuidsHandlesCollector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const vscode = require('vscode');
const path = require('path');
const { findInstancesInWorkspace } = require('../support_files/helper_functions'); // Adjust the path as necessary
const { getConfig } = require('../support_files/config'); // Adjust the path as necessary

Expand Down Expand Up @@ -52,8 +53,9 @@ function setupUuidsHandlesHoverProvider() {
if (normalizedPath.includes('localization/')) {
// Process as a localization-related file
const contentMatch = lineContent.match(/<content[^>]*>(.*?)<\/content>/);
let highlightedLineContent = contentMatch ? contentMatch[1] : '';
currentLine = `Loca Content: ***${highlightedLineContent}*** \nFile: [${relativePath}](${openFileCommandUri}) \n--- \n`;
let highlightedLineContent = contentMatch ? escapeHtml(contentMatch[1]) : '';
const commandUri = `command:extension.editText?${encodeURIComponent(JSON.stringify({ word: highlightedLineContent, range: { lineNum, path: relativePath } }))}`;
currentLine = `Loca Content: ***[${highlightedLineContent}](${commandUri})***\nFile: [${relativePath}](command:extension.openFileAtLine?${encodeURIComponent(JSON.stringify({ relativePath, lineNum }))})\n---\n`;
} else {
let modifiedLineContent = lineContent.replace(/^<\/?|\/?>$/g, '');
let highlightedLineContent = modifiedLineContent.replace(/(id="[^"]*")/g, '**$1**');
Expand Down Expand Up @@ -83,4 +85,54 @@ function setupUuidsHandlesHoverProvider() {
});
}

module.exports = setupUuidsHandlesHoverProvider;
function registerTextEditCommand() {
vscode.commands.registerCommand('extension.editText', async ({ word, range }) => {
const workspaceRoot = vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0].uri.fsPath : '';
const fullPath = path.resolve(workspaceRoot, range.path);

try {
// Open the document in the background without displaying it
const documentUri = vscode.Uri.file(fullPath);
const document = await vscode.workspace.openTextDocument(documentUri);

const lineText = document.lineAt(range.lineNum - 1).text;
const startIndex = lineText.indexOf(word);

if (startIndex === -1) {
console.error(`Word '${word}' not found in line ${range.lineNum - 1}: '${lineText}'`);
vscode.window.showErrorMessage('Could not find the specified text to replace.');
return;
}

const endIndex = startIndex + word.length;
const replacementRange = new vscode.Range(range.lineNum - 1, startIndex, range.lineNum - 1, endIndex);

const userText = await vscode.window.showInputBox({
value: word,
prompt: "Edit text"
});

if (userText !== undefined) {
// Apply the edit directly using workspace.applyEdit
const workspaceEdit = new vscode.WorkspaceEdit();
workspaceEdit.replace(documentUri, replacementRange, userText);

await vscode.workspace.applyEdit(workspaceEdit);
await document.save(); // Save the document immediately after the edit

console.log(`Text replacement successful for '${word}' with '${userText}' in ${fullPath}`);
vscode.window.showInformationMessage('Text replaced successfully.');
}
} catch (error) {
console.error(`Error handling text replacement: ${error}`);
vscode.window.showErrorMessage('Error replacing text: ' + error.toString());
}
});
}





module.exports = { setupUuidsHandlesHoverProvider, registerTextEditCommand };

2 changes: 1 addition & 1 deletion node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "bg3_mod_helper",
"publisher": "ghostboats",
"description": "This extension is designed to help you make mods in Baldur's Gate 3 by creating UUIDs and handles for you, as well as updating your .loca.xml files as well should they exist. And more to come in the future.",
"version": "2.2.3",
"version": "2.2.31",
"icon": "media/marketplace_icon.png",
"engines": {
"vscode": "^1.86.0"
Expand Down

0 comments on commit 656576a

Please sign in to comment.