问题
Although IntelliJ Scala Plugin uses Scala compiler proper to generate the actual bytecode, it seems to use its own implementation of presentation compiler to provide real-time type-aware error highlighting in the editor:
syntax highlighting feature that is implemented by Idea's Scala plugin and it requires immediate re-processing of all the files you change in a way similar but now exactly the same as what the real compiler does.
This might be the reason why sometimes projects build successfully and yet IntelliJ shows false positive red underlines in the editor. This seems to be the case even if use sbt shell is selected as evident by open ticket SCL-11079
By default, the sbt compile should mark additional errors it finds in the editor and project view. False positive errors reported by the internal annotator should be removed if possible. There should be an option to use sbt output as the "single source of truth" for highlighting errors, at the cost of instant error highlighting.
or if project is imported as BSP project
sbt shell indeed won't help with error highlighting in the editor, nor will BSP (for the time being). In-ide highlighting uses a method that is completely separate from regular compilation
Is there anyway to configure IntelliJ to use Scala out-of-the-box presentation compiler instead of its own custom implementation such that we can decrease the chance false positive errors akin to what Metals provides?
回答1:
According to show bsp diagnostics in editor SCL-16610 there exists a IntelliJ registry flag
scala.highlighting.compiler.errors.in.editor
which enables experimental feature
As error highlighting using the built-in parser and type-checker continues to be a problem for the Scala plugin, we can experiment with using compiler output from BSP... Potential downsides: Less rich error highlighting on type mismatch problems: BSP and the compiler expose only raw string messages and positions. We would lose type diffs and type error hints.
Here are the steps that made it work on my machine
- Install JetBrains Toolbox which enables installation of Early Access Programs alongside releases (such that you can revert to your working system if anything goes wrong)
- Install 2020.1
Make sure
Nightly Builds
update channel is set underPreferences | Languages & Frameworks | Scala | Updates
- Remove
.idea
from the root of the project:rm -fr .idea
Re-import the project and select BSP project
- There should be
.bloop
directory under project root if all OK - Open IntelliJ registry:
Help | Find Action... | Registry
Set key
scala.highlighting.compiler.errors.in.editor
来源:https://stackoverflow.com/questions/61099741/how-to-configure-intellij-scala-plugin-to-use-scalas-native-presentation-compil