forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code actions for struct and refactor (FuelLabs#4032)
## Description Closes FuelLabs#3519 Generate impl block for a struct  "Generate `new`" for a struct with no impl  "Generate `new`" for a struct with an existing impl block but no new function  "Generate `new`" is hidden (disabled) for a struct that already has a `new` function (requires reload)  ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: Joshua Batty <[email protected]>
- Loading branch information
1 parent
30cdb10
commit 3768105
Showing
18 changed files
with
1,017 additions
and
346 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
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
sway-lsp/src/capabilities/code_actions/abi_decl/abi_impl.rs
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,96 @@ | ||
use sway_core::{ | ||
language::ty::{TyAbiDeclaration, TyFunctionParameter, TyTraitFn}, | ||
Engines, | ||
}; | ||
use sway_types::Spanned; | ||
use tower_lsp::lsp_types::Url; | ||
|
||
use crate::capabilities::code_actions::{ | ||
CodeAction, CodeActionContext, CODE_ACTION_IMPL_TITLE, CONTRACT, | ||
}; | ||
|
||
pub(crate) struct AbiImplCodeAction<'a> { | ||
engines: Engines<'a>, | ||
decl: &'a TyAbiDeclaration, | ||
uri: &'a Url, | ||
} | ||
|
||
impl<'a> CodeAction<'a, TyAbiDeclaration> for AbiImplCodeAction<'a> { | ||
fn new(ctx: CodeActionContext<'a>, decl: &'a TyAbiDeclaration) -> Self { | ||
Self { | ||
engines: ctx.engines, | ||
decl, | ||
uri: ctx.uri, | ||
} | ||
} | ||
|
||
fn new_text(&self) -> String { | ||
self.impl_string( | ||
None, | ||
self.fn_signatures_string(), | ||
Some(CONTRACT.to_string()), | ||
) | ||
} | ||
|
||
fn title(&self) -> String { | ||
format!("{} `{}`", CODE_ACTION_IMPL_TITLE, self.decl_name()) | ||
} | ||
|
||
fn decl_name(&self) -> String { | ||
self.decl.name.to_string() | ||
} | ||
|
||
fn decl(&self) -> &TyAbiDeclaration { | ||
self.decl | ||
} | ||
|
||
fn uri(&self) -> &Url { | ||
self.uri | ||
} | ||
} | ||
|
||
impl AbiImplCodeAction<'_> { | ||
fn return_type_string(&self, function_decl: &TyTraitFn) -> String { | ||
let type_engine = self.engines.te(); | ||
// Unit is the implicit return type for ABI functions. | ||
if type_engine.get(function_decl.return_type).is_unit() { | ||
String::from("") | ||
} else { | ||
format!(" -> {}", function_decl.return_type_span.as_str()) | ||
} | ||
} | ||
|
||
fn fn_signatures_string(&self) -> String { | ||
let decl_engine = self.engines.de(); | ||
format!( | ||
"\n{}\n", | ||
self.decl | ||
.interface_surface | ||
.iter() | ||
.filter_map(|function_decl_ref| { | ||
decl_engine | ||
.get_trait_fn(function_decl_ref, &function_decl_ref.span()) | ||
.ok() | ||
.map(|function_decl| { | ||
self.fn_signature_string( | ||
function_decl.name.to_string(), | ||
self.params_string(&function_decl.parameters), | ||
&function_decl.attributes, | ||
self.return_type_string(&function_decl), | ||
None, | ||
) | ||
}) | ||
}) | ||
.collect::<Vec<String>>() | ||
.join("\n") | ||
) | ||
} | ||
|
||
fn params_string(&self, params: &[TyFunctionParameter]) -> String { | ||
params | ||
.iter() | ||
.map(|param| format!("{}: {}", param.name, param.type_argument.span.as_str())) | ||
.collect::<Vec<String>>() | ||
.join(", ") | ||
} | ||
} |
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,18 @@ | ||
pub(crate) mod abi_impl; | ||
|
||
use sway_core::decl_engine::id::DeclId; | ||
use sway_types::Span; | ||
use tower_lsp::lsp_types::CodeActionOrCommand; | ||
|
||
use self::abi_impl::AbiImplCodeAction; | ||
|
||
use super::{CodeAction, CodeActionContext}; | ||
|
||
pub(crate) fn code_actions( | ||
decl_id: &DeclId, | ||
decl_span: &Span, | ||
ctx: CodeActionContext, | ||
) -> Option<Vec<CodeActionOrCommand>> { | ||
let decl = ctx.engines.de().get_abi(decl_id, decl_span).ok()?; | ||
Some(vec![AbiImplCodeAction::new(ctx, &decl).code_action()]) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.