waldo1001 / crs-al-language-extension

Make working with the (Dynamics NAV / 365) AL Language easier and more efficient.
MIT License
34 stars 43 forks source link

Rename on File Save #207

Open HenrikHolmIT opened 3 years ago

HenrikHolmIT commented 3 years ago

Hello.

Developer of a other VSCode extension here.

We are experiencing a funky problem. We are using your extension to do rename new files according to objectname and type.

Problem happens on onDidSaveTextDocument. Our extension is also activated when file is saved where we try to do some manipulation of the content.

This presents us with a problem. Sometimes VSCode activates your eventhandler before our eventhandler. There seems to be no pattern here.

We are experiencing 2 issues.

Case 1: If your extension is activated first then when our extension is activated the fil on the path provided my the event no longer exists. You've renamed the file and the old file is deleted.

Case 2: If our extension is activated first then we start manipulating the file. It seems that sometimes your extension is activated in the middle of us processing of the file. This results in now having 2 files say temp.al and MyPage.Page.al. I believe this is caused because temp.al is open. You can copy it but you cannot delete it. Or FS cannot delete it I should say.

waldo1001 commented 3 years ago

Hm, not sure what I can do about that. Other than may be a suggestion ...

HenrikHolmIT commented 3 years ago

That could be used. Only we have a ton of AL Extensions. Obviously we could make our extension update all settings.json files as they are opened and change said setting.

I proposed a fix to the VSCode repository. Solution could be that extensions that change file names should report back to VSCode somehow that a file was in fact renamed and what the new filename is. Then other extensions also listening on that event would get the new name of the file.

If my proposal to VSCode is accepted would you be interested in updating CRS in a way that would then inform VSCode of the name change?

My suggesten is to have a function on the event something like event.fileWasRenamed(string newFilename)

I have no idea if they are even be interested in implementing something like this or if they would implement it completely differently or if they reject the issue all together.

waldo1001 commented 3 years ago

Of course I would :-).

HenrikHolmIT commented 3 years ago

Thanks. Ill get back to this issue if the VSCode team picks up my suggestion or some variant of it

HenrikHolmIT commented 3 years ago

Hm, not sure what I can do about that. Other than may be a suggestion ...

  • don't use the event for my extension "CRS.OnSaveAlFileAction": "DoNothing"
  • run my rename (crs.RenameCurrentFile) after you've done your changes?

This works perfect. This is the solution we're going for. Now to scan all projects and turn the setting off :)

waldo1001 commented 3 years ago

Great. You could simply do that in the activate-function (extension.ts) ..

HenrikHolmIT commented 3 years ago

Absolutely will. We are already scanning AL files and collecting Object Type and Object IDs for all of them on activate so will only be natural to put it in there. Almost already have a solution.

HenrikHolmIT commented 3 years ago

Sorry to bother you with this one, Waldo. It would seem like a simple task but for some reason I cannot get the setting change to propagate to the settings.json file.

I've created a config object and can read the Rename value. If I change it manually it will read the change. If I try to change it from code it just stays what ever value was last manually set.

I've tried settings the Target parameter but that does nothing. Global, workspace or workspace folder.

private static setSetting(config:vscode.WorkspaceConfiguration, key:string, value:string) { if (config.has(key)) { config.update(key, value); } }

this.setSetting(config, 'OnSaveAlFileAction', 'DoNothing');

HenrikHolmIT commented 3 years ago

Arh. I have gotten it.

Update needs to have "CRS." on the key parameter: "CRS.OnSaveAlFileAction"

HenrikHolmIT commented 3 years ago

And it worked for 2 minutes then stopped. All workspaces are now DoNothing (was Rename) but now when I try to trick the setting as a final developer test it just stays at Rename!

private static setSetting(config:vscode.WorkspaceConfiguration) { if (config.has(this.OnSaveAlFileAction)) { config.update("CRS.OnSaveAlFileAction", "DoNothing"); } }