-
Notifications
You must be signed in to change notification settings - Fork 52
Option to allow first-specifier/alias import ordering #336
Option to allow first-specifier/alias import ordering #336
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your code is awesome! :-)
One small thing to change, the rest is pretty perfect.
|
||
return localeStringSort(strA, strB, order); | ||
|
||
function getImportFirstSpecifier(imp: Import): string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add this function as a non exported function (with jsdocs) in the root of the file.
This function would be defined each time you call it, since it does return the correct specifier and is not bound to a closure, you can specify it in the root (top or bottom does not matter.. I'd use the top, beneath the imports)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang, that's obvious, should have caught on that. Hang on a sec.
This refactoring (which totally makes sense) was requested in buehler#336 (review)
@tdd Whopsie. I'm sorry, I merged the wrong one first. I'll review the changes.. edit: would you mind to resolve the conflicts? Thanks for your work on this extension! |
A fixture file used by the `OrganizeImportsOnSaveExtension` test is changed and saved during tests, but not restored eventually, resulting in a modified file for Git diffs post-test, whose committal could break later tests. This restores the fixture file's original text and saves it back once the test completes, much like other test suites (e.g. `ImportManager`) do with their fixtures already.
Many users (e.g. me, or people at #316) like their imports sorted not by library path, but by first specifier/alias name. This adds a `typescriptHero.resolver.organizeSortsByFirstSpecifier` boolean option, defaulting to `false`, that enables just such a behavior. Import grouping is unaffected: sorting happens within groups. Sorting is locale-aware, by the way, so not just lexicographical. When looking for specifier-ordering, we usually are in a "human language" mindset, so case shifts and diacritics should work in natural order.
(Also fixes a misrendered table due to missing header delimiter)
This refactoring (which totally makes sense) was requested in buehler#336 (review)
78ec1bc
to
ad25359
Compare
Aaaand… there we are, @buehler! Nicely rebased and working. Looking forward to seeing that merged in. Btw, I notice you manually squash-merge (of sorts, preserving original committer info) and commit results directly to master, instead of the usual merge-based PRs (except for your stuff, apparently?). Just curious: are you doing this using GitHub’s recent UI changes (PR merge choices), or locally with Git? Best, |
Hey @tdd Actually, I use the github UI. The workflow I use is the following: That's why I squash&merge all pull requests into develop and when I want to release a version, it's "merge" only into master (from develop). The contributors are still accounted for, github does some magic with that. There was one time where I directly merged something into the master, but that was a mistake made by me ;-) |
Hey @buehler, just a quick question about this recent set of upgrades… I'm training professionals on React and Node on a monthly basis, and our recommended editing stack is based on VSCode and a set of extensions, including yours. I'm looking forward to having my recent contributions show up in the installed ext, any idea when you're planning to push a new release that includes these? FWIW, my next training session starts next Tuesday, and we'd enjoy this specific set of features starting on next Wednesday. Then I'm teaching Node the next week. If you're waiting for some other stuff to come in before pushing 1.8, that's fine. I'm just curious about when I might expect that to come in. Best, |
Hey @tdd But despite that fact: A big thanks for the flowers 😊 Actually the last thing that definitely needs to be fixed is that crux with #331. After that, I'll release 1.8. Regards |
Hey Christoph (I'm Christophe myself; the German-style spelling like yours always feels like it's missing something to me 😉 I guess English-speaking people feel the same way with my FR spelling 😁 ) About #331: I experienced a shitload of EPIPE's on my usual projects myself, not due to using 1.7.0, but the moment I upgraded to Code 1.18. Go figure. Perhaps this is because 1.18 auto-imports like a boss (it also works on pure-ES Anyway, best of luck with figuring this out! Sounds super-hairy and, more importantly, borderline impossible to reproduce in a reasonable way 😞 |
(for some reason I'm not getting all the message notifications from GitHub; have to keep thinking about coming back and checking the threads out…) |
Hey Christophe :-) We'll see, as soon as I fixed the messed up error :-/ |
Hey @buehler ! So what's going on with 1.8.0? You seem to have fixed #331 a while ago, but no new version is out? Also, Code's auto-import feature is still super broken on non-TS code, using absolute paths or weird relative paths instead of resolved ones in many cases. So TSH is still very much useful! But mostly I'm fond of the import organization features (Code has none), and I can't wait for the new sorting options and proper group ordering to be out! Let me know! Best, |
Hey @tdd Right now, I'll release the 1.8 version with another important fix and after that I'm going to disable the import adding feature of TSH. I think the future of TSH will be organizing and refactoring. Have a nice day! |
Hey @buehler I'm delighted to see it in my VS Code, however I'm baffled as the new "order by first specifier" option doesn't seem to work at all? It worked great during dev and in tests… Made sure workspace settings were used, tried restarting Code, etc. No dice. It sorts by module name no matter what. o_O |
Hmmm. That's kinda strange. If I got some time, maybe I'll find out something. |
Hey @buehler ! OK I checked, and found out what's wrong. The thing is, this requires some refactoring and I need your input. The gist of it is, many of your tests assume that sorting imports within a group is the job of the The problem is, once we introduce an alternate sort system, as we do AOT in The tricky part is, import groups have no access to / knowledge of the sorting config option, so they can't pick the proper sorter function on their own. So we have two paths here:
I'm willing to implement either of these on top of master/v1.8.0, but I need your input on this, obviously. Best, |
Hey @tdd Honestly, I like the second option better. It's the cleaner solution to decuple those processes and leave the sorting part to the entities that hold the imports. I'm feeling lucky that I added DI in the past ;-) So that would mean we need to change the behaviour and use the organize funtion to create the groups and the groups themselves are responsible for sorting. I havn't had much time to code on this project in the past few weeks, but my guess is, over the holidays I'm gonna do some coding ;-) I'd be happy to help you in any way needed and I'll probably show TSH so love too and fix some other problems 😃 Regards and some nice remaining days before the holidays.. |
Hey @buehler, Second option is fine by me, however I have no idea how to provide the document-relevant ext config in a DI/IoC way using TS and the architecture you have in place. I don't want to blindly copy-and-paste from What troubles me most is that this needs a document URI, but import groups have no document to work on… Alternatively, they could just get the config flags from the import manager using their constructors, but I'm guessing that's not how you want it? |
(btw, this brings common impl code to all import groups; can't TS interfaces have actual code implemented in them? Or would we need to turn it into an abstract class?) |
@tdd I'm still owing you some code. Hope you had a nice new year party ;-) |
Alright, I made you a branch: 2ecee9e#diff-a8e672232745d365e134171621961a96R21 Like that, you can inject stuff that is not directly constructed by the IoC Container. Btw: do you know any good resources on how to handle recursions (i.e. flatten them into iterations)? Regards |
Introduces an option (disabled by default) to switch from lexicographical module path ordering to natural-language first-specifier/alias ordering when organizing imports.
Description
It's fairly common to order imports based on the first imported identifier (specifier, alias or default alias). TSH currently only sorts by module path, and lexicographically, too (which isn't necessarily the dominant mindset).
This adds a
typescriptHero.resolver.organizeSortsByFirstSpecifier
boolean option, defaulting tofalse
, that enables just such a behavior. Import grouping is unaffected: sorting happens within groups.Sorting is locale-aware, by the way, so not just lexicographical. When looking for specifier-ordering, we usually are in a "human language" mindset, so case shifts and diacritics should work in natural order.
Documentation, config schemas and tests are updated, linting is green.