简体   繁体   中英

Automatically refactor wildcard imports into explicit imports in IntelliJ (for Scala/Java)

Consider the code below.

Is it possible to make InteliJ to automatically refactor each wildcard import into explicit imports (whatever is used in scope)?

For example import scalatags.JsDom.all._ into import scalatags.JsDom.all.{ol,li,div} ?

My search suggests that it is not possible, but maybe I am wrong?

This is a related but different question.

package app.client

import app.client.components.RootReactComp
import app.client.pages.spatutorial.components.GlobalStyles
import japgolly.scalajs.react.ReactDOM
import org.scalajs.dom
import org.scalajs.dom.raw.Element

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport
import scalacss.Defaults._
import scalatags.JsDom.all._
import scalatags.JsDom.implicits._
import scalatags.JsDom.svgAttrs.{fill, height, points, stroke, width,strokeWidth}
import scalatags.JsDom.svgTags._
@JSExport("Main")
  object Main extends js.JSApp {
@JSExport
         def main(): Unit = {
    //    log.warn("Application starting")
    //    // send log messages also to the server
    //    log.enableServerLogging("/logging")
    //    log.info("This message goes to server as well")

    // create stylesheet
    GlobalStyles.addToDocument()
    // tell React to render the router in the document body
    //ReactDOM.render(router(), dom.document.getElementById("root"))
    ReactDOM.render(RootReactComp.themedView(), dom.document.getElementById("joco"))
    val el: Element =dom.document.getElementById("svg-example")
     val l=     div( ol(
       li("Ordered List Item 1"),
       li("Ordered List Item 2"),
       li("Ordered List Item 3")
     )).render
  val s=    svg(height := "800", width := "500")(
                polyline(
                    points := "20,20 40,25 60,40 80,120 120,140 200,180",
                    fill := "none",
                    stroke := "black",
                    strokeWidth := "3"
                  )
           )
    el.appendChild(l);
  }

Open Dialog

Preferences > Editor > Code Style > Scala

Select tab Imports

In the field "Class count to use import with _", enter a ridiculously high number, eg 5000.

From now on, the "Optimize Imports" command will resolve wildcards to individual imports.

You probably also want to activate

Editor > General > Auto Import > Scala > Optimize Imports on the Fly

This is the solution that worked for me - however sub-optimal:

  1. Optimize imports
  2. Go to any import .* and press alt-enter (a menu-question will appear to replace the * import with individual imports) then press enter again. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM