Change Option<Result> to Result<Option> in importer (#7089)

This commit is contained in:
Charlie Marsh 2023-09-03 17:23:42 +01:00 committed by GitHub
parent 3c7486817b
commit dbb34804a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -171,10 +171,8 @@ impl<'a> Importer<'a> {
at: TextSize, at: TextSize,
semantic: &SemanticModel, semantic: &SemanticModel,
) -> Result<(Edit, String), ResolutionError> { ) -> Result<(Edit, String), ResolutionError> {
match self.get_symbol(symbol, at, semantic) { self.get_symbol(symbol, at, semantic)?
Some(result) => result, .map_or_else(|| self.import_symbol(symbol, at, semantic), Ok)
None => self.import_symbol(symbol, at, semantic),
}
} }
/// Return an [`Edit`] to reference an existing symbol, if it's present in the given [`SemanticModel`]. /// Return an [`Edit`] to reference an existing symbol, if it's present in the given [`SemanticModel`].
@ -183,9 +181,13 @@ impl<'a> Importer<'a> {
symbol: &ImportRequest, symbol: &ImportRequest,
at: TextSize, at: TextSize,
semantic: &SemanticModel, semantic: &SemanticModel,
) -> Option<Result<(Edit, String), ResolutionError>> { ) -> Result<Option<(Edit, String)>, ResolutionError> {
// If the symbol is already available in the current scope, use it. // If the symbol is already available in the current scope, use it.
let imported_name = semantic.resolve_qualified_import_name(symbol.module, symbol.member)?; let Some(imported_name) =
semantic.resolve_qualified_import_name(symbol.module, symbol.member)
else {
return Ok(None);
};
// If the symbol source (i.e., the import statement) comes after the current location, // If the symbol source (i.e., the import statement) comes after the current location,
// abort. For example, we could be generating an edit within a function, and the import // abort. For example, we could be generating an edit within a function, and the import
@ -195,13 +197,13 @@ impl<'a> Importer<'a> {
// unclear whether should add an import statement at the start of the file, since it could // unclear whether should add an import statement at the start of the file, since it could
// be shadowed between the import and the current location. // be shadowed between the import and the current location.
if imported_name.start() > at { if imported_name.start() > at {
return Some(Err(ResolutionError::ImportAfterUsage)); return Err(ResolutionError::ImportAfterUsage);
} }
// If the symbol source (i.e., the import statement) is in a typing-only context, but we're // If the symbol source (i.e., the import statement) is in a typing-only context, but we're
// in a runtime context, abort. // in a runtime context, abort.
if imported_name.context().is_typing() && semantic.execution_context().is_runtime() { if imported_name.context().is_typing() && semantic.execution_context().is_runtime() {
return Some(Err(ResolutionError::IncompatibleContext)); return Err(ResolutionError::IncompatibleContext);
} }
// We also add a no-op edit to force conflicts with any other fixes that might try to // We also add a no-op edit to force conflicts with any other fixes that might try to
@ -224,7 +226,7 @@ impl<'a> Importer<'a> {
self.locator.slice(imported_name.range()).to_string(), self.locator.slice(imported_name.range()).to_string(),
imported_name.range(), imported_name.range(),
); );
Some(Ok((import_edit, imported_name.into_name()))) Ok(Some((import_edit, imported_name.into_name())))
} }
/// Generate an [`Edit`] to reference the given symbol. Returns the [`Edit`] necessary to make /// Generate an [`Edit`] to reference the given symbol. Returns the [`Edit`] necessary to make