Skip to content

Commit

Permalink
Merge pull request #49 from AtilioA/fix/dont-unconditionally-add-hand…
Browse files Browse the repository at this point in the history
…le-to-all-locas

Refactor handle insertion logic with new `addHandlesToAllLocas` extension setting
  • Loading branch information
ghostboats authored May 19, 2024
2 parents e898b79 + 8db188a commit 38d72f2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode/settings.json
*.vsix
33 changes: 28 additions & 5 deletions commands/insertHandleUUID.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { v4: uuidv4 } = require('uuid');
const { insertText } = require('../support_files/helper_functions');
const { getConfig } = require('../support_files/config');


let uuidDisposable = vscode.commands.registerCommand('bg3-mod-helper.insertUUID', function () {
insertText(uuidv4());
});
Expand Down Expand Up @@ -49,6 +48,8 @@ let handleDisposable = vscode.commands.registerCommand('bg3-mod-helper.insertHan

// Function to update all .loca.xml files in the workspace with the given changes
async function updateLocaXmlFiles(changes) {
const { addHandlesToAllLocas } = getConfig();

const activeFilePath = vscode.window.activeTextEditor?.document.uri.fsPath;
if (!activeFilePath) {
return;
Expand All @@ -70,13 +71,35 @@ async function updateLocaXmlFiles(changes) {
return;
}

let selectedLocaFiles = locaFiles;
// If user doesn't want to add handles to all loca files, prompt for selection from the list of all loca files
if (!addHandlesToAllLocas) {
const fileItems = locaFiles.map(file => ({
label: path.basename(file.fsPath),
description: path.relative(workspaceFolder.uri.fsPath, file.fsPath),
fileUri: file
}));

const selectedItems = await vscode.window.showQuickPick(fileItems, {
canPickMany: true,
placeHolder: 'Select .loca.xml files to update with handles'
});

if (!selectedItems || selectedItems.length === 0) {
vscode.window.showInformationMessage('No .loca.xml files selected. No handles were added to any files.');
return;
}

selectedLocaFiles = selectedItems.map(item => item.fileUri);
}

const edit = new vscode.WorkspaceEdit();

let nUpdatedFiles = 0;
for (const locaFile of locaFiles) {
for (const locaFile of selectedLocaFiles) {
try {
await updateLocaXmlFile(locaFile, changes, edit);
nUpdatedFiles += 1
nUpdatedFiles += 1;
} catch (error) {
console.error(error);
}
Expand All @@ -86,7 +109,7 @@ async function updateLocaXmlFiles(changes) {
if (!success) {
vscode.window.showErrorMessage('Failed to update loca .xml files.');
} else {
vscode.window.showInformationMessage(`Handles were added to ${nUpdatedFiles} loca .xml files.`)
vscode.window.showInformationMessage(`Handles were added to ${nUpdatedFiles} loca .xml files.`);
}
});
}
Expand All @@ -111,7 +134,7 @@ async function updateLocaXmlFile(locaFileUri, changes, edit) {
}

function generateContent(handle, handleContent) {
return ` <content contentuid="${handle}" version="1">${handleContent.trim()}</content>\n`
return ` <content contentuid="${handle}" version="1">${handleContent.trim()}</content>\n`;
}

function generateHandle() {
Expand Down
11 changes: 6 additions & 5 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function activate(context) {
const config = vscode.workspace.getConfiguration('bg3ModHelper');
config.update('rootModPath', mainFolderPath, vscode.ConfigurationTarget.Workspace
).then(() => {
vscode.window.showInformationMessage(`Workspace set to:
vscode.window.showInformationMessage(`Workspace set to:
${mainFolderPath}.`,
'Open Settings'
).then(selection => {
Expand All @@ -93,7 +93,7 @@ function activate(context) {
}

let config = vscode.workspace.getConfiguration('bg3ModHelper');

setConfig({
maxFilesToShow: config.get('hover.maxFiles'),
hoverEnabled: config.get('hover.enabled'),
Expand All @@ -104,6 +104,7 @@ function activate(context) {
lslibPath: config.get('lslibPath'),
autoLaunchOnPack: config.get('autoLaunchOnPack'),
launchContinueGame: config.get('launchContinueGame'),
addHandlesToAllLocas: config.get('addHandlesToAllLocas'),
excludedFiles: config.get('excludedFiles') || []
});
bg3mh_logger.info('Initial configs set:' + JSON.stringify(config, null, 2))
Expand All @@ -118,7 +119,7 @@ function activate(context) {
// Register the command to open file at a specific line
context.subscriptions.push(vscode.commands.registerCommand('extension.openFileAtLine', ({ relativePath, lineNum }) => {
const fullPath = getFullPath(relativePath)

const uri = vscode.Uri.file(fullPath);
vscode.window.showTextDocument(uri, { preview: false }).then(editor => {
const line = parseInt(lineNum, 10) - 1; // Convert line number to zero-based index
Expand All @@ -130,8 +131,8 @@ function activate(context) {

// Register autocomplete provider for text files within 'Generated' folders
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
{ scheme: 'file', pattern: '**/Generated/**/*.txt' },
new AutoCompleteProvider(),
{ scheme: 'file', pattern: '**/Generated/**/*.txt' },
new AutoCompleteProvider(),
'"' // Trigger completion when `"` is typed
));

Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@
"items": {
"type": "string"
}
},
"bg3ModHelper.addHandlesToAllLocas": {
"type": "boolean",
"default": true,
"description": "When generating handles, add them to all loca files. If disabled, prompts for which files to add handles to."
}
}
},
Expand Down

0 comments on commit 38d72f2

Please sign in to comment.