# How to refactor code in haxe?

**URL:** https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905
**Category:** Haxe
**Created:** [March 20, 2023, 12:50am UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905 "2023-03-20T00:50:54Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![distantforest](https://avatars.discourse-cdn.com/v2/letter/d/58956e/32.png) [@distantforest](https://community.haxe.org/u/distantforest)
#### Post date: [March 20, 2023, 12:50am UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/1 "2023-03-20T00:50:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![nanjizal](https://community.haxe.org/user_avatar/community.haxe.org/nanjizal/32/239_2.png) [@nanjizal](https://community.haxe.org/u/nanjizal)
#### Post date: [March 20, 2023, 3:31am UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/2 "2023-03-20T03:31:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ablum](https://community.haxe.org/user_avatar/community.haxe.org/ablum/32/415_2.png) [@ablum](https://community.haxe.org/u/ablum)
#### Post date: [March 20, 2023, 2:04pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/3 "2023-03-20T14:04:45Z")

</div>

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](https://github.com/vshaxe/vshaxe/wiki/Rename-Symbol)) 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?

---

<div class="post-metadata">

### Author: ![distantforest](https://avatars.discourse-cdn.com/v2/letter/d/58956e/32.png) [@distantforest](https://community.haxe.org/u/distantforest)
#### Post date: [March 20, 2023, 3:45pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/4 "2023-03-20T15:45:03Z")

</div>

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](https://community.haxe.org/uploads/default/original/2X/f/fcb2f91766ee545e7893e9e070592bcfc58521be.gif)

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

```haxe
# 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…

---

<div class="post-metadata">

### Author: ![ablum](https://community.haxe.org/user_avatar/community.haxe.org/ablum/32/415_2.png) [@ablum](https://community.haxe.org/u/ablum)
#### Post date: [March 20, 2023, 4:49pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/5 "2023-03-20T16:49:16Z")

</div>

from [Rename Symbol · vshaxe/vshaxe Wiki · GitHub](https://github.com/vshaxe/vshaxe/wiki/Rename-Symbol) (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.

---

<div class="post-metadata">

### Author: ![distantforest](https://avatars.discourse-cdn.com/v2/letter/d/58956e/32.png) [@distantforest](https://community.haxe.org/u/distantforest)
#### Post date: [March 20, 2023, 11:08pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/6 "2023-03-20T23:08:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cedx](https://community.haxe.org/user_avatar/community.haxe.org/cedx/32/2377_2.png) [@cedx](https://community.haxe.org/u/cedx)
#### Post date: [March 21, 2023, 5:49pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/7 "2023-03-21T17:49:01Z")

</div>

> [@distantforest](#):
>
> I thought this was a limitation in the haxe language.

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

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [March 23, 2023, 9:18pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/8 "2023-03-23T21:18:22Z")

</div>

LSP could also provide such functionalities

---

<div class="post-metadata">

### Author: ![ablum](https://community.haxe.org/user_avatar/community.haxe.org/ablum/32/415_2.png) [@ablum](https://community.haxe.org/u/ablum)
#### Post date: [March 23, 2023, 10:41pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/9 "2023-03-23T22:41:44Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Kyliathy](https://community.haxe.org/user_avatar/community.haxe.org/kyliathy/32/452_2.png) [@Kyliathy](https://community.haxe.org/u/Kyliathy)
#### Post date: [April 5, 2023, 10:47pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/10 "2023-04-05T22:47:46Z")

</div>

80% failure rate for renames? You lucky guy 😃 . For me it’s more like 99% 😃 . 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.

---

<div class="post-metadata">

### Author: ![RblSb](https://avatars.discourse-cdn.com/v2/letter/r/edb3f5/32.png) [@RblSb](https://community.haxe.org/u/RblSb)
#### Post date: [April 6, 2023, 1:20am UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/11 "2023-04-06T01:20:20Z")

</div>

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.

> **[Issues · vshaxe/vshaxe](https://github.com/vshaxe/vshaxe/issues)**
>
> Haxe Support for Visual Studio Code. Contribute to vshaxe/vshaxe development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![ablum](https://community.haxe.org/user_avatar/community.haxe.org/ablum/32/415_2.png) [@ablum](https://community.haxe.org/u/ablum)
#### Post date: [April 6, 2023, 10:10am UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/12 "2023-04-06T10:10:36Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Kyliathy](https://community.haxe.org/user_avatar/community.haxe.org/kyliathy/32/452_2.png) [@Kyliathy](https://community.haxe.org/u/Kyliathy)
#### Post date: [April 7, 2023, 10:07am UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/13 "2023-04-07T10:07:34Z")

</div>

@RblSb First of all, there WAS an issue opened about this, I even commented there ( [Rename Symbol method · Issue #188 · vshaxe/vshaxe · GitHub](https://github.com/vshaxe/vshaxe/issues/188) ). 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](https://github.com/vshaxe/vshaxe/issues/579)

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [April 30, 2023, 1:50pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/14 "2023-04-30T13:50:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kLabz](https://community.haxe.org/user_avatar/community.haxe.org/klabz/32/225_2.png) [@kLabz](https://community.haxe.org/u/kLabz)
#### Post date: [April 30, 2023, 7:32pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/15 "2023-04-30T19:32:35Z")

</div>

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…)

---

<div class="post-metadata">

### Author: ![danielo515](https://community.haxe.org/user_avatar/community.haxe.org/danielo515/32/2052_2.png) [@danielo515](https://community.haxe.org/u/danielo515)
#### Post date: [May 2, 2023, 5:26pm UTC](https://community.haxe.org/t/how-to-refactor-code-in-haxe/3905/16 "2023-05-02T17:26:38Z")

</div>

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