Skip to content
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

Don't suggest alias fragments with leading number #385

Merged

Conversation

pdbrown
Copy link
Contributor

@pdbrown pdbrown commented Aug 20, 2022

Hey there! By pure coincidence, I happened to be investigating a possible bug in cljr-slash and refactor-nrepl.ns.suggest-aliases/suggested-aliases just as you posted this writeup #384 today, which looks great btw!
I have a project with some namespaces where the last segment starts with a number like db.migration.005-alter-table. suggested-aliases offers 005-alter-table as a possible alias, but that's not a valid suggestion since the clojure reader doesn't accept symbols that start with a number. When the nrepl respose is parsed by parseclj, it also correctly rejects that symbol, but that breaks cljr-slash for the entire project.
I put together this little change to remove such invalid symbols from the suggestions, and it's working well for me so far!

  • You've added tests (if possible) to cover your change(s)
  • All tests are passing (run lein do clean, test)
  • Code inlining with mranderson works and tests pass with inlined code (run ./build.sh install -- takes a long time)
  • You've updated the changelog (if adding/changing user-visible functionality)

Thanks!

Copy link
Member

@vemv vemv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks much!

Kudos for finding the accurate cause of your problem.

Will be releasing this today as we merge this.

test/refactor_nrepl/ns/suggest_aliases_test.clj Outdated Show resolved Hide resolved
@pdbrown pdbrown force-pushed the fix/alias-suggestion-no-leading-numbers branch from f4f8e3e to e6342a6 Compare August 21, 2022 16:03
Copy link
Member

@vemv vemv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! I added a symbol? test too, since we probably shouldn't accept keywords etc either.

Good thinking, thanks!

test/refactor_nrepl/ns/suggest_aliases_test.clj Outdated Show resolved Hide resolved
@@ -21,6 +21,22 @@
'unit.foo true
'foo.unit true))

(deftest readable-as-symbol?
(are [input expected] (is (= expected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, that was a lazy hack to see the failing line expected: (is (= false (sut/readable-as-symbol? "ok.one.more!"))) vs expected: false. I put in a proper fix: a plain (readable-as-symbol? expected) does the trick vs (is (= true (...))).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I understand!

That's why there's a true at tail position in this ns's ares (kondo and Eastwood won't complain - they understand the trick)

test/refactor_nrepl/ns/suggest_aliases_test.clj Outdated Show resolved Hide resolved
test/refactor_nrepl/ns/suggest_aliases_test.clj Outdated Show resolved Hide resolved
Namespace aliases are symbols, and symbols begin with a non-numeric character,
so we shouldn't suggest aliases that start with numbers.
This fixes clj-refactor.el's cljr-slash for projects that contain ns'es like
`db.migration.25`. Previously, it failed in parseclj when that tried to parse
the nrepl response containing suggestions like `{25 [db.migration.25]}`, since
`25` isn't a valid symbol.
@pdbrown pdbrown force-pushed the fix/alias-suggestion-no-leading-numbers branch from c730a92 to 42d6ecd Compare August 21, 2022 17:24
@vemv vemv merged commit 9d88da9 into clojure-emacs:master Aug 21, 2022
@vemv
Copy link
Member

vemv commented Aug 21, 2022

Thanks much and kudos for driving it to completion!

I'll release these today and you can count on it being available by tomorrow morning.

btw, I don't know if you were using the suggest feature actively. I am, and it's been such a nice QoL improvement.

#384 will be interesting as well, backing a revamped cljr-slash.

@vemv
Copy link
Member

vemv commented Aug 21, 2022

Available in clj-refactor (3.5.6; will be released within a couple hours) / refactor-nrepl (3.5.5; released)

@pdbrown pdbrown deleted the fix/alias-suggestion-no-leading-numbers branch August 22, 2022 15:55
@pdbrown
Copy link
Contributor Author

pdbrown commented Aug 22, 2022

Awesome, and thanks for all the quick reviews! Yeah, I just started using it and it's very helpful (which is how I noticed it wasn't working in my biggest project). I actually recently enabled clj-refactor again after not using it for a few years, and it's so great! I'd disabled it back in the day when I couldn't figure out how to prevent it from building ASTs, and perf was killing me due to my slow laptop + frequent repl restarts before I'd learned how to make things more reloadable.
The new suggest-libspecs op looks cool too! I'm interested in CLJC awareness and also smaller nREPL responses. I've noticed CIDER's bdecode can create a lot of garbage, so anything we do to shrink responses should help.

@vemv
Copy link
Member

vemv commented Aug 22, 2022

Awesome! That's great and motivating input to hear.

AST building can indeed be slow, it cannot scale indefinitely but that's just how tools.analyzer is.

Normally I work on 'modular' projects anyway so no codebase will be overly big.

We'll also possibly have a protocol that would use kondo / lsp instead. There's a lot to be gained with such hybrids.

@pdbrown pdbrown restored the fix/alias-suggestion-no-leading-numbers branch August 30, 2022 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants