How to refactor code in haxe?

One of the most common things in coding is refactoring bad code into better code. Especially in Agile development. How are people doing it in haxe?

When I try to rename a file or class it fails about 80% of the time saying ‘could not find identifier to rename’. When I move files the packages don’t update. Is there a specific way people are doing to refactor?

1 Like

Not aware of refactor in VSC for haxe, usually use ‘Replace in files’ and/or manually.

renaming a file works by doing a rename on the main type name. moving a file to a different package currently needs a rename on package <your.package.name>;. there is probably a hook or callback to subscribe to in order to receive rename or drag&drop events from vscode file explorer, but that’s for future versions to figure out.

your 80% failure rate when trying to rename an identifier seems a bit high. while current rename functionality for sure has some hidden bugs, it should work in a lot of cases. so either your setup needs adjusting (see Rename Symbol · vshaxe/vshaxe Wiki · GitHub) or you are running into a bug that should be investigated in more detail. can you provide sample code where rename operations fail for you?

Dug a little deeper, I’ve added a lib folder that houses all my common library files. I have multiple projects in one VSCode project. It’s my attempt to set up my own personal monorepo. Looking deeper into this, it turns out if I try to rename anything in this lib folder it fails with this error: could not find identifier to rename. It even fails on simple variables in tiny scopes like in functions:
Code_9iKn7lLPjp

Here is my hxml file which should give a better picture of what I’m doing.

# moontools_ecstest.hxml
-cp tests/moontools_ecstest/src
-cp lib # If I try to rename anything in here it doesn't work, anything in the main src is fine though.
-lib heaps
-lib hldx
-lib quadtree
-lib thx.core
-hl tests/moontools_ecstest/game.hl
-D windowTitle=Title
-D windowSize=1280x720
-D resourcesPath=tests/moontools_ecstest/res
-main Main

I have multiple projects that pull from that single lib folder. Of course the reason for this is to save time when refactoring especially with multiple projects. The only issue though is that the refactoring doesn’t work …

Edit: This seems to be the case for anything in the .haxelib folder as well…

from Rename Symbol · vshaxe/vshaxe Wiki · GitHub (linked above)

rename will only look in folders specified via haxe.renameSourceFolders config option (defaults to ["src, "source", "Source", "test", "tests"]), if you use additional source folders, you need to add them, otherwise they won’t be included when renaming

current vshaxe does not support multiroot workspaces, so if you have multiple projects in one workspace you are bound to run into problems. e.g. all compiler services (completion, hover, etc.) will only work for the first Haxe project in your workspace.

I see. Thanks this has been plaguing me for weeks. I thought this was a limitation in the haxe language.

Refactoring is never a language feature: it’s only provided by editors/IDEs.

LSP could also provide such functionalities

it does.
vshaxe / haxe-languageserver currently only supports rename operations so far, but lsp api allows for a whole range of code refatoring actions.

1 Like

80% failure rate for renames? You lucky guy :smiley: . For me it’s more like 99% :smiley: . And yes, my project has files in multiple roots, but renaming fails even in files that are local to a single directory, even when renaming a local function variable. The moment I’m not in the “main” project, everything fails.

I really love vshaxe though. So… what I do is this: I code in VS Code and when I need to refactor, I use the severely out-dated IntelliJ plugin (which can’t comprehend all Haxe syntax, but still can refactor 90% of the situations).

This is where the small Haxe ecosystem really bites us. If we had more projects, bigger companies, stuff like this would have been addressed long ago. But we’re few, and we have to split our time between N projects.

Do you setup haxe.renameSourceFolders for your roots?
And there is zero opened issues in vshaxe repo about this feature, so it seems strange to me saying how nothing works for you, when you can open actual bug report and don’t wait for bigger companies to do it for you.

1 Like

currently vshaxe does not support multi-root workspaces which affects all languageserver services. in general most things will only work for first project in workspace, if at all.
so unless vshaxe learns how to handle multi-root workspaces, developer experience will remain far below that of a single project workspace.

@RblSb First of all, there WAS an issue opened about this, I even commented there ( Rename Symbol method · Issue #188 · vshaxe/vshaxe · GitHub ). I see now the issue was closed, even though, in effect, it is not fully implemented. Secondly, I already talked to the author of the extension at least 2 times about this, including after finding the bug. I even offered financial support should this get fixed somehow. Thirdly, I didn’t say “nothing works for me”. VsHaxe is great, but there’s this major issue with refactoring. Fourthly (!?) ok, you still bring up a good point so… Rename symbol doesn't work for multi-root workspaces · Issue #579 · vshaxe/vshaxe · GitHub

Haxe language server has several issues. Many times I try to go to definition and it goes to the definition of another module . Some other times, I select one auto completion and it fills a different one.

Do you save before sending those requests? (although that shouldn’t be an issue because the current file should be sent in the display request…)

Interesting. I will try to see if it makes a difference