-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
List possible variables and type names when cannot find reference #166
Comments
Can I take this? |
Sure @vHugoObject, no one has taken it. Have updated the description. LMK if you get stuck on anything! |
To be clear, I should create a typescript file with some code where a variable cannot be found and place that example file in checker/examples so that it can be ran with cargo run -p ezno-checker --example run_checker? |
I would first start by creating a function (possibly in a empty project) that is like Once that works you can try wiring that into the checker in the place mentioned above. To test it out, yes copy code above to a file anywhere (e.g. |
Yes, I understood the first part and I already have the function written. My question was more should I add and commit code.ts to the example folder for other people to use not just me? Also "possibles" is currently expecting a "Vec<&'a str>". Shouldn't that be the return type of get_closest instead of &str? |
Awesome. At the moment there isn't any general example ts files so I probably wouldn't commit the example file. Normally this sort of change would be shown in the specification checking tests but they don't register or check extra/labels in diagnostics so it won't have an effect there. Yes Looking forward to the PR. Once I see the new code I will have a think about testing/examples for the new functionality! |
Ok, so "possible" as "Option<&str>" or "possibles" as "Option<Vec<&str>>"? I ask because this is the signature of levenshtein: fn levenshtein(a: &str b: &str) -> usize. So I was just going to do something like items.filter(|item| levenshtein(closest_one, item) < 2).collect::<Vec<&str>>(). Then I could do a match-case based on the length of the list so that the error message would be "Did you mean |
Yep match possibles.as_slice() {
[] => None,
[a] => format!("did you mean {a}"),
[a, b] => format!("did you mean {a} or {b}"),
[a @ .., b] => format!("did you mean {items} or {b}", items = a.join(", ")),
} |
So I am able to get variables using env.variables.keys() but env.named_types.keys() returns nothing. |
Hmm? That shouldn't be non empty because it is used in Does it work for the following? type SomeType = number;
let x: SumType = 2; |
Also just wondering do you want to also add possible suggestions for import not found (which would need changes to |
Ok, the issue is there is CouldNotFindType and CannotFindType. Now that I added possibles as a field to CannotFindType, potential types are shown when I run a test file with your example. I am not sure when CouldNotFindType is called or what the difference between the two is, but for now both have possibles fields. And yeah, I can go ahead and do import not found and property not found. |
"PropertyDoesNotExist" for properties and "CannotOpenFile" for imports? |
Ah yes I found there was two errors for the same thing by mistake, should merge the two into the same variant.
Yep, if possible that would be great! |
When a variable name cannot be found an error is raised
For the current this means the
somethin
will raise a type errorHowever the current error for this sort of not found reference error is:
It would be better for the error to include possible alternatives that are in scope:
I started on this, but didn't finish. You can add the
possible
fields to theTypeCheckerError::CouldNotFindType
, use thelevenshtein
crate to find similar (aka low distance) names to the specified one andparents_iter().map(|env| env.variables.keys())
to get variable names in the scope (+named_types
for types)ezno/checker/src/diagnostics.rs
Lines 293 to 298 in 5191329
ezno/checker/Cargo.toml
Line 40 in 5191329
I would recommend approaching it by
lib.rs
that has the signaturepub fn get_closest(items: Iterator<Item=&str>, closest_to: &str) -> Option<&str>
(want it public as will be useful in the LSP Add a LSP (language service protocol) extension #22)ezno/checker/src/context/environment.rs
Line 892 in bb03d1f
cargo run -p ezno-checker --example run_checker
on the above exampleThe text was updated successfully, but these errors were encountered: