comby icon indicating copy to clipboard operation
comby copied to clipboard

Language level support for "imports"

Open talios opened this issue 3 years ago • 2 comments

On several code changes, I've found myself switching implementations of - such as:

[change-guava-map-creation]
match="Maps.newHashMap()"
rewrite="new HashMap<>()"

Here, I'm removing the use of a call from the Guava library, to the standard Java call. In some cases, there's no current import for java.util.HashMap in the file, so the resulting code won't compile without additional manual changes, the same for usages of com.google.common.collect.Maps.

I wonder if there's been any thought to handling adding/remove imports for rather than Fully Qualified Named items?

Something simple like:

[change-guava-map-creation]
match="Maps.newHashMap()"
rewrite="new HashMap<>()"
qualified_names=[ com.google.common.collect.Maps, java.util.HashMap ]

maybe? That keeps the syntax language neutral - and would allow relevant changes for Java, Python, C#, etc. etc. being defined

talios avatar Apr 26 '21 13:04 talios

Supporting semantic information has been on my mind for a while, so thanks for the nice example! Static semantics like imports/package scope fit into this, and also type information.

It would be a lot of work to include native tooling that expose specific info like "qualified names for Java". Not infeasible, just a lot of work. Instead, my thinking is to decouple this problem entirely from comby, and interface with providers like language servers, or LSIF information. This way whatever significant language-specific semantics are exposed through fairly neutral interface. For your specific example, we might check whether the file of a match has a particular import in scope.

As far as an interface to this information from comby's side, I've prototyped ways of accessing static types via the rule language (which would be the vehicle for the syntax language neutral part you refer to). I hope to make something more concrete available soon, time allowing.

rvantonder avatar Apr 27 '21 02:04 rvantonder

As a workaround I used git to figure out which files comby touched and then used sed to insert the import statement that I need just after the first import in the file. This is sufficient for me because we use isort which will clean up the ordering of the import etc.

git diff --name-only --relative | xargs sed -e '0,/import/{s/\(.*import.*\)/\1\nfrom typing import Final/}' -i

(on Mac OS you'll need something more complicated unless you install GNU sed, stack overflow)

davidshepherd7 avatar Jul 27 '23 08:07 davidshepherd7