Skip to content

Commit 562f545

Browse files
committed
Fix updating the loader.lgt and tester.lgt files when moving a file to a subdirectory
1 parent 0995523 commit 562f545

3 files changed

Lines changed: 38 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [0.85.1]
4+
5+
* Fix updating the `loader.lgt` and `tester.lgt` files when moving a file to a subdirectory
6+
37
## [0.85.0]
48

59
* Add "Go to Definition" support for file paths in `logtalk_load/1-2` calls that don't use library notation

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "logtalk-for-vscode",
33
"displayName": "Logtalk for VSCode",
44
"description": "Logtalk programming support",
5-
"version": "0.85.0",
5+
"version": "0.85.1",
66
"publisher": "LogtalkDotOrg",
77
"icon": "images/logtalk.png",
88
"license": "MIT",
@@ -1338,7 +1338,7 @@
13381338
"compile": "tsc -watch -p ./",
13391339
"test": "tsc ./tests/runTest.ts",
13401340
"vsix:make": "vsce package --baseImagesUrl https://raw.githubusercontent.com/llvm/llvm-project/master/clang-tools-extra/clangd/clients/clangd-vscode/",
1341-
"vsix:install": "code --install-extension logtalk-for-vscode-0.85.0.vsix"
1341+
"vsix:install": "code --install-extension logtalk-for-vscode-0.85.1.vsix"
13421342
},
13431343
"devDependencies": {
13441344
"@types/bluebird": "^3.5.38",

src/utils/fileRenameHandler.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,61 +38,71 @@ export class FileRenameHandler {
3838
private static async propagateFileRenameInternal(oldUri: Uri, newUri: Uri): Promise<WorkspaceEdit | null> {
3939
const oldPath = oldUri.fsPath;
4040
const newPath = newUri.fsPath;
41-
41+
4242
// Get file names without extensions
4343
const oldBaseName = path.basename(oldPath, path.extname(oldPath));
4444
const newBaseName = path.basename(newPath, path.extname(newPath));
45-
45+
4646
// Skip if renaming loader.lgt or tester.lgt themselves
4747
const oldFileName = path.basename(oldPath);
48-
if (oldFileName === 'loader.lgt' || oldFileName === 'loader.logtalk' ||
48+
if (oldFileName === 'loader.lgt' || oldFileName === 'loader.logtalk' ||
4949
oldFileName === 'tester.lgt' || oldFileName === 'tester.logtalk') {
5050
this.logger.debug(`Skipping rename propagation for ${oldFileName}`);
5151
return null;
5252
}
53-
53+
5454
// Get the directory containing the renamed file
5555
const directory = path.dirname(oldPath);
56-
56+
5757
// Check if the new file is in a different directory
5858
const newDirectory = path.dirname(newPath);
5959
const isSameDirectory = directory === newDirectory;
60-
61-
this.logger.debug(`Propagating rename from ${oldBaseName} to ${newBaseName} in directory ${directory}`);
62-
60+
61+
// Compute the relative path from old directory to new file (without extension)
62+
// This is used when updating references in the old directory's loader/tester files
63+
let newRelativePath = newBaseName;
64+
if (!isSameDirectory) {
65+
// Get the relative path from the old directory to the new directory
66+
// Always use forward slashes for Logtalk path notation
67+
const relativeDir = path.relative(directory, newDirectory).replace(/\\/g, '/');
68+
newRelativePath = relativeDir + '/' + newBaseName;
69+
}
70+
71+
this.logger.debug(`Propagating rename from ${oldBaseName} to ${newRelativePath} in directory ${directory}`);
72+
6373
const workspaceEdit = new WorkspaceEdit();
6474
let hasChanges = false;
65-
75+
6676
// Find and update loader.lgt and tester.lgt files
6777
const loaderFiles = ['loader.lgt', 'loader.logtalk'];
6878
const testerFiles = ['tester.lgt', 'tester.logtalk'];
69-
79+
7080
// Update loader files in the old directory
7181
for (const loaderFile of loaderFiles) {
7282
const loaderPath = path.join(directory, loaderFile);
7383
if (fs.existsSync(loaderPath)) {
74-
const edits = await this.updateFileReferences(loaderPath, oldBaseName, newBaseName, isSameDirectory);
84+
const edits = await this.updateFileReferences(loaderPath, oldBaseName, newRelativePath, isSameDirectory);
7585
if (edits.length > 0) {
7686
workspaceEdit.set(Uri.file(loaderPath), edits);
7787
hasChanges = true;
7888
this.logger.debug(`Added ${edits.length} edits to ${loaderFile}`);
7989
}
8090
}
8191
}
82-
92+
8393
// Update tester files in the old directory
8494
for (const testerFile of testerFiles) {
8595
const testerPath = path.join(directory, testerFile);
8696
if (fs.existsSync(testerPath)) {
87-
const edits = await this.updateFileReferences(testerPath, oldBaseName, newBaseName, isSameDirectory);
97+
const edits = await this.updateFileReferences(testerPath, oldBaseName, newRelativePath, isSameDirectory);
8898
if (edits.length > 0) {
8999
workspaceEdit.set(Uri.file(testerPath), edits);
90100
hasChanges = true;
91101
this.logger.debug(`Added ${edits.length} edits to ${testerFile}`);
92102
}
93103
}
94104
}
95-
105+
96106
// If file was moved to a different directory, also update loader/tester in new directory
97107
if (!isSameDirectory) {
98108
// Update loader files in the new directory
@@ -108,7 +118,7 @@ export class FileRenameHandler {
108118
}
109119
}
110120
}
111-
121+
112122
// Update tester files in the new directory
113123
for (const testerFile of testerFiles) {
114124
const testerPath = path.join(newDirectory, testerFile);
@@ -123,7 +133,7 @@ export class FileRenameHandler {
123133
}
124134
}
125135
}
126-
136+
127137
return hasChanges ? workspaceEdit : null;
128138
}
129139

@@ -310,12 +320,16 @@ export class FileRenameHandler {
310320
const hasExtension = match[2] !== undefined; // Group 2 captures the extension
311321
const extension = hasExtension ? match[2] : 'lgt';
312322

313-
if (quoteStyle === 'single') {
323+
// If the new path contains a slash (file moved to subdirectory), it must be quoted
324+
const needsQuotes = newBaseName.includes('/');
325+
326+
if (quoteStyle === 'single' || (quoteStyle === 'none' && needsQuotes)) {
327+
// Use single quotes for paths with slashes, or if original was single-quoted
314328
replacement = hasExtension ? `'${newBaseName}.${extension}'` : `'${newBaseName}'`;
315329
} else if (quoteStyle === 'double') {
316330
replacement = hasExtension ? `"${newBaseName}.${extension}"` : `"${newBaseName}"`;
317331
} else {
318-
// Unquoted
332+
// Unquoted (only when no slashes in path)
319333
replacement = hasExtension ? `${newBaseName}.${extension}` : newBaseName;
320334
}
321335

0 commit comments

Comments
 (0)