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

use macros for projectRuffExecutablePath #381

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

KotlinIsland
Copy link
Contributor

@KotlinIsland KotlinIsland commented Feb 23, 2024

The code in here works, but is really bad. I couldn't figure out how to make macros work properly.

Currently the plugin always saves the full path to the config file. This makes it impossible to commit project config. In this change we collapse the venv dir before it is saved.

@koxudaxi
Copy link
Owner

Thank you for creating the PR.
I will review it tonight.

@koxudaxi koxudaxi self-assigned this Feb 26, 2024
@koxudaxi
Copy link
Owner

@KotlinIsland

I would recommend against using $PROJECT_DIR$ and instead use $PyInterpreterDirectory$, it would be highly unlikely that a project would include an exe for ruff outside the venv, and some venvs are not within the project dir (poetry)

Sorry for the delay. I checked the operation.
I agree with you.
Please let me test it some more.

@koxudaxi
Copy link
Owner

@KotlinIsland
I'm sorry for my late reply.
I tested the your PR in my windows OS.
image

The ruff binary file name includes the .exe suffix.
Do you expect the behavior?
Should you use the special variable for ruff binary, like $ruff$ to inject the actual binary file name for each OS?

@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Mar 17, 2024

Ahh, good point, I didn't anticipate this complication. I see two possibilities:

  1. We point to the directory containing the executable, instead of the executable itself.
  2. We automatically handle adding/removing the .exe as needed.

@koxudaxi
Copy link
Owner

We point to the directory containing the executable, instead of the executable itself.

It is certainly easier to save to file this way. However, for clarity regarding the display on the settings screen, it might be better to include up to .exe.

@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Mar 18, 2024

for clarity

I completely agree. Additionally, this PR contains only a rough draft, I couldn't figure out how to make macro fields work properly.

@koxudaxi
Copy link
Owner

Additionally, this PR contains only a rough draft, I couldn't figure out how to make macro fields work properly.
OK, I will check it today.

Copy link
Owner

@koxudaxi koxudaxi left a comment

Choose a reason for hiding this comment

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

@KotlinIsland
I made some modifications.
Add a logic to save to file also when path is overide if necessary, because the timing written to config is only when it should be applied in config panel.

I don't know why, but RuffConfigService can't take project: project in its constructor, so the code became quite complicated. I'd like to make it a little more transparent.

@@ -59,6 +59,7 @@ val RUFF_LSP_COMMAND = when {
else -> "ruff-lsp"
}
const val WSL_RUFF_LSP_COMMAND = "ruff-lsp"
const val PY_INTERPRETER_DIRECTORY_MACRO_NAME = "PY_INTERPRETER_DIRECTORY"
Copy link
Owner

Choose a reason for hiding this comment

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

Should we use Screaming snake case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 30 to 34
private fun updateMacro(sdk: Sdk) {
sdk.homeDirectory?.parent?.presentableUrl?.let {
ruffMacroService.addMacroExpand(PY_INTERPRETER_DIRECTORY_MACRO_NAME, it)
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

The timing of the sdk path change is
I want to fire this event there if and when the sdk path changes.

Comment on lines 9 to 11
@State(name = "RuffConfigService", storages = [Storage("ruff.xml")])
@Service(Service.Level.PROJECT)
class RuffConfigService : PersistentStateComponent<RuffConfigService> {
Copy link
Owner

Choose a reason for hiding this comment

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

If I add the project: Project in the argument, the service raises a null point exception.
I had no choice but to create RuffMacroService.

@koxudaxi
Copy link
Owner

@KotlinIsland

I did some more research.
Here is a code that might be helpful.
https://github.com/JetBrains/intellij-community/blob/94282b7d391a5ed702f01a31549b4893c62974f1/python/src/com/jetbrains/python/sdk/PySdkSettings.kt#L52-L81

I think that before writing the Path to the config file, we can set the macro as inject and expand it again if we want to refer to it.
However, I think there needs to be some caching mechanism to avoid the overhead of executing the code here every time it is referenced.

@koxudaxi
Copy link
Owner

@KotlinIsland
I thought about it some more, but I felt that if We don't need to use a macro to select the bin of the project, if ruff plugin gets the ruff or ruff.exe.
In other words, what if the user can choose to either use the project's venv bin or a custom executable?
I think the custom executables should not be shared with another developer in git.

@koxudaxi
Copy link
Owner

koxudaxi commented Apr 1, 2024

@KotlinIsland
What do you think?

@KotlinIsland
Copy link
Contributor Author

Looks great!

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.

None yet

2 participants