halirutan / Wolfram-Language-IntelliJ-Plugin-Archive

Wolfram Language and Mathematica plugin for IntelliJ IDEA.

Home Page:https://wlplugin.halirutan.de/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Symbols in separate files with same context not being found.

gdelfino opened this issue · comments

Describe the bug
I have a long package that is divided in several files while still using a single context. Symbols defined in other files are not being found.

This style of partitioning a package is described as "multi-fragment (multi-file) project, single namespace" in https://mathematica.stackexchange.com/a/176489/251

To Reproduce
A minimal example screenshot is provided.

Expected behavior
Symbols in other files with same context should be found to avoid warnings.

Screenshots
IntelliJ-issue1

Please also provide:

  • Windows 10
  • IntelliJ IDEA 2019.3.3
  • Wolfram Plugin Version 2019.3

Analyzing contexts is quite hard and one would need to evaluate code to calculate the correct context for a symbol. Since I haven't found a reliable and fast solution (since it basically needs to happen on the fly for each keystroke), the plugin uses a simpler approach. In indexes ::usage messages and provides cross-file completion for all symbols that have one.

You can read about details in the documentation, but the gist is

Currently, the Wolfram Language plugin indexes package files by searching for usage messages. Therefore, as long as your symbol has a sym::usage attached to it, the indexer will find it. From the semantic viewpoint of Mathematica contexts and package-private symbols, this is not correct. However, in practice, it has proven to be a reliable and fast way to indicate a symbol should be exported.

So the solution is to give your g[x_] a usage message. If you like to keep it private, then just put the usage message above its definition. The indexer will still find it and provide autocompletion, navigation, and correct resolution.

There is a lot to say about this topic, but the main problem is that the Wolfram Language is so flexible. So in general, the correct context for a symbol can only be calculated at run-time. Just imagine a situation like this

BeginPackage["MyPack`"];
Begin["`Private`"];

includeMyFile["src/MyOtherPackage.wl"];

End[];
EndPackage[];

Now just imagine the situation for symbols inside MyOtherPackage.wl. There is no way you get their context right without evaluating the function includeMyFile to understand that this function loads the contents when it's called. And then you need to build a dependency tree, etc..

Thank you for the clarification. In practical terms this is no longer an issue for me.

I have a long package that is divided in several files while still using a single context.

"New-style" packages have served me very well, and they are compatible with the WL plugin.

https://mathematica.stackexchange.com/questions/176434/declaring-package-with-dependencies-in-multiples-files/176489

https://mathematica.stackexchange.com/questions/184711/what-to-be-aware-when-using-new-style-package