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

improvement: don't create service for folders w/ non-Scala projects #5562

Merged
merged 3 commits into from Oct 2, 2023

Conversation

kasiaMarek
Copy link
Contributor

@kasiaMarek kasiaMarek commented Aug 17, 2023

Previously: We had no distinction between workspace folders that are Scala projects and those that aren't.
Now: We create MetalsLspService only for Scala projects.

I consider something to be a Scala (metals) project if there is:

  1. .metals folder
  2. a toplevel scala+ (.scala, .sc, .sbt) file
  3. a scala+ file in .\src, .\it , .\test, .\*\src, .\*\it , .\*\test
  4. a scala+ file that belongs to that workspace folder was opened

resolves: #5558

@kasiaMarek kasiaMarek changed the title improvement: don't set up logger or initialize service for folders w/ non-Scala projects improvement: don't create service for folders w/ non-Scala projects Aug 18, 2023
@kasiaMarek kasiaMarek marked this pull request as draft August 18, 2023 16:17
@kasiaMarek kasiaMarek force-pushed the handle-non-scala-projects branch 4 times, most recently from a0f5b24 to 1ef05c4 Compare August 22, 2023 14:48
@kasiaMarek kasiaMarek marked this pull request as ready for review August 23, 2023 09:00
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Great work! Some minor suggestions and questions.

def isScalaDir(file: File): Boolean = {
def isScalaProject(extendSearchToScriptsAndSbt: Boolean = true): Boolean = {
val isScala: String => Boolean =
if (extendSearchToScriptsAndSbt) _.isScalaFilename else _.isScala
Copy link
Contributor

Choose a reason for hiding this comment

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

What about Java source files, since Metals does provide some useful support for it? 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Metals isn't really a Java LSP and for a pure Java project one should most probably use something else. However, you can always force metals (server) to acknowledge smth as a "metals project" by adding .metals dir.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, ideally you would start Java LSP server for those workspaces that are pure Java

@filipwiech
Copy link
Contributor

filipwiech commented Aug 29, 2023

I wonder about the interoperability with the nvim-metals plugin. I'm not sure about VS Code (I guess this PR introduces a similar mechanism?), but the Neovim plugin allows the user to configure which file types should cause Metals server to start and provide editor support. The default example configuration from the README defines scala, sbt and java files as triggers. Could there be a way to somehow unify those 2 approaches, so that they don't desynchronize depending on the user's configuration (vs hardcoded Metals settings)? Or maybe I misunderstood and there's no actual issue here. 🤔

@kasiaMarek
Copy link
Contributor Author

I'm not sure about VS Code

It's just Scala

Could there be a way to somehow unify those 2 approaches, so that they don't desynchronize depending on the user's configuration (vs hardcoded Metals settings)?

We could add settings to have an array of possible triggers but I'm not sure it's worth it. The biggest use case seems to be java but it would be much simpler for the client (nvim-metals) to create a .metals directory in such case. Any thoughts @ckipp01 ?

@ckipp01
Copy link
Member

ckipp01 commented Sep 1, 2023

I'm not sure about VS Code

It's just Scala

Could there be a way to somehow unify those 2 approaches, so that they don't desynchronize depending on the user's configuration (vs hardcoded Metals settings)?

We could add settings to have an array of possible triggers but I'm not sure it's worth it. The biggest use case seems to be java but it would be much simpler for the client (nvim-metals) to create a .metals directory in such case. Any thoughts @ckipp01 ?

Good questions @filipwiech. nvim-metals works quite a bit different here as it relates to the actual issue this is trying to solve. I'm assuming that any "buffer-based" editor will probably behave the same. The idea of a "workspace" is sort of artificial in nvim-metals since it's always just a buffer you're opening. Therefore it always needs to detect if that file is actually a new workspace that is being opened or part of an existing one that was already sent in. So the original issue that relates to doesn't exist in nvim-metals unless you manually tried adding a workspace root that wasn't a Scala one.

In regards to Java, I'd actually consider what this PR does to be a regression. For myself personally I do actually use Metals sometimes on minimal Java projects that I don't want to spin up IntelliJ for. For me personally I don't do enough Java to justify the setup of the Java language server, which isn't easy in Neovim, and isn't a smooth experience. So what Metals offers me is just perfect for what I need. It'd be a shame if that was lost.

While I understand the original issue, I'm not fully sure I understand why it happens. Is this just VS Code sending in every root when you open up:

scala-folder
python-folder
typescript-folder

It sends in all three? Couldn't the VS Code extension have some sort of logic to only send in the correct workspace root and filter out the non-scala ones? I wouldn't expect python-folder and typescript-folder to be added as workspace roots. The extension should be able to determine that these aren't roots meant for Scala. I get that maybe Metals should guard a bit in the case that a user tries to send in a pointless directory as a root, but I don't think it should not allow a root (java) that it can totally work with.

@kasiaMarek
Copy link
Contributor Author

Couldn't the VS Code extension have some sort of logic to only send in the correct workspace root and filter out the non-scala ones?

It would have to:

  1. filter for the initial request,
  2. filter for didChangeWorkspaceFolders ,
  3. send additional notifications when a Scala file was created in a workspace folder, that previously was not considered a Scala project.

I also suppose it will be a very similar situation for Sublime.
However, it would be very easy to allow the client to opt out of this logic all together. We already have isKnownScalaProject flag.

@tgodzik
Copy link
Contributor

tgodzik commented Sep 5, 2023

Could we instead also detect Java files to satisfy the use case that @ckipp01 mentions? Ideally, we would not turn on if the java extension is present, but I am not sure how to check that properly

Copy link
Member

@jkciesluk jkciesluk left a comment

Choose a reason for hiding this comment

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

Good job! About reports and limited files manager, this additional checks are needed because some error was occurring before? Or switching to folderPathsWithScala should solve the problem?
Although, I'm for having these checks either way.

if (!isIn(services, folder)) {
WorkspaceFoldersServices(
services :+ newService,
nonScalaProjects.filter(_ == folder),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
nonScalaProjects.filter(_ == folder),
nonScalaProjects.filterNot(_ == folder),

?

@@ -163,8 +171,21 @@ class MetalsLanguageServer(
val folderPaths = folders.map(_.path)

setupJna()

val folderPathsWithScala =
Copy link
Member

Choose a reason for hiding this comment

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

Don't we want to use this in scribe.info and for removing old reports below instead of folderPaths?

@kasiaMarek
Copy link
Contributor Author

some error was occurring before? Or switching to folderPathsWithScala should solve the problem?

Honestly I'm not sure right now. It was definitely mostly connected to folderPaths instead of folderPathsWithScala. But maybe is better anyway not to create all those dirs eagerly.

Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

LGTM from me!

@kasiaMarek kasiaMarek merged commit c85e193 into scalameta:main Oct 2, 2023
22 of 24 checks passed
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.

[Bug] Generate .metal for all projects in workspace
5 participants