Big update to prettier-extension-monkeyc

I've posted about prettier-extension-monkeyc before, but I've added a bunch of new features that developers will probably like (well, I've been missing them, so maybe you have too).

The new features it implements for VSCode include:

  • Goto Definition. Point at a symbol, Ctrl/Cmd click, and it will take you to the definition. Or F12
  • Goto References. Right click on a symbol and select "Goto References". It will show you all the references. Or Shift-F12
  • Peek Definition/Peek References. Same as above, but in a popup window so you don't lose your place in the original document.
  • Rename Symbol. Right click on a local, function, class or module name, and select "Rename Symbol". It will rename all the references. It doesn't yet work for class members/methods.
  • Goto Symbol. Type Ctrl/Cmd-Shift-O and pick a symbol from the drop down (which has a hierarchical view of all symbols in the current file). This also appears as an outline across the top of the file.
  • Open Symbol By Name. Type Ctrl/Cmd-T, then start typing letters from a symbol name. A drop down will be populated with all matching symbols from anywhere in your project.

Older features include a prettier based formatter for monkeyc, and a monkeyc optimizer that will build/run/export an optimized version of your project.

[edit: My last couple of replies seem to have just disappeared, and the whole conversation seems to be in a jumbled order, so tldr: there's a new test-release at https://github.com/markw65/prettier-extension-monkeyc/releases/tag/v2.0.9 which seems to work for me on linux. I'll do more verification tomorrow, and push a proper update to the vscode store once I'm sure everything is working]

  • This is exactly what I thought too. Maybe it could be the other way around: having a setting to set it greedy, but just in case having an annotation to exclude a specific function from being dropped by the greedy optimization.

  • Yes (if we understand correctly) — and that’s what I originally thought (:keep) did — but I got confused on that also.

    Keep does keep the function but keeps the optimised version, I needed a (:keep_unchanged) to get around a specific firmware implementation on a device where something that said returned string actually returned null. But PMC always removes my (xxxx instanceof String) check because the SDK says it’s always a string and unnecessary check.

  • to get around a specific firmware implementation on a device where something that said returned string actually returned null

    There was (a few years ago) a version of PMC that removed null checks when the API said that a function didn't return null. But the Toybox omits Nulls in so many cases that I had to remove that optimization (now you just get a warning saying that the check appears to be unnecessary- but I don't actually remove it).

    Your issue was where you had declared a function as taking "Dictionary or String", and then did an instanceof check to see if it was an Array - and the optimizer removed the check.

    It was a bit more complex - this was a callback to makeWebRequest, and Garmin's type checker complains if you pass in a method that takes Array or Dictionary or String - so in this case there were two bugs; first the Toybox API incorrectly said that it would never call the method with an Array, but in fact it *did* call it with an array; and second that the type checker won't accept a method that takes A or B if its expecting a method that takes A (I filed a bug here).

  • Because there are lots of cases where there is no direct call to a method, but the method is called anyway - eg your makeWebRequest callback; or where a Toybox api takes an object, and calls a method on it. I had to fix an issue like that a few months ago. The method name was used exactly once in the entire program (the method definition itself) - so my optimizer thought it was unused and removed it; but it was called by the Toybox.

    So normally, unused functions will be removed anyway. But if you've mentioned a symbol with the same name (even if its unrelated, as a Dictionary key, for example) your function won't be removed; or if its got the same name as a function that *is* used; or various other conditions can prevent it from being removed.

    And yes, I've been saying for years that I plan to write a better pass to clean up unused methods/classes/variables - it just hasn't happened yet. Meanwhile tomopumipumi submitted a PR for force_dce, saying they had several use cases in their own projects; so I accepted it as a stop gap.

  • Actually, you're right.

    As I said, the optimizer won't remove null checks on types that are declared non null because many Toybox APIs are mislabled.

    But if you have something whose type says it can only be a String, then x instanceof String will be removed.

    So if you have something where a Toybox API says it returns a String, but it actually returns a String or Null, then checking "x instanceof String" won't work (the check will be removed), but checking "x != null" will work.