-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
10757: Fixes add #[tokio::main] quick fix. r=ceronman a=ceronman This changes the way we are installing tokio dependency. Instead of using `cargo add` we're editing Cargo.toml directly. This allows us to have more control over the features. New helper function to add cargo dependency is added as well. changelog: Fixes undo not available after adding tokio dependency when using Add `#[tokio::main]` quickfix. 10758: T: Added some extra tests for value out of range inspection. r=ceronman a=ceronman Co-authored-by: vlad20012 <beskvlad@gmail.com> Co-authored-by: Manuel Ceron <manuel.ceron@jetbrains.com>
- Loading branch information
Showing
10 changed files
with
475 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.toml | ||
|
||
import org.rust.lang.core.crate.Crate | ||
import org.rust.openapiext.checkWriteAccessAllowed | ||
import org.toml.lang.psi.* | ||
|
||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
/** | ||
* Adds dependency [name] with version [version] to the corresponding `[dependencies]` section | ||
* in the corresponding `Cargo.toml` file if the dependency doesn't exist already. If it does, | ||
* update the features of the dependency with [features] if required. | ||
* | ||
* For example, if [name] = "tokio", [version] = "1.0.0" and [features] = "full", it inserts | ||
* ``` | ||
* [dependencies] | ||
* tokio = { version = "1.0.0", features = ["full"] } | ||
* ``` | ||
*/ | ||
fun Crate.addCargoDependency(name: String, version: String, features: List<String> = emptyList()) { | ||
checkWriteAccessAllowed() | ||
|
||
val cargoToml = cargoTarget?.pkg?.getPackageCargoTomlFile(project) ?: return | ||
val factory = TomlPsiFactory(project) | ||
|
||
val featuresArray = features.joinToString(prefix = "[", separator = ", ", postfix = "]") { "\"$it\"" } | ||
|
||
when (val existingDependency = cargoToml.findDependencyElement(name)) { | ||
is TomlKeyValueOwner -> { | ||
updateDependencyFeatures(factory, existingDependency, features) | ||
} | ||
is TomlLiteral -> { | ||
val newVersion = existingDependency.stringValue ?: version | ||
val newEntry = factory.createInlineTable("""version = "$newVersion", features = $featuresArray""") | ||
existingDependency.replace(newEntry) | ||
} | ||
else -> { | ||
val existingDependencies = cargoToml.tableList.find { | ||
it.header.key?.stringValue == "dependencies" | ||
} | ||
val dependencies = existingDependencies ?: run { | ||
val newDependenciesTable = factory.createTable("dependencies") | ||
cargoToml.add(factory.createWhitespace("\n")) | ||
cargoToml.add(newDependenciesTable) as TomlTable | ||
} | ||
val newDependencyKeyValue = if (features.isEmpty()) { | ||
factory.createKeyValue(name, version) | ||
} else { | ||
factory.createKeyValue(name, """{ version = "$version", features = $featuresArray }""") | ||
} | ||
|
||
dependencies.add(factory.createWhitespace("\n")) | ||
dependencies.add(newDependencyKeyValue) | ||
} | ||
} | ||
} | ||
|
||
private fun updateDependencyFeatures(factory: TomlPsiFactory, table: TomlKeyValueOwner, features: List<String>) { | ||
val featuresEntry = table.entries.find { entry -> entry.key.stringValue == "features" } | ||
if (featuresEntry == null) { | ||
val featuresArray = features.joinToString(prefix = "[", separator = ", ", postfix = "]") { "\"$it\"" } | ||
val newEntry = factory.createKeyValue("features", featuresArray) | ||
val newTable = (table.entries + listOf(newEntry)).joinToString(separator=", ") { | ||
"""${it.key.text} = ${it.value?.text}""" | ||
} | ||
table.replace(factory.createInlineTable(newTable)) | ||
} else { | ||
val existingFeatures = (featuresEntry.value as? TomlArray)?.elements | ||
?.mapNotNull { value -> value.stringValue } | ||
?: emptyList() | ||
val newFeatures = (existingFeatures + features).distinct() | ||
val newFeaturesArray = newFeatures.joinToString(prefix = "[", separator = ", ", postfix = "]") { "\"$it\"" } | ||
featuresEntry.replace(factory.createKeyValue("features", newFeaturesArray)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.