Skip to content

Commit

Permalink
Add code actions for struct and refactor (FuelLabs#4032)
Browse files Browse the repository at this point in the history
## Description

Closes FuelLabs#3519

Generate impl block for a struct

![Feb-09-2023
14-37-34](https://user-images.githubusercontent.com/47993817/217955070-b0118f3d-7666-44f6-8a3b-857d9b47eb72.gif)

"Generate `new`" for a struct with no impl

![Feb-09-2023
14-38-57](https://user-images.githubusercontent.com/47993817/217955257-43837cd7-7f0b-4382-938e-a6d74ac07fbb.gif)

"Generate `new`" for a struct with an existing impl block but no new
function

![Feb-09-2023
14-35-13](https://user-images.githubusercontent.com/47993817/217954717-16b8edde-ebd7-464b-9a2a-1793f9d2e985.gif)

"Generate `new`" is hidden (disabled) for a struct that already has a
`new` function (requires reload)

![Feb-09-2023
14-40-31](https://user-images.githubusercontent.com/47993817/217955520-a17dc8f7-2062-4d4e-92ec-bd17a75a2ac8.gif)


## 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
sdankel and JoshuaBatty authored Feb 14, 2023
1 parent 30cdb10 commit 3768105
Show file tree
Hide file tree
Showing 18 changed files with 1,017 additions and 346 deletions.
2 changes: 1 addition & 1 deletion sway-core/src/decl_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#[allow(clippy::module_inception)]
pub(crate) mod engine;
pub(crate) mod id;
pub mod id;
pub(crate) mod mapping;
pub(crate) mod r#ref;
pub(crate) mod replace_decls;
Expand Down
12 changes: 8 additions & 4 deletions sway-core/src/language/ty/declaration/abi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::hash::{Hash, Hasher};

use sway_types::{Ident, Span};

use crate::{decl_engine::DeclRef, engine_threading::*, transform, type_system::*};
use std::hash::{Hash, Hasher};
use sway_types::{Ident, Span, Spanned};

/// A [TyAbiDeclaration] contains the type-checked version of the parse tree's `AbiDeclaration`.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -53,3 +51,9 @@ impl CreateTypeId for TyAbiDeclaration {
type_engine.insert(decl_engine, ty)
}
}

impl Spanned for TyAbiDeclaration {
fn span(&self) -> Span {
self.span.clone()
}
}
43 changes: 0 additions & 43 deletions sway-lsp/src/capabilities/code_actions.rs

This file was deleted.

96 changes: 96 additions & 0 deletions sway-lsp/src/capabilities/code_actions/abi_decl/abi_impl.rs
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(", ")
}
}
18 changes: 18 additions & 0 deletions sway-lsp/src/capabilities/code_actions/abi_decl/mod.rs
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()])
}
116 changes: 0 additions & 116 deletions sway-lsp/src/capabilities/code_actions/abi_impl.rs

This file was deleted.

Loading

0 comments on commit 3768105

Please sign in to comment.